mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-05 18:20:34 +00:00
Condvar: Add wait_while and wait_timeout_while
Also use a single &'static str for the panic message. BUG=b:179755651 TEST=cargo test Change-Id: I48172ebf0b59ca9c021c1f451badbe7090743f23 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2987584 Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org> Reviewed-by: Noah Gold <nkgold@google.com> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
This commit is contained in:
parent
79bcb99530
commit
b7cc9f2084
1 changed files with 30 additions and 8 deletions
|
@ -6,6 +6,8 @@ use std::fmt::{self, Debug};
|
|||
use std::sync::{Condvar as StdCondvar, MutexGuard, WaitTimeoutResult};
|
||||
use std::time::Duration;
|
||||
|
||||
static CONDVAR_POISONED: &str = "condvar is poisoned";
|
||||
|
||||
/// A Condition Variable.
|
||||
#[derive(Default)]
|
||||
pub struct Condvar {
|
||||
|
@ -22,10 +24,18 @@ impl Condvar {
|
|||
|
||||
/// Waits on a condvar, blocking the current thread until it is notified.
|
||||
pub fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
|
||||
match self.std.wait(guard) {
|
||||
Ok(guard) => guard,
|
||||
Err(_) => panic!("condvar is poisoned"),
|
||||
}
|
||||
self.std.wait(guard).expect(CONDVAR_POISONED)
|
||||
}
|
||||
|
||||
/// Blocks the current thread until this condition variable receives a notification and the
|
||||
/// provided condition is false.
|
||||
pub fn wait_while<'a, T, F>(&self, guard: MutexGuard<'a, T>, condition: F) -> MutexGuard<'a, T>
|
||||
where
|
||||
F: FnMut(&mut T) -> bool,
|
||||
{
|
||||
self.std
|
||||
.wait_while(guard, condition)
|
||||
.expect(CONDVAR_POISONED)
|
||||
}
|
||||
|
||||
/// Waits on a condvar, blocking the current thread until it is notified
|
||||
|
@ -35,10 +45,22 @@ impl Condvar {
|
|||
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"),
|
||||
}
|
||||
self.std.wait_timeout(guard, dur).expect(CONDVAR_POISONED)
|
||||
}
|
||||
|
||||
/// Waits on this condition variable for a notification, timing out after a specified duration.
|
||||
pub fn wait_timeout_while<'a, T, F>(
|
||||
&self,
|
||||
guard: MutexGuard<'a, T>,
|
||||
dur: Duration,
|
||||
condition: F,
|
||||
) -> (MutexGuard<'a, T>, WaitTimeoutResult)
|
||||
where
|
||||
F: FnMut(&mut T) -> bool,
|
||||
{
|
||||
self.std
|
||||
.wait_timeout_while(guard, dur, condition)
|
||||
.expect(CONDVAR_POISONED)
|
||||
}
|
||||
|
||||
/// Notifies one thread blocked by this condvar.
|
||||
|
|
Loading…
Reference in a new issue