@@ -160,7 +160,7 @@ BOOL WINAPI SigintWatchdogHelper::WinCtrlCHandlerRoutine(DWORD dwCtrlType) {
160
160
161
161
162
162
bool SigintWatchdogHelper::InformWatchdogsAboutSignal () {
163
- uv_mutex_lock (& instance.list_mutex_ );
163
+ Mutex::ScopedLock list_lock ( instance.list_mutex_ );
164
164
165
165
bool is_stopping = false ;
166
166
#ifdef __POSIX__
@@ -176,17 +176,15 @@ bool SigintWatchdogHelper::InformWatchdogsAboutSignal() {
176
176
for (auto it : instance.watchdogs_ )
177
177
it->HandleSigint ();
178
178
179
- uv_mutex_unlock (&instance.list_mutex_ );
180
179
return is_stopping;
181
180
}
182
181
183
182
184
183
int SigintWatchdogHelper::Start () {
185
- int ret = 0 ;
186
- uv_mutex_lock (&mutex_);
184
+ Mutex::ScopedLock lock (mutex_);
187
185
188
186
if (start_stop_count_++ > 0 ) {
189
- goto dont_start ;
187
+ return 0 ;
190
188
}
191
189
192
190
#ifdef __POSIX__
@@ -197,10 +195,10 @@ int SigintWatchdogHelper::Start() {
197
195
sigset_t sigmask;
198
196
sigfillset (&sigmask);
199
197
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 );
201
199
CHECK_EQ (0 , pthread_sigmask (SIG_SETMASK, &sigmask, nullptr ));
202
200
if (ret != 0 ) {
203
- goto dont_start ;
201
+ return ret ;
204
202
}
205
203
has_running_thread_ = true ;
206
204
@@ -209,34 +207,36 @@ int SigintWatchdogHelper::Start() {
209
207
SetConsoleCtrlHandler (WinCtrlCHandlerRoutine, TRUE );
210
208
#endif
211
209
212
- dont_start:
213
- uv_mutex_unlock (&mutex_);
214
- return ret;
210
+ return 0 ;
215
211
}
216
212
217
213
218
214
bool SigintWatchdogHelper::Stop () {
219
- uv_mutex_lock (&mutex_) ;
220
- uv_mutex_lock (&list_mutex_ );
215
+ bool had_pending_signal ;
216
+ Mutex::ScopedLock lock (mutex_ );
221
217
222
- bool had_pending_signal = has_pending_signal_;
218
+ {
219
+ Mutex::ScopedLock list_lock (list_mutex_);
223
220
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
+ }
228
227
229
228
#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 ;
232
231
#endif
233
232
234
- watchdogs_.clear ();
235
- uv_mutex_unlock (&list_mutex_);
233
+ watchdogs_.clear ();
234
+ }
236
235
237
236
#ifdef __POSIX__
238
237
if (!has_running_thread_) {
239
- goto dont_stop;
238
+ has_pending_signal_ = false ;
239
+ return had_pending_signal;
240
240
}
241
241
242
242
// Wake up the helper thread.
@@ -252,32 +252,26 @@ bool SigintWatchdogHelper::Stop() {
252
252
#endif
253
253
254
254
had_pending_signal = has_pending_signal_;
255
- dont_stop:
256
- uv_mutex_unlock (&mutex_);
257
-
258
255
has_pending_signal_ = false ;
256
+
259
257
return had_pending_signal;
260
258
}
261
259
262
260
263
261
void SigintWatchdogHelper::Register (SigintWatchdog* wd) {
264
- uv_mutex_lock (& list_mutex_);
262
+ Mutex::ScopedLock lock ( list_mutex_);
265
263
266
264
watchdogs_.push_back (wd);
267
-
268
- uv_mutex_unlock (&list_mutex_);
269
265
}
270
266
271
267
272
268
void SigintWatchdogHelper::Unregister (SigintWatchdog* wd) {
273
- uv_mutex_lock (& list_mutex_);
269
+ Mutex::ScopedLock lock ( list_mutex_);
274
270
275
271
auto it = std::find (watchdogs_.begin (), watchdogs_.end (), wd);
276
272
277
273
CHECK_NE (it, watchdogs_.end ());
278
274
watchdogs_.erase (it);
279
-
280
- uv_mutex_unlock (&list_mutex_);
281
275
}
282
276
283
277
@@ -289,9 +283,6 @@ SigintWatchdogHelper::SigintWatchdogHelper()
289
283
stopping_ = false ;
290
284
CHECK_EQ (0 , uv_sem_init (&sem_, 0 ));
291
285
#endif
292
-
293
- CHECK_EQ (0 , uv_mutex_init (&mutex_));
294
- CHECK_EQ (0 , uv_mutex_init (&list_mutex_));
295
286
}
296
287
297
288
@@ -303,9 +294,6 @@ SigintWatchdogHelper::~SigintWatchdogHelper() {
303
294
CHECK_EQ (has_running_thread_, false );
304
295
uv_sem_destroy (&sem_);
305
296
#endif
306
-
307
- uv_mutex_destroy (&mutex_);
308
- uv_mutex_destroy (&list_mutex_);
309
297
}
310
298
311
299
SigintWatchdogHelper SigintWatchdogHelper::instance;
0 commit comments