Skip to content

Commit 1d3638b

Browse files
committed
src: use enum for refed flag on native immediates
Refs: #33320 (comment) PR-URL: #33444 Backport-PR-URL: #34263 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 55dc7aa commit 1d3638b

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
@@ -685,7 +685,7 @@ void AsyncWrap::EmitDestroy(Environment* env, double async_id) {
685685
}
686686

687687
if (env->destroy_async_id_list()->empty()) {
688-
env->SetUnrefImmediate(&DestroyAsyncIdsCallback);
688+
env->SetImmediate(&DestroyAsyncIdsCallback, CallbackFlags::kUnrefed);
689689
}
690690

691691
// If the list gets very large empty it faster using a Microtask.

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
@@ -786,29 +786,21 @@ inline void IsolateData::set_options(
786786
}
787787

788788
template <typename Fn>
789-
void Environment::CreateImmediate(Fn&& cb, bool ref) {
790-
auto callback = native_immediates_.CreateCallback(std::move(cb), ref);
789+
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
790+
auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
791791
native_immediates_.Push(std::move(callback));
792-
}
793-
794-
template <typename Fn>
795-
void Environment::SetImmediate(Fn&& cb) {
796-
CreateImmediate(std::move(cb), true);
797792

798-
if (immediate_info()->ref_count() == 0)
799-
ToggleImmediateRef(true);
800-
immediate_info()->ref_count_inc(1);
801-
}
802-
803-
template <typename Fn>
804-
void Environment::SetUnrefImmediate(Fn&& cb) {
805-
CreateImmediate(std::move(cb), false);
793+
if (flags & CallbackFlags::kRefed) {
794+
if (immediate_info()->ref_count() == 0)
795+
ToggleImmediateRef(true);
796+
immediate_info()->ref_count_inc(1);
797+
}
806798
}
807799

808800
template <typename Fn>
809-
void Environment::SetImmediateThreadsafe(Fn&& cb, bool refed) {
810-
auto callback =
811-
native_immediates_threadsafe_.CreateCallback(std::move(cb), refed);
801+
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
802+
auto callback = native_immediates_threadsafe_.CreateCallback(
803+
std::move(cb), flags);
812804
{
813805
Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
814806
native_immediates_threadsafe_.Push(std::move(callback));
@@ -818,8 +810,8 @@ void Environment::SetImmediateThreadsafe(Fn&& cb, bool refed) {
818810

819811
template <typename Fn>
820812
void Environment::RequestInterrupt(Fn&& cb) {
821-
auto callback =
822-
native_immediates_interrupts_.CreateCallback(std::move(cb), false);
813+
auto callback = native_immediates_interrupts_.CreateCallback(
814+
std::move(cb), CallbackFlags::kRefed);
823815
{
824816
Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
825817
native_immediates_interrupts_.Push(std::move(callback));

src/env.cc

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

545-
RunAndClearNativeImmediates(true /* skip SetUnrefImmediate()s */);
545+
RunAndClearNativeImmediates(true /* skip unrefed SetImmediate()s */);
546546

547547
for (ReqWrapBase* request : req_wrap_queue_)
548548
request->Cancel();
@@ -675,10 +675,11 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) {
675675
TryCatchScope try_catch(this);
676676
DebugSealHandleScope seal_handle_scope(isolate());
677677
while (auto head = queue->Shift()) {
678-
if (head->is_refed())
678+
bool is_refed = head->flags() & CallbackFlags::kRefed;
679+
if (is_refed)
679680
ref_count++;
680681

681-
if (head->is_refed() || !only_refed)
682+
if (is_refed || !only_refed)
682683
head->Call(this);
683684

684685
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
@@ -1192,12 +1192,12 @@ class Environment : public MemoryRetainer {
11921192
// Unlike the JS setImmediate() function, nested SetImmediate() calls will
11931193
// be run without returning control to the event loop, similar to nextTick().
11941194
template <typename Fn>
1195-
inline void SetImmediate(Fn&& cb);
1196-
template <typename Fn>
1197-
inline void SetUnrefImmediate(Fn&& cb);
1195+
inline void SetImmediate(
1196+
Fn&& cb, CallbackFlags::Flags flags = CallbackFlags::kRefed);
11981197
template <typename Fn>
11991198
// This behaves like SetImmediate() but can be called from any thread.
1200-
inline void SetImmediateThreadsafe(Fn&& cb, bool refed = true);
1199+
inline void SetImmediateThreadsafe(
1200+
Fn&& cb, CallbackFlags::Flags flags = CallbackFlags::kRefed);
12011201
// This behaves like V8's Isolate::RequestInterrupt(), but also accounts for
12021202
// the event loop (i.e. combines the V8 function with SetImmediate()).
12031203
// The passed callback may not throw exceptions.
@@ -1277,9 +1277,6 @@ class Environment : public MemoryRetainer {
12771277
std::shared_ptr<v8::ArrayBuffer::Allocator>);
12781278

12791279
private:
1280-
template <typename Fn>
1281-
inline void CreateImmediate(Fn&& cb, bool ref);
1282-
12831280
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
12841281
const char* errmsg);
12851282

src/node_dir.cc

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

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

132132
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
@@ -279,9 +279,9 @@ void MarkGarbageCollectionEnd(Isolate* isolate,
279279
static_cast<PerformanceGCFlags>(flags),
280280
state->performance_last_gc_start_mark,
281281
PERFORMANCE_NOW());
282-
env->SetUnrefImmediate([entry = std::move(entry)](Environment* env) mutable {
282+
env->SetImmediate([entry = std::move(entry)](Environment* env) mutable {
283283
PerformanceGCCallback(env, std::move(entry));
284-
});
284+
}, CallbackFlags::kUnrefed);
285285
}
286286

287287
void GarbageCollectionCleanupHook(void* data) {

src/node_worker.cc

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

test/cctest/test_environment.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,10 @@ TEST_F(EnvironmentTest, SetImmediateCleanup) {
232232
EXPECT_EQ(env_arg, *env);
233233
called++;
234234
});
235-
(*env)->SetUnrefImmediate([&](node::Environment* env_arg) {
235+
(*env)->SetImmediate([&](node::Environment* env_arg) {
236236
EXPECT_EQ(env_arg, *env);
237237
called_unref++;
238-
});
238+
}, node::CallbackFlags::kUnrefed);
239239
}
240240

241241
EXPECT_EQ(called, 1);

0 commit comments

Comments
 (0)