Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

isolates: add atexit() functionality for isolates

  • Loading branch information...
bnoordhuis committed Nov 23, 2011
1 parent 828ebb7 commit 60c0f917df26a65263d96d06f383cbca935dc8af
Showing with 793 additions and 3 deletions.
  1. +3 −1 src/node.cc
  2. +28 −0 src/node_isolate.cc
  3. +21 −2 src/node_isolate.h
  4. +741 −0 src/queue.h
@@ -2541,7 +2541,7 @@ int Start(int argc, char *argv[]) {
v8::Context::Scope context_scope(context);

// Create the main node::Isolate object
Isolate::New(uv_default_loop());
Isolate* isolate = Isolate::New(uv_default_loop());

Handle<Object> process = SetupProcessObject(argc, argv);
v8_typed_array::AttachBindings(context->Global());
@@ -2559,6 +2559,8 @@ int Start(int argc, char *argv[]) {

EmitExit(process);

isolate->Dispose();

#ifndef NDEBUG
// Clean up.
context.Dispose();
@@ -20,6 +20,9 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "node_isolate.h"

#include <stdlib.h>
#include <string.h>
#include <assert.h>


@@ -34,9 +37,34 @@ Isolate* Isolate::New(uv_loop_t* loop) {
Isolate::Isolate(uv_loop_t* loop) {
loop_ = loop;
isolate_ = v8::Isolate::GetCurrent();
SLIST_INIT(&at_exit_callbacks_);
assert(isolate_->GetData() == NULL);
isolate_->SetData(this);
}


void Isolate::AtExit(AtExitCallback callback, void* arg) {
struct AtExitCallbackInfo* it = new AtExitCallbackInfo;

NODE_ISOLATE_CHECK(this);

it->callback_ = callback;
it->arg_ = arg;

SLIST_INSERT_HEAD(&at_exit_callbacks_, it, entries_);
}


void Isolate::Dispose() {
struct AtExitCallbackInfo* it;

NODE_ISOLATE_CHECK(this);

SLIST_FOREACH(it, &at_exit_callbacks_, entries_) {
it->callback_(it->arg_);
delete it;
}
}


} // namespace node
@@ -22,8 +22,9 @@
#ifndef SRC_NODE_ISOLATE_H_
#define SRC_NODE_ISOLATE_H_

#include <v8.h>
#include <uv.h>
#include "queue.h"
#include "v8.h"
#include "uv.h"

#ifdef NDEBUG
# define NODE_ISOLATE_CHECK(ptr) ((void) (ptr))
@@ -42,6 +43,8 @@ namespace node {

class Isolate {
public:
typedef void (*AtExitCallback)(void* arg);

static Isolate* New(uv_loop_t* loop);

static Isolate* GetCurrent() {
@@ -58,8 +61,24 @@ class Isolate {
return isolate_;
}

/* Register a handler that should run when the current isolate exits.
* Handlers run in LIFO order.
*/
void AtExit(AtExitCallback callback, void *arg);

/* Shutdown the isolate. Call this method at thread death. */
void Dispose();

private:
Isolate(uv_loop_t* loop);

struct AtExitCallbackInfo {
SLIST_ENTRY(AtExitCallbackInfo) entries_;
AtExitCallback callback_;
void* arg_;
};

SLIST_HEAD(AtExitCallbacks, AtExitCallbackInfo) at_exit_callbacks_;
v8::Isolate* isolate_;
uv_loop_t* loop_;
};

0 comments on commit 60c0f91

Please sign in to comment.
You can’t perform that action at this time.