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 |
|
|
TimerWrap(Environment* env, const TimerCb& fn); |
20 |
|
|
TimerWrap(const TimerWrap&) = delete; |
21 |
|
|
|
22 |
|
2 |
inline Environment* env() const { return env_; } |
23 |
|
|
|
24 |
|
|
// Stop calling the timer callback. |
25 |
|
|
void Stop(); |
26 |
|
|
// Render the timer unusable and delete this object. |
27 |
|
|
void Close(); |
28 |
|
|
|
29 |
|
|
// Starts / Restarts the Timer |
30 |
|
|
void Update(uint64_t interval, uint64_t repeat = 0); |
31 |
|
|
|
32 |
|
|
void Ref(); |
33 |
|
|
void Unref(); |
34 |
|
|
|
35 |
|
|
SET_NO_MEMORY_INFO(); |
36 |
|
|
SET_MEMORY_INFO_NAME(TimerWrap) |
37 |
|
|
SET_SELF_SIZE(TimerWrap) |
38 |
|
|
|
39 |
|
|
private: |
40 |
|
|
static void TimerClosedCb(uv_handle_t* handle); |
41 |
|
|
static void OnTimeout(uv_timer_t* timer); |
42 |
|
4 |
~TimerWrap() = default; |
43 |
|
|
|
44 |
|
|
Environment* env_; |
45 |
|
|
TimerCb fn_; |
46 |
|
|
uv_timer_t timer_; |
47 |
|
|
|
48 |
|
|
friend std::unique_ptr<TimerWrap>::deleter_type; |
49 |
|
|
}; |
50 |
|
|
|
51 |
|
|
class TimerWrapHandle : public MemoryRetainer { |
52 |
|
|
public: |
53 |
|
|
TimerWrapHandle( |
54 |
|
|
Environment* env, |
55 |
|
|
const TimerWrap::TimerCb& fn); |
56 |
|
|
|
57 |
|
|
TimerWrapHandle(const TimerWrapHandle&) = delete; |
58 |
|
|
|
59 |
|
2 |
~TimerWrapHandle() { Close(); } |
60 |
|
|
|
61 |
|
|
void Update(uint64_t interval, uint64_t repeat = 0); |
62 |
|
|
|
63 |
|
|
void Ref(); |
64 |
|
|
void Unref(); |
65 |
|
|
|
66 |
|
|
void Stop(); |
67 |
|
|
void Close(); |
68 |
|
|
|
69 |
|
|
void MemoryInfo(node::MemoryTracker* tracker) const override; |
70 |
|
|
|
71 |
|
|
SET_MEMORY_INFO_NAME(TimerWrapHandle) |
72 |
|
|
SET_SELF_SIZE(TimerWrapHandle) |
73 |
|
|
|
74 |
|
|
private: |
75 |
|
|
static void CleanupHook(void* data); |
76 |
|
|
|
77 |
|
|
TimerWrap* timer_; |
78 |
|
|
}; |
79 |
|
|
|
80 |
|
|
} // namespace node |
81 |
|
|
|
82 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
83 |
|
|
|
84 |
|
|
#endif // SRC_TIMER_WRAP_H_ |