#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
mod posix;
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
pub use posix::Sem;
#[cfg(windows)]
mod win32;
#[cfg(windows)]
pub use win32::Sem;
#[cfg(any(target_os = "macos", target_os = "ios"))]
mod mac;
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub use mac::Sem;
pub trait Semaphore: Sized {
fn new(init: u32) -> Option<Self>;
fn wait(&self);
fn try_wait(&self) -> bool;
fn wait_timeout(&self, timeout: core::time::Duration) -> bool;
fn post(&self) -> bool;
fn signal(&self);
fn lock(&self) -> SemaphoreGuard<'_, Self> {
self.wait();
SemaphoreGuard {
sem: self
}
}
fn try_lock(&self) -> Option<SemaphoreGuard<'_, Self>> {
match self.try_wait() {
true => Some(SemaphoreGuard {
sem: self,
}),
false => None,
}
}
}
pub struct SemaphoreGuard<'a, T: Semaphore> {
sem: &'a T,
}
impl<T: Semaphore> Drop for SemaphoreGuard<'_, T> {
fn drop(&mut self) {
self.sem.signal();
}
}