From 039b6727ba1cc31f322803392f0065dd3839a67d Mon Sep 17 00:00:00 2001 From: Fletcher Woodruff Date: Fri, 6 Sep 2019 11:09:39 -0600 Subject: sync: add wait_timeout method to condvar wrapper Adds a method wait_timeout to sync::Condvar which wraps std::sync::Condvar's wait_timeout. BUG=None TEST=cargo test Change-Id: I9888568b8bac779006080b505762016b6ca381e6 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1789913 Tested-by: Fletcher Woodruff Tested-by: kokoro Reviewed-by: Dylan Reid Reviewed-by: Chih-Yang Hsia Commit-Queue: Fletcher Woodruff --- sync/src/condvar.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/sync/src/condvar.rs b/sync/src/condvar.rs index 4a6e80d..6d5d2b2 100644 --- a/sync/src/condvar.rs +++ b/sync/src/condvar.rs @@ -3,7 +3,8 @@ // found in the LICENSE file. use std::fmt::{self, Debug}; -use std::sync::{Condvar as StdCondvar, MutexGuard}; +use std::sync::{Condvar as StdCondvar, MutexGuard, WaitTimeoutResult}; +use std::time::Duration; /// A Condition Variable. #[derive(Default)] @@ -27,6 +28,19 @@ impl Condvar { } } + /// Waits on a condvar, blocking the current thread until it is notified + /// or the specified duration has elapsed. + pub fn wait_timeout<'a, T>( + &self, + guard: MutexGuard<'a, T>, + dur: Duration, + ) -> (MutexGuard<'a, T>, WaitTimeoutResult) { + match self.std.wait_timeout(guard, dur) { + Ok(result) => result, + Err(_) => panic!("condvar is poisoned"), + } + } + /// Notifies one thread blocked by this condvar. pub fn notify_one(&self) { self.std.notify_one(); -- cgit 1.4.1