Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: refactor TimerWrap class #34252

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/inspector_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ class NodeInspectorClient : public V8InspectorClient {
void* data) override {
auto result =
timers_.emplace(std::piecewise_construct, std::make_tuple(data),
std::make_tuple(env_, callback, data));
std::make_tuple(env_, [=]() { callback(data); }));
CHECK(result.second);
uint64_t interval = 1000 * interval_s;
result.first->second.Update(interval, interval);
Expand Down
4 changes: 2 additions & 2 deletions src/quic/node_quic_session-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,11 @@ void QuicSession::set_remote_transport_params() {
}

void QuicSession::StopIdleTimer() {
idle_.Stop();
idle_.Close();
}

void QuicSession::StopRetransmitTimer() {
retransmit_.Stop();
retransmit_.Close();
}

// Called by the OnVersionNegotiation callback when a version
Expand Down
4 changes: 2 additions & 2 deletions src/quic/node_quic_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1431,8 +1431,8 @@ QuicSession::QuicSession(
socket_(socket),
alpn_(alpn),
hostname_(hostname),
idle_(socket->env(), [this](void* data) { OnIdleTimeout(); }),
retransmit_(socket->env(), [this](void* data) { MaybeTimeout(); }),
idle_(socket->env(), [this]() { OnIdleTimeout(); }),
retransmit_(socket->env(), [this]() { MaybeTimeout(); }),
dcid_(dcid),
state_(env()->isolate()),
quic_state_(socket->quic_state()) {
Expand Down
42 changes: 21 additions & 21 deletions src/timer_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@

namespace node {

TimerWrap::TimerWrap(Environment* env, TimerCb fn, void* user_data)
TimerWrap::TimerWrap(Environment* env, const TimerCb& fn)
: env_(env),
fn_(fn),
user_data_(user_data) {
fn_(fn) {
uv_timer_init(env->event_loop(), &timer_);
timer_.data = this;
}

void TimerWrap::Stop(bool close) {
void TimerWrap::Stop() {
if (timer_.data == nullptr) return;
uv_timer_stop(&timer_);
if (LIKELY(close)) {
timer_.data = nullptr;
env_->CloseHandle(reinterpret_cast<uv_handle_t*>(&timer_), TimerClosedCb);
}
}

void TimerWrap::Close() {
timer_.data = nullptr;
env_->CloseHandle(reinterpret_cast<uv_handle_t*>(&timer_), TimerClosedCb);
}

void TimerWrap::TimerClosedCb(uv_handle_t* handle) {
Expand All @@ -45,24 +45,24 @@ void TimerWrap::Unref() {

void TimerWrap::OnTimeout(uv_timer_t* timer) {
TimerWrap* t = ContainerOf(&TimerWrap::timer_, timer);
t->fn_(t->user_data_);
t->fn_();
}

TimerWrapHandle::TimerWrapHandle(
Environment* env,
TimerWrap::TimerCb fn,
void* user_data) {
timer_ = new TimerWrap(env, fn, user_data);
const TimerWrap::TimerCb& fn) {
timer_ = new TimerWrap(env, fn);
env->AddCleanupHook(CleanupHook, this);
}

void TimerWrapHandle::Stop(bool close) {
if (UNLIKELY(!close))
return timer_->Stop(close);
void TimerWrapHandle::Stop() {
return timer_->Stop();
}

void TimerWrapHandle::Close() {
if (timer_ != nullptr) {
timer_->env()->RemoveCleanupHook(CleanupHook, this);
timer_->Stop();
timer_->Close();
}
timer_ = nullptr;
}
Expand All @@ -82,13 +82,13 @@ void TimerWrapHandle::Update(uint64_t interval, uint64_t repeat) {
timer_->Update(interval, repeat);
}

void TimerWrapHandle::CleanupHook(void* data) {
static_cast<TimerWrapHandle*>(data)->Stop();
}

void TimerWrapHandle::MemoryInfo(node::MemoryTracker* tracker) const {
void TimerWrapHandle::MemoryInfo(MemoryTracker* tracker) const {
if (timer_ != nullptr)
tracker->TrackField("timer", *timer_);
}

void TimerWrapHandle::CleanupHook(void* data) {
static_cast<TimerWrapHandle*>(data)->Close();
}

} // namespace node
22 changes: 11 additions & 11 deletions src/timer_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ namespace node {
// Utility class that makes working with libuv timers a bit easier.
class TimerWrap final : public MemoryRetainer {
public:
using TimerCb = std::function<void(void*)>;
using TimerCb = std::function<void()>;

TimerWrap(Environment* env, TimerCb fn, void* user_data);
TimerWrap(Environment* env, const TimerCb& fn);
TimerWrap(const TimerWrap&) = delete;

inline Environment* env() const { return env_; }

// Completely stops the timer, making it no longer usable.
void Stop(bool close = true);
// Stop calling the timer callback.
void Stop();
// Render the timer unusable and delete this object.
void Close();

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

void Ref();

void Unref();

SET_NO_MEMORY_INFO();
Expand All @@ -43,7 +44,6 @@ class TimerWrap final : public MemoryRetainer {
Environment* env_;
TimerCb fn_;
uv_timer_t timer_;
void* user_data_ = nullptr;

friend std::unique_ptr<TimerWrap>::deleter_type;
};
Expand All @@ -52,20 +52,19 @@ class TimerWrapHandle : public MemoryRetainer {
public:
TimerWrapHandle(
Environment* env,
TimerWrap::TimerCb fn,
void* user_data = nullptr);
const TimerWrap::TimerCb& fn);

TimerWrapHandle(const TimerWrapHandle&) = delete;

~TimerWrapHandle() { Stop(); }
~TimerWrapHandle() { Close(); }

void Update(uint64_t interval, uint64_t repeat = 0);

void Ref();

void Unref();

void Stop(bool close = true);
void Stop();
void Close();

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

Expand All @@ -74,6 +73,7 @@ class TimerWrapHandle : public MemoryRetainer {

private:
static void CleanupHook(void* data);

TimerWrap* timer_;
};

Expand Down