Skip to content

Commit 874460a

Browse files
committed
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 <jasnell@gmail.com> Reviewed-By: Zeyu Yang <himself65@outlook.com> Reviewed-By: David Carlier <devnexen@gmail.com>
1 parent e2f9dc6 commit 874460a

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

src/quic/node_quic_session-inl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,11 @@ void QuicSession::set_remote_transport_params() {
511511
}
512512

513513
void QuicSession::StopIdleTimer() {
514-
idle_.Stop();
514+
idle_.Close();
515515
}
516516

517517
void QuicSession::StopRetransmitTimer() {
518-
retransmit_.Stop();
518+
retransmit_.Close();
519519
}
520520

521521
// Called by the OnVersionNegotiation callback when a version

src/timer_wrap.cc

+16-14
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-
void TimerWrap::Stop(bool close) {
15+
void TimerWrap::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+
void TimerWrap::Close() {
21+
timer_.data = nullptr;
22+
env_->CloseHandle(reinterpret_cast<uv_handle_t*>(&timer_), TimerClosedCb);
2223
}
2324

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

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

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

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

src/timer_wrap.h

+8-6
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-
void Stop(bool close = true);
24+
// Stop calling the timer callback.
25+
void Stop();
26+
// Render the timer unusable and delete this object.
27+
void Close();
2628

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

3032
void Ref();
31-
3233
void Unref();
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
void Update(uint64_t interval, uint64_t repeat = 0);
6162

6263
void Ref();
63-
6464
void Unref();
6565

66-
void Stop(bool close = true);
66+
void Stop();
67+
void Close();
6768

6869
void MemoryInfo(node::MemoryTracker* tracker) const override;
6970

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

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

0 commit comments

Comments
 (0)