Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2020-07-03 17:42:45 +0300
committerJames M Snell <jasnell@gmail.com>2020-07-06 00:56:59 +0300
commitd4f69002b44c55d4db93c01d5468f79e48f46417 (patch)
tree83adddf219b2078fa02ae4c12ee0d2f87e849be2 /src/timer_wrap.h
parente7dadd376ec535e56faf3be75fe23549bf13abc9 (diff)
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> PR-URL: https://github.com/nodejs/node/pull/34186 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'src/timer_wrap.h')
-rw-r--r--src/timer_wrap.h84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/timer_wrap.h b/src/timer_wrap.h
new file mode 100644
index 00000000000..c75e17b776b
--- /dev/null
+++ b/src/timer_wrap.h
@@ -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_