Skip to content

Commit e98ad1d

Browse files
committed
Split MultiThreadedScoreLock into MultiThreadedScoreLockWrite and MultiThreadedScoreLockRead
After splitting LockableScore, we split MultiThreadedScoreLock following the same way, splitting a single score into two srtucts, one for read and other for write. MultiThreadedScoreLock is used in c_bindings.
1 parent e57d83f commit e98ad1d

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

lightning/src/routing/scoring.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ impl<'a, T: 'a + ScoreUpdate + ScoreLookUp> LockableScore<'a> for RefCell<T> {
223223
}
224224
}
225225

226+
#[cfg(not(c_bindings))]
226227
impl<'a, SP:Sized, T: 'a + ScoreUpdate + ScoreLookUp<ScoreParams = SP>> LockableScore<'a> for RwLock<T> {
227228
type ScoreUpdate = T;
228229
type ScoreLookUp = T;
@@ -249,15 +250,15 @@ pub struct MultiThreadedLockableScore<T: ScoreLookUp + ScoreUpdate> {
249250
impl<'a, SP:Sized, T: 'a + ScoreLookUp<ScoreParams = SP> + ScoreUpdate> LockableScore<'a> for MultiThreadedLockableScore<T> {
250251
type ScoreUpdate = T;
251252
type ScoreLookUp = T;
252-
type WriteLocked = RwLockWriteGuard<'a, Self::ScoreUpdate>;
253-
type ReadLocked = RwLockReadGuard<'a, Self::ScoreLookUp>;
253+
type WriteLocked = MultiThreadedScoreLockWrite<'a, Self::ScoreUpdate>;
254+
type ReadLocked = MultiThreadedScoreLockRead<'a, Self::ScoreLookUp>;
254255

255256
fn read_lock(&'a self) -> Self::ReadLocked {
256-
RwLock::read(&self.score).unwrap()
257+
MultiThreadedScoreLockRead(self.score.read().unwrap())
257258
}
258259

259260
fn write_lock(&'a self) -> Self::WriteLocked {
260-
RwLock::write(&self.score).unwrap()
261+
MultiThreadedScoreLockWrite(self.score.write().unwrap())
261262
}
262263
}
263264

@@ -281,31 +282,43 @@ impl<T: ScoreLookUp + ScoreUpdate> MultiThreadedLockableScore<T> {
281282

282283
#[cfg(c_bindings)]
283284
/// A locked `MultiThreadedLockableScore`.
284-
pub struct MultiThreadedScoreLock<'a, T: ScoreLookUp + ScoreUpdate>(MutexGuard<'a, T>);
285+
pub struct MultiThreadedScoreLockRead<'a, T: ScoreLookUp>(RwLockReadGuard<'a, T>);
286+
287+
#[cfg(c_bindings)]
288+
/// A locked `MultiThreadedLockableScore`.
289+
pub struct MultiThreadedScoreLockWrite<'a, T: ScoreUpdate>(RwLockWriteGuard<'a, T>);
285290

286291
#[cfg(c_bindings)]
287-
impl<'a, T: 'a + ScoreUpdate + ScoreLookUp> Writeable for MultiThreadedScoreLock<'a, T> {
292+
impl<'a, T: 'a + ScoreUpdate> Writeable for MultiThreadedScoreLockWrite<'a, T> {
288293
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
289294
self.0.write(writer)
290295
}
291296
}
292297

293298
#[cfg(c_bindings)]
294-
impl<'a, T: 'a + ScoreUpdate + ScoreLookUp> DerefMut for MultiThreadedScoreLock<'a, T> {
295-
fn deref_mut(&mut self) -> &mut Self::Target {
296-
self.0.deref_mut()
299+
impl<'a, T: 'a + ScoreLookUp> Deref for MultiThreadedScoreLockRead<'a, T> {
300+
type Target = T;
301+
302+
fn deref(&self) -> &Self::Target {
303+
self.0.deref()
297304
}
298305
}
299306

300307
#[cfg(c_bindings)]
301-
impl<'a, T: 'a + ScoreUpdate + ScoreLookUp> Deref for MultiThreadedScoreLock<'a, T> {
308+
impl<'a, T: 'a + ScoreUpdate> Deref for MultiThreadedScoreLockWrite<'a, T> {
302309
type Target = T;
303310

304311
fn deref(&self) -> &Self::Target {
305312
self.0.deref()
306313
}
307314
}
308315

316+
#[cfg(c_bindings)]
317+
impl<'a, T: 'a + ScoreUpdate> DerefMut for MultiThreadedScoreLockWrite<'a, T> {
318+
fn deref_mut(&mut self) -> &mut Self::Target {
319+
self.0.deref_mut()
320+
}
321+
}
309322

310323

311324
/// Proposed use of a channel passed as a parameter to [`ScoreLookUp::channel_penalty_msat`].

0 commit comments

Comments
 (0)