@@ -241,17 +241,9 @@ class Barrier {
241
241
public:
242
242
explicit Barrier (int threads) : threads_(threads), count_(0 ) {}
243
243
~Barrier () {}
244
- void Block () {
245
- std::unique_lock<std::mutex> lock (mu_);
246
- if (++count_ >= threads_) {
247
- lock.unlock ();
248
- cond_.notify_all ();
249
- } else {
250
- while (count_ < threads_) {
251
- cond_.wait (lock);
252
- }
253
- }
254
- }
244
+
245
+ // The first N-1 calls to Block block until the Nth call.
246
+ void Block ();
255
247
256
248
private:
257
249
int threads_;
@@ -267,19 +259,12 @@ class Notification {
267
259
public:
268
260
Notification () : waiting_(true ) {}
269
261
~Notification () {}
270
- void WaitForNotification () {
271
- std::unique_lock<std::mutex> lock (mu_);
272
- while (waiting_) {
273
- cond_.wait (lock);
274
- }
275
- }
276
- void Notify () {
277
- mu_.lock ();
278
- CHECK (waiting_);
279
- waiting_ = false ;
280
- mu_.unlock ();
281
- cond_.notify_all ();
282
- }
262
+
263
+ // Block until Notify is called.
264
+ void WaitForNotification ();
265
+
266
+ // Unblock WaitForNotification calls.
267
+ void Notify ();
283
268
284
269
private:
285
270
bool waiting_;
@@ -295,31 +280,14 @@ class ProducerConsumerQueue {
295
280
ProducerConsumerQueue () : closed_(false ) {}
296
281
~ProducerConsumerQueue () {}
297
282
298
- void Put (void * val) {
299
- CHECK (!closed_);
300
- std::unique_lock<std::mutex> lock (mu_);
301
- d_.push_back (val);
302
- lock.unlock ();
303
- cond_.notify_one ();
304
- }
305
- void * Get () {
306
- std::unique_lock<std::mutex> lock (mu_);
307
- while (d_.empty () && !closed_) {
308
- cond_.wait (lock);
309
- }
310
- if (d_.empty () && closed_) {
311
- return NULL ;
312
- }
313
- void * ret = d_.front ();
314
- d_.pop_front ();
315
- return ret;
316
- }
317
- void Close () {
318
- std::unique_lock<std::mutex> lock (mu_);
319
- closed_ = true ;
320
- lock.unlock ();
321
- cond_.notify_all ();
322
- }
283
+ // Add value onto the queue. Must not be NULL.
284
+ void Put (void * val);
285
+
286
+ // Get value off of the queue. Gets NULL if queue is closed.
287
+ void * Get ();
288
+
289
+ // Close queue. All subsequent Get calls will immediately return NULL.
290
+ void Close ();
323
291
324
292
private:
325
293
std::mutex mu_;
@@ -375,40 +343,19 @@ inline std::string UnhiddenFile(const std::string& dirname, int64_t micros) {
375
343
// }
376
344
// // dog falls out of scope, its thread is canceled and it quietly goes away.
377
345
class Watchdog {
378
- private:
379
- void Watch () {
380
- auto last = ctr_;
381
- while (true ) {
382
- auto now = GetCurrentTimeMicros ();
383
- auto recheck = now + kNumMicrosPerSecond * seconds_;
384
- for (; last == ctr_ && !done_ && now < recheck;
385
- now = GetCurrentTimeMicros ()) {
386
- SleepForSeconds (std::min (1.0 , double (seconds_) / 10 ));
387
- }
388
- if (done_) {
389
- return ;
390
- } else if (last != ctr_) {
391
- LOG (V2) << " Fed watchdog: " << description_;
392
- last = ctr_;
393
- continue ;
394
- }
395
- LOG (FATAL) << " WATCHDOG FAILURE: " << description_;
396
- }
397
- }
398
-
399
346
public:
400
- Watchdog (std::string description, int seconds)
401
- : description_(description), seconds_(seconds), ctr_(0 ), done_(false ) {
402
- t_ = new std::thread (&Watchdog::Watch, this );
403
- }
404
- ~Watchdog () {
405
- done_ = true ;
406
- t_->join ();
407
- delete t_;
408
- }
409
- void Feed () { ctr_++; }
347
+ // Create a watchdog that crashes if it hasn't been fed after X seconds.
348
+ Watchdog (std::string description, int seconds);
349
+
350
+ // Constructor stops the watchdog.
351
+ ~Watchdog ();
352
+
353
+ // Feed the watchdog.
354
+ void Feed ();
410
355
411
356
private:
357
+ void Watch ();
358
+
412
359
std::thread* t_;
413
360
std::string description_;
414
361
int seconds_;
0 commit comments