mc_sgx_sync/mutex.rs
1// Copyright (c) The Rust Foundation
2// Copyright (c) 2023 The MobileCoin Foundation
3
4//! mutex.rs implementation more or less copied from
5//! [rust source](https://github.com/rust-lang/rust.git) at
6//! [606c3907](https://github.com/rust-lang/rust/commit/606c3907251397a42e23d3e60de31be9d32525d5)
7//!
8//! Differences:
9//! - The imports were changed to work with the `mc-sgx` crates.
10//! - The stable attributes have been removed
11//! - The unstable attributes have been removed
12//! - Items that are crate only were converted from `pub` to `pub(crate)`
13//! - Removed examples that were not possible in an SGX enclave have been omitted
14//! - Ran `cargo fmt`
15//! - Removed unnecessary unsafe blocks
16
17#![allow(dead_code)]
18
19use crate::sys::locks as sys;
20use crate::{poison, LockResult, TryLockError, TryLockResult};
21use core::cell::UnsafeCell;
22use core::fmt;
23use core::ops::{Deref, DerefMut};
24
25/// A mutual exclusion primitive useful for protecting shared data
26///
27/// This mutex will block threads waiting for the lock to become available. The
28/// mutex can be created via a [`new`] constructor. Each mutex has a type parameter
29/// which represents the data that it is protecting. The data can only be accessed
30/// through the RAII guards returned from [`lock`] and [`try_lock`], which
31/// guarantees that the data is only ever accessed when the mutex is locked.
32///
33/// # Poisoning
34///
35/// The mutexes in this module implement a strategy called "poisoning" where a
36/// mutex is considered poisoned whenever a thread panics while holding the
37/// mutex. Once a mutex is poisoned, all other threads are unable to access the
38/// data by default as it is likely tainted (some invariant is not being
39/// upheld).
40///
41/// For a mutex, this means that the [`lock`] and [`try_lock`] methods return a
42/// [`Result`] which indicates whether a mutex has been poisoned or not. Most
43/// usage of a mutex will simply [`unwrap()`] these results, propagating panics
44/// among threads to ensure that a possibly invalid invariant is not witnessed.
45///
46/// A poisoned mutex, however, does not prevent all access to the underlying
47/// data. The [`PoisonError`] type has an [`into_inner`] method which will return
48/// the guard that would have otherwise been returned on a successful lock. This
49/// allows access to the data, despite the lock being poisoned.
50///
51/// [`new`]: Self::new
52/// [`lock`]: Self::lock
53/// [`try_lock`]: Self::try_lock
54/// [`unwrap()`]: Result::unwrap
55/// [`PoisonError`]: super::PoisonError
56/// [`into_inner`]: super::PoisonError::into_inner
57pub struct Mutex<T: ?Sized> {
58 inner: sys::Mutex,
59 poison: poison::Flag,
60 data: UnsafeCell<T>,
61}
62
63// These are the only places where `T: Send` matters; all other
64// functionality works fine on a single thread.
65unsafe impl<T: ?Sized + Send> Send for Mutex<T> {}
66unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {}
67
68/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
69/// dropped (falls out of scope), the lock will be unlocked.
70///
71/// The data protected by the mutex can be accessed through this guard via its
72/// [`Deref`] and [`DerefMut`] implementations.
73///
74/// This structure is created by the [`lock`] and [`try_lock`] methods on
75/// [`Mutex`].
76///
77/// [`lock`]: Mutex::lock
78/// [`try_lock`]: Mutex::try_lock
79#[must_use = "if unused the Mutex will immediately unlock"]
80#[must_not_suspend = "holding a MutexGuard across suspend \
81 points can cause deadlocks, delays, \
82 and cause Futures to not implement `Send`"]
83#[clippy::has_significant_drop]
84pub struct MutexGuard<'a, T: ?Sized + 'a> {
85 lock: &'a Mutex<T>,
86 poison: poison::Guard,
87}
88
89impl<T: ?Sized> !Send for MutexGuard<'_, T> {}
90unsafe impl<T: ?Sized + Sync> Sync for MutexGuard<'_, T> {}
91
92impl<T> Mutex<T> {
93 /// Creates a new mutex in an unlocked state ready for use.
94 ///
95 /// # Examples
96 ///
97 /// ```
98 /// use std::sync::Mutex;
99 ///
100 /// let mutex = Mutex::new(0);
101 /// ```
102 pub const fn new(t: T) -> Mutex<T> {
103 Mutex {
104 inner: sys::Mutex::new(),
105 poison: poison::Flag::new(),
106 data: UnsafeCell::new(t),
107 }
108 }
109}
110
111impl<T: ?Sized> Mutex<T> {
112 /// Acquires a mutex, blocking the current thread until it is able to do so.
113 ///
114 /// This function will block the local thread until it is available to acquire
115 /// the mutex. Upon returning, the thread is the only thread with the lock
116 /// held. An RAII guard is returned to allow scoped unlock of the lock. When
117 /// the guard goes out of scope, the mutex will be unlocked.
118 ///
119 /// The exact behavior on locking a mutex in the thread which already holds
120 /// the lock is left unspecified. However, this function will not return on
121 /// the second call (it might panic or deadlock, for example).
122 ///
123 /// # Errors
124 ///
125 /// If another user of this mutex panicked while holding the mutex, then
126 /// this call will return an error once the mutex is acquired.
127 ///
128 /// # Panics
129 ///
130 /// This function might panic when called if the lock is already held by
131 /// the current thread.
132 pub fn lock(&self) -> LockResult<MutexGuard<'_, T>> {
133 unsafe {
134 self.inner.lock();
135 MutexGuard::new(self)
136 }
137 }
138
139 /// Attempts to acquire this lock.
140 ///
141 /// If the lock could not be acquired at this time, then [`Err`] is returned.
142 /// Otherwise, an RAII guard is returned. The lock will be unlocked when the
143 /// guard is dropped.
144 ///
145 /// This function does not block.
146 ///
147 /// # Errors
148 ///
149 /// If another user of this mutex panicked while holding the mutex, then
150 /// this call will return the [`Poisoned`] error if the mutex would
151 /// otherwise be acquired.
152 ///
153 /// If the mutex could not be acquired because it is already locked, then
154 /// this call will return the [`WouldBlock`] error.
155 ///
156 /// [`Poisoned`]: TryLockError::Poisoned
157 /// [`WouldBlock`]: TryLockError::WouldBlock
158 pub fn try_lock(&self) -> TryLockResult<MutexGuard<'_, T>> {
159 unsafe {
160 if self.inner.try_lock() {
161 Ok(MutexGuard::new(self)?)
162 } else {
163 Err(TryLockError::WouldBlock)
164 }
165 }
166 }
167
168 /// Immediately drops the guard, and consequently unlocks the mutex.
169 ///
170 /// This function is equivalent to calling [`drop`] on the guard but is more self-documenting.
171 /// Alternately, the guard will be automatically dropped when it goes out of scope.
172 ///
173 /// ```
174 /// #![feature(mutex_unlock)]
175 ///
176 /// use std::sync::Mutex;
177 /// let mutex = Mutex::new(0);
178 ///
179 /// let mut guard = mutex.lock().unwrap();
180 /// *guard += 20;
181 /// Mutex::unlock(guard);
182 /// ```
183 pub fn unlock(guard: MutexGuard<'_, T>) {
184 drop(guard);
185 }
186
187 /// Determines whether the mutex is poisoned.
188 ///
189 /// If another thread is active, the mutex can still become poisoned at any
190 /// time. You should not trust a `false` value for program correctness
191 /// without additional synchronization.
192 pub fn is_poisoned(&self) -> bool {
193 self.poison.get()
194 }
195
196 /// Clear the poisoned state from a mutex
197 ///
198 /// If the mutex is poisoned, it will remain poisoned until this function is called. This
199 /// allows recovering from a poisoned state and marking that it has recovered. For example, if
200 /// the value is overwritten by a known good value, then the mutex can be marked as
201 /// un-poisoned. Or possibly, the value could be inspected to determine if it is in a
202 /// consistent state, and if so the poison is removed.
203 pub fn clear_poison(&self) {
204 self.poison.clear();
205 }
206
207 /// Consumes this mutex, returning the underlying data.
208 ///
209 /// # Errors
210 ///
211 /// If another user of this mutex panicked while holding the mutex, then
212 /// this call will return an error instead.
213 ///
214 /// # Examples
215 ///
216 /// ```
217 /// use std::sync::Mutex;
218 ///
219 /// let mutex = Mutex::new(0);
220 /// assert_eq!(mutex.into_inner().unwrap(), 0);
221 /// ```
222 pub fn into_inner(self) -> LockResult<T>
223 where
224 T: Sized,
225 {
226 let data = self.data.into_inner();
227 poison::map_result(self.poison.borrow(), |()| data)
228 }
229
230 /// Returns a mutable reference to the underlying data.
231 ///
232 /// Since this call borrows the `Mutex` mutably, no actual locking needs to
233 /// take place -- the mutable borrow statically guarantees no locks exist.
234 ///
235 /// # Errors
236 ///
237 /// If another user of this mutex panicked while holding the mutex, then
238 /// this call will return an error instead.
239 ///
240 /// # Examples
241 ///
242 /// ```
243 /// use std::sync::Mutex;
244 ///
245 /// let mut mutex = Mutex::new(0);
246 /// *mutex.get_mut().unwrap() = 10;
247 /// assert_eq!(*mutex.lock().unwrap(), 10);
248 /// ```
249 pub fn get_mut(&mut self) -> LockResult<&mut T> {
250 let data = self.data.get_mut();
251 poison::map_result(self.poison.borrow(), |()| data)
252 }
253}
254
255impl<T> From<T> for Mutex<T> {
256 /// Creates a new mutex in an unlocked state ready for use.
257 /// This is equivalent to [`Mutex::new`].
258 fn from(t: T) -> Self {
259 Mutex::new(t)
260 }
261}
262
263impl<T: ?Sized + Default> Default for Mutex<T> {
264 /// Creates a `Mutex<T>`, with the `Default` value for T.
265 fn default() -> Mutex<T> {
266 Mutex::new(Default::default())
267 }
268}
269
270impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
271 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
272 let mut d = f.debug_struct("Mutex");
273 match self.try_lock() {
274 Ok(guard) => {
275 d.field("data", &&*guard);
276 }
277 Err(TryLockError::Poisoned(err)) => {
278 d.field("data", &&**err.get_ref());
279 }
280 Err(TryLockError::WouldBlock) => {
281 struct LockedPlaceholder;
282 impl fmt::Debug for LockedPlaceholder {
283 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
284 f.write_str("<locked>")
285 }
286 }
287 d.field("data", &LockedPlaceholder);
288 }
289 }
290 d.field("poisoned", &self.poison.get());
291 d.finish_non_exhaustive()
292 }
293}
294
295impl<'mutex, T: ?Sized> MutexGuard<'mutex, T> {
296 unsafe fn new(lock: &'mutex Mutex<T>) -> LockResult<MutexGuard<'mutex, T>> {
297 poison::map_result(lock.poison.guard(), |guard| MutexGuard {
298 lock,
299 poison: guard,
300 })
301 }
302}
303
304impl<T: ?Sized> Deref for MutexGuard<'_, T> {
305 type Target = T;
306
307 fn deref(&self) -> &T {
308 unsafe { &*self.lock.data.get() }
309 }
310}
311
312impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
313 fn deref_mut(&mut self) -> &mut T {
314 unsafe { &mut *self.lock.data.get() }
315 }
316}
317
318impl<T: ?Sized> Drop for MutexGuard<'_, T> {
319 fn drop(&mut self) {
320 self.lock.poison.done(&self.poison);
321 self.lock.inner.unlock();
322 }
323}
324
325impl<T: ?Sized + fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
326 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
327 fmt::Debug::fmt(&**self, f)
328 }
329}
330
331impl<T: ?Sized + fmt::Display> fmt::Display for MutexGuard<'_, T> {
332 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
333 (**self).fmt(f)
334 }
335}
336
337pub(crate) fn guard_lock<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a sys::Mutex {
338 &guard.lock.inner
339}
340
341pub(crate) fn guard_poison<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a poison::Flag {
342 &guard.lock.poison
343}