Skip to content

Commit e3f0f7c

Browse files
committed
fix c-binding impls
1 parent 51314d9 commit e3f0f7c

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

lightning/src/routing/router.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6797,7 +6797,7 @@ pub(crate) mod bench_utils {
67976797
let amt = starting_amount + seed % 1_000_000;
67986798
let path_exists =
67996799
get_route(&payer, &params, &graph.read_only(), Some(&[&first_hop]),
6800-
amt, &TestLogger::new(), &scorer, score_params, &random_seed_bytes).is_ok();
6800+
amt, &TestLogger::new(), scorer, score_params, &random_seed_bytes).is_ok();
68016801
if path_exists {
68026802
// ...and seed the scorer with success and failure data...
68036803
seed = seed.overflowing_mul(6364136223846793005).0.overflowing_add(1).0;
@@ -6811,7 +6811,7 @@ pub(crate) mod bench_utils {
68116811
.with_bolt11_features(mpp_features).unwrap();
68126812

68136813
let route_res = get_route(&payer, &params, &graph.read_only(),
6814-
Some(&[&first_hop]), score_amt, &TestLogger::new(), &scorer,
6814+
Some(&[&first_hop]), score_amt, &TestLogger::new(), scorer,
68156815
score_params, &random_seed_bytes);
68166816
if let Ok(route) = route_res {
68176817
for path in route.paths {
@@ -6840,7 +6840,7 @@ pub(crate) mod bench_utils {
68406840
// requires a too-high CLTV delta.
68416841
route_endpoints.retain(|(first_hop, params, amt)| {
68426842
get_route(&payer, params, &graph.read_only(), Some(&[first_hop]), *amt,
6843-
&TestLogger::new(), &scorer, score_params, &random_seed_bytes).is_ok()
6843+
&TestLogger::new(), scorer, score_params, &random_seed_bytes).is_ok()
68446844
});
68456845
route_endpoints.truncate(route_count);
68466846
assert_eq!(route_endpoints.len(), route_count);

lightning/src/routing/scoring.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use core::convert::TryInto;
7070
use core::ops::{Deref, DerefMut};
7171
use core::time::Duration;
7272
use crate::io::{self, Read};
73-
use crate::sync::{Mutex, MutexGuard};
73+
use crate::sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
7474

7575
/// We define Score ever-so-slightly differently based on whether we are being built for C bindings
7676
/// or not. For users, `LockableScore` must somehow be writeable to disk. For Rust users, this is
@@ -212,31 +212,47 @@ impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
212212
}
213213
}
214214

215+
impl<'a, T: 'a + Score> LockableScore<'a> for RwLock<T> {
216+
type Score = T;
217+
type WriteLocked = RwLockWriteGuard<'a, T>;
218+
type ReadLocked = RwLockReadGuard<'a, T>;
219+
220+
fn read_lock(&'a self) -> Self::ReadLocked {
221+
RwLock::read(self).unwrap()
222+
}
223+
224+
fn write_lock(&'a self) -> Self::WriteLocked {
225+
RwLock::write(self).unwrap()
226+
}
227+
}
228+
215229
#[cfg(c_bindings)]
216230
/// A concrete implementation of [`LockableScore`] which supports multi-threading.
217231
pub struct MultiThreadedLockableScore<T: Score> {
218-
score: Mutex<T>,
232+
score: RwLock<T>,
219233
}
220234

221235
#[cfg(c_bindings)]
222236
impl<'a, T: 'a + Score> LockableScore<'a> for MultiThreadedLockableScore<T> {
223237
type Score = T;
224-
type ReadLocked = MultiThreadedScoreLock<'a, T>;
225-
type WriteLocked = MultiThreadedScoreLock<'a, T>;
238+
// type ReadLocked = MultiThreadedScoreLock<'a, T>;
239+
// type WriteLocked = Rw<'a, T>;
240+
type WriteLocked = RwLockWriteGuard<'a, T>;
241+
type ReadLocked = RwLockReadGuard<'a, T>;
226242

227-
fn read_lock(&'a self) -> Self::WriteLocked {
228-
MultiThreadedScoreLock(Mutex::lock(&self.score).unwrap())
243+
fn read_lock(&'a self) -> Self::ReadLocked {
244+
RwLock::read(&self.score).unwrap()
229245
}
230246

231-
fn write_lock(&'a self) -> Self::ReadLocked {
232-
MultiThreadedScoreLock(Mutex::lock(&self.score).unwrap())
247+
fn write_lock(&'a self) -> Self::WriteLocked {
248+
RwLock::write(&self.score).unwrap()
233249
}
234250
}
235251

236252
#[cfg(c_bindings)]
237253
impl<T: Score> Writeable for MultiThreadedLockableScore<T> {
238254
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
239-
self.score.lock().unwrap().write(writer)
255+
self.score.read().unwrap().write(writer)
240256
}
241257
}
242258

@@ -247,7 +263,7 @@ impl<'a, T: 'a + Score> WriteableScore<'a> for MultiThreadedLockableScore<T> {}
247263
impl<T: Score> MultiThreadedLockableScore<T> {
248264
/// Creates a new [`MultiThreadedLockableScore`] given an underlying [`Score`].
249265
pub fn new(score: T) -> Self {
250-
MultiThreadedLockableScore { score: Mutex::new(score) }
266+
MultiThreadedLockableScore { score: RwLock::new(score) }
251267
}
252268
}
253269

0 commit comments

Comments
 (0)