[][src]Struct glommio::Semaphore

pub struct Semaphore { /* fields omitted */ }

An implementation of semaphore that doesn't use helper threads, condition variables, and is friendly to single-threaded execution.

Implementations

impl Semaphore[src]

pub fn new(avail: u64) -> Semaphore[src]

Creates a new semaphore with the specified amount of units

Examples

use glommio::Semaphore;

let _ = Semaphore::new(1);

pub fn available(&self) -> u64[src]

Returns the amount of units currently available in this semaphore

Examples

use glommio::Semaphore;

let sem = Semaphore::new(1);
assert_eq!(sem.available(), 1);

pub async fn acquire_permit<'_>(&'_ self, units: u64) -> Result<Permit>[src]

Blocks until a permit can be acquired with the specified amount of units.

Returns Err() if the semaphore is closed during the wait.

Examples

use glommio::{LocalExecutor, Semaphore};

let sem = Semaphore::new(1);

let ex = LocalExecutor::make_default();
ex.run(async move {
    {
        let permit = sem.acquire_permit(1).await.unwrap();
        // once it is dropped it can be acquired again
        // going out of scope will drop
    }
    let _guard = sem.acquire_permit(1).await.unwrap();
});

pub async fn acquire<'_>(&'_ self, units: u64) -> Result<()>[src]

Acquires the specified amount of units from this semaphore.

The caller is then responsible to release it. Whenever possible, prefer acquire_permit().

Examples

use glommio::{LocalExecutor, Semaphore};

let sem = Semaphore::new(1);

let ex = LocalExecutor::make_default();
ex.run(async move {
    sem.acquire(1).await.unwrap();
    sem.signal(1); // Has to be signaled explicity. Be careful
});

pub fn signal(&self, units: u64)[src]

Signals the semaphore to release the specified amount of units.

This needs to be paired with a call to acquire(). You should not call this if the units were acquired with acquire_permit().

Examples

use glommio::{LocalExecutor, Semaphore};

let sem = Semaphore::new(0);

let ex = LocalExecutor::make_default();
ex.run(async move {
    // Note that we can signal to expand to more units than the original capacity had.
    sem.signal(1);
    sem.acquire(1).await.unwrap();
});

pub fn close(&self)[src]

Closes the semaphore

All existing waiters will return Err(), and no new waiters are allowed.

Examples

use glommio::{LocalExecutor, Semaphore};

let sem = Semaphore::new(0);

let ex = LocalExecutor::make_default();
ex.run(async move {
    // Note that we can signal to expand to more units than the original capacity had.
    sem.close();
    if let Ok(_) = sem.acquire(1).await {
        panic!("a closed semaphore should have errored");
    }
});

Trait Implementations

impl Debug for Semaphore[src]

Auto Trait Implementations

impl !RefUnwindSafe for Semaphore

impl !Send for Semaphore

impl !Sync for Semaphore

impl Unpin for Semaphore

impl !UnwindSafe for Semaphore

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T[src]

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.