|
| 1 | +#include "env-inl.h" |
| 2 | +#include "memory_tracker-inl.h" |
| 3 | +#include "timer_wrap.h" |
| 4 | +#include "uv.h" |
| 5 | + |
| 6 | +namespace node { |
| 7 | + |
| 8 | +TimerWrap::TimerWrap(Environment* env, TimerCb fn, void* user_data) |
| 9 | + : env_(env), |
| 10 | + fn_(fn), |
| 11 | + user_data_(user_data) { |
| 12 | + uv_timer_init(env->event_loop(), &timer_); |
| 13 | + timer_.data = this; |
| 14 | +} |
| 15 | + |
| 16 | +void TimerWrap::Stop(bool close) { |
| 17 | + if (timer_.data == nullptr) return; |
| 18 | + uv_timer_stop(&timer_); |
| 19 | + if (LIKELY(close)) { |
| 20 | + timer_.data = nullptr; |
| 21 | + env_->CloseHandle(reinterpret_cast<uv_handle_t*>(&timer_), TimerClosedCb); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +void TimerWrap::TimerClosedCb(uv_handle_t* handle) { |
| 26 | + std::unique_ptr<TimerWrap> ptr( |
| 27 | + ContainerOf(&TimerWrap::timer_, |
| 28 | + reinterpret_cast<uv_timer_t*>(handle))); |
| 29 | +} |
| 30 | + |
| 31 | +void TimerWrap::Update(uint64_t interval, uint64_t repeat) { |
| 32 | + if (timer_.data == nullptr) return; |
| 33 | + uv_timer_start(&timer_, OnTimeout, interval, repeat); |
| 34 | +} |
| 35 | + |
| 36 | +void TimerWrap::Ref() { |
| 37 | + if (timer_.data == nullptr) return; |
| 38 | + uv_ref(reinterpret_cast<uv_handle_t*>(&timer_)); |
| 39 | +} |
| 40 | + |
| 41 | +void TimerWrap::Unref() { |
| 42 | + if (timer_.data == nullptr) return; |
| 43 | + uv_unref(reinterpret_cast<uv_handle_t*>(&timer_)); |
| 44 | +} |
| 45 | + |
| 46 | +void TimerWrap::OnTimeout(uv_timer_t* timer) { |
| 47 | + TimerWrap* t = ContainerOf(&TimerWrap::timer_, timer); |
| 48 | + t->fn_(t->user_data_); |
| 49 | +} |
| 50 | + |
| 51 | +TimerWrapHandle::TimerWrapHandle( |
| 52 | + Environment* env, |
| 53 | + TimerWrap::TimerCb fn, |
| 54 | + void* user_data) { |
| 55 | + timer_ = new TimerWrap(env, fn, user_data); |
| 56 | + env->AddCleanupHook(CleanupHook, this); |
| 57 | +} |
| 58 | + |
| 59 | +void TimerWrapHandle::Stop(bool close) { |
| 60 | + if (UNLIKELY(!close)) |
| 61 | + return timer_->Stop(close); |
| 62 | + |
| 63 | + if (timer_ != nullptr) { |
| 64 | + timer_->env()->RemoveCleanupHook(CleanupHook, this); |
| 65 | + timer_->Stop(); |
| 66 | + } |
| 67 | + timer_ = nullptr; |
| 68 | +} |
| 69 | + |
| 70 | +void TimerWrapHandle::Ref() { |
| 71 | + if (timer_ != nullptr) |
| 72 | + timer_->Ref(); |
| 73 | +} |
| 74 | + |
| 75 | +void TimerWrapHandle::Unref() { |
| 76 | + if (timer_ != nullptr) |
| 77 | + timer_->Unref(); |
| 78 | +} |
| 79 | + |
| 80 | +void TimerWrapHandle::Update(uint64_t interval, uint64_t repeat) { |
| 81 | + if (timer_ != nullptr) |
| 82 | + timer_->Update(interval, repeat); |
| 83 | +} |
| 84 | + |
| 85 | +void TimerWrapHandle::CleanupHook(void* data) { |
| 86 | + static_cast<TimerWrapHandle*>(data)->Stop(); |
| 87 | +} |
| 88 | + |
| 89 | +void TimerWrapHandle::MemoryInfo(node::MemoryTracker* tracker) const { |
| 90 | + if (timer_ != nullptr) |
| 91 | + tracker->TrackField("timer", *timer_); |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace node |
0 commit comments