use core::{convert::TryInto, time::Duration};
use crate::{
bindings,
error::{from_errno, Error, SentinelError},
};
pub struct Semaphore(bindings::sem_t);
impl Semaphore {
#[inline]
pub fn new(max_count: u32, init_count: u32) -> Self {
Self::try_new(max_count, init_count)
.unwrap_or_else(|err| panic!("failed to create semaphore: {}", err))
}
pub fn try_new(max_count: u32, init_count: u32) -> Result<Self, Error> {
Ok(Self(
unsafe { bindings::sem_create(max_count, init_count) }.check()?,
))
}
#[inline]
pub fn wait(&self, timeout: Duration) -> Result<(), Error> {
if unsafe { bindings::sem_wait(self.0, timeout.as_millis().try_into()?) } {
Ok(())
} else {
Err(from_errno())
}
}
#[inline]
pub fn post(&self) -> Result<(), Error> {
if unsafe { bindings::sem_post(self.0) } {
Ok(())
} else {
Err(from_errno())
}
}
#[inline]
pub fn count(&self) -> u32 {
unsafe { bindings::sem_get_count(self.0) }
}
}
impl Drop for Semaphore {
fn drop(&mut self) {
unsafe { bindings::sem_delete(self.0) }
}
}
unsafe impl Send for Semaphore {}
unsafe impl Sync for Semaphore {}