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 60 65.0 %
Date: 2020-08-07 22:13:19 Branches: 6 24 25.0 %

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
5
void TimerWrap::OnTimeout(uv_timer_t* timer) {
47
5
  TimerWrap* t = ContainerOf(&TimerWrap::timer_, timer);
48
5
  t->fn_();
49
5
}
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
  if (timer_ != nullptr)
60
    return timer_->Stop();
61
}
62
63
3
void TimerWrapHandle::Close() {
64
3
  if (timer_ != nullptr) {
65
2
    timer_->env()->RemoveCleanupHook(CleanupHook, this);
66
2
    timer_->Close();
67
  }
68
3
  timer_ = nullptr;
69
3
}
70
71
void TimerWrapHandle::Ref() {
72
  if (timer_ != nullptr)
73
    timer_->Ref();
74
}
75
76
void TimerWrapHandle::Unref() {
77
  if (timer_ != nullptr)
78
    timer_->Unref();
79
}
80
81
2
void TimerWrapHandle::Update(uint64_t interval, uint64_t repeat) {
82
2
  if (timer_ != nullptr)
83
2
    timer_->Update(interval, repeat);
84
2
}
85
86
void TimerWrapHandle::MemoryInfo(MemoryTracker* tracker) const {
87
  if (timer_ != nullptr)
88
    tracker->TrackField("timer", *timer_);
89
}
90
91
1
void TimerWrapHandle::CleanupHook(void* data) {
92
1
  static_cast<TimerWrapHandle*>(data)->Close();
93
1
}
94
95

13323
}  // namespace node