1 // Copyright 2018 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 use std::fmt::{self, Debug}; 6 use std::sync::{Condvar as StdCondvar, MutexGuard}; 7 8 /// A Condition Variable. 9 #[derive(Default)] 10 pub struct Condvar { 11 std: StdCondvar, 12 } 13 14 impl Condvar { 15 /// Creates a new condvar that is ready to be waited on. new() -> Condvar16 pub fn new() -> Condvar { 17 Condvar { 18 std: StdCondvar::new(), 19 } 20 } 21 22 /// Waits on a condvar, blocking the current thread until it is notified. wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T>23 pub fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> { 24 match self.std.wait(guard) { 25 Ok(guard) => guard, 26 Err(_) => panic!("condvar is poisoned"), 27 } 28 } 29 30 /// Notifies one thread blocked by this condvar. notify_one(&self)31 pub fn notify_one(&self) { 32 self.std.notify_one(); 33 } 34 35 /// Notifies all threads blocked by this condvar. notify_all(&self)36 pub fn notify_all(&self) { 37 self.std.notify_all(); 38 } 39 } 40 41 impl Debug for Condvar { fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result42 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 43 Debug::fmt(&self.std, formatter) 44 } 45 } 46