Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Update addons.markdown #8461

Closed
wants to merge 3 commits into from
Closed

Update addons.markdown #8461

wants to merge 3 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Sep 28, 2014

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

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

Lets native addons register exit hooks that run after the event loop has quit
but before the VM is killed.

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.

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?

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
Copy link
Author

ghost commented Sep 30, 2014

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

@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
Copy link
Author

ghost commented Oct 1, 2014

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
Copy link
Member

indutny commented Oct 1, 2014

Yep, looking good.

Incorporated latest suggestions.
@ghost
Copy link
Author

ghost commented Oct 2, 2014

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

@trevnorris
Copy link

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

@trevnorris
Copy link

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
Copy link

JCMais commented Feb 28, 2015

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

IIRC it was because it make test-addon failed with this patch, but no one from core closed this issue. We were simply waiting for the patch to be fixed.

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