Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/nodePublic
forked fromnodejs/node

Commitd1f6237

Browse files
legendecasFyko
authored andcommitted
src: consolidate environment cleanup queue
Each Realm tracks its own cleanup hooks and drains the hooks when it isgoing to be destroyed.Moves the implementations of the cleanup queue to its own class so thatit can be used in `node::Realm` too.PR-URL:nodejs#44379Refs:nodejs#44348Refs:nodejs#42528Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent9a1d370 commitd1f6237

File tree

10 files changed

+212
-105
lines changed

10 files changed

+212
-105
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',
@@ -567,6 +568,8 @@
567568
'src/base64-inl.h',
568569
'src/callback_queue.h',
569570
'src/callback_queue-inl.h',
571+
'src/cleanup_queue.h',
572+
'src/cleanup_queue-inl.h',
570573
'src/connect_wrap.h',
571574
'src/connection_wrap.h',
572575
'src/debug_utils.h',

‎src/base_object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class BaseObject : public MemoryRetainer {
178178
// position of members in memory are predictable. For more information please
179179
// refer to `doc/contributing/node-postmortem-support.md`
180180
friendintGenDebugSymbols();
181-
friendclassCleanupHookCallback;
181+
friendclassCleanupQueue;
182182
template<typename T,boolkIsWeak>
183183
friendclassBaseObjectPtrImpl;
184184

‎src/cleanup_queue-inl.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifndef SRC_CLEANUP_QUEUE_INL_H_
2+
#defineSRC_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"memory_tracker-inl.h"
9+
#include"util.h"
10+
11+
namespacenode {
12+
13+
inlinevoidCleanupQueue::MemoryInfo(MemoryTracker* tracker)const {
14+
ForEachBaseObject([&](BaseObject* obj) {
15+
if (obj->IsDoneInitializing()) tracker->Track(obj);
16+
});
17+
}
18+
19+
inlinesize_tCleanupQueue::SelfSize()const {
20+
returnsizeof(CleanupQueue) +
21+
cleanup_hooks_.size() *sizeof(CleanupHookCallback);
22+
}
23+
24+
boolCleanupQueue::empty()const {
25+
return cleanup_hooks_.empty();
26+
}
27+
28+
voidCleanupQueue::Add(Callback cb,void* arg) {
29+
auto insertion_info = cleanup_hooks_.emplace(
30+
CleanupHookCallback{cb, arg, cleanup_hook_counter_++});
31+
// Make sure there was no existing element with these values.
32+
CHECK_EQ(insertion_info.second,true);
33+
}
34+
35+
voidCleanupQueue::Remove(Callback cb,void* arg) {
36+
CleanupHookCallback search{cb, arg,0};
37+
cleanup_hooks_.erase(search);
38+
}
39+
40+
template<typename T>
41+
voidCleanupQueue::ForEachBaseObject(T&& iterator)const {
42+
for (constauto& hook : cleanup_hooks_) {
43+
BaseObject* obj =GetBaseObject(hook);
44+
if (obj !=nullptr)iterator(obj);
45+
}
46+
}
47+
48+
BaseObject*CleanupQueue::GetBaseObject(
49+
const CleanupHookCallback& callback)const {
50+
if (callback.fn_ == BaseObject::DeleteMe)
51+
returnstatic_cast<BaseObject*>(callback.arg_);
52+
else
53+
returnnullptr;
54+
}
55+
56+
}// namespace node
57+
58+
#endif// defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
59+
60+
#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+
namespacenode {
6+
7+
voidCleanupQueue::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_tCleanupQueue::CleanupHookCallback::Hash::operator()(
35+
const CleanupHookCallback& cb)const {
36+
return std::hash<void*>()(cb.arg_);
37+
}
38+
39+
boolCleanupQueue::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+
#defineSRC_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+
namespacenode {
13+
14+
classBaseObject;
15+
16+
classCleanupQueue :publicMemoryRetainer {
17+
public:
18+
typedefvoid (*Callback)(void*);
19+
20+
CleanupQueue() {}
21+
22+
// Not copyable.
23+
CleanupQueue(const CleanupQueue&) =delete;
24+
25+
SET_MEMORY_INFO_NAME(CleanupQueue)
26+
inlinevoidMemoryInfo(node::MemoryTracker* tracker)constoverride;
27+
inlinesize_tSelfSize()constoverride;
28+
29+
inlineboolempty()const;
30+
31+
inlinevoidAdd(Callback cb,void* arg);
32+
inlinevoidRemove(Callback cb,void* arg);
33+
voidDrain();
34+
35+
template<typename T>
36+
inlinevoidForEachBaseObject(T&& iterator)const;
37+
38+
private:
39+
classCleanupHookCallback {
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+
structHash {
50+
size_toperator()(const CleanupHookCallback& cb)const;
51+
};
52+
53+
// Compares by `fn_` and `arg_` being equal.
54+
structEqual {
55+
booloperator()(const CleanupHookCallback& a,
56+
const CleanupHookCallback& b)const;
57+
};
58+
59+
private:
60+
friendclassCleanupQueue;
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
@@ -781,43 +781,17 @@ inline void Environment::ThrowUVException(int errorno,
781781
UVException(isolate(), errorno, syscall, message, path, dest));
782782
}
783783

784-
voidEnvironment::AddCleanupHook(CleanupCallback fn,void* arg) {
785-
auto insertion_info = cleanup_hooks_.emplace(CleanupHookCallback {
786-
fn, arg, cleanup_hook_counter_++
787-
});
788-
// Make sure there was no existing element with these values.
789-
CHECK_EQ(insertion_info.second,true);
790-
}
791-
792-
voidEnvironment::RemoveCleanupHook(CleanupCallback fn,void* arg) {
793-
CleanupHookCallback search { fn, arg,0 };
794-
cleanup_hooks_.erase(search);
795-
}
796-
797-
size_tCleanupHookCallback::Hash::operator()(
798-
const CleanupHookCallback& cb)const {
799-
return std::hash<void*>()(cb.arg_);
784+
voidEnvironment::AddCleanupHook(CleanupQueue::Callback fn,void* arg) {
785+
cleanup_queue_.Add(fn, arg);
800786
}
801787

802-
boolCleanupHookCallback::Equal::operator()(
803-
const CleanupHookCallback& a,const CleanupHookCallback& b)const {
804-
return a.fn_ == b.fn_ && a.arg_ == b.arg_;
805-
}
806-
807-
BaseObject*CleanupHookCallback::GetBaseObject()const {
808-
if (fn_ == BaseObject::DeleteMe)
809-
returnstatic_cast<BaseObject*>(arg_);
810-
else
811-
returnnullptr;
788+
voidEnvironment::RemoveCleanupHook(CleanupQueue::Callback fn,void* arg) {
789+
cleanup_queue_.Remove(fn, arg);
812790
}
813791

814792
template<typename T>
815793
voidEnvironment::ForEachBaseObject(T&& iterator) {
816-
for (constauto& hook : cleanup_hooks_) {
817-
BaseObject* obj = hook.GetBaseObject();
818-
if (obj !=nullptr)
819-
iterator(obj);
820-
}
794+
cleanup_queue_.ForEachBaseObject(std::forward<T>(iterator));
821795
}
822796

823797
voidEnvironment::modify_base_object_count(int64_t delta) {

‎src/env.cc

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,33 +1000,10 @@ void Environment::RunCleanup() {
10001000
bindings_.clear();
10011001
CleanupHandles();
10021002

1003-
while (!cleanup_hooks_.empty() ||
1004-
native_immediates_.size() >0 ||
1003+
while (!cleanup_queue_.empty() || native_immediates_.size() >0 ||
10051004
native_immediates_threadsafe_.size() >0 ||
10061005
native_immediates_interrupts_.size() >0) {
1007-
// Copy into a vector, since we can't sort an unordered_set in-place.
1008-
std::vector<CleanupHookCallback>callbacks(
1009-
cleanup_hooks_.begin(), cleanup_hooks_.end());
1010-
// We can't erase the copied elements from `cleanup_hooks_` yet, because we
1011-
// need to be able to check whether they were un-scheduled by another hook.
1012-
1013-
std::sort(callbacks.begin(), callbacks.end(),
1014-
[](const CleanupHookCallback& a,const CleanupHookCallback& b) {
1015-
// Sort in descending order so that the most recently inserted callbacks
1016-
// are run first.
1017-
return a.insertion_order_counter_ > b.insertion_order_counter_;
1018-
});
1019-
1020-
for (const CleanupHookCallback& cb : callbacks) {
1021-
if (cleanup_hooks_.count(cb) ==0) {
1022-
// This hook was removed from the `cleanup_hooks_` set during another
1023-
// hook that was run earlier. Nothing to do here.
1024-
continue;
1025-
}
1026-
1027-
cb.fn_(cb.arg_);
1028-
cleanup_hooks_.erase(cb);
1029-
}
1006+
cleanup_queue_.Drain();
10301007
CleanupHandles();
10311008
}
10321009

@@ -1730,10 +1707,6 @@ void Environment::BuildEmbedderGraph(Isolate* isolate,
17301707
MemoryTrackertracker(isolate, graph);
17311708
Environment* env =static_cast<Environment*>(data);
17321709
tracker.Track(env);
1733-
env->ForEachBaseObject([&](BaseObject* obj) {
1734-
if (obj->IsDoneInitializing())
1735-
tracker.Track(obj);
1736-
});
17371710
}
17381711

17391712
size_tEnvironment::NearHeapLimitCallback(void* data,
@@ -1868,6 +1841,7 @@ inline size_t Environment::SelfSize() const {
18681841
// this can be done for common types within the Track* calls automatically
18691842
// if a certain scope is entered.
18701843
size -=sizeof(async_hooks_);
1844+
size -=sizeof(cleanup_queue_);
18711845
size -=sizeof(tick_info_);
18721846
size -=sizeof(immediate_info_);
18731847
return size;
@@ -1885,8 +1859,7 @@ void Environment::MemoryInfo(MemoryTracker* tracker) const {
18851859
tracker->TrackField("should_abort_on_uncaught_toggle",
18861860
should_abort_on_uncaught_toggle_);
18871861
tracker->TrackField("stream_base_state", stream_base_state_);
1888-
tracker->TrackFieldWithSize(
1889-
"cleanup_hooks", cleanup_hooks_.size() *sizeof(CleanupHookCallback));
1862+
tracker->TrackField("cleanup_queue", cleanup_queue_);
18901863
tracker->TrackField("async_hooks", async_hooks_);
18911864
tracker->TrackField("immediate_info", immediate_info_);
18921865
tracker->TrackField("tick_info", tick_info_);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp