mutex 1.0.2

An abstraction over closure-based mutexes
Documentation
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(feature = "fmt", warn(missing_debug_implementations))]

pub mod raw_impls;

use core::cell::UnsafeCell;
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};
use core::panic::AssertUnwindSafe;
pub use mutex_traits::{ConstInit, RawMutex, ScopedRawMutex};

/// Blocking mutex (not async)
///
/// Provides a blocking mutual exclusion primitive backed by an implementation of [`ScopedRawMutex`].
///
/// Which implementation you select depends on the context in which you're using the mutex, and you can choose which kind
/// of interior mutability fits your use case.
///
/// Use [`CriticalSectionRawMutex`] when data can be shared between threads and interrupts.
///
/// Use [`LocalRawMutex`] when data is only shared between tasks running on the same executor.
///
/// Use [`ThreadModeRawMutex`] when data is shared between tasks running on the same executor but you want a global singleton.
///
/// Use [`LockApiRawMutex`] if you wish to use a type implementing
/// [`lock_api::RawMutex`] as the mutex implementation.
///
/// In all cases, the blocking mutex is intended to be short lived and not held across await points.
///
/// [`CriticalSectionRawMutex`]: crate::raw_impls::cs::CriticalSectionRawMutex
/// [`LocalRawMutex`]: crate::raw_impls::local::LocalRawMutex
/// [`ThreadModeRawMutex`]:
///     crate::raw_impls::single_core_thread_mode::ThreadModeRawMutex
/// [`LockApiRawMutex`]: crate::raw_impls::lock_api_0_4::LockApiRawMutex
/// [`lock_api::RawMutex`]:
///     https://docs.rs/lock_api/0.4.0/lock_api/trait.RawMutex.html
///
/// ## Unwinding
///
/// When the "std" feature is active, calls to [`BlockingMutex::with_lock()`] and
/// [`BlockingMutex::try_with_lock()`] will attempt to [`catch_unwind()`], allowing
/// the mutex to be unlocked. After the mutex has been unlocked, the unwind will be
/// resumed with [`resume_unwind()`]. This comes with [all the caveats] that normally
/// come with [`catch_unwind()`].
///
/// [`catch_unwind()`]: https://doc.rust-lang.org/stable/std/panic/fn.catch_unwind.html
/// [`resume_unwind()`]: https://doc.rust-lang.org/stable/std/panic/fn.resume_unwind.html
/// [all the caveats]: https://doc.rust-lang.org/stable/std/panic/fn.catch_unwind.html#notes
pub struct BlockingMutex<R, T: ?Sized> {
    // NOTE: `raw` must be FIRST, so when using ThreadModeMutex the "can't drop in non-thread-mode" gets
    // to run BEFORE dropping `data`.
    raw: R,
    data: UnsafeCell<T>,
}

/// A RAII guard that allows access to the data guarded by a [`BlockingMutex`].
#[must_use]
pub struct MutexGuard<'mutex, R: RawMutex, T: ?Sized> {
    lock: &'mutex BlockingMutex<R, T>,
    /// This marker makes the guard `Send` or `!Send` based on the `RawMutex`
    /// implementation.
    _marker: PhantomData<R::GuardMarker>,
}

unsafe impl<R: ScopedRawMutex + Send, T: ?Sized + Send> Send for BlockingMutex<R, T> {}
unsafe impl<R: ScopedRawMutex + Sync, T: ?Sized + Send> Sync for BlockingMutex<R, T> {}

/// A wrapper function for std::panic::catch_unwind. Exists so we can stub it out
/// on non-std targets
#[cfg(feature = "std")]
#[inline(always)]
fn catch_unwind<F: FnOnce() -> R + std::panic::UnwindSafe, R>(
    f: F,
) -> Result<R, Box<dyn std::any::Any + Send>> {
    std::panic::catch_unwind(f)
}

/// A no-op wrapper function for no-std. We can't catch unwinds outside of std.
#[cfg(not(feature = "std"))]
#[inline(always)]
fn catch_unwind<F: FnOnce() -> R, R>(f: F) -> Result<R, core::convert::Infallible> {
    Ok(f())
}

impl<R: ConstInit, T> BlockingMutex<R, T> {
    /// Creates a new mutex in an unlocked state ready for use.
    #[inline]
    pub const fn new(val: T) -> BlockingMutex<R, T> {
        BlockingMutex {
            raw: R::INIT,
            data: UnsafeCell::new(val),
        }
    }
}

impl<R: ScopedRawMutex, T: ?Sized> BlockingMutex<R, T> {
    /// Locks the raw mutex and grants temporary access to the inner data
    ///
    /// Behavior when the lock is already locked is dependent on the behavior
    /// of the Raw mutex. See [`ScopedRawMutex::with_lock()`]'s documentation for
    /// more details
    pub fn with_lock<U>(&self, f: impl FnOnce(&mut T) -> U) -> U {
        let res = self.raw.with_lock(|| {
            let ptr = self.data.get();
            // SAFETY: Raw Mutex proves we have exclusive access to the inner data
            let inner = unsafe { &mut *ptr };

            // NOTE: We attempt to catch the unwind (if the "std" feature is active)
            // to defer the unwind until AFTER we have released the lock. Although
            // AssertUnwindSafe does not have load-bearing SAFETY requirements, we
            // believe this is fine because we will not observe the contents of
            // `inner` after an unwind has begun, we will only allow the raw lock
            // to be set back to unlocked. Once unlocked, we will resume unwinding
            // if a panic was caught.
            //
            // This allows us to avoid deadlocks in testing when panics occur in the
            // provided closure.
            catch_unwind(AssertUnwindSafe(|| f(inner)))
        });
        match res {
            Ok(g) => g,

            // If we do not have the "std" feature active, the no-std verrsion of
            // `catch_unwind` returns an `Infallible` error, which makes this statically
            // checked as impossible.
            #[cfg(feature = "std")]
            Err(b) => std::panic::resume_unwind(b),
        }
    }

    /// Locks the raw mutex and grants temporary access to the inner data
    ///
    /// Returns `Some(U)` if the lock was obtained. Returns `None` if the lock
    /// was already locked
    #[must_use]
    pub fn try_with_lock<U>(&self, f: impl FnOnce(&mut T) -> U) -> Option<U> {
        let res = self.raw.try_with_lock(|| {
            let ptr = self.data.get();
            // SAFETY: Raw Mutex proves we have exclusive access to the inner data
            let inner = unsafe { &mut *ptr };

            // NOTE: We attempt to catch the unwind (if the "std" feature is active)
            // to defer the unwind until AFTER we have released the lock. Although
            // AssertUnwindSafe does not have load-bearing SAFETY requirements, we
            // believe this is fine because we will not observe the contents of
            // `inner` after an unwind has begun, we will only allow the raw lock
            // to be set back to unlocked. Once unlocked, we will resume unwinding
            // if a panic was caught.
            //
            // This allows us to avoid deadlocks in testing when panics occur in the
            // provided closure.
            catch_unwind(AssertUnwindSafe(|| f(inner)))
        });
        match res {
            None => None,
            Some(Ok(g)) => Some(g),

            // If we do not have the "std" feature active, the no-std verrsion of
            // `catch_unwind` returns an `Infallible` error, which makes this statically
            // checked as impossible.
            #[cfg(feature = "std")]
            Some(Err(b)) => std::panic::resume_unwind(b),
        }
    }
}

impl<R: RawMutex, T: ?Sized> BlockingMutex<R, T> {
    /// Locks the raw mutex, returning a [`MutexGuard`] that grants temporary
    /// access to the inner data.
    ///
    /// Behavior when the lock is already locked is dependent on the behavior
    /// of the raw mutex. See [`RawMutex::lock()`]'s documentation for
    /// more details
    ///
    /// This method is only available when the `R` type parameter implements the
    /// [`RawMutex`] trait. If `R` can only implement the [`ScopedRawMutex`]
    /// subset, consider [`BlockingMutex::with_lock()`] instead.
    pub fn lock(&self) -> MutexGuard<'_, R, T> {
        self.raw.lock();
        MutexGuard {
            lock: self,
            _marker: PhantomData,
        }
    }

    /// Attempts to lock the raw mutex, returning a [`MutexGuard`] that grants
    /// temporary access to the inner data if the lock can be acquired.
    ///
    /// This method will never block, and instead returns [`None`] immediately
    /// if the mutex is already locked. To block until the mutex can be
    /// acquired, use [`BlockingMutex::lock()`] instead.
    ///
    /// This method is only available when the `R` type parameter implements the
    /// [`RawMutex`] trait. If `R` can only implement the [`ScopedRawMutex`]
    /// subset, consider [`BlockingMutex::try_with_lock()`] instead.
    ///
    /// # Returns
    ///
    /// - [`Some`]`(`[`MutexGuard`]`<R, T>)` if the mutex was not already
    ///   locked.
    /// - [`None`] if the mutex is already locked.
    pub fn try_lock(&self) -> Option<MutexGuard<'_, R, T>> {
        if self.raw.try_lock() {
            Some(MutexGuard {
                lock: self,
                _marker: PhantomData,
            })
        } else {
            None
        }
    }
}

impl<R, T> BlockingMutex<R, T> {
    /// Creates a new mutex based on a pre-existing raw mutex.
    ///
    /// This allows creating a mutex in a constant context on stable Rust.
    #[inline]
    pub const fn const_new(raw_mutex: R, val: T) -> BlockingMutex<R, T> {
        BlockingMutex {
            raw: raw_mutex,
            data: UnsafeCell::new(val),
        }
    }

    /// Consumes this mutex, returning the underlying data.
    #[inline]
    pub fn into_inner(self) -> T {
        self.data.into_inner()
    }

    /// Returns a mutable reference to the underlying data.
    ///
    /// Since this call borrows the `Mutex` mutably, no actual locking needs to
    /// take place---the mutable borrow statically guarantees no locks exist.
    #[inline]
    pub fn get_mut(&mut self) -> &mut T {
        unsafe { &mut *self.data.get() }
    }

    /// Returns a pointer to the inner storage
    ///
    /// # Safety
    ///
    /// Must NOT be called when the lock is taken
    pub unsafe fn get_unchecked(&self) -> *mut T {
        self.data.get()
    }
}

#[cfg(feature = "fmt")]
impl<R, T> core::fmt::Debug for BlockingMutex<R, T>
where
    R: ScopedRawMutex + core::fmt::Debug,
    T: ?Sized + core::fmt::Debug,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut s = f.debug_struct("BlockingMutex");
        s.field("raw", &self.raw);

        self.try_with_lock(|data| s.field("data", &data).finish())
            .unwrap_or_else(|| s.field("data", &format_args!("<locked>")).finish())
    }
}

// === impl MutexGuard ===

impl<R: RawMutex, T: ?Sized> Drop for MutexGuard<'_, R, T> {
    fn drop(&mut self) {
        debug_assert!(
            self.lock.raw.is_locked(),
            "tried to unlock a `Mutex` that was not locked! this is almost \
             certainly a bug in the `RawMutex` implementation (`{}`)",
            core::any::type_name::<R>(),
        );
        unsafe {
            // SAFETY: a `MutexGuard` is only created when the lock has
            // been acquired, so we are allowed to unlock it.
            self.lock.raw.unlock();
        }
    }
}

impl<R: RawMutex, T: ?Sized> Deref for MutexGuard<'_, R, T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &Self::Target {
        debug_assert!(
            self.lock.raw.is_locked(),
            "tried to dereference a `MutexGuard` that was not locked! this is \
             almost certainly a bug in the `RawMutex` implementation (`{}`)",
            core::any::type_name::<R>(),
        );
        unsafe {
            // SAFETY: a `MutexGuard` should only be constructed once the lock
            // is locked, and the lock should not be unlocked until the guard is
            // dropped.
            &*self.lock.data.get()
        }
    }
}

impl<R: RawMutex, T: ?Sized> DerefMut for MutexGuard<'_, R, T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        debug_assert!(
            self.lock.raw.is_locked(),
            "tried to mutably dereference a `MutexGuard` that was not locked! \
             this is almost certainly a bug in the `RawMutex` implementation \
             (`{}`)",
            core::any::type_name::<R>(),
        );
        unsafe {
            // SAFETY: a `MutexGuard` should only be constructed once the lock
            // is locked, and the lock should not be unlocked until the guard is
            // dropped.
            &mut *self.lock.data.get()
        }
    }
}

unsafe impl<R, T> Send for MutexGuard<'_, R, T>
where
    // A `MutexGuard` can only be `Send` if the protected data is `Send`, because
    // owning the guard can be used to move the data out of the lock using
    // `mem::replace` or similar.
    T: ?Sized + Send,
    // This is just required by the bounds on the declaration of `MutexGuard`:
    R: RawMutex,
    // The guard marker must be `Send` to allow sending the guard to another
    // thread/core.
    R::GuardMarker: Send,
{
}
unsafe impl<R, T> Sync for MutexGuard<'_, R, T>
where
    // An `&`-reference to a `MutexGuard` is morally equivalent to an
    // `&`-reference to a `T`.
    T: ?Sized + Sync,
    // This is just required by the bounds on the declaration of `MutexGuard`:
    R: RawMutex,
{
}

#[cfg(feature = "fmt")]
impl<R, T> core::fmt::Debug for MutexGuard<'_, R, T>
where
    T: ?Sized + core::fmt::Debug,
    R: RawMutex,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        core::fmt::Debug::fmt(&self.data, f)
    }
}

#[cfg(feature = "fmt")]
impl<R, T> core::fmt::Display for MutexGuard<'_, R, T>
where
    T: ?Sized + core::fmt::Display,
    R: RawMutex,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        core::fmt::Display::fmt(&self.data, f)
    }
}

#[cfg(feature = "std")]
#[cfg(test)]
mod test {
    use core::sync::atomic::{AtomicBool, Ordering};

    use crate::{raw_impls::cs::CriticalSectionRawMutex, BlockingMutex};

    #[test]
    fn unlocks_on_unwind() {
        static MUTEX: BlockingMutex<CriticalSectionRawMutex, u64> = BlockingMutex::new(0);

        // Normal access works
        let res = std::thread::spawn(|| {
            MUTEX.with_lock(|num| {
                let old = *num;
                *num += 1;
                old
            })
        })
        .join()
        .unwrap();
        assert_eq!(0, res);

        // Panic while accessing
        std::thread::spawn(|| {
            MUTEX.with_lock(|_num| {
                panic!();
            })
        })
        .join()
        .unwrap_err();

        // Normal access works
        let res = std::thread::spawn(|| {
            MUTEX.with_lock(|num| {
                let old = *num;
                *num += 1;
                old
            })
        })
        .join()
        .unwrap();
        assert_eq!(1, res);
    }

    #[test]
    fn try_unlocks_on_unwind() {
        static MUTEX: BlockingMutex<CriticalSectionRawMutex, u64> = BlockingMutex::new(0);

        // Normal access works
        let res = std::thread::spawn(|| {
            MUTEX.try_with_lock(|num| {
                let old = *num;
                *num += 1;
                old
            })
        })
        .join()
        .unwrap();
        assert_eq!(Some(0), res);

        // Panic while accessing. Use a bool to ensure the closure
        // did actually run (with access to the locked item)
        static TRIED: AtomicBool = AtomicBool::new(false);
        std::thread::spawn(|| {
            MUTEX.try_with_lock(|_num| {
                // Note that we DID try before panicking
                TRIED.store(true, Ordering::Relaxed);
                panic!();
            })
        })
        .join()
        .unwrap_err();
        // make sure that we did actually obtain the lock at some point
        assert_eq!(true, TRIED.load(Ordering::Relaxed));

        // Normal access works
        let res = std::thread::spawn(|| {
            MUTEX.try_with_lock(|num| {
                let old = *num;
                *num += 1;
                old
            })
        })
        .join()
        .unwrap();
        assert_eq!(Some(1), res);
    }
}