GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_watchdog.cc Lines: 132 134 98.5 %
Date: 2017-12-18 Branches: 35 52 67.3 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#include "node_watchdog.h"
23
#include "node_internals.h"
24
#include <algorithm>
25
26
namespace node {
27
28
9
Watchdog::Watchdog(v8::Isolate* isolate, uint64_t ms, bool* timed_out)
29
9
    : isolate_(isolate), timed_out_(timed_out) {
30
31
  int rc;
32
9
  loop_ = new uv_loop_t;
33
9
  CHECK(loop_);
34
9
  rc = uv_loop_init(loop_);
35
9
  if (rc != 0) {
36
    FatalError("node::Watchdog::Watchdog()",
37
               "Failed to initialize uv loop.");
38
  }
39
40
9
  rc = uv_async_init(loop_, &async_, &Watchdog::Async);
41
9
  CHECK_EQ(0, rc);
42
43
9
  rc = uv_timer_init(loop_, &timer_);
44
9
  CHECK_EQ(0, rc);
45
46
9
  rc = uv_timer_start(&timer_, &Watchdog::Timer, ms, 0);
47
9
  CHECK_EQ(0, rc);
48
49
9
  rc = uv_thread_create(&thread_, &Watchdog::Run, this);
50
9
  CHECK_EQ(0, rc);
51
9
}
52
53
54
9
Watchdog::~Watchdog() {
55
9
  uv_async_send(&async_);
56
9
  uv_thread_join(&thread_);
57
58
9
  uv_close(reinterpret_cast<uv_handle_t*>(&async_), nullptr);
59
60
  // UV_RUN_DEFAULT so that libuv has a chance to clean up.
61
9
  uv_run(loop_, UV_RUN_DEFAULT);
62
63
9
  int rc = uv_loop_close(loop_);
64
9
  CHECK_EQ(0, rc);
65
9
  delete loop_;
66
9
  loop_ = nullptr;
67
9
}
68
69
70
9
void Watchdog::Run(void* arg) {
71
9
  Watchdog* wd = static_cast<Watchdog*>(arg);
72
73
  // UV_RUN_DEFAULT the loop will be stopped either by the async or the
74
  // timer handle.
75
9
  uv_run(wd->loop_, UV_RUN_DEFAULT);
76
77
  // Loop ref count reaches zero when both handles are closed.
78
  // Close the timer handle on this side and let ~Watchdog() close async_
79
9
  uv_close(reinterpret_cast<uv_handle_t*>(&wd->timer_), nullptr);
80
9
}
81
82
83
5
void Watchdog::Async(uv_async_t* async) {
84
5
  Watchdog* w = ContainerOf(&Watchdog::async_, async);
85
5
  uv_stop(w->loop_);
86
5
}
87
88
89
4
void Watchdog::Timer(uv_timer_t* timer) {
90
4
  Watchdog* w = ContainerOf(&Watchdog::timer_, timer);
91
4
  *w->timed_out_ = true;
92
4
  w->isolate()->TerminateExecution();
93
4
  uv_stop(w->loop_);
94
4
}
95
96
97
82
SigintWatchdog::SigintWatchdog(
98
  v8::Isolate* isolate, bool* received_signal)
99
82
    : isolate_(isolate), received_signal_(received_signal) {
100
  // Register this watchdog with the global SIGINT/Ctrl+C listener.
101
82
  SigintWatchdogHelper::GetInstance()->Register(this);
102
  // Start the helper thread, if that has not already happened.
103
82
  SigintWatchdogHelper::GetInstance()->Start();
104
82
}
105
106
107
80
SigintWatchdog::~SigintWatchdog() {
108
80
  SigintWatchdogHelper::GetInstance()->Unregister(this);
109
80
  SigintWatchdogHelper::GetInstance()->Stop();
110
80
}
111
112
113
11
void SigintWatchdog::HandleSigint() {
114
11
  *received_signal_ = true;
115
11
  isolate_->TerminateExecution();
116
11
}
117
118
#ifdef __POSIX__
119
98
void* SigintWatchdogHelper::RunSigintWatchdog(void* arg) {
120
  // Inside the helper thread.
121
  bool is_stopping;
122
123
98
  do {
124
98
    uv_sem_wait(&instance.sem_);
125
98
    is_stopping = InformWatchdogsAboutSignal();
126
98
  } while (!is_stopping);
127
128
85
  return nullptr;
129
}
130
131
132
13
void SigintWatchdogHelper::HandleSignal(int signum) {
133
13
  uv_sem_post(&instance.sem_);
134
13
}
135
136
#else
137
138
// Windows starts a separate thread for executing the handler, so no extra
139
// helper thread is required.
140
BOOL WINAPI SigintWatchdogHelper::WinCtrlCHandlerRoutine(DWORD dwCtrlType) {
141
  if (!instance.watchdog_disabled_ &&
142
      (dwCtrlType == CTRL_C_EVENT || dwCtrlType == CTRL_BREAK_EVENT)) {
143
    InformWatchdogsAboutSignal();
144
145
    // Return true because the signal has been handled.
146
    return TRUE;
147
  } else {
148
    return FALSE;
149
  }
150
}
151
#endif
152
153
154
98
bool SigintWatchdogHelper::InformWatchdogsAboutSignal() {
155
98
  Mutex::ScopedLock list_lock(instance.list_mutex_);
156
157
98
  bool is_stopping = false;
158
#ifdef __POSIX__
159
98
  is_stopping = instance.stopping_;
160
#endif
161
162
  // If there are no listeners and the helper thread has been awoken by a signal
163
  // (= not when stopping it), indicate that by setting has_pending_signal_.
164

98
  if (instance.watchdogs_.empty() && !is_stopping) {
165
3
    instance.has_pending_signal_ = true;
166
  }
167
168
109
  for (auto it : instance.watchdogs_)
169
11
    it->HandleSigint();
170
171
98
  return is_stopping;
172
}
173
174
175
161
int SigintWatchdogHelper::Start() {
176
161
  Mutex::ScopedLock lock(mutex_);
177
178
161
  if (start_stop_count_++ > 0) {
179
76
    return 0;
180
  }
181
182
#ifdef __POSIX__
183
85
  CHECK_EQ(has_running_thread_, false);
184
85
  has_pending_signal_ = false;
185
85
  stopping_ = false;
186
187
  sigset_t sigmask;
188
85
  sigfillset(&sigmask);
189
85
  CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, &sigmask));
190
85
  int ret = pthread_create(&thread_, nullptr, RunSigintWatchdog, nullptr);
191
85
  CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, nullptr));
192
85
  if (ret != 0) {
193
    return ret;
194
  }
195
85
  has_running_thread_ = true;
196
197
85
  RegisterSignalHandler(SIGINT, HandleSignal);
198
#else
199
  if (watchdog_disabled_) {
200
    watchdog_disabled_ = false;
201
  } else {
202
    SetConsoleCtrlHandler(WinCtrlCHandlerRoutine, TRUE);
203
  }
204
#endif
205
206
85
  return 0;
207
}
208
209
210
3548
bool SigintWatchdogHelper::Stop() {
211
  bool had_pending_signal;
212
3548
  Mutex::ScopedLock lock(mutex_);
213
214
  {
215
3548
    Mutex::ScopedLock list_lock(list_mutex_);
216
217
3548
    had_pending_signal = has_pending_signal_;
218
219
3548
    if (--start_stop_count_ > 0) {
220
74
      has_pending_signal_ = false;
221
74
      return had_pending_signal;
222
    }
223
224
#ifdef __POSIX__
225
    // Set stopping now because it's only protected by list_mutex_.
226
3474
    stopping_ = true;
227
#endif
228
229
3474
    watchdogs_.clear();
230
  }
231
232
#ifdef __POSIX__
233
3474
  if (!has_running_thread_) {
234
3389
    has_pending_signal_ = false;
235
3389
    return had_pending_signal;
236
  }
237
238
  // Wake up the helper thread.
239
85
  uv_sem_post(&sem_);
240
241
  // Wait for the helper thread to finish.
242
85
  CHECK_EQ(0, pthread_join(thread_, nullptr));
243
85
  has_running_thread_ = false;
244
245
85
  RegisterSignalHandler(SIGINT, SignalExit, true);
246
#else
247
  watchdog_disabled_ = true;
248
#endif
249
250
85
  had_pending_signal = has_pending_signal_;
251
85
  has_pending_signal_ = false;
252
253
85
  return had_pending_signal;
254
}
255
256
257
3
bool SigintWatchdogHelper::HasPendingSignal() {
258
3
  Mutex::ScopedLock lock(list_mutex_);
259
260
3
  return has_pending_signal_;
261
}
262
263
264
82
void SigintWatchdogHelper::Register(SigintWatchdog* wd) {
265
82
  Mutex::ScopedLock lock(list_mutex_);
266
267
82
  watchdogs_.push_back(wd);
268
82
}
269
270
271
80
void SigintWatchdogHelper::Unregister(SigintWatchdog* wd) {
272
80
  Mutex::ScopedLock lock(list_mutex_);
273
274
80
  auto it = std::find(watchdogs_.begin(), watchdogs_.end(), wd);
275
276
80
  CHECK_NE(it, watchdogs_.end());
277
80
  watchdogs_.erase(it);
278
80
}
279
280
281
3391
SigintWatchdogHelper::SigintWatchdogHelper()
282
    : start_stop_count_(0),
283
3391
      has_pending_signal_(false) {
284
#ifdef __POSIX__
285
3391
  has_running_thread_ = false;
286
3391
  stopping_ = false;
287
3391
  CHECK_EQ(0, uv_sem_init(&sem_, 0));
288
#else
289
  watchdog_disabled_ = false;
290
#endif
291
3391
}
292
293
294
6782
SigintWatchdogHelper::~SigintWatchdogHelper() {
295
3391
  start_stop_count_ = 0;
296
3391
  Stop();
297
298
#ifdef __POSIX__
299
3391
  CHECK_EQ(has_running_thread_, false);
300
3391
  uv_sem_destroy(&sem_);
301
#endif
302
3391
}
303
304
3391
SigintWatchdogHelper SigintWatchdogHelper::instance;
305
306

10173
}  // namespace node