use crate::{raw, SemaphoreError};
use core::{
ops::Deref,
sync::atomic::{AtomicUsize, Ordering},
};
pub struct Semaphore<T: ?Sized> {
raw: raw::Semaphore,
data: T,
}
impl<T: ?Sized> Semaphore<T> {
#[must_use]
pub fn at_max(&self, ordering: Ordering) -> bool {
self.raw.at_max(ordering)
}
#[must_use]
pub fn count(&self, ordering: Ordering) -> usize {
self.raw.count(ordering)
}
pub fn get(&self) -> SemaphoreGuard<T> {
assert_ne!(
self.raw.max, 0,
"Calling 'Semaphore::get' on a semaphore with a max of 0 will loop forever!"
);
while self.at_max(Ordering::Relaxed) {
#[cfg(feature = "std")]
std::thread::sleep(std::time::Duration::from_millis(50));
#[cfg(not(feature = "std"))]
core::hint::spin_loop();
}
self.try_get().unwrap()
}
#[inline]
pub fn try_get(&self) -> Result<SemaphoreGuard<T>, SemaphoreError> {
Ok(SemaphoreGuard::new(self.raw.try_get()?, &self.data))
}
#[inline]
pub fn get_mut(&mut self) -> &mut T {
&mut self.data
}
}
impl<T> Semaphore<T> {
pub fn new(value: T, max: usize) -> Self {
debug_assert_ne!(
max, 0,
"A semaphore with a maximum count of '0' generally useless"
);
Semaphore {
raw: raw::Semaphore::new(max),
data: value,
}
}
pub fn into_inner(self) -> T {
self.data
}
}
unsafe impl<T: ?Sized + Send> Send for Semaphore<T> {}
unsafe impl<T: ?Sized + Send> Sync for Semaphore<T> {}
#[must_use = "if unused, the guard will immediatly unlock"]
pub struct SemaphoreGuard<'guard, T: ?Sized> {
_inner: raw::SemaphoreGuard<'guard>,
data: &'guard T,
}
impl<'guard, T: ?Sized> SemaphoreGuard<'guard, T> {
fn new(raw_guard: raw::SemaphoreGuard<'guard>, data: &'guard T) -> Self {
SemaphoreGuard {
_inner: raw_guard,
data,
}
}
}
impl<'guard, T: ?Sized> Deref for SemaphoreGuard<'guard, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.data
}
}
unsafe impl<'guard, T: ?Sized + Sync> Sync for SemaphoreGuard<'guard, T> {}