]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unsupported/locks/mutex.rs
std: make `ReentrantMutex` movable and `const`; simplify `Stdout` initialization
[rust.git] / library / std / src / sys / unsupported / locks / mutex.rs
1 use crate::cell::Cell;
2
3 pub struct Mutex {
4     // This platform has no threads, so we can use a Cell here.
5     locked: Cell<bool>,
6 }
7
8 pub type MovableMutex = Mutex;
9
10 unsafe impl Send for Mutex {}
11 unsafe impl Sync for Mutex {} // no threads on this platform
12
13 impl Mutex {
14     #[inline]
15     pub const fn new() -> Mutex {
16         Mutex { locked: Cell::new(false) }
17     }
18
19     #[inline]
20     pub unsafe fn lock(&self) {
21         assert_eq!(self.locked.replace(true), false, "cannot recursively acquire mutex");
22     }
23
24     #[inline]
25     pub unsafe fn unlock(&self) {
26         self.locked.set(false);
27     }
28
29     #[inline]
30     pub unsafe fn try_lock(&self) -> bool {
31         self.locked.replace(true) == false
32     }
33 }