mcslock 0.4.1

An implementation of Mellor-Crummey and Scott contention-free lock for mutual exclusion, referred to as MCS lock.
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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
use core::ops::{Deref, DerefMut};

use crate::cfg::sync::Arc;

/// A trait for convertion from `&Self` to a type that implements the [`Deref`]
/// trait.
pub trait AsDeref {
    /// The type of the value that `Self::Deref` dereferences to.
    type Target: ?Sized;

    /// The type that implements [`Deref`] trait.
    type Deref<'a>: Deref<Target = Self::Target>
    where
        Self: 'a,
        Self::Target: 'a;

    /// Returns a instance of the type that implements the [`Deref`] trait.
    fn as_deref(&self) -> Self::Deref<'_>;
}

/// A trait for convertion from `&mut Self` to a type that implements the
/// [`DerefMut`] trait.
pub trait AsDerefMut: AsDeref {
    /// The type that implements [`DerefMut`] trait.
    type DerefMut<'a>: DerefMut<Target = Self::Target>
    where
        Self: 'a,
        Self::Target: 'a;

    /// Returns a instance of the type that implements the [`DerefMut`] trait.
    fn as_deref_mut(&mut self) -> Self::DerefMut<'_>;
}

/// A trait for lock types that can hold user defined values.
pub trait LockNew {
    /// The type of the value this lock holds.
    type Target: ?Sized;

    /// Creates a new mutex in an unlocked state ready for use.
    fn new(value: Self::Target) -> Self
    where
        Self::Target: Sized;
}

/// A trait for locks that mutably borrow from nodes and run closures against
/// the shared protected data.
pub trait LockWithThen: LockNew {
    /// The queue node type to borrow from and form the implicit queue.
    type Node: Default;

    /// A `guard` has access to a type that can can give shared and exclusive
    /// references to the protected data.
    type Guard<'a>: AsDerefMut<Target = Self::Target>
    where
        Self: 'a,
        Self::Target: 'a;

    /// Acquires a mutex and then runs the closure against the protected data.
    ///
    /// Requires mutably borrowing a `Self::Node` for the closure scope.
    fn lock_with_then<F, Ret>(&self, node: &mut Self::Node, f: F) -> Ret
    where
        F: FnOnce(Self::Guard<'_>) -> Ret;
}

/// A trait for locks that run closures against the shared protected data.
pub trait LockThen: LockWithThen {
    /// Acquires a mutex and then runs the closure against the protected data.
    ///
    /// A `Self::Node` is transparently allocated in the stack.
    fn lock_then<F, Ret>(&self, f: F) -> Ret
    where
        F: FnOnce(Self::Guard<'_>) -> Ret,
    {
        self.lock_with_then(&mut Self::Node::default(), f)
    }
}

/// A trait for locks that mutably borrow from nodes, test if the lock is busy
/// and run closures against the protected data in case of success.
pub trait TryLockWithThen: LockWithThen {
    /// Attempts to acquire this lock and then runs the closure against the
    /// protected data if successful.
    ///
    /// Requires mutably borrowing a `Self::Node` for the closure scope.
    fn try_lock_with_then<F, Ret>(&self, node: &mut Self::Node, f: F) -> Ret
    where
        F: FnOnce(Option<Self::Guard<'_>>) -> Ret;

    /// Returns `true` if the lock is currently held.
    #[cfg_attr(all(loom, test), allow(dead_code))]
    fn is_locked(&self) -> bool;
}

/// A trait for locks that test if the lock is busy and run closures against the
/// protected data in case of success.
pub trait TryLockThen: TryLockWithThen + LockThen {
    /// Attempts to acquire this lock and then runs the closure against the
    /// protected data if successful.
    ///
    /// A `Self::Node` is transparently allocated in the stack.
    #[cfg_attr(all(loom, test), allow(dead_code))]
    fn try_lock_then<F, Ret>(&self, f: F) -> Ret
    where
        F: FnOnce(Option<Self::Guard<'_>>) -> Ret,
    {
        self.try_lock_with_then(&mut Self::Node::default(), f)
    }
}

/// A trait for lock types that can return either the underlying value (by
// consuming the mutex) or a exclusive reference to it.
#[cfg(not(loom))]
pub trait LockData: LockNew {
    /// Consumes this mutex, returning the underlying data.
    fn into_inner(self) -> Self::Target
    where
        Self::Target: Sized;

    /// Returns a mutable reference to the underlying data.
    fn get_mut(&mut self) -> &mut Self::Target;
}

// Trivial implementation of `AsDeref` for `T` where `T: Deref`.
impl<T: Deref> AsDeref for T {
    type Target = <Self as Deref>::Target;

    type Deref<'a>
        = &'a <Self as Deref>::Target
    where
        Self: 'a,
        Self::Target: 'a;

    fn as_deref(&self) -> Self::Deref<'_> {
        self
    }
}

// Trivial implementation of `AsDerefMut` for `T` where `T: DerefMut`.
impl<T: DerefMut> AsDerefMut for T {
    type DerefMut<'a>
        = &'a mut <Self as Deref>::Target
    where
        Self: 'a,
        Self::Target: 'a;

    fn as_deref_mut(&mut self) -> Self::DerefMut<'_> {
        self
    }
}

/// An arbitrary unsigned integer type.
pub type Int = u32;

/// Get a copy of the mutex protected data.
pub fn get<L>(mutex: &Arc<L>) -> L::Target
where
    L: LockThen<Target: Sized + Copy>,
{
    mutex.lock_then(|data| *data.as_deref())
}

/// Increments a shared integer.
pub fn inc<L>(mutex: &Arc<L>)
where
    L: LockThen<Target = Int>,
{
    mutex.lock_then(inc_inner::<L>);
}

/// Increments a shared integer, mutably borrows a queue node.
#[cfg(not(all(loom, test)))]
pub fn inc_with<L>(mutex: &Arc<L>, node: &mut L::Node)
where
    L: LockWithThen<Target = Int>,
{
    mutex.lock_with_then(node, inc_inner::<L>);
}

/// Tries to increment a shared integer, mutably borrows a queue node.
#[cfg(not(all(loom, test)))]
pub fn try_inc_with<L>(mutex: &Arc<L>, node: &mut L::Node)
where
    L: TryLockWithThen<Target = Int>,
{
    mutex.try_lock_with_then(node, try_inc_inner::<L>);
}

/// Tries to increment a shared integer.
#[cfg(all(loom, test))]
pub fn try_inc<L>(mutex: &Arc<L>)
where
    L: TryLockThen<Target = Int>,
{
    mutex.try_lock_then(try_inc_inner::<L>);
}

/// Increments a shared integer through a guard instance.
fn inc_inner<L>(mut guard: L::Guard<'_>)
where
    L: LockWithThen<Target = Int>,
{
    *guard.as_deref_mut() += 1;
}

/// Tries to increment a shared integer through a guard instance.
fn try_inc_inner<L>(guard: Option<L::Guard<'_>>)
where
    L: TryLockWithThen<Target = Int>,
{
    guard.map(inc_inner::<L>);
}

#[cfg(all(not(loom), test))]
pub mod tests {
    // Modified test suite from the Rust's Mutex implementation with minor changes
    // since the API is not compatible with this crate implementation and some
    // new tests as well.
    //
    // Copyright 2014 The Rust Project Developers.
    //
    // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
    // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
    // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
    // option. This file may not be copied, modified, or distributed
    // except according to those terms.

    use core::ops::RangeInclusive;
    use std::fmt::Debug;
    use std::format;
    use std::string::ToString;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::mpsc::channel;
    use std::sync::Arc;
    use std::thread;

    #[cfg(feature = "barging")]
    use std::fmt::Display;

    use super::{get, inc, inc_with, try_inc_with, Int};
    use super::{AsDeref, AsDerefMut};
    use super::{LockData, LockThen, LockWithThen, TryLockThen, TryLockWithThen};

    #[derive(Eq, PartialEq, Debug)]
    pub struct NonCopy(u32);

    pub struct Foo(Arc<AtomicUsize>);

    impl Drop for Foo {
        fn drop(&mut self) {
            self.0.fetch_add(1, Ordering::SeqCst);
        }
    }

    const ITERS: Int = 1000;
    const THREADS: Int = 4;
    const EXPECTED_VALUE: Int = ITERS * THREADS;
    const EXPECTED_RANGE: RangeInclusive<Int> = 1..=EXPECTED_VALUE;

    fn inc_for<L, const END: Int>(mutex: &Arc<L>)
    where
        L: LockWithThen<Target = Int>,
    {
        let mut node = L::Node::default();
        for _ in 0..END {
            inc_with::<L>(mutex, &mut node);
        }
    }

    fn try_inc_for<L, const END: Int>(mutex: &Arc<L>)
    where
        L: TryLockWithThen<Target = Int>,
    {
        let mut node = L::Node::default();
        for _ in 0..END {
            try_inc_with::<L>(mutex, &mut node);
        }
    }

    fn mixed_inc_for<L, const END: Int>(mutex: &Arc<L>)
    where
        L: TryLockWithThen<Target = Int>,
    {
        let mut node = L::Node::default();
        for r in 0..END {
            let f = if r % 2 == 0 { inc_with } else { try_inc_with };
            f(mutex, &mut node);
        }
    }

    fn lots_and_lots<L, const THREADS: Int>(f: fn(&Arc<L>)) -> Int
    where
        L: LockThen<Target = Int> + Send + Sync + 'static,
    {
        let mutex = Arc::new(L::new(0));
        let (tx, rx) = channel();
        for _ in 0..THREADS {
            let c_mutex = Arc::clone(&mutex);
            let c_tx = tx.clone();
            thread::spawn(move || {
                f(&c_mutex);
                c_tx.send(()).unwrap();
            });
        }
        drop(tx);
        for _ in 0..THREADS {
            rx.recv().unwrap();
        }
        get(&mutex)
    }

    pub fn node_waiter_drop_does_not_matter<W>() {
        use crate::inner::raw::{MutexNode, MutexNodeInit};
        assert!(!core::mem::needs_drop::<W>());
        assert!(!core::mem::needs_drop::<MutexNode<W>>());
        assert!(!core::mem::needs_drop::<MutexNodeInit<W>>());
    }

    pub fn lots_and_lots_lock<L>()
    where
        L: LockThen<Target = Int> + Send + Sync + 'static,
    {
        let value = lots_and_lots::<L, THREADS>(inc_for::<L, ITERS>);
        assert_eq!(value, EXPECTED_VALUE);
    }

    pub fn lots_and_lots_try_lock<L>()
    where
        L: TryLockThen<Target = Int> + Send + Sync + 'static,
    {
        let value = lots_and_lots::<L, THREADS>(try_inc_for::<L, ITERS>);
        assert!(EXPECTED_RANGE.contains(&value));
    }

    pub fn lots_and_lots_mixed_lock<L>()
    where
        L: TryLockThen<Target = Int> + Send + Sync + 'static,
    {
        let value = lots_and_lots::<L, THREADS>(mixed_inc_for::<L, ITERS>);
        assert!(EXPECTED_RANGE.contains(&value));
    }

    pub fn smoke<L>()
    where
        L: LockWithThen<Target = Int>,
    {
        let mutex = L::new(1);
        let mut node = L::Node::default();
        mutex.lock_with_then(&mut node, |data| drop(data));
        mutex.lock_with_then(&mut node, |data| drop(data));
    }

    #[cfg(feature = "barging")]
    pub fn test_guard_debug_display<L>()
    where
        L: LockThen<Target = Int>,
        for<'a> <L as LockWithThen>::Guard<'a>: Debug + Display,
    {
        let value = 42;
        let mutex = L::new(value);
        mutex.lock_then(|data| {
            assert_eq!(format!("{value:?}"), format!("{data:?}"));
            assert_eq!(format!("{value}"), format!("{data}"));
        });
    }

    pub fn test_mutex_debug<L>()
    where
        L: LockThen<Target = Int> + Debug + Send + Sync + 'static,
    {
        let value = 42;
        let mutex = Arc::new(L::new(value));
        let msg = format!("Mutex {{ data: {value:?} }}");
        assert_eq!(msg, format!("{mutex:?}"));

        let c_mutex = Arc::clone(&mutex);
        let msg = "Mutex { data: <locked> }".to_string();
        mutex.lock_then(|_data| {
            assert_eq!(msg, format!("{:?}", *c_mutex));
        });
    }

    pub fn test_mutex_default<L>()
    where
        L: LockData<Target = Int> + Default,
    {
        let mutex: L = Default::default();
        assert_eq!(u32::default(), mutex.into_inner());
    }

    pub fn test_mutex_from<L>()
    where
        L: LockData<Target = Int> + From<Int>,
    {
        let value = 42;
        let mutex = L::from(value);
        assert_eq!(value, mutex.into_inner());
    }

    pub fn test_try_lock<L>()
    where
        L: TryLockThen<Target = ()>,
    {
        use std::rc::Rc;
        let mutex = Rc::new(L::new(()));
        let c_mutex = Rc::clone(&mutex);
        mutex.try_lock_then(|data| {
            assert!(c_mutex.is_locked());
            *data.unwrap().as_deref_mut() = ();
        });
        assert!(!mutex.is_locked());
    }

    pub fn test_into_inner<M>()
    where
        M: LockData<Target = NonCopy>,
    {
        let mutex = M::new(NonCopy(10));
        assert_eq!(mutex.into_inner(), NonCopy(10));
    }

    pub fn test_into_inner_drop<M>()
    where
        M: LockData<Target = Foo>,
    {
        let num_drops = Arc::new(AtomicUsize::new(0));
        let mutex = M::new(Foo(num_drops.clone()));
        assert_eq!(num_drops.load(Ordering::SeqCst), 0);
        {
            let _inner = mutex.into_inner();
            assert_eq!(num_drops.load(Ordering::SeqCst), 0);
        }
        assert_eq!(num_drops.load(Ordering::SeqCst), 1);
    }

    pub fn test_get_mut<M>()
    where
        M: LockData<Target = NonCopy>,
    {
        let mut mutex = M::new(NonCopy(10));
        *mutex.get_mut() = NonCopy(20);
        assert_eq!(mutex.into_inner(), NonCopy(20));
    }

    pub fn test_lock_arc_nested<L1, L2>()
    where
        L1: LockThen<Target = Int>,
        L2: LockThen<Target = Arc<L1>> + Send + Sync + 'static,
    {
        // Tests nested locks and access
        // to underlying data.
        let arc = Arc::new(L1::new(1));
        let arc2 = Arc::new(L2::new(arc));
        let (tx, rx) = channel();
        let _t = thread::spawn(move || {
            let val = arc2.lock_then(|arc2| {
                let arc2 = arc2.as_deref();
                get(&arc2)
            });
            assert_eq!(val, 1);
            tx.send(()).unwrap();
        });
        rx.recv().unwrap();
    }

    pub fn test_acquire_more_than_one_lock<L>()
    where
        L: LockThen<Target = Int> + Send + Sync + 'static,
    {
        let arc = Arc::new(L::new(1));
        let (tx, rx) = channel();
        for _ in 0..4 {
            let tx2 = tx.clone();
            let c_arc = Arc::clone(&arc);
            let _t = thread::spawn(move || {
                c_arc.lock_then(|_d| {
                    let mutex = L::new(1);
                    mutex.lock_then(|_d| ());
                });
                tx2.send(()).unwrap();
            });
        }
        drop(tx);
        for _ in 0..4 {
            rx.recv().unwrap();
        }
    }

    pub fn test_lock_arc_access_in_unwind<L>()
    where
        L: LockThen<Target = Int> + Send + Sync + 'static,
    {
        let arc = Arc::new(L::new(1));
        let arc2 = arc.clone();
        let _ = thread::spawn(move || {
            struct Unwinder<T: LockThen<Target = Int>> {
                i: Arc<T>,
            }
            impl<T: LockThen<Target = Int>> Drop for Unwinder<T> {
                fn drop(&mut self) {
                    inc(&self.i);
                }
            }
            let _u = Unwinder { i: arc2 };
            panic!();
        })
        .join();
        let value = get(&arc);
        assert_eq!(value, 2);
    }

    pub fn test_lock_unsized<L>()
    where
        L: LockThen<Target = [Int; 3]>,
    {
        let mutex = Arc::new(L::new([1, 2, 3]));
        {
            mutex.lock_then(|mut d| {
                d.as_deref_mut()[0] = 4;
                d.as_deref_mut()[2] = 5;
            });
        }
        let comp: &[Int] = &[4, 2, 5];
        let data = get(&mutex);
        assert_eq!(comp, data);
    }
}