Update addons.markdown · Pull Request #8461 · nodejs/node-v0.x-archive · GitHub
Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Update addons.markdown#8461

Closed
ghost wants to merge 3 commits into
masterfrom
unknown repository
Closed

Update addons.markdown#8461
ghost wants to merge 3 commits into
masterfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Sep 28, 2014

Copy link
Copy Markdown

Updates addons.markdown to mention AtExit() function. Addresses issue #8324.

Updates addons.markdown to mention AtExit() function.  Addresses issue #8324.
Comment thread doc/api/addons.markdown Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be best phrased as "Registers exit hooks that run after the event loop has ended, but before the VM is killed." Also, it would be good to include the arguments, argument types, and a quick code example of how you might
use the function.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AtExit functions only run in vm, not when main node is exiting?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sam-github the documentation is pulled from the text of the commit that introduces the function -- I think it runs right before the process ends.

Updated description, added arguments and arguments types, and quick code example.
@ghost

ghost commented Sep 30, 2014

Copy link
Copy Markdown
Author

How about:

Registers exit hooks that run after the event loop has ended, but before the VM is killed.
Callbacks are run in reverse order of registration, i.e. newest first. AtExit takes callback
and its arguments as arguments. See implementation below:

 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;
 }

The file binding.cc implements AtExit below:

 #undef NDEBUG
 #include <assert.h>
 #include <stdlib.h>
 #include <node.h>
 #include <v8.h>

 using node::AtExit;
 using v8::Handle;
 using v8::HandleScope;
 using v8::Local;
 using v8::Object;

 static char cookie[] = "yum yum";
 static int at_exit_cb1_called = 0;
 static int at_exit_cb2_called = 0;

 static void at_exit_cb1(void* arg) {
   HandleScope scope;
   assert(arg == 0);
   Local<Object> obj = Object::New();
   assert(!obj.IsEmpty()); // assert VM is still alive
   assert(obj->IsObject());
   at_exit_cb1_called++;
 }

 static void at_exit_cb2(void* arg) {
   assert(arg == static_cast<void*>(cookie));
   at_exit_cb2_called++;
 }

 static void sanity_check(void) {
   assert(at_exit_cb1_called == 1);
   assert(at_exit_cb2_called == 2);
 }

 void init(Handle<Object> target) {
   AtExit(at_exit_cb1);
   AtExit(at_exit_cb2, cookie);
   AtExit(at_exit_cb2, cookie);
   atexit(sanity_check);
 }

 NODE_MODULE(binding, init);

Test in JavaScript by running:

 var binding = require('./build/Release/binding');

@chrisdickinson

Copy link
Copy Markdown

@sjsharp great work! The only other thing I'd add is a list of the arguments after the function definition -- something like we do for javascript:

### void AtExit(callback, args)

* `callback`: `void (*)(void*)` - A pointer to the function to call at exit.
* `args`: `void*` - A pointer to pass to the callback at exit.

@ghost

ghost commented Oct 1, 2014

Copy link
Copy Markdown
Author

Like this?

void AtExit(callback, args)

  • callback: void (*)(void*) - A pointer to the function to call at exit.
  • args: void* - A pointer to pass to the callback at exit.

Registers exit hooks that run after the event loop has ended, but before the VM is killed.

Callbacks are run in reverse order of registration, i.e. newest first. AtExit takes callback
and its arguments as arguments. See implementation below:

  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;
  }

The file binding.cc implements AtExit below:

  #undef NDEBUG
  #include <assert.h>
  #include <stdlib.h>
  #include <node.h>
  #include <v8.h>

  using node::AtExit;
  using v8::Handle;
  using v8::HandleScope;
  using v8::Local;
  using v8::Object;

  static char cookie[] = "yum yum";
  static int at_exit_cb1_called = 0;
  static int at_exit_cb2_called = 0;

  static void at_exit_cb1(void* arg) {
    HandleScope scope;
    assert(arg == 0);
    Local<Object> obj = Object::New();
    assert(!obj.IsEmpty()); // assert VM is still alive
    assert(obj->IsObject());
    at_exit_cb1_called++;
  }

  static void at_exit_cb2(void* arg) {
    assert(arg == static_cast<void*>(cookie));
    at_exit_cb2_called++;
  }

  static void sanity_check(void) {
    assert(at_exit_cb1_called == 1);
    assert(at_exit_cb2_called == 2);
  }

  void init(Handle<Object> target) {
    AtExit(at_exit_cb1);
    AtExit(at_exit_cb2, cookie);
    AtExit(at_exit_cb2, cookie);
    atexit(sanity_check);
  }

  NODE_MODULE(binding, init);

Test in JavaScript by running:

  var binding = require('./build/Release/binding');

@indutny

indutny commented Oct 1, 2014

Copy link
Copy Markdown
Member

Yep, looking good.

Incorporated latest suggestions.
@ghost

ghost commented Oct 2, 2014

Copy link
Copy Markdown
Author

Pushed changes to patch-1 branch on sjsharp/node.

@trevnorris

Copy link
Copy Markdown

Run make test-addon and ensure all tests pass please.

@trevnorris

Copy link
Copy Markdown

Future note to whoever merges this: Add the following in the commit message:

Fixes: https://github.com/joyent/node/issues/8324

@ghost ghost closed this Nov 25, 2014
@JCMais

JCMais commented Feb 28, 2015

Copy link
Copy Markdown

What is the status here? Why it was not merged?

This function is very handy for addons to cleanup resources, and it's still undocumented.

@trevnorris

Copy link
Copy Markdown

This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants