Skip to content

Commit fb8840c

Browse files
addaleaxcjihrig
authored andcommitted
src: use RAII for mutexes in node_watchdog.cc
PR-URL: #7933 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 14b762f commit fb8840c

File tree

2 files changed

+28
-39
lines changed

2 files changed

+28
-39
lines changed

src/node_watchdog.cc

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ BOOL WINAPI SigintWatchdogHelper::WinCtrlCHandlerRoutine(DWORD dwCtrlType) {
160160

161161

162162
bool SigintWatchdogHelper::InformWatchdogsAboutSignal() {
163-
uv_mutex_lock(&instance.list_mutex_);
163+
Mutex::ScopedLock list_lock(instance.list_mutex_);
164164

165165
bool is_stopping = false;
166166
#ifdef __POSIX__
@@ -176,17 +176,15 @@ bool SigintWatchdogHelper::InformWatchdogsAboutSignal() {
176176
for (auto it : instance.watchdogs_)
177177
it->HandleSigint();
178178

179-
uv_mutex_unlock(&instance.list_mutex_);
180179
return is_stopping;
181180
}
182181

183182

184183
int SigintWatchdogHelper::Start() {
185-
int ret = 0;
186-
uv_mutex_lock(&mutex_);
184+
Mutex::ScopedLock lock(mutex_);
187185

188186
if (start_stop_count_++ > 0) {
189-
goto dont_start;
187+
return 0;
190188
}
191189

192190
#ifdef __POSIX__
@@ -197,10 +195,10 @@ int SigintWatchdogHelper::Start() {
197195
sigset_t sigmask;
198196
sigfillset(&sigmask);
199197
CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, &sigmask));
200-
ret = pthread_create(&thread_, nullptr, RunSigintWatchdog, nullptr);
198+
int ret = pthread_create(&thread_, nullptr, RunSigintWatchdog, nullptr);
201199
CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, nullptr));
202200
if (ret != 0) {
203-
goto dont_start;
201+
return ret;
204202
}
205203
has_running_thread_ = true;
206204

@@ -209,34 +207,36 @@ int SigintWatchdogHelper::Start() {
209207
SetConsoleCtrlHandler(WinCtrlCHandlerRoutine, TRUE);
210208
#endif
211209

212-
dont_start:
213-
uv_mutex_unlock(&mutex_);
214-
return ret;
210+
return 0;
215211
}
216212

217213

218214
bool SigintWatchdogHelper::Stop() {
219-
uv_mutex_lock(&mutex_);
220-
uv_mutex_lock(&list_mutex_);
215+
bool had_pending_signal;
216+
Mutex::ScopedLock lock(mutex_);
221217

222-
bool had_pending_signal = has_pending_signal_;
218+
{
219+
Mutex::ScopedLock list_lock(list_mutex_);
223220

224-
if (--start_stop_count_ > 0) {
225-
uv_mutex_unlock(&list_mutex_);
226-
goto dont_stop;
227-
}
221+
had_pending_signal = has_pending_signal_;
222+
223+
if (--start_stop_count_ > 0) {
224+
has_pending_signal_ = false;
225+
return had_pending_signal;
226+
}
228227

229228
#ifdef __POSIX__
230-
// Set stopping now because it's only protected by list_mutex_.
231-
stopping_ = true;
229+
// Set stopping now because it's only protected by list_mutex_.
230+
stopping_ = true;
232231
#endif
233232

234-
watchdogs_.clear();
235-
uv_mutex_unlock(&list_mutex_);
233+
watchdogs_.clear();
234+
}
236235

237236
#ifdef __POSIX__
238237
if (!has_running_thread_) {
239-
goto dont_stop;
238+
has_pending_signal_ = false;
239+
return had_pending_signal;
240240
}
241241

242242
// Wake up the helper thread.
@@ -252,32 +252,26 @@ bool SigintWatchdogHelper::Stop() {
252252
#endif
253253

254254
had_pending_signal = has_pending_signal_;
255-
dont_stop:
256-
uv_mutex_unlock(&mutex_);
257-
258255
has_pending_signal_ = false;
256+
259257
return had_pending_signal;
260258
}
261259

262260

263261
void SigintWatchdogHelper::Register(SigintWatchdog* wd) {
264-
uv_mutex_lock(&list_mutex_);
262+
Mutex::ScopedLock lock(list_mutex_);
265263

266264
watchdogs_.push_back(wd);
267-
268-
uv_mutex_unlock(&list_mutex_);
269265
}
270266

271267

272268
void SigintWatchdogHelper::Unregister(SigintWatchdog* wd) {
273-
uv_mutex_lock(&list_mutex_);
269+
Mutex::ScopedLock lock(list_mutex_);
274270

275271
auto it = std::find(watchdogs_.begin(), watchdogs_.end(), wd);
276272

277273
CHECK_NE(it, watchdogs_.end());
278274
watchdogs_.erase(it);
279-
280-
uv_mutex_unlock(&list_mutex_);
281275
}
282276

283277

@@ -289,9 +283,6 @@ SigintWatchdogHelper::SigintWatchdogHelper()
289283
stopping_ = false;
290284
CHECK_EQ(0, uv_sem_init(&sem_, 0));
291285
#endif
292-
293-
CHECK_EQ(0, uv_mutex_init(&mutex_));
294-
CHECK_EQ(0, uv_mutex_init(&list_mutex_));
295286
}
296287

297288

@@ -303,9 +294,6 @@ SigintWatchdogHelper::~SigintWatchdogHelper() {
303294
CHECK_EQ(has_running_thread_, false);
304295
uv_sem_destroy(&sem_);
305296
#endif
306-
307-
uv_mutex_destroy(&mutex_);
308-
uv_mutex_destroy(&list_mutex_);
309297
}
310298

311299
SigintWatchdogHelper SigintWatchdogHelper::instance;

src/node_watchdog.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "v8.h"
77
#include "uv.h"
8+
#include "node_mutex.h"
89
#include <vector>
910

1011
#ifdef __POSIX__
@@ -75,8 +76,8 @@ class SigintWatchdogHelper {
7576

7677
int start_stop_count_;
7778

78-
uv_mutex_t mutex_;
79-
uv_mutex_t list_mutex_;
79+
Mutex mutex_;
80+
Mutex list_mutex_;
8081
std::vector<SigintWatchdog*> watchdogs_;
8182
bool has_pending_signal_;
8283

0 commit comments

Comments
 (0)