1 #ifndef SRC_TIMER_WRAP_H_ 2 #define SRC_TIMER_WRAP_H_ 3 4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 5 6 #include "memory_tracker.h" 7 #include "env.h" 8 #include "uv.h" 9 10 #include <functional> 11 12 namespace node { 13 14 // Utility class that makes working with libuv timers a bit easier. 15 class TimerWrap final : public MemoryRetainer { 16 public: 17 using TimerCb = std::function<void()>; 18 19 template <typename... Args> 20 explicit inline TimerWrap(Environment* env, Args&&... args); 21 22 TimerWrap(const TimerWrap&) = delete; 23 env()24 inline Environment* env() const { return env_; } 25 26 // Stop calling the timer callback. 27 void Stop(); 28 // Render the timer unusable and delete this object. 29 void Close(); 30 31 // Starts / Restarts the Timer 32 void Update(uint64_t interval, uint64_t repeat = 0); 33 34 void Ref(); 35 void Unref(); 36 37 SET_NO_MEMORY_INFO() 38 SET_MEMORY_INFO_NAME(TimerWrap) 39 SET_SELF_SIZE(TimerWrap) 40 41 private: 42 static void TimerClosedCb(uv_handle_t* handle); 43 static void OnTimeout(uv_timer_t* timer); 44 ~TimerWrap() = default; 45 46 Environment* env_; 47 TimerCb fn_; 48 uv_timer_t timer_; 49 50 friend std::unique_ptr<TimerWrap>::deleter_type; 51 }; 52 53 class TimerWrapHandle : public MemoryRetainer { 54 public: 55 template <typename... Args> 56 explicit inline TimerWrapHandle(Environment* env, Args&&... args); 57 58 TimerWrapHandle(const TimerWrapHandle&) = delete; 59 ~TimerWrapHandle()60 ~TimerWrapHandle() { Close(); } 61 62 void Update(uint64_t interval, uint64_t repeat = 0); 63 64 void Ref(); 65 void Unref(); 66 67 void Stop(); 68 void Close(); 69 70 void MemoryInfo(node::MemoryTracker* tracker) const override; 71 72 SET_MEMORY_INFO_NAME(TimerWrapHandle) 73 SET_SELF_SIZE(TimerWrapHandle) 74 75 private: 76 static void CleanupHook(void* data); 77 78 TimerWrap* timer_; 79 }; 80 81 } // namespace node 82 83 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 84 85 #endif // SRC_TIMER_WRAP_H_ 86