Skip to content

Commit 7d1d00f

Browse files
addaleaxcodebytere
authored andcommitted
src: use enum for refed flag on native immediates
Refs: #33320 (comment) PR-URL: #33444 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent e8cc269 commit 7d1d00f

11 files changed

+52
-54
lines changed

src/async_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ void AsyncWrap::EmitDestroy(Environment* env, double async_id) {
753753
}
754754

755755
if (env->destroy_async_id_list()->empty()) {
756-
env->SetUnrefImmediate(&DestroyAsyncIdsCallback);
756+
env->SetImmediate(&DestroyAsyncIdsCallback, CallbackFlags::kUnrefed);
757757
}
758758

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

src/callback_queue-inl.h

+8-8
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

+13-5
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

+12-20
Original file line numberDiff line numberDiff line change
@@ -707,29 +707,21 @@ inline void IsolateData::set_options(
707707
}
708708

709709
template <typename Fn>
710-
void Environment::CreateImmediate(Fn&& cb, bool ref) {
711-
auto callback = native_immediates_.CreateCallback(std::move(cb), ref);
710+
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
711+
auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
712712
native_immediates_.Push(std::move(callback));
713-
}
714-
715-
template <typename Fn>
716-
void Environment::SetImmediate(Fn&& cb) {
717-
CreateImmediate(std::move(cb), true);
718713

719-
if (immediate_info()->ref_count() == 0)
720-
ToggleImmediateRef(true);
721-
immediate_info()->ref_count_inc(1);
722-
}
723-
724-
template <typename Fn>
725-
void Environment::SetUnrefImmediate(Fn&& cb) {
726-
CreateImmediate(std::move(cb), false);
714+
if (flags & CallbackFlags::kRefed) {
715+
if (immediate_info()->ref_count() == 0)
716+
ToggleImmediateRef(true);
717+
immediate_info()->ref_count_inc(1);
718+
}
727719
}
728720

729721
template <typename Fn>
730-
void Environment::SetImmediateThreadsafe(Fn&& cb, bool refed) {
731-
auto callback =
732-
native_immediates_threadsafe_.CreateCallback(std::move(cb), refed);
722+
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
723+
auto callback = native_immediates_threadsafe_.CreateCallback(
724+
std::move(cb), flags);
733725
{
734726
Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
735727
native_immediates_threadsafe_.Push(std::move(callback));
@@ -740,8 +732,8 @@ void Environment::SetImmediateThreadsafe(Fn&& cb, bool refed) {
740732

741733
template <typename Fn>
742734
void Environment::RequestInterrupt(Fn&& cb) {
743-
auto callback =
744-
native_immediates_interrupts_.CreateCallback(std::move(cb), false);
735+
auto callback = native_immediates_interrupts_.CreateCallback(
736+
std::move(cb), CallbackFlags::kRefed);
745737
{
746738
Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
747739
native_immediates_interrupts_.Push(std::move(callback));

src/env.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ void Environment::CleanupHandles() {
587587
Isolate::DisallowJavascriptExecutionScope disallow_js(isolate(),
588588
Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
589589

590-
RunAndClearNativeImmediates(true /* skip SetUnrefImmediate()s */);
590+
RunAndClearNativeImmediates(true /* skip unrefed SetImmediate()s */);
591591

592592
for (ReqWrapBase* request : req_wrap_queue_)
593593
request->Cancel();
@@ -730,10 +730,11 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) {
730730
TryCatchScope try_catch(this);
731731
DebugSealHandleScope seal_handle_scope(isolate());
732732
while (auto head = queue->Shift()) {
733-
if (head->is_refed())
733+
bool is_refed = head->flags() & CallbackFlags::kRefed;
734+
if (is_refed)
734735
ref_count++;
735736

736-
if (head->is_refed() || !only_refed)
737+
if (is_refed || !only_refed)
737738
head->Call(this);
738739

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

src/env.h

+4-7
Original file line numberDiff line numberDiff line change
@@ -1163,12 +1163,12 @@ class Environment : public MemoryRetainer {
11631163
// Unlike the JS setImmediate() function, nested SetImmediate() calls will
11641164
// be run without returning control to the event loop, similar to nextTick().
11651165
template <typename Fn>
1166-
inline void SetImmediate(Fn&& cb);
1167-
template <typename Fn>
1168-
inline void SetUnrefImmediate(Fn&& cb);
1166+
inline void SetImmediate(
1167+
Fn&& cb, CallbackFlags::Flags flags = CallbackFlags::kRefed);
11691168
template <typename Fn>
11701169
// This behaves like SetImmediate() but can be called from any thread.
1171-
inline void SetImmediateThreadsafe(Fn&& cb, bool refed = true);
1170+
inline void SetImmediateThreadsafe(
1171+
Fn&& cb, CallbackFlags::Flags flags = CallbackFlags::kRefed);
11721172
// This behaves like V8's Isolate::RequestInterrupt(), but also accounts for
11731173
// the event loop (i.e. combines the V8 function with SetImmediate()).
11741174
// The passed callback may not throw exceptions.
@@ -1251,9 +1251,6 @@ class Environment : public MemoryRetainer {
12511251
void RunAndClearInterrupts();
12521252

12531253
private:
1254-
template <typename Fn>
1255-
inline void CreateImmediate(Fn&& cb, bool ref);
1256-
12571254
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
12581255
const char* errmsg);
12591256

src/node_dir.cc

+2-2
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ inline void FileHandle::Close() {
226226
// to notify that the file descriptor was gc'd. We want to be noisy about
227227
// this because not explicitly closing the FileHandle is a bug.
228228

229-
env()->SetUnrefImmediate([detail](Environment* env) {
229+
env()->SetImmediate([detail](Environment* env) {
230230
ProcessEmitWarning(env,
231231
"Closing file descriptor %d on garbage collection",
232232
detail.fd);
@@ -240,7 +240,7 @@ inline void FileHandle::Close() {
240240
"thrown if a file descriptor is closed during garbage collection.",
241241
"DEP0137").IsNothing();
242242
}
243-
});
243+
}, CallbackFlags::kUnrefed);
244244
}
245245

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

src/node_perf.cc

+2-2
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

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

test/cctest/test_environment.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,11 @@ TEST_F(EnvironmentTest, SetImmediateCleanup) {
266266
(*env)->SetImmediate([&](node::Environment* env_arg) {
267267
EXPECT_EQ(env_arg, *env);
268268
called++;
269-
});
270-
(*env)->SetUnrefImmediate([&](node::Environment* env_arg) {
269+
}, node::CallbackFlags::kRefed);
270+
(*env)->SetImmediate([&](node::Environment* env_arg) {
271271
EXPECT_EQ(env_arg, *env);
272272
called_unref++;
273-
});
273+
}, node::CallbackFlags::kUnrefed);
274274
}
275275

276276
EXPECT_EQ(called, 1);

0 commit comments

Comments
 (0)