Skip to content

Commit

Permalink
addon: add AtExit() function
Browse files Browse the repository at this point in the history
Lets native addons register exit hooks that run after the event loop has quit
but before the VM is disposed.

Fixes nodejs#3147.
  • Loading branch information
bnoordhuis committed Apr 21, 2012
1 parent 525253d commit 1c20cac
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2766,6 +2766,37 @@ char** Init(int argc, char *argv[]) {
}


struct AtExitCallback {
AtExitCallback* next_;
void (*cb_)(void* arg);
void* arg_;
};

static AtExitCallback* at_exit_functions_;


void RunAtExit() {
AtExitCallback* p = at_exit_functions_;
at_exit_functions_ = NULL;

while (p) {
AtExitCallback* q = p->next_;
p->cb_(p->arg_);
delete p;
p = q;
}
}


void AtExit(void (*cb)(void* arg), void* arg) {
AtExitCallback* p = new AtExitCallback;
p->cb_ = cb;
p->arg_ = arg;
p->next_ = at_exit_functions_;
at_exit_functions_ = p;
}


void EmitExit(v8::Handle<v8::Object> process_l) {
// process.emit('exit')
Local<Value> emit_v = process_l->Get(String::New("emit"));
Expand Down Expand Up @@ -2845,6 +2876,7 @@ int Start(int argc, char *argv[]) {
uv_run(uv_default_loop());

EmitExit(process_l);
RunAtExit();

#ifndef NDEBUG
// Clean up.
Expand Down
5 changes: 5 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ node_module_struct* get_builtin_module(const char *name);
#define NODE_MODULE_DECL(modname) \
extern "C" node::node_module_struct modname ## _module;

/* Called after the event loop exits but before the VM is disposed.
* Callbacks are run in reverse order of registration, i.e. newest first.
*/
NODE_EXTERN void AtExit(void (*cb)(void* arg), void* arg);

NODE_EXTERN void SetErrno(uv_err_t err);
NODE_EXTERN v8::Handle<v8::Value>
MakeCallback(const v8::Handle<v8::Object> object,
Expand Down

0 comments on commit 1c20cac

Please sign in to comment.