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-10-21 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
34
SigintWatchdog::SigintWatchdog(
98
  v8::Isolate* isolate, bool* received_signal)
99
34
    : isolate_(isolate), received_signal_(received_signal) {
100
  // Register this watchdog with the global SIGINT/Ctrl+C listener.
101
34
  SigintWatchdogHelper::GetInstance()->Register(this);
102
  // Start the helper thread, if that has not already happened.
103
34
  SigintWatchdogHelper::GetInstance()->Start();
104
34
}
105
106
107
32
SigintWatchdog::~SigintWatchdog() {
108
32
  SigintWatchdogHelper::GetInstance()->Unregister(this);
109
32
  SigintWatchdogHelper::GetInstance()->Stop();
110
32
}
111
112
113
11
void SigintWatchdog::HandleSigint() {
114
11
  *received_signal_ = true;
115
11
  isolate_->TerminateExecution();
116
11
}
117
118
#ifdef __POSIX__
119
50
void* SigintWatchdogHelper::RunSigintWatchdog(void* arg) {
120
  // Inside the helper thread.
121
  bool is_stopping;
122
123
50
  do {
124
50
    uv_sem_wait(&instance.sem_);
125
50
    is_stopping = InformWatchdogsAboutSignal();
126
50
  } while (!is_stopping);
127
128
37
  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
50
bool SigintWatchdogHelper::InformWatchdogsAboutSignal() {
155
50
  Mutex::ScopedLock list_lock(instance.list_mutex_);
156
157
50
  bool is_stopping = false;
158
#ifdef __POSIX__
159
50
  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

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

9735
}  // namespace node