GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: timer_wrap.cc Lines: 28 49 57.1 %
Date: 2022-05-23 04:15:47 Branches: 4 20 20.0 %

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