Skip to content

Commit cf2475c

Browse files
addaleaxtargos
authored andcommitted
src: refactor TimerWrap lifetime management
Split `Stop(true)` and `Stop(false)` into separate methods since the actions performed by these are fully distinct. PR-URL: #34252 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: David Carlier <[email protected]>
1 parent b8bccc3 commit cf2475c

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

‎src/timer_wrap.cc‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ TimerWrap::TimerWrap(Environment* env, const TimerCb& fn)
1212
timer_.data = this;
1313
}
1414

15-
voidTimerWrap::Stop(bool close){
15+
voidTimerWrap::Stop(){
1616
if (timer_.data == nullptr) return;
1717
uv_timer_stop(&timer_);
18-
if (LIKELY(close)){
19-
timer_.data = nullptr;
20-
env_->CloseHandle(reinterpret_cast<uv_handle_t*>(&timer_), TimerClosedCb);
21-
}
18+
}
19+
20+
voidTimerWrap::Close(){
21+
timer_.data = nullptr;
22+
env_->CloseHandle(reinterpret_cast<uv_handle_t*>(&timer_), TimerClosedCb);
2223
}
2324

2425
voidTimerWrap::TimerClosedCb(uv_handle_t* handle){
@@ -54,13 +55,14 @@ TimerWrapHandle::TimerWrapHandle(
5455
env->AddCleanupHook(CleanupHook, this);
5556
}
5657

57-
voidTimerWrapHandle::Stop(bool close){
58-
if (UNLIKELY(!close))
59-
return timer_->Stop(close);
58+
voidTimerWrapHandle::Stop(){
59+
return timer_->Stop();
60+
}
6061

62+
voidTimerWrapHandle::Close(){
6163
if (timer_ != nullptr){
6264
timer_->env()->RemoveCleanupHook(CleanupHook, this);
63-
timer_->Stop();
65+
timer_->Close();
6466
}
6567
timer_ = nullptr;
6668
}
@@ -80,13 +82,13 @@ void TimerWrapHandle::Update(uint64_t interval, uint64_t repeat){
8082
timer_->Update(interval, repeat);
8183
}
8284

83-
voidTimerWrapHandle::CleanupHook(void* data){
84-
static_cast<TimerWrapHandle*>(data)->Stop();
85-
}
86-
87-
voidTimerWrapHandle::MemoryInfo(node::MemoryTracker* tracker) const{
85+
voidTimerWrapHandle::MemoryInfo(MemoryTracker* tracker) const{
8886
if (timer_ != nullptr)
8987
tracker->TrackField("timer", *timer_);
9088
}
9189

90+
voidTimerWrapHandle::CleanupHook(void* data){
91+
static_cast<TimerWrapHandle*>(data)->Close();
92+
}
93+
9294
} // namespace node

‎src/timer_wrap.h‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ class TimerWrap final : public MemoryRetainer{
2121

2222
inline Environment* env() const{return env_}
2323

24-
// Completely stops the timer, making it no longer usable.
25-
voidStop(bool close = true);
24+
// Stop calling the timer callback.
25+
voidStop();
26+
// Render the timer unusable and delete this object.
27+
voidClose();
2628

2729
// Starts / Restarts the Timer
2830
voidUpdate(uint64_t interval, uint64_t repeat = 0);
2931

3032
voidRef();
31-
3233
voidUnref();
3334

3435
SET_NO_MEMORY_INFO();
@@ -55,15 +56,15 @@ class TimerWrapHandle : public MemoryRetainer{
5556

5657
TimerWrapHandle(const TimerWrapHandle&) = delete;
5758

58-
~TimerWrapHandle(){Stop()}
59+
~TimerWrapHandle(){Close()}
5960

6061
voidUpdate(uint64_t interval, uint64_t repeat = 0);
6162

6263
voidRef();
63-
6464
voidUnref();
6565

66-
voidStop(bool close = true);
66+
voidStop();
67+
voidClose();
6768

6869
voidMemoryInfo(node::MemoryTracker* tracker) constoverride;
6970

@@ -72,6 +73,7 @@ class TimerWrapHandle : public MemoryRetainer{
7273

7374
private:
7475
staticvoidCleanupHook(void* data);
76+
7577
TimerWrap* timer_;
7678
};
7779

0 commit comments

Comments
(0)