X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fsync%2Fnostd_sync.rs;h=ee3e375028eba44bbb0ac723b0eb520c398be227;hb=a1b5a1bba38e0a4ab08603adbcf9e54f27e86f19;hp=caf88a7cc04a8617a86e63988a7cc7a291fe96bc;hpb=e8b91a478bd2fd37fd726901271a8299847def3d;p=rust-lightning diff --git a/lightning/src/sync/nostd_sync.rs b/lightning/src/sync/nostd_sync.rs index caf88a7c..ee3e3750 100644 --- a/lightning/src/sync/nostd_sync.rs +++ b/lightning/src/sync/nostd_sync.rs @@ -2,11 +2,17 @@ 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 = Result; pub struct Condvar {} +pub struct WaitTimeoutResult(bool); +impl WaitTimeoutResult { + pub fn timed_out(&self) -> bool { self.0 } +} + impl Condvar { pub fn new() -> Condvar { Condvar { } @@ -21,6 +27,22 @@ impl Condvar { Ok((guard, ())) } + pub fn wait_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, mut guard: MutexGuard<'a, T>, mut condition: F) + -> LockResult> { + assert!(!condition(&mut *guard)); + Ok(guard) + } + + #[allow(unused)] + pub fn wait_timeout_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, mut guard: MutexGuard<'a, T>, dur: Duration, mut condition: F) + -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> { + if condition(&mut *guard) { + Ok((guard, WaitTimeoutResult(true))) + } else { + Ok((guard, WaitTimeoutResult(false))) + } + } + pub fn notify_all(&self) {} } @@ -59,6 +81,21 @@ impl Mutex { pub fn try_lock<'a>(&'a self) -> LockResult> { Ok(MutexGuard { lock: self.inner.borrow_mut() }) } + + pub fn into_inner(self) -> LockResult { + Ok(self.inner.into_inner()) + } +} + +impl<'a, T: 'a> LockTestExt<'a> for Mutex { + #[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 { self.lock().unwrap() } } pub struct RwLock { @@ -116,4 +153,15 @@ impl RwLock { } } +impl<'a, T: 'a> LockTestExt<'a> for RwLock { + #[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 { self.write().unwrap() } +} + pub type FairRwLock = RwLock;