Permalink
Browse files

Move process.stdout unref hack to handle_wrap.cc

See #1726
  • Loading branch information...
1 parent 0a42266 commit 7e62bc9828ab0cf322a0769702656f824fe8165c @ry ry committed Sep 27, 2011
Showing with 30 additions and 41 deletions.
  1. +23 −0 src/handle_wrap.cc
  2. +2 −0 src/handle_wrap.h
  3. +3 −9 src/node.js
  4. +0 −32 src/node_stdio.cc
  5. +1 −0 src/pipe_wrap.cc
  6. +1 −0 src/tty_wrap.cc
View
@@ -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;
@@ -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;
View
@@ -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);
@@ -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;
};
View
@@ -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':
@@ -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:
View
@@ -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;
@@ -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;
View
@@ -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);
View
@@ -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);

0 comments on commit 7e62bc9

Please sign in to comment.