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

Commit

Permalink
Move process.stdout unref hack to handle_wrap.cc
Browse files Browse the repository at this point in the history
See #1726
  • Loading branch information
ry committed Sep 27, 2011
1 parent 0a42266 commit 7e62bc9
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 41 deletions.
23 changes: 23 additions & 0 deletions src/handle_wrap.cc
Expand Up @@ -34,6 +34,23 @@ void HandleWrap::Initialize(Handle<Object> target) {
}


// This function is used only for process.stdout. It's put here instead of
// in TTYWrap because here we have access to the Close binding.
Handle<Value> HandleWrap::Unref(const Arguments& args) {
HandleScope scope;

UNWRAP

// Calling this function twice should never happen.
assert(wrap->unref == false);

wrap->unref = true;
uv_unref(uv_default_loop());

return v8::Undefined();
}


Handle<Value> HandleWrap::Close(const Arguments& args) {
HandleScope scope;

Expand All @@ -42,13 +59,19 @@ Handle<Value> HandleWrap::Close(const Arguments& args) {
assert(!wrap->object_.IsEmpty());
uv_close(wrap->handle__, OnClose);

if (wrap->unref) {
uv_ref(uv_default_loop());
wrap->unref = false;
}

wrap->StateChange();

return v8::Null();
}


HandleWrap::HandleWrap(Handle<Object> object, uv_handle_t* h) {
unref = false;
handle__ = h;
if (h) {
h->data = this;
Expand Down
2 changes: 2 additions & 0 deletions src/handle_wrap.h
Expand Up @@ -27,6 +27,7 @@ class HandleWrap {
public:
static void Initialize(v8::Handle<v8::Object> target);
static v8::Handle<v8::Value> Close(const v8::Arguments& args);
static v8::Handle<v8::Value> Unref(const v8::Arguments& args);

protected:
HandleWrap(v8::Handle<v8::Object> object, uv_handle_t* handle);
Expand All @@ -42,6 +43,7 @@ class HandleWrap {
// Using double underscore due to handle_ member in tcp_wrap. Probably
// tcp_wrap should rename it's member to 'handle'.
uv_handle_t* handle__;
bool unref;
};


Expand Down
12 changes: 3 additions & 9 deletions src/node.js
Expand Up @@ -235,12 +235,9 @@
stdout = new tty.WriteStream(fd);
stdout._type = "tty";

// FIXME Hack to have stdout not keep the event loop alive.
// Hack to have stdout not keep the event loop alive.
// See https://github.com/joyent/node/issues/1726
binding.unref();
stdout.on('close', function() {
binding.ref();
});
stdout._handle.unref();
break;

case 'FILE':
Expand All @@ -262,10 +259,7 @@

// FIXME Hack to have stdout not keep the event loop alive.
// See https://github.com/joyent/node/issues/1726
binding.unref();
stdout.on('close', function() {
binding.ref();
});
stdout._handle.unref();
break;

default:
Expand Down
32 changes: 0 additions & 32 deletions src/node_stdio.cc
Expand Up @@ -191,35 +191,6 @@ static Handle<Value> WriteError (const Arguments& args) {
}


// This exists to prevent process.stdout from keeping the event loop alive.
// It is only ever called in src/node.js during the initalization of
// process.stdout and will fail if called more than once. We do not want to
// expose uv_ref and uv_unref to javascript in general.
// This should be removed in the future!
static bool unref_called = false;
static Handle<Value> Unref(const Arguments& args) {
HandleScope scope;

assert(unref_called == false);

uv_unref(uv_default_loop());
unref_called = true;

return Null();
}


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

assert(unref_called == true);

uv_ref(uv_default_loop());

return Null();
}


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

Expand Down Expand Up @@ -347,9 +318,6 @@ void Stdio::Initialize(v8::Handle<v8::Object> target) {
NODE_SET_METHOD(target, "isatty", IsATTY);
NODE_SET_METHOD(target, "openpty", OpenPTY);

NODE_SET_METHOD(target, "unref", Unref);
NODE_SET_METHOD(target, "ref", Ref);

struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = HandleSIGCONT;
Expand Down
1 change: 1 addition & 0 deletions src/pipe_wrap.cc
Expand Up @@ -61,6 +61,7 @@ void PipeWrap::Initialize(Handle<Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(1);

NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);

NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
Expand Down
1 change: 1 addition & 0 deletions src/tty_wrap.cc
Expand Up @@ -45,6 +45,7 @@ class TTYWrap : StreamWrap {
t->InstanceTemplate()->SetInternalFieldCount(1);

NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);

NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
Expand Down

0 comments on commit 7e62bc9

Please sign in to comment.