-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
src: introduce TimerWrap/TimerWrapHandle utility #34186
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
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
src: add TimerWrap utility
Consolidate uv_timer_t boilerplate code into a shared utility. There are several places throughout the code where we use uv_timer_t internally (inspector, perf, quic), with some code duplication. This eliminates the duplicated code, ensures that cleanup occurs correctly, and simplifies use of the timers. Signed-off-by: James M Snell <jasnell@gmail.com>
- Loading branch information
commit 9707be185e01984948526aa12de64924b43f3e41
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| #include "env-inl.h" | ||
| #include "memory_tracker-inl.h" | ||
| #include "timer_wrap.h" | ||
| #include "uv.h" | ||
|
|
||
| namespace node { | ||
|
|
||
| TimerWrap::TimerWrap(Environment* env, TimerCb fn, void* user_data) | ||
| : env_(env), | ||
| fn_(fn), | ||
| user_data_(user_data) { | ||
| uv_timer_init(env->event_loop(), &timer_); | ||
| timer_.data = this; | ||
| } | ||
|
|
||
| void TimerWrap::Stop(bool close) { | ||
| 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::TimerClosedCb(uv_handle_t* handle) { | ||
| std::unique_ptr<TimerWrap> ptr( | ||
| ContainerOf(&TimerWrap::timer_, | ||
| reinterpret_cast<uv_timer_t*>(handle))); | ||
| } | ||
|
|
||
| void TimerWrap::Update(uint64_t interval, uint64_t repeat) { | ||
| if (timer_.data == nullptr) return; | ||
| uv_timer_start(&timer_, OnTimeout, interval, repeat); | ||
| } | ||
|
|
||
| void TimerWrap::Ref() { | ||
| if (timer_.data == nullptr) return; | ||
| uv_ref(reinterpret_cast<uv_handle_t*>(&timer_)); | ||
| } | ||
|
|
||
| void TimerWrap::Unref() { | ||
| if (timer_.data == nullptr) return; | ||
| uv_unref(reinterpret_cast<uv_handle_t*>(&timer_)); | ||
| } | ||
|
|
||
| void TimerWrap::OnTimeout(uv_timer_t* timer) { | ||
| TimerWrap* t = ContainerOf(&TimerWrap::timer_, timer); | ||
| t->fn_(t->user_data_); | ||
| } | ||
|
|
||
| TimerWrapHandle::TimerWrapHandle( | ||
| Environment* env, | ||
| TimerWrap::TimerCb fn, | ||
| void* user_data) { | ||
| timer_ = new TimerWrap(env, fn, user_data); | ||
| env->AddCleanupHook(CleanupHook, this); | ||
| } | ||
|
|
||
| void TimerWrapHandle::Stop(bool close) { | ||
| if (UNLIKELY(!close)) | ||
| return timer_->Stop(close); | ||
|
|
||
| if (timer_ != nullptr) { | ||
| timer_->env()->RemoveCleanupHook(CleanupHook, this); | ||
| timer_->Stop(); | ||
| } | ||
| timer_ = nullptr; | ||
| } | ||
|
|
||
| void TimerWrapHandle::Ref() { | ||
| if (timer_ != nullptr) | ||
| timer_->Ref(); | ||
| } | ||
|
|
||
| void TimerWrapHandle::Unref() { | ||
| if (timer_ != nullptr) | ||
| timer_->Unref(); | ||
| } | ||
|
|
||
| void TimerWrapHandle::Update(uint64_t interval, uint64_t repeat) { | ||
| if (timer_ != nullptr) | ||
| timer_->Update(interval, repeat); | ||
| } | ||
|
|
||
| void TimerWrapHandle::CleanupHook(void* data) { | ||
| static_cast<TimerWrapHandle*>(data)->Stop(); | ||
| } | ||
|
|
||
| void TimerWrapHandle::MemoryInfo(node::MemoryTracker* tracker) const { | ||
| if (timer_ != nullptr) | ||
| tracker->TrackField("timer", *timer_); | ||
| } | ||
|
|
||
| } // namespace node | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| #ifndef SRC_TIMER_WRAP_H_ | ||
| #define SRC_TIMER_WRAP_H_ | ||
|
|
||
| #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
|
||
| #include "memory_tracker.h" | ||
| #include "env.h" | ||
| #include "uv.h" | ||
|
|
||
| #include <functional> | ||
|
|
||
| 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*)>; | ||
|
|
||
| TimerWrap(Environment* env, TimerCb fn, void* user_data); | ||
| TimerWrap(const TimerWrap&) = delete; | ||
|
|
||
| inline Environment* env() const { return env_; } | ||
|
|
||
| // Completely stops the timer, making it no longer usable. | ||
| void Stop(bool close = true); | ||
|
|
||
| // Starts / Restarts the Timer | ||
| void Update(uint64_t interval, uint64_t repeat = 0); | ||
|
|
||
| void Ref(); | ||
|
|
||
| void Unref(); | ||
|
|
||
| SET_NO_MEMORY_INFO(); | ||
| SET_MEMORY_INFO_NAME(TimerWrap) | ||
| SET_SELF_SIZE(TimerWrap) | ||
|
|
||
| private: | ||
| static void TimerClosedCb(uv_handle_t* handle); | ||
| static void OnTimeout(uv_timer_t* timer); | ||
| ~TimerWrap() = default; | ||
|
|
||
| Environment* env_; | ||
| TimerCb fn_; | ||
| uv_timer_t timer_; | ||
| void* user_data_ = nullptr; | ||
|
|
||
| friend std::unique_ptr<TimerWrap>::deleter_type; | ||
| }; | ||
|
|
||
| class TimerWrapHandle : public MemoryRetainer { | ||
| public: | ||
| TimerWrapHandle( | ||
| Environment* env, | ||
| TimerWrap::TimerCb fn, | ||
| void* user_data = nullptr); | ||
|
|
||
| TimerWrapHandle(const TimerWrapHandle&) = delete; | ||
|
|
||
| ~TimerWrapHandle() { Stop(); } | ||
|
|
||
| void Update(uint64_t interval, uint64_t repeat = 0); | ||
|
|
||
| void Ref(); | ||
|
|
||
| void Unref(); | ||
|
|
||
| void Stop(bool close = true); | ||
|
|
||
| void MemoryInfo(node::MemoryTracker* tracker) const override; | ||
|
|
||
| SET_MEMORY_INFO_NAME(TimerWrapHandle) | ||
| SET_SELF_SIZE(TimerWrapHandle) | ||
|
|
||
| private: | ||
| static void CleanupHook(void* data); | ||
| TimerWrap* timer_; | ||
| }; | ||
|
|
||
| } // namespace node | ||
|
|
||
| #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
|
||
| #endif // SRC_TIMER_WRAP_H_ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.