Skip to content

Commit a1a409a

Browse files
joyeecheungaddaleax
authored andcommitted
src: simplify Environment::HandleCleanup
- Make the HandleCleanup a struct, and make the queue a std::list, iterate over it in CleanupHandles() and clear it after that. - Put the handle cleanup registration into a method and document that they will not be called in the one environemt per process setup. PR-URL: #19319 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Jackson Tian <shyvo1987@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 855dabd commit a1a409a

File tree

3 files changed

+48
-51
lines changed

3 files changed

+48
-51
lines changed

src/env-inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ inline uv_idle_t* Environment::immediate_idle_handle() {
341341
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
342342
HandleCleanupCb cb,
343343
void *arg) {
344-
handle_cleanup_queue_.PushBack(new HandleCleanup(handle, cb, arg));
344+
handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
345345
}
346346

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

src/env.cc

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,34 @@ void Environment::Start(int argc,
175175
uv_unref(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
176176
uv_unref(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
177177

178-
auto close_and_finish = [](Environment* env, uv_handle_t* handle, void* arg) {
178+
// Register clean-up cb to be called to clean up the handles
179+
// when the environment is freed, note that they are not cleaned in
180+
// the one environment per process setup, but will be called in
181+
// FreeEnvironment.
182+
RegisterHandleCleanups();
183+
184+
if (start_profiler_idle_notifier) {
185+
StartProfilerIdleNotifier();
186+
}
187+
188+
auto process_template = FunctionTemplate::New(isolate());
189+
process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate(), "process"));
190+
191+
auto process_object =
192+
process_template->GetFunction()->NewInstance(context()).ToLocalChecked();
193+
set_process_object(process_object);
194+
195+
SetupProcessObject(this, argc, argv, exec_argc, exec_argv);
196+
LoadAsyncWrapperInfo(this);
197+
198+
static uv_once_t init_once = UV_ONCE_INIT;
199+
uv_once(&init_once, InitThreadLocalOnce);
200+
uv_key_set(&thread_local_env, this);
201+
}
202+
203+
void Environment::RegisterHandleCleanups() {
204+
HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t* handle,
205+
void* arg) {
179206
handle->data = env;
180207

181208
uv_close(handle, [](uv_handle_t* handle) {
@@ -199,32 +226,14 @@ void Environment::Start(int argc,
199226
reinterpret_cast<uv_handle_t*>(&idle_check_handle_),
200227
close_and_finish,
201228
nullptr);
202-
203-
if (start_profiler_idle_notifier) {
204-
StartProfilerIdleNotifier();
205-
}
206-
207-
auto process_template = FunctionTemplate::New(isolate());
208-
process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate(), "process"));
209-
210-
auto process_object =
211-
process_template->GetFunction()->NewInstance(context()).ToLocalChecked();
212-
set_process_object(process_object);
213-
214-
SetupProcessObject(this, argc, argv, exec_argc, exec_argv);
215-
LoadAsyncWrapperInfo(this);
216-
217-
static uv_once_t init_once = UV_ONCE_INIT;
218-
uv_once(&init_once, InitThreadLocalOnce);
219-
uv_key_set(&thread_local_env, this);
220229
}
221230

222231
void Environment::CleanupHandles() {
223-
while (HandleCleanup* hc = handle_cleanup_queue_.PopFront()) {
232+
for (HandleCleanup& hc : handle_cleanup_queue_) {
224233
handle_cleanup_waiting_++;
225-
hc->cb_(this, hc->handle_, hc->arg_);
226-
delete hc;
234+
hc.cb_(this, hc.handle_, hc.arg_);
227235
}
236+
handle_cleanup_queue_.clear();
228237

229238
while (handle_cleanup_waiting_ != 0)
230239
uv_run(event_loop(), UV_RUN_ONCE);

src/env.h

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -540,26 +540,6 @@ class Environment {
540540
DISALLOW_COPY_AND_ASSIGN(TickInfo);
541541
};
542542

543-
typedef void (*HandleCleanupCb)(Environment* env,
544-
uv_handle_t* handle,
545-
void* arg);
546-
547-
class HandleCleanup {
548-
private:
549-
friend class Environment;
550-
551-
HandleCleanup(uv_handle_t* handle, HandleCleanupCb cb, void* arg)
552-
: handle_(handle),
553-
cb_(cb),
554-
arg_(arg) {
555-
}
556-
557-
uv_handle_t* handle_;
558-
HandleCleanupCb cb_;
559-
void* arg_;
560-
ListNode<HandleCleanup> handle_cleanup_queue_;
561-
};
562-
563543
static inline Environment* GetCurrent(v8::Isolate* isolate);
564544
static inline Environment* GetCurrent(v8::Local<v8::Context> context);
565545
static inline Environment* GetCurrent(
@@ -580,7 +560,22 @@ class Environment {
580560
int exec_argc,
581561
const char* const* exec_argv,
582562
bool start_profiler_idle_notifier);
563+
564+
typedef void (*HandleCleanupCb)(Environment* env,
565+
uv_handle_t* handle,
566+
void* arg);
567+
struct HandleCleanup {
568+
uv_handle_t* handle_;
569+
HandleCleanupCb cb_;
570+
void* arg_;
571+
};
572+
573+
void RegisterHandleCleanups();
583574
void CleanupHandles();
575+
inline void RegisterHandleCleanup(uv_handle_t* handle,
576+
HandleCleanupCb cb,
577+
void *arg);
578+
inline void FinishHandleCleanup(uv_handle_t* handle);
584579

585580
inline void AssignToContext(v8::Local<v8::Context> context,
586581
const ContextInfo& info);
@@ -596,12 +591,6 @@ class Environment {
596591
inline uv_check_t* immediate_check_handle();
597592
inline uv_idle_t* immediate_idle_handle();
598593

599-
// Register clean-up cb to be called on environment destruction.
600-
inline void RegisterHandleCleanup(uv_handle_t* handle,
601-
HandleCleanupCb cb,
602-
void *arg);
603-
inline void FinishHandleCleanup(uv_handle_t* handle);
604-
605594
inline AsyncHooks* async_hooks();
606595
inline ImmediateInfo* immediate_info();
607596
inline TickInfo* tick_info();
@@ -822,8 +811,7 @@ class Environment {
822811
friend int GenDebugSymbols();
823812
HandleWrapQueue handle_wrap_queue_;
824813
ReqWrapQueue req_wrap_queue_;
825-
ListHead<HandleCleanup,
826-
&HandleCleanup::handle_cleanup_queue_> handle_cleanup_queue_;
814+
std::list<HandleCleanup> handle_cleanup_queue_;
827815
int handle_cleanup_waiting_;
828816

829817
double* heap_statistics_buffer_ = nullptr;

0 commit comments

Comments
 (0)