use super::gate::Gate;
use crate::{Result, SuspensionReason};
pub struct Notify {
gate: Gate,
}
impl Notify {
pub fn new() -> Self {
Self::with_wait_capacity(super::DEFAULT_WAIT_CAPACITY)
.expect("default waiter capacity is positive")
}
pub fn with_wait_capacity(wait_capacity: usize) -> Result<Self> {
Ok(Self {
gate: Gate::new(0, 1, wait_capacity)?,
})
}
pub fn notified(&self) -> Result<()> {
self.gate.take(SuspensionReason::Notify)
}
pub fn try_notified(&self) -> Result<()> {
self.gate.try_take()
}
pub fn notify_one(&self) {
self.gate.signal();
}
pub fn notify_waiters(&self) {
self.gate.broadcast();
}
pub fn close(&self) {
self.gate.close();
}
pub fn is_closed(&self) -> bool {
self.gate.is_closed()
}
pub fn waiting(&self) -> usize {
self.gate.waiting()
}
pub fn wait_capacity(&self) -> usize {
self.gate.wait_capacity()
}
}
impl Default for Notify {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
#[path = "notify_test.rs"]
mod notify_test;