about summary refs log tree commit diff
path: root/library/std/src/sys/sync/rwlock/teeos.rs
blob: 4a71a3abc272981f2c86a73dae8f999a36a59ad9 (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
use crate::sys::sync::mutex::Mutex;

/// we do not supported rwlock, so use mutex to simulate rwlock.
/// it's useful because so many code in std will use rwlock.
pub struct RwLock {
    inner: Mutex,
}

impl RwLock {
    #[inline]
    pub const fn new() -> RwLock {
        RwLock { inner: Mutex::new() }
    }

    #[inline]
    pub fn read(&self) {
        self.inner.lock()
    }

    #[inline]
    pub fn try_read(&self) -> bool {
        self.inner.try_lock()
    }

    #[inline]
    pub fn write(&self) {
        self.inner.lock()
    }

    #[inline]
    pub unsafe fn try_write(&self) -> bool {
        self.inner.try_lock()
    }

    #[inline]
    pub unsafe fn read_unlock(&self) {
        unsafe { self.inner.unlock() };
    }

    #[inline]
    pub unsafe fn write_unlock(&self) {
        unsafe { self.inner.unlock() };
    }

    #[inline]
    pub unsafe fn downgrade(&self) {
        // Since there is no difference between read-locked and write-locked on this platform, this
        // function is simply a no-op as only 1 reader can read: the original writer.
    }
}