GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_watchdog.cc Lines: 24 123 19.5 %
Date: 2019-02-01 22:03:38 Branches: 7 50 14.0 %

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 <algorithm>
24
#include "debug_utils.h"
25
#include "node_errors.h"
26
#include "node_internals.h"
27
28
namespace node {
29
30
Watchdog::Watchdog(v8::Isolate* isolate, uint64_t ms, bool* timed_out)
31
    : isolate_(isolate), timed_out_(timed_out) {
32
33
  int rc;
34
  loop_ = new uv_loop_t;
35
  CHECK(loop_);
36
  rc = uv_loop_init(loop_);
37
  if (rc != 0) {
38
    FatalError("node::Watchdog::Watchdog()",
39
               "Failed to initialize uv loop.");
40
  }
41
42
  rc = uv_async_init(loop_, &async_, &Watchdog::Async);
43
  CHECK_EQ(0, rc);
44
45
  rc = uv_timer_init(loop_, &timer_);
46
  CHECK_EQ(0, rc);
47
48
  rc = uv_timer_start(&timer_, &Watchdog::Timer, ms, 0);
49
  CHECK_EQ(0, rc);
50
51
  rc = uv_thread_create(&thread_, &Watchdog::Run, this);
52
  CHECK_EQ(0, rc);
53
}
54
55
56
Watchdog::~Watchdog() {
57
  uv_async_send(&async_);
58
  uv_thread_join(&thread_);
59
60
  uv_close(reinterpret_cast<uv_handle_t*>(&async_), nullptr);
61
62
  // UV_RUN_DEFAULT so that libuv has a chance to clean up.
63
  uv_run(loop_, UV_RUN_DEFAULT);
64
65
  CheckedUvLoopClose(loop_);
66
  delete loop_;
67
  loop_ = nullptr;
68
}
69
70
71
void Watchdog::Run(void* arg) {
72
  Watchdog* wd = static_cast<Watchdog*>(arg);
73
74
  // UV_RUN_DEFAULT the loop will be stopped either by the async or the
75
  // timer handle.
76
  uv_run(wd->loop_, UV_RUN_DEFAULT);
77
78
  // Loop ref count reaches zero when both handles are closed.
79
  // Close the timer handle on this side and let ~Watchdog() close async_
80
  uv_close(reinterpret_cast<uv_handle_t*>(&wd->timer_), nullptr);
81
}
82
83
84
void Watchdog::Async(uv_async_t* async) {
85
  Watchdog* w = ContainerOf(&Watchdog::async_, async);
86
  uv_stop(w->loop_);
87
}
88
89
90
void Watchdog::Timer(uv_timer_t* timer) {
91
  Watchdog* w = ContainerOf(&Watchdog::timer_, timer);
92
  *w->timed_out_ = true;
93
  w->isolate()->TerminateExecution();
94
  uv_stop(w->loop_);
95
}
96
97
98
SigintWatchdog::SigintWatchdog(
99
  v8::Isolate* isolate, bool* received_signal)
100
    : isolate_(isolate), received_signal_(received_signal) {
101
  // Register this watchdog with the global SIGINT/Ctrl+C listener.
102
  SigintWatchdogHelper::GetInstance()->Register(this);
103
  // Start the helper thread, if that has not already happened.
104
  SigintWatchdogHelper::GetInstance()->Start();
105
}
106
107
108
SigintWatchdog::~SigintWatchdog() {
109
  SigintWatchdogHelper::GetInstance()->Unregister(this);
110
  SigintWatchdogHelper::GetInstance()->Stop();
111
}
112
113
114
void SigintWatchdog::HandleSigint() {
115
  *received_signal_ = true;
116
  isolate_->TerminateExecution();
117
}
118
119
#ifdef __POSIX__
120
void* SigintWatchdogHelper::RunSigintWatchdog(void* arg) {
121
  // Inside the helper thread.
122
  bool is_stopping;
123
124
  do {
125
    uv_sem_wait(&instance.sem_);
126
    is_stopping = InformWatchdogsAboutSignal();
127
  } while (!is_stopping);
128
129
  return nullptr;
130
}
131
132
133
void SigintWatchdogHelper::HandleSignal(int signum) {
134
  uv_sem_post(&instance.sem_);
135
}
136
137
#else
138
139
// Windows starts a separate thread for executing the handler, so no extra
140
// helper thread is required.
141
BOOL WINAPI SigintWatchdogHelper::WinCtrlCHandlerRoutine(DWORD dwCtrlType) {
142
  if (!instance.watchdog_disabled_ &&
143
      (dwCtrlType == CTRL_C_EVENT || dwCtrlType == CTRL_BREAK_EVENT)) {
144
    InformWatchdogsAboutSignal();
145
146
    // Return true because the signal has been handled.
147
    return TRUE;
148
  } else {
149
    return FALSE;
150
  }
151
}
152
#endif
153
154
155
bool SigintWatchdogHelper::InformWatchdogsAboutSignal() {
156
  Mutex::ScopedLock list_lock(instance.list_mutex_);
157
158
  bool is_stopping = false;
159
#ifdef __POSIX__
160
  is_stopping = instance.stopping_;
161
#endif
162
163
  // If there are no listeners and the helper thread has been awoken by a signal
164
  // (= not when stopping it), indicate that by setting has_pending_signal_.
165
  if (instance.watchdogs_.empty() && !is_stopping) {
166
    instance.has_pending_signal_ = true;
167
  }
168
169
  for (auto it : instance.watchdogs_)
170
    it->HandleSigint();
171
172
  return is_stopping;
173
}
174
175
176
int SigintWatchdogHelper::Start() {
177
  Mutex::ScopedLock lock(mutex_);
178
179
  if (start_stop_count_++ > 0) {
180
    return 0;
181
  }
182
183
#ifdef __POSIX__
184
  CHECK_EQ(has_running_thread_, false);
185
  has_pending_signal_ = false;
186
  stopping_ = false;
187
188
  sigset_t sigmask;
189
  sigfillset(&sigmask);
190
  sigset_t savemask;
191
  CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, &savemask));
192
  sigmask = savemask;
193
  int ret = pthread_create(&thread_, nullptr, RunSigintWatchdog, nullptr);
194
  CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, nullptr));
195
  if (ret != 0) {
196
    return ret;
197
  }
198
  has_running_thread_ = true;
199
200
  RegisterSignalHandler(SIGINT, HandleSignal);
201
#else
202
  if (watchdog_disabled_) {
203
    watchdog_disabled_ = false;
204
  } else {
205
    SetConsoleCtrlHandler(WinCtrlCHandlerRoutine, TRUE);
206
  }
207
#endif
208
209
  return 0;
210
}
211
212
213
164
bool SigintWatchdogHelper::Stop() {
214
  bool had_pending_signal;
215
164
  Mutex::ScopedLock lock(mutex_);
216
217
  {
218
164
    Mutex::ScopedLock list_lock(list_mutex_);
219
220
164
    had_pending_signal = has_pending_signal_;
221
222
164
    if (--start_stop_count_ > 0) {
223
      has_pending_signal_ = false;
224
      return had_pending_signal;
225
    }
226
227
#ifdef __POSIX__
228
    // Set stopping now because it's only protected by list_mutex_.
229
164
    stopping_ = true;
230
#endif
231
232
164
    watchdogs_.clear();
233
  }
234
235
#ifdef __POSIX__
236
164
  if (!has_running_thread_) {
237
164
    has_pending_signal_ = false;
238
164
    return had_pending_signal;
239
  }
240
241
  // Wake up the helper thread.
242
  uv_sem_post(&sem_);
243
244
  // Wait for the helper thread to finish.
245
  CHECK_EQ(0, pthread_join(thread_, nullptr));
246
  has_running_thread_ = false;
247
248
  RegisterSignalHandler(SIGINT, SignalExit, true);
249
#else
250
  watchdog_disabled_ = true;
251
#endif
252
253
  had_pending_signal = has_pending_signal_;
254
  has_pending_signal_ = false;
255
256
  return had_pending_signal;
257
}
258
259
260
bool SigintWatchdogHelper::HasPendingSignal() {
261
  Mutex::ScopedLock lock(list_mutex_);
262
263
  return has_pending_signal_;
264
}
265
266
267
void SigintWatchdogHelper::Register(SigintWatchdog* wd) {
268
  Mutex::ScopedLock lock(list_mutex_);
269
270
  watchdogs_.push_back(wd);
271
}
272
273
274
void SigintWatchdogHelper::Unregister(SigintWatchdog* wd) {
275
  Mutex::ScopedLock lock(list_mutex_);
276
277
  auto it = std::find(watchdogs_.begin(), watchdogs_.end(), wd);
278
279
  CHECK_NE(it, watchdogs_.end());
280
  watchdogs_.erase(it);
281
}
282
283
284
164
SigintWatchdogHelper::SigintWatchdogHelper()
285
    : start_stop_count_(0),
286
164
      has_pending_signal_(false) {
287
#ifdef __POSIX__
288
164
  has_running_thread_ = false;
289
164
  stopping_ = false;
290
164
  CHECK_EQ(0, uv_sem_init(&sem_, 0));
291
#else
292
  watchdog_disabled_ = false;
293
#endif
294
164
}
295
296
297
328
SigintWatchdogHelper::~SigintWatchdogHelper() {
298
164
  start_stop_count_ = 0;
299
164
  Stop();
300
301
#ifdef __POSIX__
302
164
  CHECK_EQ(has_running_thread_, false);
303
164
  uv_sem_destroy(&sem_);
304
#endif
305
164
}
306
307
164
SigintWatchdogHelper SigintWatchdogHelper::instance;
308
309

492
}  // namespace node