os_sync/mutex/
mod.rs

1//! Mutex implementations
2
3mod sem;
4pub use sem::Mutex as SemMutex;
5
6///Token passed by [MutexGuard](struct.MutexGuard.html)
7pub struct GuardToken {
8}
9
10///Describes Mutex interface
11pub trait Mutex: Sized {
12    ///Creates new instance
13    ///
14    ///Returns if `Semaphore` is successfully created.
15    fn new() -> Option<Self>;
16
17    ///Acquires lock, returning guard that unlocks self on drop.
18    ///
19    ///If lock is already acquired, it blocks until mutex is unlocked
20    fn lock(&self) -> MutexGuard<'_, Self>;
21
22    ///Attempts to acquire lock, returning guard that unlocks self on drop.
23    ///
24    ///If lock is already acquired, it returns `None`
25    fn try_lock(&self) -> Option<MutexGuard<'_, Self>>;
26
27    ///Tells how to perform unlock.
28    ///
29    ///Method implementation should be safe, but is allowed to mis-behave when invoked without
30    ///prior `lock`
31    fn unlock(&self, token: GuardToken);
32}
33
34///Guard, created by locking Mutex.
35pub struct MutexGuard<'a, T: Mutex> {
36    mutex: &'a T
37}
38
39impl<T: Mutex> Drop for MutexGuard<'_, T> {
40    fn drop(&mut self) {
41        self.mutex.unlock(GuardToken {});
42    }
43}