summaryrefslogtreecommitdiffstats
path: root/library/std/src/sys/sgx/mutex.rs
blob: 0dbf020ebe06af53bd12e53de14d655aa89001a4 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use super::waitqueue::{try_lock_or_false, SpinMutex, WaitQueue, WaitVariable};
use crate::sys_common::lazy_box::{LazyBox, LazyInit};

/// FIXME: `UnsafeList` is not movable.
struct AllocatedMutex(SpinMutex<WaitVariable<bool>>);

pub struct Mutex {
    inner: LazyBox<AllocatedMutex>,
}

impl LazyInit for AllocatedMutex {
    fn init() -> Box<Self> {
        Box::new(AllocatedMutex(SpinMutex::new(WaitVariable::new(false))))
    }
}

// Implementation according to “Operating Systems: Three Easy Pieces”, chapter 28
impl Mutex {
    pub const fn new() -> Mutex {
        Mutex { inner: LazyBox::new() }
    }

    #[inline]
    pub fn lock(&self) {
        let mut guard = self.inner.0.lock();
        if *guard.lock_var() {
            // Another thread has the lock, wait
            WaitQueue::wait(guard, || {})
        // Another thread has passed the lock to us
        } else {
            // We are just now obtaining the lock
            *guard.lock_var_mut() = true;
        }
    }

    #[inline]
    pub unsafe fn unlock(&self) {
        let guard = self.inner.0.lock();
        if let Err(mut guard) = WaitQueue::notify_one(guard) {
            // No other waiters, unlock
            *guard.lock_var_mut() = false;
        } else {
            // There was a thread waiting, just pass the lock
        }
    }

    #[inline]
    pub fn try_lock(&self) -> bool {
        let mut guard = try_lock_or_false!(self.inner.0);
        if *guard.lock_var() {
            // Another thread has the lock
            false
        } else {
            // We are just now obtaining the lock
            *guard.lock_var_mut() = true;
            true
        }
    }
}