X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fsync.rs;fp=lightning%2Fsrc%2Fsync.rs;h=bde547036536b479bcf181740e0ea31a7cf42ebb;hb=a0a3a6b204395a63647a7914f35077b117315b1d;hp=0000000000000000000000000000000000000000;hpb=002a5db5b07fd317545d3aa4f1a217cd40b608e2;p=rust-lightning diff --git a/lightning/src/sync.rs b/lightning/src/sync.rs new file mode 100644 index 00000000..bde54703 --- /dev/null +++ b/lightning/src/sync.rs @@ -0,0 +1,115 @@ +pub use ::alloc::sync::Arc; +use core::ops::{Deref, DerefMut}; +use core::time::Duration; +use core::cell::{RefCell, Ref, RefMut}; + +pub type LockResult = Result; + +pub struct Condvar {} + +impl Condvar { + pub fn new() -> Condvar { + Condvar { } + } + + pub fn wait<'a, T>(&'a self, guard: MutexGuard<'a, T>) -> LockResult> { + Ok(guard) + } + + #[allow(unused)] + pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, _dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> { + Ok((guard, ())) + } + + pub fn notify_all(&self) {} +} + +pub struct Mutex { + inner: RefCell +} + +#[must_use = "if unused the Mutex will immediately unlock"] +pub struct MutexGuard<'a, T: ?Sized + 'a> { + lock: RefMut<'a, T>, +} + +impl Deref for MutexGuard<'_, T> { + type Target = T; + + fn deref(&self) -> &T { + &self.lock.deref() + } +} + +impl DerefMut for MutexGuard<'_, T> { + fn deref_mut(&mut self) -> &mut T { + self.lock.deref_mut() + } +} + +impl Mutex { + pub fn new(inner: T) -> Mutex { + Mutex { inner: RefCell::new(inner) } + } + + pub fn lock<'a>(&'a self) -> LockResult> { + Ok(MutexGuard { lock: self.inner.borrow_mut() }) + } + + pub fn try_lock<'a>(&'a self) -> LockResult> { + Ok(MutexGuard { lock: self.inner.borrow_mut() }) + } +} + +pub struct RwLock { + inner: RefCell +} + +pub struct RwLockReadGuard<'a, T: ?Sized + 'a> { + lock: Ref<'a, T>, +} + +pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> { + lock: RefMut<'a, T>, +} + +impl Deref for RwLockReadGuard<'_, T> { + type Target = T; + + fn deref(&self) -> &T { + &self.lock.deref() + } +} + +impl Deref for RwLockWriteGuard<'_, T> { + type Target = T; + + fn deref(&self) -> &T { + &self.lock.deref() + } +} + +impl DerefMut for RwLockWriteGuard<'_, T> { + fn deref_mut(&mut self) -> &mut T { + self.lock.deref_mut() + } +} + +impl RwLock { + pub fn new(inner: T) -> RwLock { + RwLock { inner: RefCell::new(inner) } + } + + pub fn read<'a>(&'a self) -> LockResult> { + Ok(RwLockReadGuard { lock: self.inner.borrow() }) + } + + pub fn write<'a>(&'a self) -> LockResult> { + Ok(RwLockWriteGuard { lock: self.inner.borrow_mut() }) + } + + pub fn try_write<'a>(&'a self) -> LockResult> { + // There is no try, grasshopper - only used for tests and expected to fail + Err(()) + } +}