Struct Semaphore

Source
pub struct Semaphore { /* private fields */ }
Expand description

A type that asynchronously distributes “permits.”

A permit is a token that allows the holder to perform some action. The semaphore itself does not dictate this action, but instead handles only the distribution of permits. This is useful for cases where you want to limit the number of concurrent operations, such as in a mutex.

n permits can be acquired through acquire. They can later be released through release.

§Examples

For examples, look at the implementations of Mutex and RwLock. Mutex uses a semaphore with a maximum of 1 permit to allow a single lock at a time. RwLock uses a semaphore with a maximum of max_readers permits to allow any number of readers. When a write call is encountered, it acquires all of the permits, blocking any new readers from locking.

Implementations§

Source§

impl Semaphore

Source

pub const fn new(initial_count: usize) -> Self

Creates a new Semaphore with the given initial number of permits.

Source

pub fn acquire(&self, n: usize) -> Acquire<'_>

Acquires n permits from the semaphore. These permits should be released later, or they will be permanently removed.

Source

pub fn release(&self, n: usize)

Releases n permits back to the semaphore.

Source

pub fn remaining(&self) -> usize

Returns the number of remaining permits.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.