Skip to content

Commit 4347b2a

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 7c2c752 commit 4347b2a

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
@@ -224,6 +224,7 @@ impl<'a, T: 'a + ScoreUpdate + ScoreLookUp> LockableScore<'a> for RefCell<T> {
224224
}
225225
}
226226

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

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

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

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

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

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

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

301308
#[cfg(c_bindings)]
302-
impl<'a, T: 'a + ScoreUpdate + ScoreLookUp> Deref for MultiThreadedScoreLock<'a, T> {
309+
impl<'a, T: 'a + ScoreUpdate> Deref for MultiThreadedScoreLockWrite<'a, T> {
303310
type Target = T;
304311

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

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

311324

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

0 commit comments

Comments
 (0)