]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/unsupported/locks/mutex.rs
New upstream version 1.61.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 pub const fn new() -> Mutex {
15 Mutex { locked: Cell::new(false) }
16 }
17
18 #[inline]
19 pub unsafe fn init(&mut self) {}
20
21 #[inline]
22 pub unsafe fn lock(&self) {
23 assert_eq!(self.locked.replace(true), false, "cannot recursively acquire mutex");
24 }
25
26 #[inline]
27 pub unsafe fn unlock(&self) {
28 self.locked.set(false);
29 }
30
31 #[inline]
32 pub unsafe fn try_lock(&self) -> bool {
33 self.locked.replace(true) == false
34 }
35
36 #[inline]
37 pub unsafe fn destroy(&self) {}
38 }
39
40 // All empty stubs because this platform does not yet support threads, so lock
41 // acquisition always succeeds.
42 pub struct ReentrantMutex {}
43
44 impl ReentrantMutex {
45 pub const unsafe fn uninitialized() -> ReentrantMutex {
46 ReentrantMutex {}
47 }
48
49 pub unsafe fn init(&self) {}
50
51 pub unsafe fn lock(&self) {}
52
53 #[inline]
54 pub unsafe fn try_lock(&self) -> bool {
55 true
56 }
57
58 pub unsafe fn unlock(&self) {}
59
60 pub unsafe fn destroy(&self) {}
61 }