pub struct Semaphore { /* private fields */ }
Expand description
Atomically counting semaphore for efficiently waiting on other threads, intended to be created by the platform implementation, making use of OS synchronization primitives.
On a single-threaded platform, these operations can be no-ops, because a decrement without a preceding increment would be a deadlock anyway. Users of the semaphore should expect this and probably panic if this happens.
Implementations§
Source§impl Semaphore
impl Semaphore
Sourcepub unsafe fn new(
semaphore_ptr: *const dyn SemaphoreImpl + Sync,
drop_fn: Option<fn(*const dyn SemaphoreImpl + Sync)>,
) -> Semaphore
pub unsafe fn new( semaphore_ptr: *const dyn SemaphoreImpl + Sync, drop_fn: Option<fn(*const dyn SemaphoreImpl + Sync)>, ) -> Semaphore
Creates a semaphore from very raw parts. Intended to be used in a platform implementation.
drop_fn
is called in Semaphore’s drop implementation and
semaphore_ptr
is passed in. The semaphore_ptr
isn’t used after that.
§Safety
semaphore_ptr
should be valid for the whole lifetime of the semaphore
(until drop).
Sourcepub fn single_threaded() -> Semaphore
pub fn single_threaded() -> Semaphore
Creates a no-op semaphore. Fits single-threaded platforms — will cause panics if used in multi-threaded ones.
Sourcepub fn decrement(&self)
pub fn decrement(&self)
Waits until the count is positive, and then decrements the semaphore’s count.
Allowed to wake up without a matching increment if the alternative is deadlocking. So this being matched by an increment can’t be depended on for unsafe operations. However, it’s fine to panic in such a case, because it’s a clear bug.