Struct bern_kernel::sync::Mutex [−][src]
pub struct Mutex<T> { /* fields omitted */ }Expand description
Atomic mutual exclusion
similar to std::sync::Mutex.
A mutex can be used
- as a simple and efficient form of communication between tasks, by synchronizing acces to share data between multiple tasks
- to protect data races on shared data or peripherals
Example
// put the mutex in a section where all tasks have access #[link_section = ".shared"] // create mutex containing an integer with an initial values of 42 static MUTEX: Mutex<u32> = Mutex::new(42); fn main() -> ! { /*...*/ sched::init(); // allocate an event slot in the kernel, so that tasks can wait for a lock MUTEX.register().ok(); Task::new() .static_stack(kernel::alloc_static_stack!(512)) .spawn(move || { loop { /*...*/ // attempt to lock a mutex with timeout match MUTEX.lock(1000) { // access the inner value mutably Ok(mut value) => *value = 134, Err(_) => {} }; // mutex unlocks automatically } }); }
Security
For multiple tasks to access a mutex it must be placed in shared memory section. If the data is placed in a shared section any part of the software can access the data. So the lock can be placed in the shared section instead of hiding it in the kernel behind a syscall. Using a shared mutex thus has limitted security but direct access to the lock decreases overhead.
Implementations
Allocate an event ot the mutex.
Note: The kernel must be initialized before calling this method.
Try to lock the mutex (non-blocking). Returns a MutexGuard or an
error if the mutex is not available or poisoned.
Try to lock the mutex (blocking). Returns a MutexGuard or an
error if the request timed out or the mutex was poisoned.
Note: The timeout function is not implemented yet.