]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/unsupported/locks/mutex.rs
New upstream version 1.63.0+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 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 init(&mut self) {}
21
22 #[inline]
23 pub unsafe fn lock(&self) {
24 assert_eq!(self.locked.replace(true), false, "cannot recursively acquire mutex");
25 }
26
27 #[inline]
28 pub unsafe fn unlock(&self) {
29 self.locked.set(false);
30 }
31
32 #[inline]
33 pub unsafe fn try_lock(&self) -> bool {
34 self.locked.replace(true) == false
35 }
36 }