1 |
|
|
#include "cleanup_queue.h" // NOLINT(build/include_inline) |
2 |
|
|
#include <vector> |
3 |
|
|
#include "cleanup_queue-inl.h" |
4 |
|
|
|
5 |
|
|
namespace node { |
6 |
|
|
|
7 |
|
5638 |
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 |
|
11276 |
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 |
|
5638 |
std::sort(callbacks.begin(), |
15 |
|
|
callbacks.end(), |
16 |
|
1285335 |
[](const CleanupHookCallback& a, const CleanupHookCallback& b) { |
17 |
|
|
// Sort in descending order so that the most recently inserted |
18 |
|
|
// callbacks are run first. |
19 |
|
1285335 |
return a.insertion_order_counter_ > b.insertion_order_counter_; |
20 |
|
|
}); |
21 |
|
|
|
22 |
✓✓ |
192114 |
for (const CleanupHookCallback& cb : callbacks) { |
23 |
✓✓ |
186476 |
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 |
|
1026 |
continue; |
27 |
|
|
} |
28 |
|
|
|
29 |
|
185450 |
cb.fn_(cb.arg_); |
30 |
|
185450 |
cleanup_hooks_.erase(cb); |
31 |
|
|
} |
32 |
|
5638 |
} |
33 |
|
|
|
34 |
|
3362979 |
size_t CleanupQueue::CleanupHookCallback::Hash::operator()( |
35 |
|
|
const CleanupHookCallback& cb) const { |
36 |
|
3362979 |
return std::hash<void*>()(cb.arg_); |
37 |
|
|
} |
38 |
|
|
|
39 |
|
1681528 |
bool CleanupQueue::CleanupHookCallback::Equal::operator()( |
40 |
|
|
const CleanupHookCallback& a, const CleanupHookCallback& b) const { |
41 |
✓✓✓✗
|
1681528 |
return a.fn_ == b.fn_ && a.arg_ == b.arg_; |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
} // namespace node |