Skip to content

Commit

Permalink
construct AsyncResource for callback when callback is stored
Browse files Browse the repository at this point in the history
  • Loading branch information
fivdi committed Jul 22, 2018
1 parent 60d006a commit 1d913b4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Unpublished
===========

* add troubleshooting guide (thank you [@sznowicki](https://github.com/sznowicki))
* construct AsyncResource for callback when callback is stored

1.1.0 / May 20 2018
===================
Expand Down
44 changes: 37 additions & 7 deletions src/pigpio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@ static void gpioAlertEventLoopHandler(uv_async_t* handle);

class GpioCallback_t {
public:
GpioCallback_t() : callback_(0) {
GpioCallback_t() : callback_(0), async_resource_(0) {
Nan::HandleScope scope;
}

virtual ~GpioCallback_t() {
Nan::HandleScope scope;

if (callback_) {
uv_unref((uv_handle_t *) &async_);
delete callback_;
}

if (async_resource_) {
delete async_resource_;
}

uv_close((uv_handle_t *) &async_, 0);

callback_ = 0;
async_resource_ = 0;
}

void AsyncSend() {
Expand All @@ -39,9 +47,15 @@ class GpioCallback_t {
delete callback_;
}

if (async_resource_) {
delete async_resource_;
}

callback_ = callback;
async_resource_ = 0;

if (callback_) {
async_resource_ = new Nan::AsyncResource("pigpio:eventHandler");
uv_ref((uv_handle_t *) &async_);
}
}
Expand All @@ -50,11 +64,16 @@ class GpioCallback_t {
return callback_;
}

Nan::AsyncResource *Resource() {
return async_resource_;
}

protected:
uv_async_t async_;

private:
Nan::Callback *callback_;
Nan::AsyncResource *async_resource_;
};


Expand All @@ -80,8 +99,8 @@ class GpioAlert_t : public GpioCallback_t {
};


static GpioISR_t gpioISR_g[PI_MAX_USER_GPIO + 1];
static GpioAlert_t gpioAlert_g[PI_MAX_USER_GPIO + 1];
static GpioISR_t *gpioISR_g;
static GpioAlert_t *gpioAlert_g;
static int gpio_g;
static int level_g;
static uint32_t tick_g;
Expand Down Expand Up @@ -391,8 +410,12 @@ static void gpioISREventLoopHandler(uv_async_t* handle) {
Nan::New<v8::Integer>(level_g),
Nan::New<v8::Integer>(tick_g)
};
Nan::AsyncResource resource("pigpio:gpioISREventLoopHandler");
gpioISR_g[gpio_g].Callback()->Call(3, args, &resource);

gpioISR_g[gpio_g].Callback()->Call(
3,
args,
gpioISR_g[gpio_g].Resource()
);
}

uv_sem_post(&sem_g);
Expand Down Expand Up @@ -456,8 +479,12 @@ static void gpioAlertEventLoopHandler(uv_async_t* handle) {
Nan::New<v8::Integer>(level_g),
Nan::New<v8::Integer>(tick_g)
};
Nan::AsyncResource resource("pigpio:gpioAlertEventLoopHandler");
gpioAlert_g[gpio_g].Callback()->Call(3, args, &resource);

gpioAlert_g[gpio_g].Callback()->Call(
3,
args,
gpioAlert_g[gpio_g].Resource()
);
}

uv_sem_post(&sem_g);
Expand Down Expand Up @@ -797,6 +824,9 @@ NAN_MODULE_INIT(InitAll) {

SetFunction(target, "gpioCfgClock", gpioCfgClock);
SetFunction(target, "gpioCfgSocketPort", gpioCfgSocketPort);

gpioISR_g = new GpioISR_t[PI_MAX_USER_GPIO + 1];
gpioAlert_g = new GpioAlert_t[PI_MAX_USER_GPIO + 1];
}

NODE_MODULE(pigpio, InitAll)
Expand Down

0 comments on commit 1d913b4

Please sign in to comment.