Skip to content

Commit 38dc0cd

Browse files
committed
src: switch from QUEUE to intrusive list
This commit also breaks up req_wrap.h into req-wrap.h and req-wrap-inl.h to work around a circular dependency issue in env.h. PR-URL: #667 Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent 58eb00c commit 38dc0cd

25 files changed

+171
-181
lines changed

node.gyp

+2-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@
149149
'src/tty_wrap.h',
150150
'src/tcp_wrap.h',
151151
'src/udp_wrap.h',
152-
'src/req_wrap.h',
152+
'src/req-wrap.h',
153+
'src/req-wrap-inl.h',
153154
'src/string_bytes.h',
154155
'src/stream_wrap.h',
155156
'src/tree.h',

src/async-wrap.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define SRC_ASYNC_WRAP_H_
33

44
#include "base-object.h"
5-
#include "env.h"
65
#include "v8.h"
76

87
namespace node {
@@ -31,6 +30,8 @@ namespace node {
3130
V(WRITEWRAP) \
3231
V(ZLIB)
3332

33+
class Environment;
34+
3435
class AsyncWrap : public BaseObject {
3536
public:
3637
enum ProviderType {

src/base-object-inl.h

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define SRC_BASE_OBJECT_INL_H_
33

44
#include "base-object.h"
5+
#include "env.h"
6+
#include "env-inl.h"
57
#include "util.h"
68
#include "util-inl.h"
79
#include "v8.h"

src/base-object.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#ifndef SRC_BASE_OBJECT_H_
22
#define SRC_BASE_OBJECT_H_
33

4-
#include "env.h"
54
#include "v8.h"
65

76
namespace node {
87

8+
class Environment;
9+
910
class BaseObject {
1011
public:
1112
BaseObject(Environment* env, v8::Local<v8::Object> handle);

src/cares_wrap.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#include "env.h"
66
#include "env-inl.h"
77
#include "node.h"
8-
#include "req_wrap.h"
8+
#include "req-wrap.h"
9+
#include "req-wrap-inl.h"
910
#include "tree.h"
1011
#include "util.h"
1112
#include "uv.h"

src/debug-agent.cc

+6-17
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "v8-debug.h"
3030
#include "util.h"
3131
#include "util-inl.h"
32-
#include "queue.h"
3332

3433
#include <string.h>
3534

@@ -64,8 +63,6 @@ Agent::Agent(Environment* env) : state_(kNone),
6463

6564
err = uv_mutex_init(&message_mutex_);
6665
CHECK_EQ(err, 0);
67-
68-
QUEUE_INIT(&messages_);
6966
}
7067

7168

@@ -75,13 +72,8 @@ Agent::~Agent() {
7572
uv_sem_destroy(&start_sem_);
7673
uv_mutex_destroy(&message_mutex_);
7774

78-
// Clean-up messages
79-
while (!QUEUE_EMPTY(&messages_)) {
80-
QUEUE* q = QUEUE_HEAD(&messages_);
81-
QUEUE_REMOVE(q);
82-
AgentMessage* msg = ContainerOf(&AgentMessage::member, q);
75+
while (AgentMessage* msg = messages_.PopFront())
8376
delete msg;
84-
}
8577
}
8678

8779

@@ -281,13 +273,9 @@ void Agent::ChildSignalCb(uv_async_t* signal) {
281273
Local<Object> api = PersistentToLocal(isolate, a->api_);
282274

283275
uv_mutex_lock(&a->message_mutex_);
284-
while (!QUEUE_EMPTY(&a->messages_)) {
285-
QUEUE* q = QUEUE_HEAD(&a->messages_);
286-
AgentMessage* msg = ContainerOf(&AgentMessage::member, q);
287-
276+
while (AgentMessage* msg = a->messages_.PopFront()) {
288277
// Time to close everything
289278
if (msg->data() == nullptr) {
290-
QUEUE_REMOVE(q);
291279
delete msg;
292280

293281
MakeCallback(isolate, api, "onclose", 0, nullptr);
@@ -296,10 +284,11 @@ void Agent::ChildSignalCb(uv_async_t* signal) {
296284

297285
// Waiting for client, do not send anything just yet
298286
// TODO(indutny): move this to js-land
299-
if (a->wait_)
287+
if (a->wait_) {
288+
a->messages_.PushFront(msg); // Push message back into the ready queue.
300289
break;
290+
}
301291

302-
QUEUE_REMOVE(q);
303292
Local<Value> argv[] = {
304293
String::NewFromTwoByte(isolate,
305294
msg->data(),
@@ -321,7 +310,7 @@ void Agent::ChildSignalCb(uv_async_t* signal) {
321310

322311
void Agent::EnqueueMessage(AgentMessage* message) {
323312
uv_mutex_lock(&message_mutex_);
324-
QUEUE_INSERT_TAIL(&messages_, &message->member);
313+
messages_.PushBack(message);
325314
uv_mutex_unlock(&message_mutex_);
326315
uv_async_send(&child_signal_);
327316
}

src/debug-agent.h

+28-29
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
#ifndef SRC_DEBUG_AGENT_H_
2323
#define SRC_DEBUG_AGENT_H_
2424

25+
#include "util.h"
26+
#include "util-inl.h"
2527
#include "uv.h"
2628
#include "v8.h"
2729
#include "v8-debug.h"
28-
#include "queue.h"
2930

3031
#include <string.h>
3132

@@ -37,7 +38,31 @@ class Environment;
3738
namespace node {
3839
namespace debugger {
3940

40-
class AgentMessage;
41+
class AgentMessage {
42+
public:
43+
AgentMessage(uint16_t* val, int length) : length_(length) {
44+
if (val == nullptr) {
45+
data_ = val;
46+
} else {
47+
data_ = new uint16_t[length];
48+
memcpy(data_, val, length * sizeof(*data_));
49+
}
50+
}
51+
52+
~AgentMessage() {
53+
delete[] data_;
54+
data_ = nullptr;
55+
}
56+
57+
inline const uint16_t* data() const { return data_; }
58+
inline int length() const { return length_; }
59+
60+
ListNode<AgentMessage> member;
61+
62+
private:
63+
uint16_t* data_;
64+
int length_;
65+
};
4166

4267
class Agent {
4368
public:
@@ -100,37 +125,11 @@ class Agent {
100125
uv_loop_t child_loop_;
101126
v8::Persistent<v8::Object> api_;
102127

103-
QUEUE messages_;
128+
ListHead<AgentMessage, &AgentMessage::member> messages_;
104129

105130
DispatchHandler dispatch_handler_;
106131
};
107132

108-
class AgentMessage {
109-
public:
110-
AgentMessage(uint16_t* val, int length) : length_(length) {
111-
if (val == nullptr) {
112-
data_ = val;
113-
} else {
114-
data_ = new uint16_t[length];
115-
memcpy(data_, val, length * sizeof(*data_));
116-
}
117-
}
118-
119-
~AgentMessage() {
120-
delete[] data_;
121-
data_ = nullptr;
122-
}
123-
124-
inline const uint16_t* data() const { return data_; }
125-
inline int length() const { return length_; }
126-
127-
QUEUE member;
128-
129-
private:
130-
uint16_t* data_;
131-
int length_;
132-
};
133-
134133
} // namespace debugger
135134
} // namespace node
136135

src/env-inl.h

+2-10
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,6 @@ inline Environment::Environment(v8::Local<v8::Context> context,
175175
set_binding_cache_object(v8::Object::New(isolate()));
176176
set_module_load_list_array(v8::Array::New(isolate()));
177177
RB_INIT(&cares_task_list_);
178-
QUEUE_INIT(&req_wrap_queue_);
179-
QUEUE_INIT(&handle_wrap_queue_);
180-
QUEUE_INIT(&handle_cleanup_queue_);
181178
handle_cleanup_waiting_ = 0;
182179
}
183180

@@ -193,11 +190,7 @@ inline Environment::~Environment() {
193190
}
194191

195192
inline void Environment::CleanupHandles() {
196-
while (!QUEUE_EMPTY(&handle_cleanup_queue_)) {
197-
QUEUE* q = QUEUE_HEAD(&handle_cleanup_queue_);
198-
QUEUE_REMOVE(q);
199-
200-
HandleCleanup* hc = ContainerOf(&HandleCleanup::handle_cleanup_queue_, q);
193+
while (HandleCleanup* hc = handle_cleanup_queue_.PopFront()) {
201194
handle_cleanup_waiting_++;
202195
hc->cb_(this, hc->handle_, hc->arg_);
203196
delete hc;
@@ -259,8 +252,7 @@ inline uv_check_t* Environment::idle_check_handle() {
259252
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
260253
HandleCleanupCb cb,
261254
void *arg) {
262-
HandleCleanup* hc = new HandleCleanup(handle, cb, arg);
263-
QUEUE_INSERT_TAIL(&handle_cleanup_queue_, &hc->handle_cleanup_queue_);
255+
handle_cleanup_queue_.PushBack(new HandleCleanup(handle, cb, arg));
264256
}
265257

266258
inline void Environment::FinishHandleCleanup(uv_handle_t* handle) {

src/env.h

+13-8
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
#include "ares.h"
55
#include "debug-agent.h"
6+
#include "handle_wrap.h"
7+
#include "req-wrap.h"
68
#include "tree.h"
79
#include "util.h"
810
#include "uv.h"
911
#include "v8.h"
10-
#include "queue.h"
1112

1213
#include <stdint.h>
1314

@@ -333,13 +334,12 @@ class Environment {
333334
: handle_(handle),
334335
cb_(cb),
335336
arg_(arg) {
336-
QUEUE_INIT(&handle_cleanup_queue_);
337337
}
338338

339339
uv_handle_t* handle_;
340340
HandleCleanupCb cb_;
341341
void* arg_;
342-
QUEUE handle_cleanup_queue_;
342+
ListNode<HandleCleanup> handle_cleanup_queue_;
343343
};
344344

345345
static inline Environment* GetCurrent(v8::Isolate* isolate);
@@ -453,8 +453,12 @@ class Environment {
453453
return &debugger_agent_;
454454
}
455455

456-
inline QUEUE* handle_wrap_queue() { return &handle_wrap_queue_; }
457-
inline QUEUE* req_wrap_queue() { return &req_wrap_queue_; }
456+
typedef ListHead<HandleWrap, &HandleWrap::handle_wrap_queue_> HandleWrapQueue;
457+
typedef ListHead<ReqWrap<uv_req_t>, &ReqWrap<uv_req_t>::req_wrap_queue_>
458+
ReqWrapQueue;
459+
460+
inline HandleWrapQueue* handle_wrap_queue() { return &handle_wrap_queue_; }
461+
inline ReqWrapQueue* req_wrap_queue() { return &req_wrap_queue_; }
458462

459463
private:
460464
static const int kIsolateSlot = NODE_ISOLATE_SLOT;
@@ -486,9 +490,10 @@ class Environment {
486490
bool printed_error_;
487491
debugger::Agent debugger_agent_;
488492

489-
QUEUE handle_wrap_queue_;
490-
QUEUE req_wrap_queue_;
491-
QUEUE handle_cleanup_queue_;
493+
HandleWrapQueue handle_wrap_queue_;
494+
ReqWrapQueue req_wrap_queue_;
495+
ListHead<HandleCleanup,
496+
&HandleCleanup::handle_cleanup_queue_> handle_cleanup_queue_;
492497
int handle_cleanup_waiting_;
493498

494499
v8::Persistent<v8::External> external_;

src/handle_wrap.cc

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "util.h"
77
#include "util-inl.h"
88
#include "node.h"
9-
#include "queue.h"
109

1110
namespace node {
1211

@@ -70,13 +69,12 @@ HandleWrap::HandleWrap(Environment* env,
7069
handle__->data = this;
7170
HandleScope scope(env->isolate());
7271
Wrap(object, this);
73-
QUEUE_INSERT_TAIL(env->handle_wrap_queue(), &handle_wrap_queue_);
72+
env->handle_wrap_queue()->PushBack(this);
7473
}
7574

7675

7776
HandleWrap::~HandleWrap() {
7877
CHECK(persistent().IsEmpty());
79-
QUEUE_REMOVE(&handle_wrap_queue_);
8078
}
8179

8280

src/handle_wrap.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
#define SRC_HANDLE_WRAP_H_
33

44
#include "async-wrap.h"
5-
#include "env.h"
6-
#include "node.h"
7-
#include "queue.h"
5+
#include "util.h"
86
#include "uv.h"
97
#include "v8.h"
108

119
namespace node {
1210

11+
class Environment;
12+
1313
// Rules:
1414
//
1515
// - Do not throw from handle methods. Set errno.
@@ -51,9 +51,10 @@ class HandleWrap : public AsyncWrap {
5151
virtual ~HandleWrap() override;
5252

5353
private:
54+
friend class Environment;
5455
friend void GetActiveHandles(const v8::FunctionCallbackInfo<v8::Value>&);
5556
static void OnClose(uv_handle_t* handle);
56-
QUEUE handle_wrap_queue_;
57+
ListNode<HandleWrap> handle_wrap_queue_;
5758
unsigned int flags_;
5859
// Using double underscore due to handle_ member in tcp_wrap. Probably
5960
// tcp_wrap should rename it's member to 'handle'.

0 commit comments

Comments
 (0)