pub struct Semaphore<T: ?Sized> { /* private fields */ }
Expand description
Allows up to max
references to the data in the Semaphore
This behaves like RwLock<T>
with some key differences
- You can’t get a
&mut T
, only a&T
- You can have up to a maximum number of references at once
Implementations§
Source§impl<T: ?Sized> Semaphore<T>
impl<T: ?Sized> Semaphore<T>
Sourcepub fn at_max(&self, ordering: Ordering) -> bool
pub fn at_max(&self, ordering: Ordering) -> bool
Returns true if the current cound is >= the maximum count
Sourcepub fn count(&self, ordering: Ordering) -> usize
pub fn count(&self, ordering: Ordering) -> usize
Get the current number of references to the data
Sourcepub fn get(&self) -> SemaphoreGuard<'_, T>
pub fn get(&self) -> SemaphoreGuard<'_, T>
This function can be inefficient, as it uses std::thread::sleep
on std
and core::hint::spin_loop
on no_std
.
§Panics
This function will panic if max
== 0 because that will cause an infinite loop
Sourcepub fn try_get(&self) -> Result<SemaphoreGuard<'_, T>, SemaphoreError>
pub fn try_get(&self) -> Result<SemaphoreGuard<'_, T>, SemaphoreError>
Attempt to get the value in the semaphore.
This function will never block
§Errors
This function will return [SemaphoreError::AtMax
] if the current count is >= the maximum count
Trait Implementations§
impl<T: ?Sized + Send> Send for Semaphore<T>
impl<T: ?Sized + Send> Sync for Semaphore<T>
Auto Trait Implementations§
impl<T> !Freeze for Semaphore<T>
impl<T> RefUnwindSafe for Semaphore<T>where
T: RefUnwindSafe + ?Sized,
impl<T> Unpin for Semaphore<T>
impl<T> UnwindSafe for Semaphore<T>where
T: UnwindSafe + ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more