Browse Source

Add link-list of all isolates

v0.7.4-release
Ryan Dahl 13 years ago
committed by Ben Noordhuis
parent
commit
4f46ee4400
  1. 10
      src/node.cc
  2. 35
      src/node_isolate.cc
  3. 4
      src/node_isolate.h

10
src/node.cc

@ -1894,7 +1894,10 @@ static void RunIsolate(void* arg) {
Isolate* isolate = Isolate::New();
StartThread(isolate, ti->argc_, ti->argv_);
isolate->Dispose();
delete ti;
delete isolate;
}
@ -1927,6 +1930,12 @@ static Handle<Value> NewIsolate(const Arguments& args) {
}
static Handle<Value> CountIsolate(const Arguments& args) {
HandleScope scope;
return scope.Close(Integer::New(Isolate::Count()));
}
static Handle<Value> JoinIsolate(const Arguments& args) {
HandleScope scope;
@ -2218,6 +2227,7 @@ Handle<Object> SetupProcessObject(int argc, char *argv[]) {
NODE_SET_METHOD(process, "binding", Binding);
NODE_SET_METHOD(process, "_newIsolate", NewIsolate);
NODE_SET_METHOD(process, "_countIsolate", CountIsolate);
NODE_SET_METHOD(process, "_joinIsolate", JoinIsolate);
return process;

35
src/node_isolate.cc

@ -29,14 +29,19 @@
namespace node {
static volatile bool initialized;
static volatile int id;
static uv_mutex_t id_lock;
static volatile int isolate_count;
static uv_mutex_t list_lock;
static ngx_queue_t list_head;
void Isolate::Initialize() {
if (!initialized) {
initialized = true;
if (uv_mutex_init(&id_lock)) abort();
if (uv_mutex_init(&list_lock)) abort();
ngx_queue_init(&list_head);
}
}
@ -46,12 +51,17 @@ Isolate* Isolate::New() {
}
int Isolate::Count() {
return isolate_count;
}
Isolate::Isolate() {
uv_mutex_lock(&list_lock);
assert(initialized && "node::Isolate::Initialize() hasn't been called");
uv_mutex_lock(&id_lock);
id_ = ++id;
uv_mutex_unlock(&id_lock);
if (id_ == 1) {
loop_ = uv_default_loop();
@ -61,6 +71,14 @@ Isolate::Isolate() {
ngx_queue_init(&at_exit_callbacks_);
ngx_queue_init(&list_member_);
// Add this isolate into the list of all isolates.
ngx_queue_insert_tail(&list_head, &list_member_);
isolate_count++;
uv_mutex_unlock(&list_lock);
v8_isolate_ = v8::Isolate::GetCurrent();
if (v8_isolate_ == NULL) {
v8_isolate_ = v8::Isolate::New();
@ -95,6 +113,8 @@ void Isolate::AtExit(AtExitCallback callback, void* arg) {
void Isolate::Dispose() {
uv_mutex_lock(&list_lock);
struct AtExitCallbackInfo* it;
ngx_queue_t* q;
@ -115,6 +135,13 @@ void Isolate::Dispose() {
v8_isolate_->Exit();
v8_isolate_->Dispose();
v8_isolate_ = NULL;
ngx_queue_remove(&list_member_);
isolate_count--;
assert(isolate_count >= 0);
assert(isolate_count > 0 || ngx_queue_empty(&list_head));
uv_mutex_unlock(&list_lock);
}

4
src/node_isolate.h

@ -45,6 +45,7 @@ class Isolate {
public:
// Call this before instantiating any Isolate
static void Initialize();
static int Count();
typedef void (*AtExitCallback)(void* arg);
@ -95,6 +96,9 @@ private:
v8::Isolate* v8_isolate_;
uv_loop_t* loop_;
// Each isolate is a member of the static list_head.
ngx_queue_t list_member_;
// Global variables for this isolate.
struct globals globals_;
};

Loading…
Cancel
Save