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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use core::{
cell::UnsafeCell,
fmt,
fmt::{Debug, Display, Formatter},
ops::{Deref, DerefMut},
};
use crate::{bindings, error::*};
use super::TIMEOUT_MAX;
pub struct Mutex<T: ?Sized> {
mutex: bindings::mutex_t,
data: UnsafeCell<T>,
}
unsafe impl<T: ?Sized + Send> Send for Mutex<T> {}
unsafe impl<T: ?Sized + Sync> Sync for Mutex<T> {}
impl<T> Mutex<T> {
#[inline]
pub fn new(data: T) -> Self {
Self::try_new(data).unwrap_or_else(|err| panic!("failed to create mutex: {:?}", err))
}
pub fn try_new(data: T) -> Result<Self, Error> {
Ok(Self {
data: UnsafeCell::new(data),
mutex: unsafe { bindings::mutex_recursive_create() }.check()?,
})
}
}
impl<T: ?Sized> Mutex<T> {
#[inline]
pub fn lock(&'_ self) -> MutexGuard<'_, T> {
self.try_lock()
.unwrap_or_else(|err| panic!("Failed to lock mutex: {:?}", err))
}
#[inline]
pub fn try_lock(&'_ self) -> Result<MutexGuard<'_, T>, Error> {
if unsafe { bindings::mutex_recursive_take(self.mutex, TIMEOUT_MAX) } {
Ok(MutexGuard(self))
} else {
Err(from_errno())
}
}
#[inline]
pub fn poll(&'_ self) -> Option<MutexGuard<'_, T>> {
if unsafe { bindings::mutex_recursive_take(self.mutex, 0) } {
Some(MutexGuard(self))
} else {
None
}
}
}
impl<T: ?Sized> Drop for Mutex<T> {
#[inline]
fn drop(&mut self) {
unsafe { bindings::mutex_delete(self.mutex) }
}
}
impl<T: ?Sized + Debug> Debug for Mutex<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self.poll() {
Some(guard) => f.debug_struct("Mutex").field("data", &&*guard).finish(),
None => {
struct LockedPlaceholder;
impl Debug for LockedPlaceholder {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("<locked>")
}
}
f.debug_struct("Mutex")
.field("data", &LockedPlaceholder)
.finish()
}
}
}
}
impl<T: ?Sized + Default> Default for Mutex<T> {
#[inline]
fn default() -> Self {
Self::new(Default::default())
}
}
impl<T> From<T> for Mutex<T> {
#[inline]
fn from(data: T) -> Self {
Self::new(data)
}
}
pub struct MutexGuard<'a, T: ?Sized>(&'a Mutex<T>);
impl<T: ?Sized> Deref for MutexGuard<'_, T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
unsafe { &*self.0.data.get() }
}
}
impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *self.0.data.get() }
}
}
impl<T: ?Sized> Drop for MutexGuard<'_, T> {
#[inline]
fn drop(&mut self) {
if !unsafe { bindings::mutex_recursive_give(self.0.mutex) } {
panic!("failed to return mutex: {:?}", from_errno());
}
}
}
impl<T: ?Sized + Debug> Debug for MutexGuard<'_, T> {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Debug::fmt(&**self, f)
}
}
impl<T: ?Sized + Display> Display for MutexGuard<'_, T> {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
(**self).fmt(f)
}
}
impl<T: ?Sized> !Send for MutexGuard<'_, T> {}
unsafe impl<T: ?Sized + Sync> Sync for MutexGuard<'_, T> {}