-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: consolidate environment cleanup queue
Each Realm tracks its own cleanup hooks and drains 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. PR-URL: #44379 Refs: #44348 Refs: #42528 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
- Loading branch information
1 parent
f0cf100
commit 0b5b5ed
Showing
10 changed files
with
212 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#ifndef SRC_CLEANUP_QUEUE_INL_H_ | ||
#define SRC_CLEANUP_QUEUE_INL_H_ | ||
|
||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#include "base_object.h" | ||
#include "cleanup_queue.h" | ||
#include "memory_tracker-inl.h" | ||
#include "util.h" | ||
|
||
namespace node { | ||
|
||
inline void CleanupQueue::MemoryInfo(MemoryTracker* tracker) const { | ||
ForEachBaseObject([&](BaseObject* obj) { | ||
if (obj->IsDoneInitializing()) tracker->Track(obj); | ||
}); | ||
} | ||
|
||
inline size_t CleanupQueue::SelfSize() const { | ||
return sizeof(CleanupQueue) + | ||
cleanup_hooks_.size() * sizeof(CleanupHookCallback); | ||
} | ||
|
||
bool CleanupQueue::empty() const { | ||
return cleanup_hooks_.empty(); | ||
} | ||
|
||
void CleanupQueue::Add(Callback cb, void* arg) { | ||
auto insertion_info = cleanup_hooks_.emplace( | ||
CleanupHookCallback{cb, arg, cleanup_hook_counter_++}); | ||
// Make sure there was no existing element with these values. | ||
CHECK_EQ(insertion_info.second, true); | ||
} | ||
|
||
void CleanupQueue::Remove(Callback cb, void* arg) { | ||
CleanupHookCallback search{cb, arg, 0}; | ||
cleanup_hooks_.erase(search); | ||
} | ||
|
||
template <typename T> | ||
void CleanupQueue::ForEachBaseObject(T&& iterator) const { | ||
for (const auto& hook : cleanup_hooks_) { | ||
BaseObject* obj = GetBaseObject(hook); | ||
if (obj != nullptr) iterator(obj); | ||
} | ||
} | ||
|
||
BaseObject* CleanupQueue::GetBaseObject( | ||
const CleanupHookCallback& callback) const { | ||
if (callback.fn_ == BaseObject::DeleteMe) | ||
return static_cast<BaseObject*>(callback.arg_); | ||
else | ||
return nullptr; | ||
} | ||
|
||
} // namespace node | ||
|
||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#endif // SRC_CLEANUP_QUEUE_INL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "cleanup_queue.h" // NOLINT(build/include_inline) | ||
#include <vector> | ||
#include "cleanup_queue-inl.h" | ||
|
||
namespace node { | ||
|
||
void CleanupQueue::Drain() { | ||
// Copy into a vector, since we can't sort an unordered_set in-place. | ||
std::vector<CleanupHookCallback> callbacks(cleanup_hooks_.begin(), | ||
cleanup_hooks_.end()); | ||
// We can't erase the copied elements from `cleanup_hooks_` yet, because we | ||
// need to be able to check whether they were un-scheduled by another hook. | ||
|
||
std::sort(callbacks.begin(), | ||
callbacks.end(), | ||
[](const CleanupHookCallback& a, const CleanupHookCallback& b) { | ||
// Sort in descending order so that the most recently inserted | ||
// callbacks are run first. | ||
return a.insertion_order_counter_ > b.insertion_order_counter_; | ||
}); | ||
|
||
for (const CleanupHookCallback& cb : callbacks) { | ||
if (cleanup_hooks_.count(cb) == 0) { | ||
// This hook was removed from the `cleanup_hooks_` set during another | ||
// hook that was run earlier. Nothing to do here. | ||
continue; | ||
} | ||
|
||
cb.fn_(cb.arg_); | ||
cleanup_hooks_.erase(cb); | ||
} | ||
} | ||
|
||
size_t CleanupQueue::CleanupHookCallback::Hash::operator()( | ||
const CleanupHookCallback& cb) const { | ||
return std::hash<void*>()(cb.arg_); | ||
} | ||
|
||
bool CleanupQueue::CleanupHookCallback::Equal::operator()( | ||
const CleanupHookCallback& a, const CleanupHookCallback& b) const { | ||
return a.fn_ == b.fn_ && a.arg_ == b.arg_; | ||
} | ||
|
||
} // namespace node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#ifndef SRC_CLEANUP_QUEUE_H_ | ||
#define SRC_CLEANUP_QUEUE_H_ | ||
|
||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
#include <unordered_set> | ||
|
||
#include "memory_tracker.h" | ||
|
||
namespace node { | ||
|
||
class BaseObject; | ||
|
||
class CleanupQueue : public MemoryRetainer { | ||
public: | ||
typedef void (*Callback)(void*); | ||
|
||
CleanupQueue() {} | ||
|
||
// Not copyable. | ||
CleanupQueue(const CleanupQueue&) = delete; | ||
|
||
SET_MEMORY_INFO_NAME(CleanupQueue) | ||
inline void MemoryInfo(node::MemoryTracker* tracker) const override; | ||
inline size_t SelfSize() const override; | ||
|
||
inline bool empty() const; | ||
|
||
inline void Add(Callback cb, void* arg); | ||
inline void Remove(Callback cb, void* arg); | ||
void Drain(); | ||
|
||
template <typename T> | ||
inline void ForEachBaseObject(T&& iterator) const; | ||
|
||
private: | ||
class CleanupHookCallback { | ||
public: | ||
CleanupHookCallback(Callback fn, | ||
void* arg, | ||
uint64_t insertion_order_counter) | ||
: fn_(fn), | ||
arg_(arg), | ||
insertion_order_counter_(insertion_order_counter) {} | ||
|
||
// Only hashes `arg_`, since that is usually enough to identify the hook. | ||
struct Hash { | ||
size_t operator()(const CleanupHookCallback& cb) const; | ||
}; | ||
|
||
// Compares by `fn_` and `arg_` being equal. | ||
struct Equal { | ||
bool operator()(const CleanupHookCallback& a, | ||
const CleanupHookCallback& b) const; | ||
}; | ||
|
||
private: | ||
friend class CleanupQueue; | ||
Callback fn_; | ||
void* arg_; | ||
|
||
// We keep track of the insertion order for these objects, so that we can | ||
// call the callbacks in reverse order when we are cleaning up. | ||
uint64_t insertion_order_counter_; | ||
}; | ||
|
||
inline BaseObject* GetBaseObject(const CleanupHookCallback& callback) const; | ||
|
||
// Use an unordered_set, so that we have efficient insertion and removal. | ||
std::unordered_set<CleanupHookCallback, | ||
CleanupHookCallback::Hash, | ||
CleanupHookCallback::Equal> | ||
cleanup_hooks_; | ||
uint64_t cleanup_hook_counter_ = 0; | ||
}; | ||
|
||
} // namespace node | ||
|
||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#endif // SRC_CLEANUP_QUEUE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.