Use lightning::sync via symlink in lightning-liquidity

.. to which end we also need to make some additions to `debug_sync` and
`nostd_sync`.
This commit is contained in:
Elias Rohrer 2024-12-09 10:56:34 +01:00
parent 3a8e1bffcc
commit 9c9b4a8df7
No known key found for this signature in database
GPG key ID: 36153082BDF676FD
10 changed files with 39 additions and 124 deletions

View file

@ -16,6 +16,7 @@ categories = ["cryptography::cryptocurrencies"]
[features]
default = ["std"]
std = ["lightning/std"]
backtrace = ["dep:backtrace"]
[dependencies]
lightning = { version = "0.0.124", path = "../lightning", default-features = false }
@ -27,6 +28,7 @@ bitcoin = { version = "0.32.2", default-features = false, features = ["serde"] }
chrono = { version = "0.4", default-features = false, features = ["serde", "alloc"] }
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
serde_json = "1.0"
backtrace = { version = "0.3", optional = true }
[dev-dependencies]
lightning = { version = "0.0.124", path = "../lightning", default-features = false, features = ["_test_utils"] }
@ -43,4 +45,6 @@ level = "forbid"
check-cfg = [
"cfg(lsps1_service)",
"cfg(c_bindings)",
"cfg(backtrace)",
"cfg(ldk_bench)",
]

View file

@ -28,7 +28,7 @@ pub(crate) struct EventQueue {
queue: Arc<Mutex<VecDeque<Event>>>,
waker: Arc<Mutex<Option<Waker>>>,
#[cfg(feature = "std")]
condvar: std::sync::Condvar,
condvar: crate::sync::Condvar,
}
impl EventQueue {
@ -37,7 +37,7 @@ impl EventQueue {
let waker = Arc::new(Mutex::new(None));
#[cfg(feature = "std")]
{
let condvar = std::sync::Condvar::new();
let condvar = crate::sync::Condvar::new();
Self { queue, waker, condvar }
}
#[cfg(not(feature = "std"))]
@ -67,8 +67,10 @@ impl EventQueue {
#[cfg(feature = "std")]
pub fn wait_next_event(&self) -> Event {
let mut queue =
self.condvar.wait_while(self.queue.lock().unwrap(), |queue| queue.is_empty()).unwrap();
let mut queue = self
.condvar
.wait_while(self.queue.lock().unwrap(), |queue: &mut VecDeque<Event>| queue.is_empty())
.unwrap();
let event = queue.pop_front().expect("non-empty queue");
let should_notify = !queue.is_empty();

View file

@ -62,6 +62,8 @@ pub mod lsps1;
pub mod lsps2;
mod manager;
pub mod message_queue;
#[allow(dead_code)]
#[allow(unused_imports)]
mod sync;
#[cfg(test)]
mod tests;

View file

@ -0,0 +1 @@
../../../lightning/src/sync/debug_sync.rs

View file

@ -0,0 +1 @@
../../../lightning/src/sync/fairrwlock.rs

View file

@ -1,9 +0,0 @@
#![allow(unused_imports)]
#[cfg(feature = "std")]
pub use std::sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
#[cfg(not(feature = "std"))]
mod nostd_sync;
#[cfg(not(feature = "std"))]
pub use nostd_sync::*;

View file

@ -0,0 +1 @@
../../../lightning/src/sync/mod.rs

View file

@ -1,111 +0,0 @@
#![allow(dead_code)]
//! This file was copied from `rust-lightning`.
pub use ::alloc::sync::Arc;
use core::cell::{Ref, RefCell, RefMut};
use core::fmt;
use core::ops::{Deref, DerefMut};
pub type LockResult<Guard> = Result<Guard, ()>;
pub struct Mutex<T: ?Sized> {
inner: RefCell<T>,
}
impl<T: fmt::Debug> fmt::Debug for Mutex<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let t = self.lock().unwrap();
fmt::Debug::fmt(t.deref(), f)
}
}
#[must_use = "if unused the Mutex will immediately unlock"]
pub struct MutexGuard<'a, T: ?Sized + 'a> {
lock: RefMut<'a, T>,
}
impl<T: ?Sized> Deref for MutexGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
&self.lock.deref()
}
}
impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
self.lock.deref_mut()
}
}
impl<T> Mutex<T> {
pub fn new(inner: T) -> Mutex<T> {
Mutex { inner: RefCell::new(inner) }
}
pub fn lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
Ok(MutexGuard { lock: self.inner.borrow_mut() })
}
pub fn try_lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
Ok(MutexGuard { lock: self.inner.borrow_mut() })
}
pub fn into_inner(self) -> LockResult<T> {
Ok(self.inner.into_inner())
}
}
pub struct RwLock<T: ?Sized> {
inner: RefCell<T>,
}
pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
lock: Ref<'a, T>,
}
pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
lock: RefMut<'a, T>,
}
impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
&self.lock.deref()
}
}
impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
&self.lock.deref()
}
}
impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
self.lock.deref_mut()
}
}
impl<T> RwLock<T> {
pub fn new(inner: T) -> RwLock<T> {
RwLock { inner: RefCell::new(inner) }
}
pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
Ok(RwLockReadGuard { lock: self.inner.borrow() })
}
pub fn write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
Ok(RwLockWriteGuard { lock: self.inner.borrow_mut() })
}
pub fn try_write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
match self.inner.try_borrow_mut() {
Ok(lock) => Ok(RwLockWriteGuard { lock }),
Err(_) => Err(()),
}
}
}

View file

@ -0,0 +1 @@
../../../lightning/src/sync/nostd_sync.rs

View file

@ -0,0 +1 @@
../../../lightning/src/sync/test_lockorder_checks.rs

View file

@ -1,4 +1,5 @@
pub use alloc::sync::Arc;
use core::fmt;
use core::ops::{Deref, DerefMut};
use core::time::Duration;
@ -65,6 +66,11 @@ impl Condvar {
pub fn notify_all(&self) {
self.inner.notify_all();
}
#[allow(unused)]
pub fn notify_one(&self) {
self.inner.notify_one();
}
}
thread_local! {
@ -254,6 +260,21 @@ impl<T: Sized> Mutex<T> {
}
}
impl<T: Sized + fmt::Debug> fmt::Debug for Mutex<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_struct("Mutex");
match self.try_lock() {
Ok(guard) => {
d.field("data", &&*guard);
},
Err(()) => {
d.field("data", &format_args!("<locked>"));
},
}
d.finish_non_exhaustive()
}
}
#[must_use = "if unused the Mutex will immediately unlock"]
pub struct MutexGuard<'a, T: Sized + 'a> {
mutex: &'a Mutex<T>,

View file

@ -5,6 +5,7 @@ use core::ops::{Deref, DerefMut};
pub type LockResult<Guard> = Result<Guard, ()>;
#[derive(Debug)]
pub struct Mutex<T: ?Sized> {
inner: RefCell<T>,
}