Merge pull request #2046 from TheBlueMatt/2023-02-rgs-robust-and-log
[rust-lightning] / lightning / src / sync / nostd_sync.rs
index caf88a7cc04a8617a86e63988a7cc7a291fe96bc..858f60db5b5b46a5bb703f2d454d8d2b2c1f2934 100644 (file)
@@ -2,6 +2,7 @@ pub use ::alloc::sync::Arc;
 use core::ops::{Deref, DerefMut};
 use core::time::Duration;
 use core::cell::{RefCell, Ref, RefMut};
+use super::{LockTestExt, LockHeldState};
 
 pub type LockResult<Guard> = Result<Guard, ()>;
 
@@ -61,6 +62,17 @@ impl<T> Mutex<T> {
        }
 }
 
+impl<'a, T: 'a> LockTestExt<'a> for Mutex<T> {
+       #[inline]
+       fn held_by_thread(&self) -> LockHeldState {
+               if self.lock().is_err() { return LockHeldState::HeldByThread; }
+               else { return LockHeldState::NotHeldByThread; }
+       }
+       type ExclLock = MutexGuard<'a, T>;
+       #[inline]
+       fn unsafe_well_ordered_double_lock_self(&'a self) -> MutexGuard<T> { self.lock().unwrap() }
+}
+
 pub struct RwLock<T: ?Sized> {
        inner: RefCell<T>
 }
@@ -116,4 +128,15 @@ impl<T> RwLock<T> {
        }
 }
 
+impl<'a, T: 'a> LockTestExt<'a> for RwLock<T> {
+       #[inline]
+       fn held_by_thread(&self) -> LockHeldState {
+               if self.write().is_err() { return LockHeldState::HeldByThread; }
+               else { return LockHeldState::NotHeldByThread; }
+       }
+       type ExclLock = RwLockWriteGuard<'a, T>;
+       #[inline]
+       fn unsafe_well_ordered_double_lock_self(&'a self) -> RwLockWriteGuard<T> { self.write().unwrap() }
+}
+
 pub type FairRwLock<T> = RwLock<T>;