Skip to content

Commit 885ed6c

Browse files
committed
src: consolidate environment cleanup queue
Each Realm tracks its own cleanup hooks and drain the hooks when it is going to be destroyed. Moves the implementations of the cleanup queue to its own class so that it can be used in `node::Realm` too.
1 parent ab89024 commit 885ed6c

File tree

8 files changed

+195
-99
lines changed

8 files changed

+195
-99
lines changed

node.gyp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@
466466
'src/api/utils.cc',
467467
'src/async_wrap.cc',
468468
'src/cares_wrap.cc',
469+
'src/cleanup_queue.cc',
469470
'src/connect_wrap.cc',
470471
'src/connection_wrap.cc',
471472
'src/debug_utils.cc',
@@ -566,6 +567,8 @@
566567
'src/base64-inl.h',
567568
'src/callback_queue.h',
568569
'src/callback_queue-inl.h',
570+
'src/cleanup_queue.h',
571+
'src/cleanup_queue-inl.h',
569572
'src/connect_wrap.h',
570573
'src/connection_wrap.h',
571574
'src/debug_utils.h',

src/base_object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class BaseObject : public MemoryRetainer {
175175
// position of members in memory are predictable. For more information please
176176
// refer to `doc/contributing/node-postmortem-support.md`
177177
friend int GenDebugSymbols();
178-
friend class CleanupHookCallback;
178+
friend class CleanupQueue;
179179
template <typename T, bool kIsWeak>
180180
friend class BaseObjectPtrImpl;
181181

src/cleanup_queue-inl.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef SRC_CLEANUP_QUEUE_INL_H_
2+
#define SRC_CLEANUP_QUEUE_INL_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include "base_object.h"
7+
#include "cleanup_queue.h"
8+
#include "util.h"
9+
10+
namespace node {
11+
12+
inline size_t CleanupQueue::SelfSize() const {
13+
return cleanup_hooks_.size() * sizeof(CleanupHookCallback);
14+
}
15+
16+
bool CleanupQueue::empty() const {
17+
return cleanup_hooks_.empty();
18+
}
19+
20+
void CleanupQueue::Add(Callback cb, void* arg) {
21+
auto insertion_info = cleanup_hooks_.emplace(
22+
CleanupHookCallback{cb, arg, cleanup_hook_counter_++});
23+
// Make sure there was no existing element with these values.
24+
CHECK_EQ(insertion_info.second, true);
25+
}
26+
27+
void CleanupQueue::Remove(Callback cb, void* arg) {
28+
CleanupHookCallback search{cb, arg, 0};
29+
cleanup_hooks_.erase(search);
30+
}
31+
32+
template <typename T>
33+
void CleanupQueue::ForEachBaseObject(T&& iterator) {
34+
for (const auto& hook : cleanup_hooks_) {
35+
BaseObject* obj = GetBaseObject(hook);
36+
if (obj != nullptr) iterator(obj);
37+
}
38+
}
39+
40+
BaseObject* CleanupQueue::GetBaseObject(
41+
const CleanupHookCallback& callback) const {
42+
if (callback.fn_ == BaseObject::DeleteMe)
43+
return static_cast<BaseObject*>(callback.arg_);
44+
else
45+
return nullptr;
46+
}
47+
48+
} // namespace node
49+
50+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
51+
52+
#endif // SRC_CLEANUP_QUEUE_INL_H_

src/cleanup_queue.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "cleanup_queue.h" // NOLINT(build/include_inline)
2+
#include <vector>
3+
#include "cleanup_queue-inl.h"
4+
5+
namespace node {
6+
7+
void CleanupQueue::Drain() {
8+
// Copy into a vector, since we can't sort an unordered_set in-place.
9+
std::vector<CleanupHookCallback> callbacks(cleanup_hooks_.begin(),
10+
cleanup_hooks_.end());
11+
// We can't erase the copied elements from `cleanup_hooks_` yet, because we
12+
// need to be able to check whether they were un-scheduled by another hook.
13+
14+
std::sort(callbacks.begin(),
15+
callbacks.end(),
16+
[](const CleanupHookCallback& a, const CleanupHookCallback& b) {
17+
// Sort in descending order so that the most recently inserted
18+
// callbacks are run first.
19+
return a.insertion_order_counter_ > b.insertion_order_counter_;
20+
});
21+
22+
for (const CleanupHookCallback& cb : callbacks) {
23+
if (cleanup_hooks_.count(cb) == 0) {
24+
// This hook was removed from the `cleanup_hooks_` set during another
25+
// hook that was run earlier. Nothing to do here.
26+
continue;
27+
}
28+
29+
cb.fn_(cb.arg_);
30+
cleanup_hooks_.erase(cb);
31+
}
32+
}
33+
34+
size_t CleanupQueue::CleanupHookCallback::Hash::operator()(
35+
const CleanupHookCallback& cb) const {
36+
return std::hash<void*>()(cb.arg_);
37+
}
38+
39+
bool CleanupQueue::CleanupHookCallback::Equal::operator()(
40+
const CleanupHookCallback& a, const CleanupHookCallback& b) const {
41+
return a.fn_ == b.fn_ && a.arg_ == b.arg_;
42+
}
43+
44+
} // namespace node

src/cleanup_queue.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#ifndef SRC_CLEANUP_QUEUE_H_
2+
#define SRC_CLEANUP_QUEUE_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include <cstddef>
7+
#include <cstdint>
8+
#include <unordered_set>
9+
10+
#include "memory_tracker.h"
11+
12+
namespace node {
13+
14+
class BaseObject;
15+
16+
class CleanupQueue : public MemoryRetainer {
17+
public:
18+
typedef void (*Callback)(void*);
19+
20+
CleanupQueue() {}
21+
22+
// Not copyable.
23+
CleanupQueue(const CleanupQueue&) = delete;
24+
25+
SET_MEMORY_INFO_NAME(CleanupQueue)
26+
SET_NO_MEMORY_INFO()
27+
inline size_t SelfSize() const override;
28+
29+
inline bool empty() const;
30+
31+
inline void Add(Callback cb, void* arg);
32+
inline void Remove(Callback cb, void* arg);
33+
void Drain();
34+
35+
template <typename T>
36+
inline void ForEachBaseObject(T&& iterator);
37+
38+
private:
39+
class CleanupHookCallback {
40+
public:
41+
CleanupHookCallback(Callback fn,
42+
void* arg,
43+
uint64_t insertion_order_counter)
44+
: fn_(fn),
45+
arg_(arg),
46+
insertion_order_counter_(insertion_order_counter) {}
47+
48+
// Only hashes `arg_`, since that is usually enough to identify the hook.
49+
struct Hash {
50+
size_t operator()(const CleanupHookCallback& cb) const;
51+
};
52+
53+
// Compares by `fn_` and `arg_` being equal.
54+
struct Equal {
55+
bool operator()(const CleanupHookCallback& a,
56+
const CleanupHookCallback& b) const;
57+
};
58+
59+
private:
60+
friend class CleanupQueue;
61+
Callback fn_;
62+
void* arg_;
63+
64+
// We keep track of the insertion order for these objects, so that we can
65+
// call the callbacks in reverse order when we are cleaning up.
66+
uint64_t insertion_order_counter_;
67+
};
68+
69+
inline BaseObject* GetBaseObject(const CleanupHookCallback& callback) const;
70+
71+
// Use an unordered_set, so that we have efficient insertion and removal.
72+
std::unordered_set<CleanupHookCallback,
73+
CleanupHookCallback::Hash,
74+
CleanupHookCallback::Equal>
75+
cleanup_hooks_;
76+
uint64_t cleanup_hook_counter_ = 0;
77+
};
78+
79+
} // namespace node
80+
81+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
82+
83+
#endif // SRC_CLEANUP_QUEUE_H_

src/env-inl.h

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -787,43 +787,17 @@ inline void Environment::ThrowUVException(int errorno,
787787
UVException(isolate(), errorno, syscall, message, path, dest));
788788
}
789789

790-
void Environment::AddCleanupHook(CleanupCallback fn, void* arg) {
791-
auto insertion_info = cleanup_hooks_.emplace(CleanupHookCallback {
792-
fn, arg, cleanup_hook_counter_++
793-
});
794-
// Make sure there was no existing element with these values.
795-
CHECK_EQ(insertion_info.second, true);
796-
}
797-
798-
void Environment::RemoveCleanupHook(CleanupCallback fn, void* arg) {
799-
CleanupHookCallback search { fn, arg, 0 };
800-
cleanup_hooks_.erase(search);
801-
}
802-
803-
size_t CleanupHookCallback::Hash::operator()(
804-
const CleanupHookCallback& cb) const {
805-
return std::hash<void*>()(cb.arg_);
790+
void Environment::AddCleanupHook(CleanupQueue::Callback fn, void* arg) {
791+
cleanup_queue_.Add(fn, arg);
806792
}
807793

808-
bool CleanupHookCallback::Equal::operator()(
809-
const CleanupHookCallback& a, const CleanupHookCallback& b) const {
810-
return a.fn_ == b.fn_ && a.arg_ == b.arg_;
811-
}
812-
813-
BaseObject* CleanupHookCallback::GetBaseObject() const {
814-
if (fn_ == BaseObject::DeleteMe)
815-
return static_cast<BaseObject*>(arg_);
816-
else
817-
return nullptr;
794+
void Environment::RemoveCleanupHook(CleanupQueue::Callback fn, void* arg) {
795+
cleanup_queue_.Remove(fn, arg);
818796
}
819797

820798
template <typename T>
821799
void Environment::ForEachBaseObject(T&& iterator) {
822-
for (const auto& hook : cleanup_hooks_) {
823-
BaseObject* obj = hook.GetBaseObject();
824-
if (obj != nullptr)
825-
iterator(obj);
826-
}
800+
cleanup_queue_.ForEachBaseObject(std::forward<T>(iterator));
827801
}
828802

829803
void Environment::modify_base_object_count(int64_t delta) {

src/env.cc

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,33 +1038,10 @@ void Environment::RunCleanup() {
10381038
bindings_.clear();
10391039
CleanupHandles();
10401040

1041-
while (!cleanup_hooks_.empty() ||
1042-
native_immediates_.size() > 0 ||
1041+
while (!cleanup_queue_.empty() || native_immediates_.size() > 0 ||
10431042
native_immediates_threadsafe_.size() > 0 ||
10441043
native_immediates_interrupts_.size() > 0) {
1045-
// Copy into a vector, since we can't sort an unordered_set in-place.
1046-
std::vector<CleanupHookCallback> callbacks(
1047-
cleanup_hooks_.begin(), cleanup_hooks_.end());
1048-
// We can't erase the copied elements from `cleanup_hooks_` yet, because we
1049-
// need to be able to check whether they were un-scheduled by another hook.
1050-
1051-
std::sort(callbacks.begin(), callbacks.end(),
1052-
[](const CleanupHookCallback& a, const CleanupHookCallback& b) {
1053-
// Sort in descending order so that the most recently inserted callbacks
1054-
// are run first.
1055-
return a.insertion_order_counter_ > b.insertion_order_counter_;
1056-
});
1057-
1058-
for (const CleanupHookCallback& cb : callbacks) {
1059-
if (cleanup_hooks_.count(cb) == 0) {
1060-
// This hook was removed from the `cleanup_hooks_` set during another
1061-
// hook that was run earlier. Nothing to do here.
1062-
continue;
1063-
}
1064-
1065-
cb.fn_(cb.arg_);
1066-
cleanup_hooks_.erase(cb);
1067-
}
1044+
cleanup_queue_.Drain();
10681045
CleanupHandles();
10691046
}
10701047

@@ -2022,8 +1999,7 @@ void Environment::MemoryInfo(MemoryTracker* tracker) const {
20221999
tracker->TrackField("should_abort_on_uncaught_toggle",
20232000
should_abort_on_uncaught_toggle_);
20242001
tracker->TrackField("stream_base_state", stream_base_state_);
2025-
tracker->TrackFieldWithSize(
2026-
"cleanup_hooks", cleanup_hooks_.size() * sizeof(CleanupHookCallback));
2002+
tracker->TrackField("cleanup_queue", cleanup_queue_);
20272003
tracker->TrackField("async_hooks", async_hooks_);
20282004
tracker->TrackField("immediate_info", immediate_info_);
20292005
tracker->TrackField("tick_info", tick_info_);

src/env.h

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "inspector_profiler.h"
3131
#endif
3232
#include "callback_queue.h"
33+
#include "cleanup_queue-inl.h"
3334
#include "debug_utils.h"
3435
#include "handle_wrap.h"
3536
#include "node.h"
@@ -922,38 +923,6 @@ class ShouldNotAbortOnUncaughtScope {
922923
Environment* env_;
923924
};
924925

925-
class CleanupHookCallback {
926-
public:
927-
typedef void (*Callback)(void*);
928-
929-
CleanupHookCallback(Callback fn,
930-
void* arg,
931-
uint64_t insertion_order_counter)
932-
: fn_(fn), arg_(arg), insertion_order_counter_(insertion_order_counter) {}
933-
934-
// Only hashes `arg_`, since that is usually enough to identify the hook.
935-
struct Hash {
936-
inline size_t operator()(const CleanupHookCallback& cb) const;
937-
};
938-
939-
// Compares by `fn_` and `arg_` being equal.
940-
struct Equal {
941-
inline bool operator()(const CleanupHookCallback& a,
942-
const CleanupHookCallback& b) const;
943-
};
944-
945-
inline BaseObject* GetBaseObject() const;
946-
947-
private:
948-
friend class Environment;
949-
Callback fn_;
950-
void* arg_;
951-
952-
// We keep track of the insertion order for these objects, so that we can
953-
// call the callbacks in reverse order when we are cleaning up.
954-
uint64_t insertion_order_counter_;
955-
};
956-
957926
typedef void (*DeserializeRequestCallback)(v8::Local<v8::Context> context,
958927
v8::Local<v8::Object> holder,
959928
int index,
@@ -1413,9 +1382,8 @@ class Environment : public MemoryRetainer {
14131382
void ScheduleTimer(int64_t duration);
14141383
void ToggleTimerRef(bool ref);
14151384

1416-
using CleanupCallback = CleanupHookCallback::Callback;
1417-
inline void AddCleanupHook(CleanupCallback cb, void* arg);
1418-
inline void RemoveCleanupHook(CleanupCallback cb, void* arg);
1385+
inline void AddCleanupHook(CleanupQueue::Callback cb, void* arg);
1386+
inline void RemoveCleanupHook(CleanupQueue::Callback cb, void* arg);
14191387
void RunCleanup();
14201388

14211389
static size_t NearHeapLimitCallback(void* data,
@@ -1626,11 +1594,7 @@ class Environment : public MemoryRetainer {
16261594

16271595
BindingDataStore bindings_;
16281596

1629-
// Use an unordered_set, so that we have efficient insertion and removal.
1630-
std::unordered_set<CleanupHookCallback,
1631-
CleanupHookCallback::Hash,
1632-
CleanupHookCallback::Equal> cleanup_hooks_;
1633-
uint64_t cleanup_hook_counter_ = 0;
1597+
CleanupQueue cleanup_queue_;
16341598
bool started_cleanup_ = false;
16351599

16361600
int64_t base_object_count_ = 0;

0 commit comments

Comments
 (0)