-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathteeos.rs
50 lines (41 loc) · 1.06 KB
/
teeos.rs
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.
}
}