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

Commit

Permalink
Browse files Browse the repository at this point in the history
isolates: add atexit() functionality for isolates
  • Loading branch information
bnoordhuis committed Nov 28, 2011
1 parent 828ebb7 commit 60c0f91
Show file tree
Hide file tree
Showing 4 changed files with 793 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/node.cc
Expand Up @@ -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());
Expand All @@ -2559,6 +2559,8 @@ int Start(int argc, char *argv[]) {

EmitExit(process);

isolate->Dispose();

#ifndef NDEBUG
// Clean up.
context.Dispose();
Expand Down
28 changes: 28 additions & 0 deletions src/node_isolate.cc
Expand Up @@ -20,6 +20,9 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "node_isolate.h"

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


Expand All @@ -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
23 changes: 21 additions & 2 deletions src/node_isolate.h
Expand Up @@ -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))
Expand All @@ -42,6 +43,8 @@ namespace node {

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

static Isolate* New(uv_loop_t* loop);

static Isolate* GetCurrent() {
Expand All @@ -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_;
};
Expand Down

0 comments on commit 60c0f91

Please sign in to comment.