pub struct Semphore { /* private fields */ }
Expand description

Semphore primitive

semaphores allow threads and coroutines to synchronize their actions.

A semaphore is an integer whose value is never allowed to fall below zero. Two operations can be performed on semaphores: increment the semaphore value by one (post()); and decrement the semaphore value by one (wait()). If the value of a semaphore is currently zero, then a wait() operation will block until the value becomes greater than zero.

Examples

use std::sync::Arc;
use may::coroutine;
use may::sync::Semphore;

let sem = Arc::new(Semphore::new(0));
let sem2 = sem.clone();

// spawn a coroutine, and then wait for it to start
unsafe {
    coroutine::spawn(move || {
        sem2.post();
    });
}

// wait for the coroutine to start up
sem.wait();

Implementations

create a semphore with the initial value

wait for a semphore if the semphore value is bigger than zero the function returns immediately otherwise it would block the until a post is executed

same as wait except that with an extra timeout value return false if timeout happened

return false if would block return true if successfully acquire one semphore resource

increment the semphore value and would wakeup a thread/coroutine that is calling wait

return the current semphore value

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.