1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//! Mutex implementations

mod sem;
pub use sem::Mutex as SemMutex;

///Token passed by [MutexGuard](struct.MutexGuard.html)
pub struct GuardToken {
}

///Describes Mutex interface
pub trait Mutex: Sized {
    ///Creates new instance
    ///
    ///Returns if `Semaphore` is successfully created.
    fn new() -> Option<Self>;

    ///Acquires lock, returning guard that unlocks self on drop.
    ///
    ///If lock is already acquired, it blocks until mutex is unlocked
    fn lock(&self) -> MutexGuard<'_, Self>;

    ///Attempts to acquire lock, returning guard that unlocks self on drop.
    ///
    ///If lock is already acquired, it returns `None`
    fn try_lock(&self) -> Option<MutexGuard<'_, Self>>;

    ///Tells how to perform unlock.
    ///
    ///Method implementation should be safe, but is allowed to mis-behave when invoked without
    ///prior `lock`
    fn unlock(&self, token: GuardToken);
}

///Guard, created by locking Mutex.
pub struct MutexGuard<'a, T: Mutex> {
    mutex: &'a T
}

impl<T: Mutex> Drop for MutexGuard<'_, T> {
    fn drop(&mut self) {
        self.mutex.unlock(GuardToken {});
    }
}