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 _newIsolate() and _joinIsolate() to process object
  • Loading branch information
bnoordhuis committed Nov 23, 2011
1 parent 8ebe08a commit 2b0bdc8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/node.cc
Expand Up @@ -1690,6 +1690,52 @@ static Handle<Value> Binding(const Arguments& args) {
}


static void RunIsolate(void* arg) {
uv_loop_t* loop = uv_loop_new();
Isolate* isolate = Isolate::New(loop);
}


static char magic_isolate_cookie_[] = "magic isolate cookie";


static Handle<Value> NewIsolate(const Arguments& args) {
HandleScope scope;

uv_thread_t* tid = new uv_thread_t;

if (uv_thread_create(tid, RunIsolate, NULL))
return Null();

Local<ObjectTemplate> tpl = ObjectTemplate::New();
tpl->SetInternalFieldCount(2);

Local<Object> obj = tpl->NewInstance();
obj->SetPointerInInternalField(0, magic_isolate_cookie_);
obj->SetPointerInInternalField(1, tid);

return scope.Close(obj);
}


static Handle<Value> JoinIsolate(const Arguments& args) {
HandleScope scope;

assert(args[0]->IsObject());

Local<Object> obj = args[0]->ToObject();
assert(obj->InternalFieldCount() == 2);
assert(obj->GetPointerFromInternalField(0) == magic_isolate_cookie_);

uv_thread_t* tid = (uv_thread_t*) obj->GetPointerFromInternalField(1);

if (uv_thread_join(tid))
return False(); // error
else
return True(); // ok
}


static Handle<Value> ProcessTitleGetter(Local<String> property,
const AccessorInfo& info) {
HandleScope scope;
Expand Down Expand Up @@ -1953,6 +1999,9 @@ Handle<Object> SetupProcessObject(int argc, char *argv[]) {

NODE_SET_METHOD(process, "binding", Binding);

NODE_SET_METHOD(process, "_newIsolate", NewIsolate);
NODE_SET_METHOD(process, "_joinIsolate", JoinIsolate);

return process;
}

Expand Down
8 changes: 7 additions & 1 deletion src/node_isolate.cc
Expand Up @@ -35,9 +35,15 @@ Isolate* Isolate::New(uv_loop_t* loop) {


Isolate::Isolate(uv_loop_t* loop) {
SLIST_INIT(&at_exit_callbacks_);
loop_ = loop;

isolate_ = v8::Isolate::GetCurrent();
SLIST_INIT(&at_exit_callbacks_);
if (isolate_ == NULL) {
isolate_ = v8::Isolate::New();
isolate_->Enter();
}

assert(isolate_->GetData() == NULL);
isolate_->SetData(this);
}
Expand Down

5 comments on commit 2b0bdc8

@paddybyers
Copy link

Choose a reason for hiding this comment

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

I would like also to have a way to create an isolate and then enter its loop with a thread I created myself. I need this for Android, and this is how it works on the version I did. Were you planning to support something like this?

@bnoordhuis
Copy link
Member Author

Choose a reason for hiding this comment

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

The current interface is a stop-gap measure to have something to test against. I'm open to suggestions.

@paddybyers
Copy link

Choose a reason for hiding this comment

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

I went for minimum change to the current API - not necessarily because that's the best API, but to make it easier to track upstream. So I'm sure there is room to improve it.

Anyway, FWIW the Isolate API is here; https://github.com/paddybyers/node/blob/master-isolate/src/node.h#L128

This API forces you to create a thread - and I think your idea of having a method that includes creation of the thread is useful as well - and enter Isolate::Start(). You pass in an argv in the same way as to the regular node::Start(), or alternatively a uv_process_options_t, which is used for fork().

I think the best might be to have two entrypoints; one that creates the thread and one where the caller's thread enters the loop and blocks there.

Are there any special requirements on a caller-supplied thread that enters the loop? Joinable? Or only if the caller itself wants to join()?

@paddybyers
Copy link

Choose a reason for hiding this comment

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

I've thought about it some more, and I was wrong; I'm OK if the library creates the thread for me.

@bnoordhuis
Copy link
Member Author

Choose a reason for hiding this comment

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

Cool. I like the simplicity of that.

Please sign in to comment.