]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/unsupported/locks/mutex.rs
New upstream version 1.67.1+dfsg1
[rustc.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 unsafe impl Send for Mutex {}
9 unsafe impl Sync for Mutex {} // no threads on this platform
10
11 impl Mutex {
12 #[inline]
13 #[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
14 pub const fn new() -> Mutex {
15 Mutex { locked: Cell::new(false) }
16 }
17
18 #[inline]
19 pub fn lock(&self) {
20 assert_eq!(self.locked.replace(true), false, "cannot recursively acquire mutex");
21 }
22
23 #[inline]
24 pub unsafe fn unlock(&self) {
25 self.locked.set(false);
26 }
27
28 #[inline]
29 pub fn try_lock(&self) -> bool {
30 self.locked.replace(true) == false
31 }
32 }