src: cleanup per env handles directly without a list
Environment handles can be cleaned up directly without saving the references in a list and iterate the list. PR-URL: https://github.com/nodejs/node/pull/54993 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
parent
62383cd113
commit
8b8fc53c9a
@ -235,12 +235,6 @@ inline uv_idle_t* Environment::immediate_idle_handle() {
|
|||||||
return &immediate_idle_handle_;
|
return &immediate_idle_handle_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
|
|
||||||
HandleCleanupCb cb,
|
|
||||||
void* arg) {
|
|
||||||
handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, typename OnCloseCallback>
|
template <typename T, typename OnCloseCallback>
|
||||||
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
|
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
|
||||||
handle_cleanup_waiting_++;
|
handle_cleanup_waiting_++;
|
||||||
|
40
src/env.cc
40
src/env.cc
@ -1099,13 +1099,8 @@ void Environment::InitializeLibuv() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register clean-up cb to be called to clean up the handles
|
|
||||||
// when the environment is freed, note that they are not cleaned in
|
|
||||||
// the one environment per process setup, but will be called in
|
|
||||||
// FreeEnvironment.
|
|
||||||
RegisterHandleCleanups();
|
|
||||||
|
|
||||||
StartProfilerIdleNotifier();
|
StartProfilerIdleNotifier();
|
||||||
|
env_handle_initialized_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Environment::InitializeCompileCache() {
|
void Environment::InitializeCompileCache() {
|
||||||
@ -1183,27 +1178,27 @@ void Environment::ExitEnv(StopFlags::Flags flags) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Environment::RegisterHandleCleanups() {
|
void Environment::ClosePerEnvHandles() {
|
||||||
HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t* handle,
|
// If LoadEnvironment and InitializeLibuv are not called, like when building
|
||||||
void* arg) {
|
// snapshots, skip closing the per environment handles.
|
||||||
handle->data = env;
|
if (!env_handle_initialized_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
env->CloseHandle(handle, [](uv_handle_t* handle) {
|
auto close_and_finish = [&](uv_handle_t* handle) {
|
||||||
|
CloseHandle(handle, [](uv_handle_t* handle) {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
memset(handle, 0xab, uv_handle_size(handle->type));
|
memset(handle, 0xab, uv_handle_size(handle->type));
|
||||||
#endif
|
#endif
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
auto register_handle = [&](uv_handle_t* handle) {
|
close_and_finish(reinterpret_cast<uv_handle_t*>(timer_handle()));
|
||||||
RegisterHandleCleanup(handle, close_and_finish, nullptr);
|
close_and_finish(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
|
||||||
};
|
close_and_finish(reinterpret_cast<uv_handle_t*>(immediate_idle_handle()));
|
||||||
register_handle(reinterpret_cast<uv_handle_t*>(timer_handle()));
|
close_and_finish(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
|
||||||
register_handle(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
|
close_and_finish(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
|
||||||
register_handle(reinterpret_cast<uv_handle_t*>(immediate_idle_handle()));
|
close_and_finish(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
|
||||||
register_handle(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
|
|
||||||
register_handle(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
|
|
||||||
register_handle(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Environment::CleanupHandles() {
|
void Environment::CleanupHandles() {
|
||||||
@ -1223,10 +1218,6 @@ void Environment::CleanupHandles() {
|
|||||||
for (HandleWrap* handle : handle_wrap_queue_)
|
for (HandleWrap* handle : handle_wrap_queue_)
|
||||||
handle->Close();
|
handle->Close();
|
||||||
|
|
||||||
for (HandleCleanup& hc : handle_cleanup_queue_)
|
|
||||||
hc.cb_(this, hc.handle_, hc.arg_);
|
|
||||||
handle_cleanup_queue_.clear();
|
|
||||||
|
|
||||||
while (handle_cleanup_waiting_ != 0 ||
|
while (handle_cleanup_waiting_ != 0 ||
|
||||||
request_waiting_ != 0 ||
|
request_waiting_ != 0 ||
|
||||||
!handle_wrap_queue_.IsEmpty()) {
|
!handle_wrap_queue_.IsEmpty()) {
|
||||||
@ -1280,6 +1271,7 @@ MaybeLocal<Value> Environment::RunSnapshotDeserializeMain() const {
|
|||||||
void Environment::RunCleanup() {
|
void Environment::RunCleanup() {
|
||||||
started_cleanup_ = true;
|
started_cleanup_ = true;
|
||||||
TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunCleanup");
|
TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunCleanup");
|
||||||
|
ClosePerEnvHandles();
|
||||||
// Only BaseObject's cleanups are registered as per-realm cleanup hooks now.
|
// Only BaseObject's cleanups are registered as per-realm cleanup hooks now.
|
||||||
// Defer the BaseObject cleanup after handles are cleaned up.
|
// Defer the BaseObject cleanup after handles are cleaned up.
|
||||||
CleanupHandles();
|
CleanupHandles();
|
||||||
|
19
src/env.h
19
src/env.h
@ -678,24 +678,10 @@ class Environment final : public MemoryRetainer {
|
|||||||
inline const std::vector<std::string>& argv();
|
inline const std::vector<std::string>& argv();
|
||||||
const std::string& exec_path() const;
|
const std::string& exec_path() const;
|
||||||
|
|
||||||
typedef void (*HandleCleanupCb)(Environment* env,
|
|
||||||
uv_handle_t* handle,
|
|
||||||
void* arg);
|
|
||||||
struct HandleCleanup {
|
|
||||||
uv_handle_t* handle_;
|
|
||||||
HandleCleanupCb cb_;
|
|
||||||
void* arg_;
|
|
||||||
};
|
|
||||||
|
|
||||||
void RegisterHandleCleanups();
|
|
||||||
void CleanupHandles();
|
void CleanupHandles();
|
||||||
void Exit(ExitCode code);
|
void Exit(ExitCode code);
|
||||||
void ExitEnv(StopFlags::Flags flags);
|
void ExitEnv(StopFlags::Flags flags);
|
||||||
|
void ClosePerEnvHandles();
|
||||||
// Register clean-up cb to be called on environment destruction.
|
|
||||||
inline void RegisterHandleCleanup(uv_handle_t* handle,
|
|
||||||
HandleCleanupCb cb,
|
|
||||||
void* arg);
|
|
||||||
|
|
||||||
template <typename T, typename OnCloseCallback>
|
template <typename T, typename OnCloseCallback>
|
||||||
inline void CloseHandle(T* handle, OnCloseCallback callback);
|
inline void CloseHandle(T* handle, OnCloseCallback callback);
|
||||||
@ -1100,6 +1086,8 @@ class Environment final : public MemoryRetainer {
|
|||||||
std::list<binding::DLib> loaded_addons_;
|
std::list<binding::DLib> loaded_addons_;
|
||||||
v8::Isolate* const isolate_;
|
v8::Isolate* const isolate_;
|
||||||
IsolateData* const isolate_data_;
|
IsolateData* const isolate_data_;
|
||||||
|
|
||||||
|
bool env_handle_initialized_ = false;
|
||||||
uv_timer_t timer_handle_;
|
uv_timer_t timer_handle_;
|
||||||
uv_check_t immediate_check_handle_;
|
uv_check_t immediate_check_handle_;
|
||||||
uv_idle_t immediate_idle_handle_;
|
uv_idle_t immediate_idle_handle_;
|
||||||
@ -1212,7 +1200,6 @@ class Environment final : public MemoryRetainer {
|
|||||||
CleanableQueue cleanable_queue_;
|
CleanableQueue cleanable_queue_;
|
||||||
HandleWrapQueue handle_wrap_queue_;
|
HandleWrapQueue handle_wrap_queue_;
|
||||||
ReqWrapQueue req_wrap_queue_;
|
ReqWrapQueue req_wrap_queue_;
|
||||||
std::list<HandleCleanup> handle_cleanup_queue_;
|
|
||||||
int handle_cleanup_waiting_ = 0;
|
int handle_cleanup_waiting_ = 0;
|
||||||
int request_waiting_ = 0;
|
int request_waiting_ = 0;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user