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 <fletcherw@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Chih-Yang Hsia <paulhsia@chromium.org>
Commit-Queue: Fletcher Woodruff <fletcherw@chromium.org>
This commit is contained in:
Fletcher Woodruff 2019-09-06 11:09:39 -06:00 committed by Commit Bot
parent 506105dc0d
commit 039b6727ba

View file

@ -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();