Struct bern_kernel::sync::Semaphore [−][src]
pub struct Semaphore { /* fields omitted */ }Expand description
Atomic counting semaphore
similar to Struct tokio::sync::Semaphore
and super::Mutex.
A semaphore can be used to
- synchronize on one or more event (e.g. interrupt)
- synchronize multiple tasks
Example
// put the mutex in a section where all tasks have access #[link_section = ".shared"] // create a counting semaphore that can be taken 4 times static SEMAPHORE: Semaphore = Semaphore::new(4); fn main() -> ! { /*...*/ sched::init(); // allocate an event slot in the kernel, so that tasks can wait for a permit SEMAPHORE.register().ok(); Task::new() .static_stack(kernel::alloc_static_stack!(512)) .spawn(move || { loop { /*...*/ // attempt to acquire a permit with timeout match SEMAPHORE.acquire(1000) { do_something(); }; // permit released automatically } }); }
Implementations
Allocate an event ot the semaphore.
Note: The kernel must be initialized before calling this method.
Try to acquire a semaphore permit (non-blocking). Returns a
SemaphorePermit or an error if no permit is available or semaphore
is poisoned.
Try to acquire a semaphore permit (blocking). Returns a
SemaphorePermit or an error if the request timed out or the semaphore
was poisoned.
Note: The timeout function is not implemented yet.
Number of permits that can be issued from this semaphore