1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Non-sticky condition notifications registered before predicate-lock release.
use super::{MutexGuard, gate::Gate, wait::Wait};
use crate::{Result, SuspensionReason};
/// A bounded condition variable used with a virtual mutex and a predicate loop.
/// Notifications without waiters are discarded. Always change the predicate
/// while holding the same mutex that the waiter uses.
pub struct Condvar {
gate: Gate,
}
impl Condvar {
/// Creates a condition variable using [`super::DEFAULT_WAIT_CAPACITY`].
pub fn new() -> Self {
Self::with_wait_capacity(super::DEFAULT_WAIT_CAPACITY)
.expect("default waiter capacity is positive")
}
/// Creates a condition variable with a positive outstanding-wait limit.
pub fn with_wait_capacity(wait_capacity: usize) -> Result<Self> {
Ok(Self {
gate: Gate::new(0, 0, wait_capacity)?,
})
}
/// Registers, unlocks, waits, then reacquires the predicate mutex.
///
/// On any error (including cancellation, deadline, or waiter capacity), the
/// supplied guard is dropped and the mutex is left unlocked. Reacquisition
/// itself obeys task policy and can fail. Notifications do not assert that
/// the predicate is true; callers must loop and recheck it.
pub fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> Result<MutexGuard<'a, T>> {
let wait = Wait::enter(SuspensionReason::Condvar)?;
let ticket = self.gate.subscribe(&wait)?;
let mutex = guard.mutex;
drop(guard);
ticket.wait(&wait)?;
mutex.lock()
}
/// Selects the oldest unselected waiter, if any.
pub fn notify_one(&self) {
self.gate.signal();
}
/// Selects all currently registered waiters without storing future permits.
pub fn notify_all(&self) {
self.gate.broadcast();
}
/// Permanently closes this variable and fails outstanding waits.
pub fn close(&self) {
self.gate.close();
}
/// Outstanding condition waits, including selected waiters.
pub fn waiting(&self) -> usize {
self.gate.waiting()
}
/// Configured outstanding-wait limit, including selected waiters.
pub fn wait_capacity(&self) -> usize {
self.gate.wait_capacity()
}
}
impl Default for Condvar {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
#[path = "condvar_test.rs"]
mod condvar_test;