Skip to content

Commit 965274d

Browse files
bnoordhuisevanlucas
authored andcommitted
src: use libuv's refcounting directly
Don't implement an additional reference counting scheme on top of libuv. Libuv is the canonical source for that information so use it directly. PR-URL: #6395 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2000072 commit 965274d

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

src/handle_wrap.cc

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,25 @@ using v8::Value;
2121
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
2222
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder());
2323

24-
if (IsAlive(wrap)) {
25-
uv_ref(wrap->handle__);
26-
wrap->flags_ &= ~kUnref;
27-
}
24+
if (IsAlive(wrap))
25+
uv_ref(wrap->GetHandle());
2826
}
2927

3028

3129
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
3230
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder());
3331

34-
if (IsAlive(wrap)) {
35-
uv_unref(wrap->handle__);
36-
wrap->flags_ |= kUnref;
37-
}
32+
if (IsAlive(wrap))
33+
uv_unref(wrap->GetHandle());
3834
}
3935

4036

4137
void HandleWrap::Unrefed(const FunctionCallbackInfo<Value>& args) {
4238
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder());
43-
44-
bool unrefed = wrap->flags_ & kUnref == 1;
45-
args.GetReturnValue().Set(unrefed);
39+
// XXX(bnoordhuis) It's debatable whether a nullptr wrap should count
40+
// as having a reference count but it's compatible with the logic that
41+
// it replaces.
42+
args.GetReturnValue().Set(wrap == nullptr || !HasRef(wrap));
4643
}
4744

4845

@@ -51,17 +48,17 @@ void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
5148

5249
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder());
5350

54-
// guard against uninitialized handle or double close
51+
// Guard against uninitialized handle or double close.
5552
if (!IsAlive(wrap))
5653
return;
5754

5855
CHECK_EQ(false, wrap->persistent().IsEmpty());
5956
uv_close(wrap->handle__, OnClose);
60-
wrap->handle__ = nullptr;
57+
wrap->state_ = kClosing;
6158

6259
if (args[0]->IsFunction()) {
6360
wrap->object()->Set(env->onclose_string(), args[0]);
64-
wrap->flags_ |= kCloseCallback;
61+
wrap->state_ = kClosingWithCallback;
6562
}
6663
}
6764

@@ -72,7 +69,7 @@ HandleWrap::HandleWrap(Environment* env,
7269
AsyncWrap::ProviderType provider,
7370
AsyncWrap* parent)
7471
: AsyncWrap(env, object, provider, parent),
75-
flags_(0),
72+
state_(kInitialized),
7673
handle__(handle) {
7774
handle__->data = this;
7875
HandleScope scope(env->isolate());
@@ -90,22 +87,19 @@ void HandleWrap::OnClose(uv_handle_t* handle) {
9087
HandleWrap* wrap = static_cast<HandleWrap*>(handle->data);
9188
Environment* env = wrap->env();
9289
HandleScope scope(env->isolate());
90+
Context::Scope context_scope(env->context());
9391

9492
// The wrap object should still be there.
9593
CHECK_EQ(wrap->persistent().IsEmpty(), false);
94+
CHECK(wrap->state_ >= kClosing && wrap->state_ <= kClosingWithCallback);
9695

97-
// But the handle pointer should be gone.
98-
CHECK_EQ(wrap->handle__, nullptr);
96+
const bool have_close_callback = (wrap->state_ == kClosingWithCallback);
97+
wrap->state_ = kClosed;
9998

100-
HandleScope handle_scope(env->isolate());
101-
Context::Scope context_scope(env->context());
102-
Local<Object> object = wrap->object();
103-
104-
if (wrap->flags_ & kCloseCallback) {
99+
if (have_close_callback)
105100
wrap->MakeCallback(env->onclose_string(), 0, nullptr);
106-
}
107101

108-
object->SetAlignedPointerInInternalField(0, nullptr);
102+
wrap->object()->SetAlignedPointerInInternalField(0, nullptr);
109103
wrap->persistent().Reset();
110104
delete wrap;
111105
}

src/handle_wrap.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ class HandleWrap : public AsyncWrap {
3838
static void Unrefed(const v8::FunctionCallbackInfo<v8::Value>& args);
3939

4040
static inline bool IsAlive(const HandleWrap* wrap) {
41-
return wrap != nullptr && wrap->GetHandle() != nullptr;
41+
// XXX(bnoordhuis) It's debatable whether only kInitialized should
42+
// count as alive but it's compatible with the check that it replaces.
43+
return wrap != nullptr && wrap->state_ == kInitialized;
44+
}
45+
46+
static inline bool HasRef(const HandleWrap* wrap) {
47+
return wrap != nullptr &&
48+
wrap->state_ != kClosed &&
49+
uv_has_ref(wrap->GetHandle());
4250
}
4351

4452
inline uv_handle_t* GetHandle() const { return handle__; }
@@ -56,13 +64,10 @@ class HandleWrap : public AsyncWrap {
5664
friend void GetActiveHandles(const v8::FunctionCallbackInfo<v8::Value>&);
5765
static void OnClose(uv_handle_t* handle);
5866
ListNode<HandleWrap> handle_wrap_queue_;
59-
unsigned int flags_;
67+
enum { kInitialized, kClosing, kClosingWithCallback, kClosed } state_;
6068
// Using double underscore due to handle_ member in tcp_wrap. Probably
6169
// tcp_wrap should rename it's member to 'handle'.
62-
uv_handle_t* handle__;
63-
64-
static const unsigned int kUnref = 1;
65-
static const unsigned int kCloseCallback = 2;
70+
uv_handle_t* const handle__;
6671
};
6772

6873

src/node.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,7 @@ void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
17261726
Local<String> owner_sym = env->owner_string();
17271727

17281728
for (auto w : *env->handle_wrap_queue()) {
1729-
if (w->persistent().IsEmpty() || (w->flags_ & HandleWrap::kUnref))
1729+
if (w->persistent().IsEmpty() || !HandleWrap::HasRef(w))
17301730
continue;
17311731
Local<Object> object = w->object();
17321732
Local<Value> owner = object->Get(owner_sym);

0 commit comments

Comments
 (0)