Skip to content

Commit 4b2a048

Browse files
committed
src: use enum for refed flag on native immediates
Refs: nodejs#33320 (comment) PR-URL: nodejs#33444 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Zeyu Yang <himself65@outlook.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent ff4f9a9 commit 4b2a048

File tree

11 files changed

+52
-53
lines changed

11 files changed

+52
-53
lines changed

src/async_wrap.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ void AsyncWrap::EmitDestroy(Environment* env, double async_id) {
657657
}
658658

659659
if (env->destroy_async_id_list()->empty()) {
660-
env->SetUnrefImmediate(&DestroyAsyncIdsCallback);
660+
env->SetImmediate(&DestroyAsyncIdsCallback, CallbackFlags::kUnrefed);
661661
}
662662

663663
env->destroy_async_id_list()->push_back(async_id);

src/callback_queue-inl.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace node {
1010
template <typename R, typename... Args>
1111
template <typename Fn>
1212
std::unique_ptr<typename CallbackQueue<R, Args...>::Callback>
13-
CallbackQueue<R, Args...>::CreateCallback(Fn&& fn, bool refed) {
14-
return std::make_unique<CallbackImpl<Fn>>(std::move(fn), refed);
13+
CallbackQueue<R, Args...>::CreateCallback(Fn&& fn, CallbackFlags::Flags flags) {
14+
return std::make_unique<CallbackImpl<Fn>>(std::move(fn), flags);
1515
}
1616

1717
template <typename R, typename... Args>
@@ -57,12 +57,12 @@ size_t CallbackQueue<R, Args...>::size() const {
5757
}
5858

5959
template <typename R, typename... Args>
60-
CallbackQueue<R, Args...>::Callback::Callback(bool refed)
61-
: refed_(refed) {}
60+
CallbackQueue<R, Args...>::Callback::Callback(CallbackFlags::Flags flags)
61+
: flags_(flags) {}
6262

6363
template <typename R, typename... Args>
64-
bool CallbackQueue<R, Args...>::Callback::is_refed() const {
65-
return refed_;
64+
CallbackFlags::Flags CallbackQueue<R, Args...>::Callback::flags() const {
65+
return flags_;
6666
}
6767

6868
template <typename R, typename... Args>
@@ -80,8 +80,8 @@ void CallbackQueue<R, Args...>::Callback::set_next(
8080
template <typename R, typename... Args>
8181
template <typename Fn>
8282
CallbackQueue<R, Args...>::CallbackImpl<Fn>::CallbackImpl(
83-
Fn&& callback, bool refed)
84-
: Callback(refed),
83+
Fn&& callback, CallbackFlags::Flags flags)
84+
: Callback(flags),
8585
callback_(std::move(callback)) {}
8686

8787
template <typename R, typename... Args>

src/callback_queue.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
namespace node {
99

10+
namespace CallbackFlags {
11+
enum Flags {
12+
kUnrefed = 0,
13+
kRefed = 1,
14+
};
15+
}
16+
1017
// A queue of C++ functions that take Args... as arguments and return R
1118
// (this is similar to the signature of std::function).
1219
// New entries are added using `CreateCallback()`/`Push()`, and removed using
@@ -18,25 +25,26 @@ class CallbackQueue {
1825
public:
1926
class Callback {
2027
public:
21-
explicit inline Callback(bool refed);
28+
explicit inline Callback(CallbackFlags::Flags flags);
2229

2330
virtual ~Callback() = default;
2431
virtual R Call(Args... args) = 0;
2532

26-
inline bool is_refed() const;
33+
inline CallbackFlags::Flags flags() const;
2734

2835
private:
2936
inline std::unique_ptr<Callback> get_next();
3037
inline void set_next(std::unique_ptr<Callback> next);
3138

32-
bool refed_;
39+
CallbackFlags::Flags flags_;
3340
std::unique_ptr<Callback> next_;
3441

3542
friend class CallbackQueue;
3643
};
3744

3845
template <typename Fn>
39-
inline std::unique_ptr<Callback> CreateCallback(Fn&& fn, bool refed);
46+
inline std::unique_ptr<Callback> CreateCallback(
47+
Fn&& fn, CallbackFlags::Flags);
4048

4149
inline std::unique_ptr<Callback> Shift();
4250
inline void Push(std::unique_ptr<Callback> cb);
@@ -51,7 +59,7 @@ class CallbackQueue {
5159
template <typename Fn>
5260
class CallbackImpl final : public Callback {
5361
public:
54-
CallbackImpl(Fn&& callback, bool refed);
62+
CallbackImpl(Fn&& callback, CallbackFlags::Flags flags);
5563
R Call(Args... args) override;
5664

5765
private:

src/env-inl.h

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -732,29 +732,21 @@ inline void IsolateData::set_options(
732732
}
733733

734734
template <typename Fn>
735-
void Environment::CreateImmediate(Fn&& cb, bool ref) {
736-
auto callback = native_immediates_.CreateCallback(std::move(cb), ref);
735+
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
736+
auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
737737
native_immediates_.Push(std::move(callback));
738-
}
739-
740-
template <typename Fn>
741-
void Environment::SetImmediate(Fn&& cb) {
742-
CreateImmediate(std::move(cb), true);
743738

744-
if (immediate_info()->ref_count() == 0)
745-
ToggleImmediateRef(true);
746-
immediate_info()->ref_count_inc(1);
747-
}
748-
749-
template <typename Fn>
750-
void Environment::SetUnrefImmediate(Fn&& cb) {
751-
CreateImmediate(std::move(cb), false);
739+
if (flags & CallbackFlags::kRefed) {
740+
if (immediate_info()->ref_count() == 0)
741+
ToggleImmediateRef(true);
742+
immediate_info()->ref_count_inc(1);
743+
}
752744
}
753745

754746
template <typename Fn>
755-
void Environment::SetImmediateThreadsafe(Fn&& cb, bool refed) {
756-
auto callback =
757-
native_immediates_threadsafe_.CreateCallback(std::move(cb), refed);
747+
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
748+
auto callback = native_immediates_threadsafe_.CreateCallback(
749+
std::move(cb), flags);
758750
{
759751
Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
760752
native_immediates_threadsafe_.Push(std::move(callback));
@@ -764,8 +756,8 @@ void Environment::SetImmediateThreadsafe(Fn&& cb, bool refed) {
764756

765757
template <typename Fn>
766758
void Environment::RequestInterrupt(Fn&& cb) {
767-
auto callback =
768-
native_immediates_interrupts_.CreateCallback(std::move(cb), false);
759+
auto callback = native_immediates_interrupts_.CreateCallback(
760+
std::move(cb), CallbackFlags::kRefed);
769761
{
770762
Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
771763
native_immediates_interrupts_.Push(std::move(callback));

src/env.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ void Environment::CleanupHandles() {
543543
Isolate::DisallowJavascriptExecutionScope disallow_js(isolate(),
544544
Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
545545

546-
RunAndClearNativeImmediates(true /* skip SetUnrefImmediate()s */);
546+
RunAndClearNativeImmediates(true /* skip unrefed SetImmediate()s */);
547547

548548
for (ReqWrapBase* request : req_wrap_queue_)
549549
request->Cancel();
@@ -673,10 +673,11 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) {
673673
TryCatchScope try_catch(this);
674674
DebugSealHandleScope seal_handle_scope(isolate());
675675
while (auto head = queue->Shift()) {
676-
if (head->is_refed())
676+
bool is_refed = head->flags() & CallbackFlags::kRefed;
677+
if (is_refed)
677678
ref_count++;
678679

679-
if (head->is_refed() || !only_refed)
680+
if (is_refed || !only_refed)
680681
head->Call(this);
681682

682683
head.reset(); // Destroy now so that this is also observed by try_catch.

src/env.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,12 +1180,12 @@ class Environment : public MemoryRetainer {
11801180
// Unlike the JS setImmediate() function, nested SetImmediate() calls will
11811181
// be run without returning control to the event loop, similar to nextTick().
11821182
template <typename Fn>
1183-
inline void SetImmediate(Fn&& cb);
1184-
template <typename Fn>
1185-
inline void SetUnrefImmediate(Fn&& cb);
1183+
inline void SetImmediate(
1184+
Fn&& cb, CallbackFlags::Flags flags = CallbackFlags::kRefed);
11861185
template <typename Fn>
11871186
// This behaves like SetImmediate() but can be called from any thread.
1188-
inline void SetImmediateThreadsafe(Fn&& cb, bool refed = true);
1187+
inline void SetImmediateThreadsafe(
1188+
Fn&& cb, CallbackFlags::Flags flags = CallbackFlags::kRefed);
11891189
// This behaves like V8's Isolate::RequestInterrupt(), but also accounts for
11901190
// the event loop (i.e. combines the V8 function with SetImmediate()).
11911191
// The passed callback may not throw exceptions.
@@ -1265,9 +1265,6 @@ class Environment : public MemoryRetainer {
12651265
std::shared_ptr<v8::ArrayBuffer::Allocator>);
12661266

12671267
private:
1268-
template <typename Fn>
1269-
inline void CreateImmediate(Fn&& cb, bool ref);
1270-
12711268
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
12721269
const char* errmsg);
12731270

src/node_dir.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ inline void DirHandle::GCClose() {
126126
// to notify that the file descriptor was gc'd. We want to be noisy about
127127
// this because not explicitly closing the DirHandle is a bug.
128128

129-
env()->SetUnrefImmediate([](Environment* env) {
129+
env()->SetImmediate([](Environment* env) {
130130
ProcessEmitWarning(env,
131131
"Closing directory handle on garbage collection");
132-
});
132+
}, CallbackFlags::kUnrefed);
133133
}
134134

135135
void AfterClose(uv_fs_t* req) {

src/node_file.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,12 @@ inline void FileHandle::Close() {
221221
// If the close was successful, we still want to emit a process warning
222222
// to notify that the file descriptor was gc'd. We want to be noisy about
223223
// this because not explicitly closing the FileHandle is a bug.
224-
env()->SetUnrefImmediate([detail](Environment* env) {
224+
225+
env()->SetImmediate([detail](Environment* env) {
225226
ProcessEmitWarning(env,
226227
"Closing file descriptor %d on garbage collection",
227228
detail.fd);
228-
});
229+
}, CallbackFlags::kUnrefed);
229230
}
230231

231232
void FileHandle::CloseReq::Resolve() {

src/node_perf.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ void MarkGarbageCollectionEnd(Isolate* isolate,
282282
static_cast<PerformanceGCFlags>(flags),
283283
state->performance_last_gc_start_mark,
284284
PERFORMANCE_NOW());
285-
env->SetUnrefImmediate([entry = std::move(entry)](Environment* env) mutable {
285+
env->SetImmediate([entry = std::move(entry)](Environment* env) mutable {
286286
PerformanceGCCallback(env, std::move(entry));
287-
});
287+
}, CallbackFlags::kUnrefed);
288288
}
289289

290290
void GarbageCollectionCleanupHook(void* data) {

src/node_worker.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ void Worker::TakeHeapSnapshot(const FunctionCallbackInfo<Value>& args) {
764764
env, std::move(snapshot));
765765
Local<Value> args[] = { stream->object() };
766766
taker->MakeCallback(env->ondone_string(), arraysize(args), args);
767-
}, /* refed */ false);
767+
}, CallbackFlags::kUnrefed);
768768
});
769769
args.GetReturnValue().Set(scheduled ? taker->object() : Local<Object>());
770770
}

0 commit comments

Comments
 (0)