GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/timer_wrap.cc Lines: 39 59 66.1 %
Date: 2020-07-19 22:14:24 Branches: 6 22 27.3 %

Line Branch Exec Source
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
2
TimerWrap::TimerWrap(Environment* env, const TimerCb& fn)
9
    : env_(env),
10
2
      fn_(fn) {
11
2
  uv_timer_init(env->event_loop(), &timer_);
12
2
  timer_.data = this;
13
2
}
14
15
void TimerWrap::Stop() {
16
  if (timer_.data == nullptr) return;
17
  uv_timer_stop(&timer_);
18
}
19
20
2
void TimerWrap::Close() {
21
2
  timer_.data = nullptr;
22
2
  env_->CloseHandle(reinterpret_cast<uv_handle_t*>(&timer_), TimerClosedCb);
23
2
}
24
25
2
void TimerWrap::TimerClosedCb(uv_handle_t* handle) {
26
  std::unique_ptr<TimerWrap> ptr(
27
4
      ContainerOf(&TimerWrap::timer_,
28
4
                  reinterpret_cast<uv_timer_t*>(handle)));
29
2
}
30
31
2
void TimerWrap::Update(uint64_t interval, uint64_t repeat) {
32
2
  if (timer_.data == nullptr) return;
33
2
  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
6
void TimerWrap::OnTimeout(uv_timer_t* timer) {
47
6
  TimerWrap* t = ContainerOf(&TimerWrap::timer_, timer);
48
6
  t->fn_();
49
6
}
50
51
2
TimerWrapHandle::TimerWrapHandle(
52
    Environment* env,
53
2
    const TimerWrap::TimerCb& fn) {
54
2
  timer_ = new TimerWrap(env, fn);
55
2
  env->AddCleanupHook(CleanupHook, this);
56
2
}
57
58
void TimerWrapHandle::Stop() {
59
  return timer_->Stop();
60
}
61
62
3
void TimerWrapHandle::Close() {
63
3
  if (timer_ != nullptr) {
64
2
    timer_->env()->RemoveCleanupHook(CleanupHook, this);
65
2
    timer_->Close();
66
  }
67
3
  timer_ = nullptr;
68
3
}
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
2
void TimerWrapHandle::Update(uint64_t interval, uint64_t repeat) {
81
2
  if (timer_ != nullptr)
82
2
    timer_->Update(interval, repeat);
83
2
}
84
85
void TimerWrapHandle::MemoryInfo(MemoryTracker* tracker) const {
86
  if (timer_ != nullptr)
87
    tracker->TrackField("timer", *timer_);
88
}
89
90
1
void TimerWrapHandle::CleanupHook(void* data) {
91
1
  static_cast<TimerWrapHandle*>(data)->Close();
92
1
}
93
94

14970
}  // namespace node