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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use crate::sys::mutex as imp;
pub struct StaticMutex(imp::Mutex);
unsafe impl Sync for StaticMutex {}
impl StaticMutex {
pub const fn new() -> Self {
Self(imp::Mutex::new())
}
#[inline]
pub unsafe fn lock(&'static self) -> StaticMutexGuard {
self.0.lock();
StaticMutexGuard(&self.0)
}
}
#[must_use]
pub struct StaticMutexGuard(&'static imp::Mutex);
impl Drop for StaticMutexGuard {
#[inline]
fn drop(&mut self) {
unsafe {
self.0.unlock();
}
}
}
pub struct MovableMutex(imp::MovableMutex);
unsafe impl Sync for MovableMutex {}
impl MovableMutex {
pub fn new() -> Self {
let mut mutex = imp::MovableMutex::from(imp::Mutex::new());
unsafe { mutex.init() };
Self(mutex)
}
pub(super) fn raw(&self) -> &imp::Mutex {
&self.0
}
#[inline]
pub fn raw_lock(&self) {
unsafe { self.0.lock() }
}
#[inline]
pub fn try_lock(&self) -> bool {
unsafe { self.0.try_lock() }
}
#[inline]
pub unsafe fn raw_unlock(&self) {
self.0.unlock()
}
}
impl Drop for MovableMutex {
fn drop(&mut self) {
unsafe { self.0.destroy() };
}
}