Browse Source

src: simplify handlewrap state tracking logic

This also updates the tests to expect that a closed handle has no
reference count.

PR-URL: https://github.com/nodejs/node/pull/6395
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
process-exit-stdio-flushing
Ben Noordhuis 9 years ago
parent
commit
a58d4839af
  1. 8
      src/handle_wrap.cc
  2. 8
      src/handle_wrap.h
  3. 2
      test/parallel/test-handle-wrap-isrefed-tty.js
  4. 16
      test/parallel/test-handle-wrap-isrefed.js

8
src/handle_wrap.cc

@ -35,10 +35,7 @@ void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
void HandleWrap::Unrefed(const FunctionCallbackInfo<Value>& args) { void HandleWrap::Unrefed(const FunctionCallbackInfo<Value>& args) {
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder()); HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder());
// XXX(bnoordhuis) It's debatable whether a nullptr wrap should count args.GetReturnValue().Set(!HasRef(wrap));
// as having a reference count but it's compatible with the logic that
// it replaces.
args.GetReturnValue().Set(wrap == nullptr || !HasRef(wrap));
} }
@ -51,6 +48,9 @@ void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
if (!IsAlive(wrap)) if (!IsAlive(wrap))
return; return;
if (wrap->state_ != kInitialized)
return;
CHECK_EQ(false, wrap->persistent().IsEmpty()); CHECK_EQ(false, wrap->persistent().IsEmpty());
uv_close(wrap->handle__, OnClose); uv_close(wrap->handle__, OnClose);
wrap->state_ = kClosing; wrap->state_ = kClosing;

8
src/handle_wrap.h

@ -38,15 +38,11 @@ class HandleWrap : public AsyncWrap {
static void Unrefed(const v8::FunctionCallbackInfo<v8::Value>& args); static void Unrefed(const v8::FunctionCallbackInfo<v8::Value>& args);
static inline bool IsAlive(const HandleWrap* wrap) { static inline bool IsAlive(const HandleWrap* wrap) {
// XXX(bnoordhuis) It's debatable whether only kInitialized should return wrap != nullptr && wrap->state_ != kClosed;
// count as alive but it's compatible with the check that it replaces.
return wrap != nullptr && wrap->state_ == kInitialized;
} }
static inline bool HasRef(const HandleWrap* wrap) { static inline bool HasRef(const HandleWrap* wrap) {
return wrap != nullptr && return IsAlive(wrap) && uv_has_ref(wrap->GetHandle());
wrap->state_ != kClosed &&
uv_has_ref(wrap->GetHandle());
} }
inline uv_handle_t* GetHandle() const { return handle__; } inline uv_handle_t* GetHandle() const { return handle__; }

2
test/parallel/test-handle-wrap-isrefed-tty.js

@ -19,7 +19,7 @@ if (process.argv[2] === 'child') {
assert(tty._handle.unrefed(), false); assert(tty._handle.unrefed(), false);
tty.unref(); tty.unref();
assert(tty._handle.unrefed(), true); assert(tty._handle.unrefed(), true);
tty._handle.close(); tty._handle.close(common.mustCall(() => assert(tty._handle.unrefed(), true)));
assert(tty._handle.unrefed(), true); assert(tty._handle.unrefed(), true);
return; return;
} }

16
test/parallel/test-handle-wrap-isrefed.js

@ -22,7 +22,7 @@ function makeAssert(message) {
assert(cp._handle.unrefed(), true); assert(cp._handle.unrefed(), true);
cp.ref(); cp.ref();
assert(cp._handle.unrefed(), false); assert(cp._handle.unrefed(), false);
cp._handle.close(); cp._handle.close(common.mustCall(() => assert(cp._handle.unrefed(), true)));
assert(cp._handle.unrefed(), false); assert(cp._handle.unrefed(), false);
} }
@ -39,7 +39,8 @@ function makeAssert(message) {
assert(sock4._handle.unrefed(), true); assert(sock4._handle.unrefed(), true);
sock4.ref(); sock4.ref();
assert(sock4._handle.unrefed(), false); assert(sock4._handle.unrefed(), false);
sock4._handle.close(); sock4._handle.close(
common.mustCall(() => assert(sock4._handle.unrefed(), true)));
assert(sock4._handle.unrefed(), false); assert(sock4._handle.unrefed(), false);
const sock6 = dgram.createSocket('udp6'); const sock6 = dgram.createSocket('udp6');
@ -49,7 +50,8 @@ function makeAssert(message) {
assert(sock6._handle.unrefed(), true); assert(sock6._handle.unrefed(), true);
sock6.ref(); sock6.ref();
assert(sock6._handle.unrefed(), false); assert(sock6._handle.unrefed(), false);
sock6._handle.close(); sock6._handle.close(
common.mustCall(() => assert(sock6._handle.unrefed(), true)));
assert(sock6._handle.unrefed(), false); assert(sock6._handle.unrefed(), false);
} }
@ -65,7 +67,7 @@ function makeAssert(message) {
assert(handle.unrefed(), true); assert(handle.unrefed(), true);
handle.ref(); handle.ref();
assert(handle.unrefed(), false); assert(handle.unrefed(), false);
handle.close(); handle.close(common.mustCall(() => assert(handle.unrefed(), true)));
assert(handle.unrefed(), false); assert(handle.unrefed(), false);
} }
@ -84,7 +86,8 @@ function makeAssert(message) {
server.ref(); server.ref();
assert(server._handle.unrefed(), false); assert(server._handle.unrefed(), false);
assert(server._unref, false); assert(server._unref, false);
server._handle.close(); server._handle.close(
common.mustCall(() => assert(server._handle.unrefed(), true)));
assert(server._handle.unrefed(), false); assert(server._handle.unrefed(), false);
} }
@ -98,6 +101,7 @@ function makeAssert(message) {
assert(timer._handle.unrefed(), true); assert(timer._handle.unrefed(), true);
timer.ref(); timer.ref();
assert(timer._handle.unrefed(), false); assert(timer._handle.unrefed(), false);
timer.close(); timer._handle.close(
common.mustCall(() => assert(timer._handle.unrefed(), true)));
assert(timer._handle.unrefed(), false); assert(timer._handle.unrefed(), false);
} }

Loading…
Cancel
Save