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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use crate::{
    blocking::DefaultMutex,
    loom::cell::{MutPtr, UnsafeCell},
    util::fmt,
};
use core::{
    marker::PhantomData,
    ops::{Deref, DerefMut},
};

pub use mutex_traits::{RawMutex, ScopedRawMutex};

/// A blocking mutual exclusion lock for protecting shared data.
/// Each mutex has a type parameter which represents
/// the data that it is protecting. The data can only be accessed through the
/// RAII guards returned from [`lock`] and [`try_lock`], or within the closures
/// passed to [`with_lock`] and [`try_with_lock`], which guarantees that
/// the data is only ever accessed when the mutex is locked.
///
/// # Fairness
///
/// This is *not* a fair mutex.
///
/// # Overriding mutex implementations
///
/// This type is generic over a `Lock` type parameter which represents a raw
/// mutex implementation. By default, this is the [`DefaultMutex`]. To construct
/// a new `Mutex` with an alternative raw mutex implementation, use the
/// [`Mutex::new_with_raw_mutex`] cosntructor. See the [module-level documentation
/// on overriding mutex
/// implementations](crate::blocking#overriding-mutex-implementations) for
/// more details.
///
/// When `Lock` implements the [`RawMutex`] trait, the [`Mutex`] type provides
/// the [`lock`] and [`try_lock`] methods, which return a RAII [`MutexGuard`],
/// similar to the [`std::sync::Mutex`] API, in addition to the scoped
/// [`with_lock`] and  [`try_with_lock`] methods. When `Lock` only implements
/// [`ScopedRawMutex`], the [`Mutex`] type provides only the scoped
/// [`with_lock`] and  [`try_with_lock`] methods.
///
/// :warning: Note that [`DefaultMutex`] does *not* implement `RawMutex`, so
/// using the [`lock`] and [`try_lock`] RAII API requires selecting an
/// alternative [`RawMutex`] implementation.
///
/// # Loom-specific behavior
///
/// When `cfg(loom)` is enabled, this mutex will use Loom's simulated atomics,
/// checked `UnsafeCell`, and simulated spin loop hints.
///
/// [`lock`]: Mutex::lock
/// [`try_lock`]: Mutex::try_lock
/// [`with_lock`]: Mutex::with_lock
/// [`try_with_lock`]: Mutex::try_with_lock
/// [`std::sync::Mutex`]: https://doc.rust-lang.org/stable/std/sync/struct.Mutex.html
pub struct Mutex<T, Lock = DefaultMutex> {
    lock: Lock,
    data: UnsafeCell<T>,
}

/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
/// dropped (falls out of scope), the lock will be unlocked.
///
/// The data protected by the mutex can be accessed through this guard via its
/// [`Deref`] and [`DerefMut`] implementations.
///
/// This structure is created by the [`lock`] and [`try_lock`] methods on
/// [`Mutex`].
///
/// [`lock`]: Mutex::lock
/// [`try_lock`]: Mutex::try_lock
#[must_use = "if unused, the `Mutex` will immediately unlock"]
pub struct MutexGuard<'a, T, Lock: RawMutex> {
    ptr: MutPtr<T>,
    lock: &'a Lock,
    _marker: PhantomData<Lock::GuardMarker>,
}

impl<T> Mutex<T> {
    loom_const_fn! {
        /// Returns a new `Mutex` protecting the provided `data`.
        ///
        /// The returned `Mutex` is in an unlocked state, ready for use.
        ///
        /// This constructor returns a mutex that uses the [`DefaultMutex`]
        /// implementation. To use an alternative `RawMutex` type, use the
        /// [`new_with_raw_mutex`](Self::new_with_raw_mutex) constructor, instead.
        ///
        /// # Examples
        ///
        /// ```
        /// use maitake_sync::blocking::Mutex;
        ///
        /// let mutex = Mutex::new(0);
        /// ```
        #[must_use]
        pub fn new(data: T) -> Self {
            Self {
                lock: DefaultMutex::new(),
                data: UnsafeCell::new(data),
            }
        }
    }
}

impl<T, Lock> Mutex<T, Lock> {
    loom_const_fn! {
        /// Returns a new `Mutex` protecting the provided `data`, using
        /// `lock` type parameter as the raw mutex implementation.
        ///
        /// See the [module-level documentation on overriding mutex
        /// implementations](crate::blocking#overriding-mutex-implementations) for
        /// more details.
        ///
        /// The returned `Mutex` is in an unlocked state, ready for use.
        #[must_use]
        pub fn new_with_raw_mutex(data: T, lock: Lock) -> Self {
            Self {
                lock,
                data: UnsafeCell::new(data),
            }
        }
    }

    /// Consumes this `Mutex`, returning the guarded data.
    #[inline]
    #[must_use]
    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.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut lock = maitake_sync::blocking::Mutex::new(0);
    /// lock.with_lock(|data| *data = 10);
    /// assert_eq!(*lock.get_mut(), 10);
    /// ```
    pub fn get_mut(&mut self) -> &mut T {
        unsafe {
            // Safety: since this call borrows the `Mutex` mutably, no actual
            // locking needs to take place -- the mutable borrow statically
            // guarantees no locks exist.
            self.data.with_mut(|data| &mut *data)
        }
    }
}

impl<T, Lock: ScopedRawMutex> Mutex<T, Lock> {
    /// Lock this `Mutex`, blocking if it is not currently unlocked, and call
    /// `f()` with the locked data once the lock is acquired.
    ///
    /// When the `Mutex` is unlocked, this method locks it, calls `f()` with the
    /// data protected by the `Mutex`, and then unlocks the `Mutex` and returns
    /// the result of `f()`. If the `Mutex` is locked, this method blocks until
    /// it is unlocked, and then takes the lock.
    ///
    /// To return immediately rather than blocking, use [`Mutex::try_with_lock`]
    /// instead.
    ///
    /// This method is available as long as the `Mutex`'s `Lock` type parameter
    /// implements the [`ScopedRawMutex`] trait. See the [module-level
    /// documentation on overriding mutex
    /// implementations](crate::blocking#overriding-mutex-implementations) for
    /// more details.
    #[track_caller]
    pub fn with_lock<U>(&self, f: impl FnOnce(&mut T) -> U) -> U {
        self.lock.with_lock(|| {
            self.data.with_mut(|data| unsafe {
                // Safety: we just locked the mutex.
                f(&mut *data)
            })
        })
    }

    /// Attempt to lock this `Mutex` without blocking and call `f()` with the
    /// locked data if the lock is acquired.
    ///
    /// If the `Mutex` is unlocked, this method locks it, calls `f()` with the
    /// data protected by the `Mutex`, and then unlocks the `Mutex` and returns
    /// [`Some`]`(U)`. Otherwise, if the lock is already held, this method
    /// returns `None` immediately, without blocking.
    ///
    /// To block until the `Mutex` is unlocked instead of returning `None`, use
    /// [`Mutex::with_lock`] instead.
    ///
    /// This method is available as long as the `Mutex`'s `Lock` type parameter
    /// implements the [`ScopedRawMutex`] trait. See the [module-level
    /// documentation on overriding mutex
    /// implementations](crate::blocking#overriding-mutex-implementations) for
    /// more details.
    ///
    /// # Returns
    ///
    /// - [`Some`]`(U)` if the lock was acquired, containing the result of
    ///   `f()`.
    /// - [`None`] if the lock is currently held and could not be acquired
    ///   without blocking.
    #[track_caller]
    pub fn try_with_lock<U>(&self, f: impl FnOnce(&mut T) -> U) -> Option<U> {
        self.lock.try_with_lock(|| {
            self.data.with_mut(|data| unsafe {
                // Safety: we just locked the mutex.
                f(&mut *data)
            })
        })
    }
}

impl<T, Lock> Mutex<T, Lock>
where
    Lock: RawMutex,
{
    fn guard(&self) -> MutexGuard<'_, T, Lock> {
        MutexGuard {
            ptr: self.data.get_mut(),
            lock: &self.lock,
            _marker: PhantomData,
        }
    }

    /// Attempts to acquire this lock without blocking
    ///
    /// If the lock could not be acquired at this time, then [`None`] is returned.
    /// Otherwise, an RAII guard is returned. The lock will be unlocked when the
    /// guard is dropped.
    ///
    /// This function will never block.
    ///
    /// This method is only availble if the `Mutex`'s `Lock` type parameter
    /// implements the [`RawMutex`] trait. See the [module-level documentation
    /// on overriding mutex
    /// implementations](crate::blocking#overriding-mutex-implementations) for
    /// more details.
    #[must_use]
    #[cfg_attr(test, track_caller)]
    pub fn try_lock(&self) -> Option<MutexGuard<'_, T, Lock>> {
        if self.lock.try_lock() {
            Some(self.guard())
        } else {
            None
        }
    }

    /// Acquires a mutex, blocking until it is locked.
    ///
    /// This function will block until the mutex is available to lock. Upon
    /// returning, the thread is the only thread with the lock
    /// held. An RAII guard is returned to allow scoped unlock of the lock. When
    /// the guard goes out of scope, the mutex will be unlocked.
    ///
    /// This method is only availble if the `Mutex`'s `Lock` type parameter
    /// implements the [`RawMutex`] trait. See the [module-level documentation
    /// on overriding mutex
    /// implementations](crate::blocking#overriding-mutex-implementations) for
    /// more details.
    #[cfg_attr(test, track_caller)]
    pub fn lock(&self) -> MutexGuard<'_, T, Lock> {
        self.lock.lock();
        self.guard()
    }

    /// Forcibly unlock the mutex.
    ///
    /// If a lock is currently held, it will be released, regardless of who's
    /// holding it. Of course, this is **outrageously, disgustingly unsafe** and
    /// you should never do it.
    ///
    /// This method is only availble if the `Mutex`'s `Lock` type parameter
    /// implements the [`RawMutex`] trait. See the [module-level documentation
    /// on overriding mutex
    /// implementations](crate::blocking#overriding-mutex-implementations) for
    /// more details.
    ///
    /// # Safety
    ///
    /// This deliberately violates mutual exclusion.
    ///
    /// Only call this method when it is _guaranteed_ that no stack frame that
    /// has previously locked the mutex will ever continue executing.
    /// Essentially, this is only okay to call when the kernel is oopsing and
    /// all code running on other cores has already been killed.
    pub unsafe fn force_unlock(&self) {
        self.lock.unlock()
    }
}

impl<T: Default, Lock: Default> Default for Mutex<T, Lock> {
    fn default() -> Self {
        Self {
            lock: Default::default(),
            data: UnsafeCell::new(Default::default()),
        }
    }
}

impl<T, Lock> fmt::Debug for Mutex<T, Lock>
where
    T: fmt::Debug,
    Lock: ScopedRawMutex,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.try_with_lock(|data| {
            f.debug_struct("Mutex")
                .field("data", data)
                .field("lock", &format_args!("{}", core::any::type_name::<Lock>()))
                .finish()
        })
        .unwrap_or_else(|| {
            f.debug_struct("Mutex")
                .field("data", &format_args!("<locked>"))
                .field("lock", &format_args!("{}", core::any::type_name::<Lock>()))
                .finish()
        })
    }
}

unsafe impl<T: Send, Lock: Send> Send for Mutex<T, Lock> {}
/// A `Mutex` is [`Sync`] if `T` is [`Send`] and `Lock` is [`Sync`].
///
/// `T` must be [`Send`] because shared references to the `Mutex` allow mutable
/// access to `T` (via a [`MutexGuard`] or [`Mutex::with_lock`]), which can be
/// used to move `T` between threads using [`core::mem::replace`] or similar.
/// `T` does **not** need to be [`Sync`], and, in fact, a `Mutex` is often used
/// to protect `!Sync` data.
///
/// The `Lock` type must be `Sync` because sharing references to a mutex
/// implicitly share references to the `Lock` type as well --- locking the mutex
/// references it.
unsafe impl<T: Send, Lock: Sync> Sync for Mutex<T, Lock> {}

// === impl MutexGuard ===

impl<T, Lock: RawMutex> Deref for MutexGuard<'_, T, Lock> {
    type Target = T;
    #[inline]
    fn deref(&self) -> &Self::Target {
        unsafe {
            // Safety: we are holding the lock, so it is okay to dereference the
            // mut pointer.
            &*self.ptr.deref()
        }
    }
}

impl<T, Lock: RawMutex> DerefMut for MutexGuard<'_, T, Lock> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe {
            // Safety: we are holding the lock, so it is okay to dereference the
            // mut pointer.
            self.ptr.deref()
        }
    }
}

impl<T, Lock, R: ?Sized> AsRef<R> for MutexGuard<'_, T, Lock>
where
    T: AsRef<R>,
    Lock: RawMutex,
{
    #[inline]
    fn as_ref(&self) -> &R {
        self.deref().as_ref()
    }
}

impl<T, Lock, R: ?Sized> AsMut<R> for MutexGuard<'_, T, Lock>
where
    T: AsMut<R>,
    Lock: RawMutex,
{
    #[inline]
    fn as_mut(&mut self) -> &mut R {
        self.deref_mut().as_mut()
    }
}

impl<T, Lock> Drop for MutexGuard<'_, T, Lock>
where
    Lock: RawMutex,
{
    #[inline]
    #[cfg_attr(test, track_caller)]
    fn drop(&mut self) {
        unsafe { self.lock.unlock() }
    }
}

impl<T, Lock> fmt::Debug for MutexGuard<'_, T, Lock>
where
    T: fmt::Debug,
    Lock: RawMutex,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.deref().fmt(f)
    }
}

impl<T, Lock> fmt::Display for MutexGuard<'_, T, Lock>
where
    T: fmt::Display,
    Lock: RawMutex,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.deref().fmt(f)
    }
}

/// A [`MutexGuard`] is only [`Send`] if:
///
/// 1. the protected data (`T`) is `Send`, because the guard may be used to
///    mutably access the protected data, and can therefore be used to move it
///    using [`core::mem::replace`] or similar.
/// 2. the `Lock` type parameter is [`Sync`], because the guard contains a
///    reference to the `Lock` type, and therefore, sending the guard is sharing
///    a reference to the `Lock`.
/// 3. the `Lock` type's [`RawMutex::GuardMarker`] associated type is [`Send`],
///    because this indicates that the `Lock` type agrees that guards may be
///    [`Send`].
unsafe impl<T, Lock> Send for MutexGuard<'_, T, Lock>
where
    T: Send,
    Lock: RawMutex + Sync,
    Lock::GuardMarker: Send,
{
}

#[cfg(test)]
mod tests {
    use crate::loom::{self, thread};
    use crate::spin::Spinlock;
    use std::prelude::v1::*;
    use std::sync::Arc;

    use super::*;

    #[test]
    fn multithreaded() {
        loom::model(|| {
            let mutex = Arc::new(Mutex::new_with_raw_mutex(String::new(), Spinlock::new()));
            let mutex2 = mutex.clone();

            let t1 = thread::spawn(move || {
                tracing::info!("t1: locking...");
                let mut lock = mutex2.lock();
                tracing::info!("t1: locked");
                lock.push_str("bbbbb");
                tracing::info!("t1: dropping...");
            });

            {
                tracing::info!("t2: locking...");
                let mut lock = mutex.lock();
                tracing::info!("t2: locked");
                lock.push_str("bbbbb");
                tracing::info!("t2: dropping...");
            }
            t1.join().unwrap();
        });
    }

    #[test]
    fn try_lock() {
        loom::model(|| {
            let mutex = Mutex::new_with_raw_mutex(42, Spinlock::new());
            // First lock succeeds
            let a = mutex.try_lock();
            assert_eq!(a.as_ref().map(|r| **r), Some(42));

            // Additional lock failes
            let b = mutex.try_lock();
            assert!(b.is_none());

            // After dropping lock, it succeeds again
            ::core::mem::drop(a);
            let c = mutex.try_lock();
            assert_eq!(c.as_ref().map(|r| **r), Some(42));
        });
    }
}