Skip to main content

Semaphore

Struct Semaphore 

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

A counting semaphore whose waits are futures, for the tasks of one shard.

A request for n permits waits in line behind the requests that arrived before it, and is granted when it reaches the front and n permits are free. A large request at the front is therefore never bypassed by smaller ones behind it, and try_acquire fails while anyone waits. close ends every wait with an AcquireError.

It is !Send and !Sync: share it between the tasks of a shard with an Rc.

use nuclei::sync::TryAcquireError;
use nuclei::sync::local::Semaphore;

let budget = Semaphore::new(4);
let first = budget.try_acquire_many(3).expect("three of four are free");
assert_eq!(budget.try_acquire_many(2).unwrap_err(), TryAcquireError::NoPermits);
drop(first);
assert_eq!(budget.available_permits(), 4);

Implementations§

Source§

impl Semaphore

Source

pub const MAX_PERMITS: usize

The most permits a semaphore can hold: usize::MAX >> 3.

Source

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

A semaphore holding permits permits.

§Panics

When permits exceeds MAX_PERMITS.

Source

pub fn available_permits(&self) -> usize

The permits free right now, including the ones kept for the front waiter until its request fits.

Source

pub fn add_permits(&self, n: usize)

Adds n new permits, handing them to waiters first. Permits added to a closed semaphore are counted, but nothing is handed out.

§Panics

When the semaphore would hold more than MAX_PERMITS.

Source

pub fn forget_permits(&self, n: usize) -> usize

Removes up to n free permits for good and returns how many it removed, which is fewer than n when fewer are free. Permits held by outstanding SemaphorePermits are not touched.

Source

pub fn acquire(&self) -> Acquire<'_> ⓘ

Waits for one permit.

Completes with an AcquireError once the semaphore is closed.

Source

pub fn acquire_many(&self, n: u32) -> Acquire<'_> ⓘ

Waits for n permits, which it takes together.

Completes with an AcquireError once the semaphore is closed.

§Panics

When n exceeds MAX_PERMITS, which only a target with a word narrower than 64 bits allows and which no semaphore can ever satisfy.

Source

pub fn try_acquire(&self) -> Result<SemaphorePermit<'_>, TryAcquireError>

Takes one permit if it is free and nobody waits.

Source

pub fn try_acquire_many( &self, n: u32, ) -> Result<SemaphorePermit<'_>, TryAcquireError>

Takes n permits together if they are free and nobody waits.

§Panics

When n exceeds MAX_PERMITS, as for acquire_many.

Source

pub fn close(&self)

Closes the semaphore. Every wait still queued ends in an AcquireError, and every later acquire fails at once: try_acquire and its sibling return TryAcquireError::Closed, whatever permits are free. A wait that was granted its permits before the close keeps them. Permits returned or added afterwards are counted, and available_permits shows them, but none is ever handed out. Closing twice does nothing.

Source

pub fn is_closed(&self) -> bool

Whether close was called.

Trait Implementations§

Source§

impl Debug for Semaphore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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.