1 //! A minimal adaption of the `parking_lot` synchronization primitives to the 2 //! equivalent `std::sync` types. 3 //! 4 //! This can be extended to additional types/methods as required. 5 6 use std::sync::LockResult; 7 use std::time::Duration; 8 9 // Types that do not need wrapping 10 pub(crate) use parking_lot::{MutexGuard, RwLockReadGuard, RwLockWriteGuard, WaitTimeoutResult}; 11 12 /// Adapter for `parking_lot::Mutex` to the `std::sync::Mutex` interface. 13 #[derive(Debug)] 14 pub(crate) struct Mutex<T: ?Sized>(parking_lot::Mutex<T>); 15 16 #[derive(Debug)] 17 pub(crate) struct RwLock<T>(parking_lot::RwLock<T>); 18 19 /// Adapter for `parking_lot::Condvar` to the `std::sync::Condvar` interface. 20 #[derive(Debug)] 21 pub(crate) struct Condvar(parking_lot::Condvar); 22 23 impl<T> Mutex<T> { 24 #[inline] new(t: T) -> Mutex<T>25 pub(crate) fn new(t: T) -> Mutex<T> { 26 Mutex(parking_lot::Mutex::new(t)) 27 } 28 29 #[inline] 30 #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] 31 #[cfg_attr(docsrs, doc(cfg(all(feature = "parking_lot",))))] const_new(t: T) -> Mutex<T>32 pub(crate) const fn const_new(t: T) -> Mutex<T> { 33 Mutex(parking_lot::const_mutex(t)) 34 } 35 36 #[inline] lock(&self) -> MutexGuard<'_, T>37 pub(crate) fn lock(&self) -> MutexGuard<'_, T> { 38 self.0.lock() 39 } 40 41 #[inline] try_lock(&self) -> Option<MutexGuard<'_, T>>42 pub(crate) fn try_lock(&self) -> Option<MutexGuard<'_, T>> { 43 self.0.try_lock() 44 } 45 46 #[inline] get_mut(&mut self) -> &mut T47 pub(crate) fn get_mut(&mut self) -> &mut T { 48 self.0.get_mut() 49 } 50 51 // Note: Additional methods `is_poisoned` and `into_inner`, can be 52 // provided here as needed. 53 } 54 55 impl<T> RwLock<T> { new(t: T) -> RwLock<T>56 pub(crate) fn new(t: T) -> RwLock<T> { 57 RwLock(parking_lot::RwLock::new(t)) 58 } 59 read(&self) -> LockResult<RwLockReadGuard<'_, T>>60 pub(crate) fn read(&self) -> LockResult<RwLockReadGuard<'_, T>> { 61 Ok(self.0.read()) 62 } 63 write(&self) -> LockResult<RwLockWriteGuard<'_, T>>64 pub(crate) fn write(&self) -> LockResult<RwLockWriteGuard<'_, T>> { 65 Ok(self.0.write()) 66 } 67 } 68 69 impl Condvar { 70 #[inline] new() -> Condvar71 pub(crate) fn new() -> Condvar { 72 Condvar(parking_lot::Condvar::new()) 73 } 74 75 #[inline] notify_one(&self)76 pub(crate) fn notify_one(&self) { 77 self.0.notify_one(); 78 } 79 80 #[inline] notify_all(&self)81 pub(crate) fn notify_all(&self) { 82 self.0.notify_all(); 83 } 84 85 #[inline] wait<'a, T>( &self, mut guard: MutexGuard<'a, T>, ) -> LockResult<MutexGuard<'a, T>>86 pub(crate) fn wait<'a, T>( 87 &self, 88 mut guard: MutexGuard<'a, T>, 89 ) -> LockResult<MutexGuard<'a, T>> { 90 self.0.wait(&mut guard); 91 Ok(guard) 92 } 93 94 #[inline] wait_timeout<'a, T>( &self, mut guard: MutexGuard<'a, T>, timeout: Duration, ) -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)>95 pub(crate) fn wait_timeout<'a, T>( 96 &self, 97 mut guard: MutexGuard<'a, T>, 98 timeout: Duration, 99 ) -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> { 100 let wtr = self.0.wait_for(&mut guard, timeout); 101 Ok((guard, wtr)) 102 } 103 104 // Note: Additional methods `wait_timeout_ms`, `wait_timeout_until`, 105 // `wait_until` can be provided here as needed. 106 } 107