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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#![cfg_attr(test, deny(warnings))]
#![deny(missing_docs)]

//! # shared-mutex
//!
//! A RwLock that can be used with a Condvar.

#[cfg(test)]
extern crate scoped_pool;

use std::sync::{Condvar, LockResult, TryLockResult, TryLockError};
use std::cell::UnsafeCell;
use std::ops::{Deref, DerefMut};
use std::{mem, ptr, fmt};

use poison::{Poison, PoisonGuard, RawPoisonGuard};

pub use raw::RawSharedMutex;

pub mod poison;
pub mod monitor;
mod raw;

/// A lock providing both shared read locks and exclusive write locks.
///
/// Similar to `std::sync::RwLock`, except that its guards (`SharedMutexReadGuard` and
/// `SharedMutexWriteGuard`) can wait on `std::sync::Condvar`s, which is very
/// useful for implementing efficient concurrent programs.
///
/// Another difference from `std::sync::RwLock` is that the guard types are `Send`.
pub struct SharedMutex<T: ?Sized> {
    raw: RawSharedMutex,
    data: UnsafeCell<Poison<T>>
}

unsafe impl<T: ?Sized + Send> Send for SharedMutex<T> {}
unsafe impl<T: ?Sized + Sync> Sync for SharedMutex<T> {}

impl<T> SharedMutex<T> {
    /// Create a new SharedMutex protecting the given value.
    #[inline]
    pub fn new(value: T) -> Self {
        SharedMutex {
            raw: RawSharedMutex::new(),
            data: UnsafeCell::new(Poison::new(value))
        }
    }

    /// Extract the data from the lock and destroy the lock.
    ///
    /// Safe since it requires ownership of the lock.
    #[inline]
    pub fn into_inner(self) -> LockResult<T> {
        unsafe { self.data.into_inner().into_inner() }
    }
}

impl<T: ?Sized> SharedMutex<T> {
    /// Acquire an exclusive Write lock on the data.
    #[inline]
    pub fn write(&self) -> LockResult<SharedMutexWriteGuard<T>> {
        self.raw.write();
        unsafe { SharedMutexWriteGuard::new(self) }
    }

    /// Acquire a shared Read lock on the data.
    #[inline]
    pub fn read(&self) -> LockResult<SharedMutexReadGuard<T>> {
        self.raw.read();
        unsafe { SharedMutexReadGuard::new(self) }
    }

    /// Attempt to acquire a shared Read lock on the data.
    ///
    /// If acquiring the lock would block, returns `TryLockError::WouldBlock`.
    #[inline]
    pub fn try_read(&self) -> TryLockResult<SharedMutexReadGuard<T>> {
        if self.raw.try_read() {
            Ok(try!(unsafe { SharedMutexReadGuard::new(self) }))
        } else {
            Err(TryLockError::WouldBlock)
        }
    }

    /// Attempt to acquire an exclusive Write lock on the data.
    ///
    /// If acquiring the lock would block, returns `TryLockError::WouldBlock`.
    #[inline]
    pub fn try_write(&self) -> TryLockResult<SharedMutexWriteGuard<T>> {
        if self.raw.try_write() {
            Ok(try!(unsafe { SharedMutexWriteGuard::new(self) }))
        } else {
            Err(TryLockError::WouldBlock)
        }
    }

    /// Get a mutable reference to the data without locking.
    ///
    /// Safe since it requires exclusive access to the lock itself.
    #[inline]
    pub fn get_mut(&mut self) -> LockResult<&mut T> {
        poison::map_result(unsafe { &mut *self.data.get() }.lock(),
                           |poison| unsafe { poison.into_mut() })
    }
}

/// A shared read guard on a SharedMutex.
pub struct SharedMutexReadGuard<'mutex, T: ?Sized + 'mutex> {
    data: &'mutex T,
    mutex: &'mutex SharedMutex<T>
}

unsafe impl<'mutex, T: ?Sized + Send> Send for SharedMutexReadGuard<'mutex, T> {}
unsafe impl<'mutex, T: ?Sized + Sync> Sync for SharedMutexReadGuard<'mutex, T> {}

/// An exclusive write guard on a SharedMutex.
pub struct SharedMutexWriteGuard<'mutex, T: ?Sized + 'mutex> {
    data: PoisonGuard<'mutex, T>,
    mutex: &'mutex SharedMutex<T>
}

impl<'mutex, T: ?Sized> Deref for SharedMutexReadGuard<'mutex, T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &T { self.data }
}

impl<'mutex, T: ?Sized> Deref for SharedMutexWriteGuard<'mutex, T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &T { self.data.get() }
}

impl<'mutex, T: ?Sized> DerefMut for SharedMutexWriteGuard<'mutex, T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut T { self.data.get_mut() }
}

impl<'mutex, T: ?Sized> SharedMutexReadGuard<'mutex, T> {
    #[inline]
    unsafe fn new(mutex: &'mutex SharedMutex<T>) -> LockResult<Self> {
        poison::map_result((&*mutex.data.get()).get(), |data| {
            SharedMutexReadGuard {
                data: data,
                mutex: mutex
            }
        })
    }
}

impl<'mutex, T: ?Sized> SharedMutexWriteGuard<'mutex, T> {
    #[inline]
    unsafe fn new(mutex: &'mutex SharedMutex<T>) -> LockResult<Self> {
        poison::map_result((&mut *mutex.data.get()).lock(), |poison| {
            SharedMutexWriteGuard {
                data: poison,
                mutex: mutex
            }
        })
    }
}

impl<'mutex, T: ?Sized> SharedMutexReadGuard<'mutex, T> {
    /// Turn this guard into a guard which can be mapped to a sub-borrow.
    ///
    /// Note that a mapped guard cannot wait on a `Condvar`.
    pub fn into_mapped(self) -> MappedSharedMutexReadGuard<'mutex, T> {
        let guard = MappedSharedMutexReadGuard {
            mutex: &self.mutex.raw,
            data: self.data
        };

        // Don't double-unlock.
        mem::forget(self);

        guard
    }

    /// Wait on the given condition variable, and resume with a write lock.
    ///
    /// See the documentation for `std::sync::Condvar::wait` for more information.
    pub fn wait_for_write(self, cond: &Condvar) -> LockResult<SharedMutexWriteGuard<'mutex, T>> {
        self.mutex.raw.wait_from_read_to_write(cond);

        let guard = unsafe { SharedMutexWriteGuard::new(self.mutex) };

        // Don't double-unlock.
        mem::forget(self);

        guard
    }

    /// Wait on the given condition variable, and resume with another read lock.
    ///
    /// See the documentation for `std::sync::Condvar::wait` for more information.
    pub fn wait_for_read(self, cond: &Condvar) -> LockResult<Self> {
        self.mutex.raw.wait_from_read_to_read(cond);

        let guard = unsafe { SharedMutexReadGuard::new(self.mutex) };

        // Don't double-unlock.
        mem::forget(self);

        guard
    }
}

impl<'mutex, T: ?Sized> SharedMutexWriteGuard<'mutex, T> {
    /// Turn this guard into a guard which can be mapped to a sub-borrow.
    ///
    /// Note that a mapped guard cannot wait on a `Condvar`.
    pub fn into_mapped(self) -> MappedSharedMutexWriteGuard<'mutex, T> {
        let guard = MappedSharedMutexWriteGuard {
            mutex: &self.mutex.raw,
            poison: unsafe { ptr::read(&self.data).into_raw() },
            data: unsafe { (&mut *self.mutex.data.get()).get_mut() }
        };

        // Don't double-unlock.
        mem::forget(self);

        guard
    }

    /// Wait on the given condition variable, and resume with another write lock.
    pub fn wait_for_write(self, cond: &Condvar) -> LockResult<Self> {
        self.mutex.raw.wait_from_write_to_write(cond);

        let guard = unsafe { SharedMutexWriteGuard::new(self.mutex) };

        // Don't double-unlock.
        mem::forget(self);

        guard
    }

    /// Wait on the given condition variable, and resume with a read lock.
    pub fn wait_for_read(self, cond: &Condvar) -> LockResult<SharedMutexReadGuard<'mutex, T>> {
        self.mutex.raw.wait_from_write_to_read(cond);

        let guard = unsafe { SharedMutexReadGuard::new(self.mutex) };

        // Don't double-unlock.
        mem::forget(self);

        guard
    }
}

impl<'mutex, T: ?Sized> Drop for SharedMutexReadGuard<'mutex, T> {
    #[inline]
    fn drop(&mut self) { self.mutex.raw.unlock_read() }
}

impl<'mutex, T: ?Sized> Drop for SharedMutexWriteGuard<'mutex, T> {
    #[inline]
    fn drop(&mut self) { self.mutex.raw.unlock_write() }
}

/// A read guard to a sub-borrow of an original SharedMutexReadGuard.
///
/// Unlike SharedMutexReadGuard, it cannot be used to wait on a
/// `Condvar`.
pub struct MappedSharedMutexReadGuard<'mutex, T: ?Sized + 'mutex> {
    mutex: &'mutex RawSharedMutex,
    data: &'mutex T
}

/// A write guard to a sub-borrow of an original `SharedMutexWriteGuard`.
///
/// Unlike `SharedMutexWriteGuard`, it cannot be used to wait on a
/// `Condvar`.
pub struct MappedSharedMutexWriteGuard<'mutex, T: ?Sized + 'mutex> {
    mutex: &'mutex RawSharedMutex,
    poison: RawPoisonGuard<'mutex>,
    data: &'mutex mut T,
}

impl<'mutex, T: ?Sized> MappedSharedMutexReadGuard<'mutex, T> {
    /// Transform this guard into a sub-borrow of the original data.
    #[inline]
    pub fn map<U, F>(self, action: F) -> MappedSharedMutexReadGuard<'mutex, U>
    where F: FnOnce(&T) -> &U {
        self.option_map(move |t| Some(action(t))).unwrap()
    }

    /// Conditionally transform this guard into a sub-borrow of the original data.
    #[inline]
    pub fn option_map<U, F>(self, action: F) -> Option<MappedSharedMutexReadGuard<'mutex, U>>
    where F: FnOnce(&T) -> Option<&U> {
        self.result_map(move |t| action(t).ok_or(())).ok()
    }

    /// Conditionally transform this guard into a sub-borrow of the original data.
    ///
    /// If the transformation operation is aborted, returns the original guard.
    #[inline]
    pub fn result_map<U, E, F>(self, action: F)
        -> Result<MappedSharedMutexReadGuard<'mutex, U>, (Self, E)>
    where F: FnOnce(&T) -> Result<&U, E> {
        let data = self.data;
        let mutex = self.mutex;

        match action(data) {
            Ok(new_data) => {
                // Don't double-unlock.
                mem::forget(self);

                Ok(MappedSharedMutexReadGuard {
                    data: new_data,
                    mutex: mutex
                })
            },
            Err(e) => { Err((self, e)) }
        }
    }

    /// Recover the original guard for waiting.
    ///
    /// Takes the original mutex to recover the original type and data. If the
    /// passed mutex is not the same object as the original mutex, returns `Err`.
    #[inline]
    pub fn recover<U: ?Sized>(self, mutex: &'mutex SharedMutex<U>) -> Result<SharedMutexReadGuard<'mutex, U>, Self> {
        if self.mutex.is(&mutex.raw) {
            // The mutex can't have become poisoned since we are continuously holding a guard.
            let guard = unsafe { SharedMutexReadGuard::new(mutex) }.unwrap();

            // Don't double-unlock.
            mem::forget(self);

            Ok(guard)
        } else {
            Err(self)
        }
    }
}

impl<'mutex, T: ?Sized> MappedSharedMutexWriteGuard<'mutex, T> {
    /// Transform this guard into a sub-borrow of the original data.
    #[inline]
    pub fn map<U, F>(self, action: F) -> MappedSharedMutexWriteGuard<'mutex, U>
    where F: FnOnce(&mut T) -> &mut U {
        self.option_map(move |t| Some(action(t))).unwrap()
    }

    /// Conditionally transform this guard into a sub-borrow of the original data.
    #[inline]
    pub fn option_map<U, F>(self, action: F) -> Option<MappedSharedMutexWriteGuard<'mutex, U>>
    where F: FnOnce(&mut T) -> Option<&mut U> {
        self.result_map(move |t| action(t).ok_or(())).ok()
    }

    /// Conditionally transform this guard into a sub-borrow of the original data.
    ///
    /// If the transformation operation is aborted, returns the original guard.
    #[inline]
    pub fn result_map<U, E, F>(self, action: F)
        -> Result<MappedSharedMutexWriteGuard<'mutex, U>, (Self, E)>
    where F: FnOnce(&mut T) -> Result<&mut U, E> {
        let data = unsafe { ptr::read(&self.data) };
        let mutex = self.mutex;

        match action(data) {
            Ok(new_data) => {
                let poison = unsafe { ptr::read(&self.poison) };

                // Don't double-unlock.
                mem::forget(self);

                Ok(MappedSharedMutexWriteGuard {
                    data: new_data,
                    poison: poison,
                    mutex: mutex
                })
            },
            Err(e) => { Err((self, e)) }
        }
    }

    /// Recover the original guard for waiting.
    ///
    /// Takes the original mutex to recover the original type and data. If the
    /// passed mutex is not the same object as the original mutex, returns `Err`.
    #[inline]
    pub fn recover<U: ?Sized>(self, mutex: &'mutex SharedMutex<U>) -> Result<SharedMutexWriteGuard<'mutex, U>, Self> {
        if self.mutex.is(&mutex.raw) {
            // The mutex can't have become poisoned since we are continuously holding a guard.
            let guard = unsafe { SharedMutexWriteGuard::new(mutex) }.unwrap();

            // Don't double-unlock.
            mem::forget(self);

            Ok(guard)
        } else {
            Err(self)
        }
    }
}

impl<'mutex, T: ?Sized> Deref for MappedSharedMutexReadGuard<'mutex, T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &T { self.data }
}

impl<'mutex, T: ?Sized> Deref for MappedSharedMutexWriteGuard<'mutex, T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &T { self.data }
}

impl<'mutex, T: ?Sized> DerefMut for MappedSharedMutexWriteGuard<'mutex, T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut T { self.data }
}

impl<'mutex, T: ?Sized> Drop for MappedSharedMutexReadGuard<'mutex, T> {
    #[inline]
    fn drop(&mut self) { self.mutex.unlock_read() }
}

impl<'mutex, T: ?Sized> Drop for MappedSharedMutexWriteGuard<'mutex, T> {
    #[inline]
    fn drop(&mut self) { self.mutex.unlock_write() }
}

impl<T: ?Sized + fmt::Debug> fmt::Debug for SharedMutex<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut writer = f.debug_struct("SharedMutex");

        match self.try_read() {
            Ok(l) => writer.field("data", &&*l),
            Err(TryLockError::WouldBlock) => writer.field("data", &"{{ locked }}"),
            Err(TryLockError::Poisoned(_)) => writer.field("data", &"{{ poisoned }}")
        }.finish()
    }
}

impl<'mutex, T: ?Sized + fmt::Debug> fmt::Debug for SharedMutexReadGuard<'mutex, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("SharedMutexReadGuard")
            .field("data", &*self)
            .finish()
    }
}

impl<'mutex, T: ?Sized + fmt::Debug> fmt::Debug for SharedMutexWriteGuard<'mutex, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("SharedMutexWriteGuard")
            .field("data", &*self)
            .finish()
    }
}

impl<'mutex, T: ?Sized + fmt::Debug> fmt::Debug for MappedSharedMutexReadGuard<'mutex, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("MappedSharedMutexReadGuard")
            .field("data", &*self)
            .finish()
    }
}

impl<'mutex, T: ?Sized + fmt::Debug> fmt::Debug for MappedSharedMutexWriteGuard<'mutex, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("MappedSharedMutexWriteGuard")
            .field("data", &*self)
            .finish()
    }
}

#[cfg(test)]
mod test {
    use std::sync::{Condvar, Barrier};
    use std::sync::atomic::{AtomicUsize, Ordering};
    use scoped_pool::Pool;

    use super::*;

    fn _check_bounds() {
        fn _is_send_sync<T: Send + Sync>() {}

        _is_send_sync::<RawSharedMutex>();
        _is_send_sync::<SharedMutex<()>>();
        _is_send_sync::<SharedMutexReadGuard<()>>();
        _is_send_sync::<SharedMutexWriteGuard<()>>();
    }

    #[test]
    fn test_simple_multithreaded() {
        let pool = Pool::new(8);
        let mut mutex = SharedMutex::new(0);
        let n = 100;

        pool.scoped(|scope| {
            for _ in 0..n {
                scope.execute(|| {
                    let before = *mutex.read().unwrap();
                    *mutex.write().unwrap() += 1;
                    let after = *mutex.read().unwrap();

                    assert!(before < after, "Time travel! {:?} >= {:?}", before, after);
                })
            }
        });

        assert_eq!(*mutex.get_mut().unwrap(), 100);
        pool.shutdown();
    }

    #[test]
    fn test_simple_single_thread() {
        let mut mutex = SharedMutex::new(0);
        let n = 100;

        for _ in 0..n {
            let before = *mutex.read().unwrap();
            *mutex.write().unwrap() += 1;
            let after = *mutex.read().unwrap();

            assert!(before < after, "Time travel! {:?} >= {:?}", before, after);
        }

        assert_eq!(*mutex.get_mut().unwrap(), 100);
    }

    #[test]
    fn test_locking_multithreaded() {
        // This test makes a best effort to test the actual locking
        // behavior of the mutex.
        //
        // Read locks attempt to read from an atomic many times,
        // while write locks write to them many times.
        //
        // If any of these operations interleave (readers read different
        // values under the same lock, writers observe other writers) then
        // we know there is a bug.
        //
        // We make use of a barrier to attempt to cluster threads together.

        let mut mutex = SharedMutex::new(());
        let value = AtomicUsize::new(0);

        let threads = 50;
        let actors = threads * 20; // Must be a multiple threads.
        let actions_per_actor = 100;
        let start_barrier = Barrier::new(threads);
        let pool = Pool::new(threads);

        pool.scoped(|scope| {
            for _ in 0..actors {
                // Reader
                scope.execute(|| {
                    start_barrier.wait();

                    let _read = mutex.read().unwrap();
                    let original = value.load(Ordering::SeqCst);

                    for _ in 0..actions_per_actor {
                        assert_eq!(original, value.load(Ordering::SeqCst));
                    }
                });

                // Writer
                scope.execute(|| {
                    start_barrier.wait();

                    let _write = mutex.write().unwrap();
                    let mut previous = value.load(Ordering::SeqCst);

                    for _ in 0..actions_per_actor {
                        let next = value.fetch_add(1, Ordering::SeqCst);

                        // fetch_add returns the old value
                        assert_eq!(previous, next);

                        // next time we will expect the old value + 1
                        previous = next + 1;
                    }
                });
            }
        });

        mutex.get_mut().unwrap();
        pool.shutdown();
    }

    #[test]
    fn test_simple_waiting() {
        let pool = Pool::new(20);
        let mutex = SharedMutex::new(());
        let cond = Condvar::new();

        pool.scoped(|scope| {
            let lock = mutex.write().unwrap();

            scope.execute(|| {
                let _ = mutex.write().unwrap();
                cond.notify_one();
            });

            // Write -> Read
            let lock = lock.wait_for_read(&cond).unwrap();

            scope.execute(|| {
                drop(mutex.write().unwrap());
                cond.notify_one();
            });

            // Read -> Read
            let lock = lock.wait_for_read(&cond).unwrap();

            scope.execute(|| {
                drop(mutex.write().unwrap());
                cond.notify_one();
            });


            // Read -> Write
            let lock = lock.wait_for_write(&cond).unwrap();

            scope.execute(|| {
                drop(mutex.write().unwrap());
                cond.notify_one();
            });

            // Write -> Write
            lock.wait_for_write(&cond).unwrap();
        });

        pool.shutdown();
    }

    #[test]
    fn test_mapping() {
        let mutex = SharedMutex::new(vec![1, 2, 3]);

        *mutex.write().unwrap().into_mapped()
            .map(|v| &mut v[0]) = 100;

        assert_eq!(*mutex.read().unwrap().into_mapped().map(|v| &v[0]), 100);
    }

    #[test]
    fn test_map_recover() {
        let mutex = SharedMutex::new(vec![1, 2]);

        let mut write_map = mutex.write().unwrap().into_mapped()
            .map(|v| &mut v[0]);
        *write_map = 123;

        let whole_guard = write_map.recover(&mutex).unwrap();
        assert_eq!(&*whole_guard, &[123, 2]);
    }

    #[test]
    fn test_try_locking() {
        let mutex = SharedMutex::new(10);
        mutex.try_read().unwrap();
        mutex.try_write().unwrap();
    }
}