Skip to content

Commit 65cfcb2

Browse files
committed
split single lock into write and read lock
1 parent c5c5f3f commit 65cfcb2

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ fn handle_network_graph_update<L: Deref>(
241241
fn update_scorer<'a, S: 'static + Deref<Target = SC> + Send + Sync, SC: 'a + WriteableScore<'a>>(
242242
scorer: &'a S, event: &Event
243243
) -> bool {
244-
let mut score = scorer.lock();
244+
let mut score = scorer.write_lock();
245245
match event {
246246
Event::PaymentPathFailed { ref path, short_channel_id: Some(scid), .. } => {
247247
score.payment_path_failed(path, *scid);

lightning/src/routing/router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::util::chacha20::ChaCha20;
2727

2828
use crate::io;
2929
use crate::prelude::*;
30-
use crate::sync::{Mutex};
30+
use crate::sync::Mutex;
3131
use alloc::collections::BinaryHeap;
3232
use core::{cmp, fmt};
3333
use core::ops::{Deref, DerefMut};
@@ -73,7 +73,7 @@ impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Sco
7373
};
7474
find_route(
7575
payer, params, &self.network_graph, first_hops, &*self.logger,
76-
&ScorerAccountingForInFlightHtlcs::new(self.scorer.lock().deref_mut(), inflight_htlcs),
76+
&ScorerAccountingForInFlightHtlcs::new(self.scorer.write_lock().deref_mut(), inflight_htlcs),
7777
&self.score_params,
7878
&random_seed_bytes
7979
)

lightning/src/routing/scoring.rs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use crate::util::time::Time;
6565

6666
use crate::prelude::*;
6767
use core::{cmp, fmt};
68-
use core::cell::{RefCell, RefMut};
68+
use core::cell::{RefCell, RefMut, Ref};
6969
use core::convert::TryInto;
7070
use core::ops::{Deref, DerefMut};
7171
use core::time::Duration;
@@ -145,6 +145,7 @@ impl<S: Score, T: DerefMut<Target=S> $(+ $supertrait)*> Score for T {
145145

146146
#[cfg(c_bindings)]
147147
define_score!(Writeable);
148+
148149
#[cfg(not(c_bindings))]
149150
define_score!();
150151

@@ -160,11 +161,17 @@ pub trait LockableScore<'a> {
160161
/// The [`Score`] type.
161162
type Score: 'a + Score;
162163

163-
/// The locked [`Score`] type.
164-
type Locked: DerefMut<Target = Self::Score> + Sized;
164+
/// The Write locked [`Score`] type.
165+
type WriteLocked: DerefMut<Target = Self::Score> + Sized;
166+
167+
/// The Read locked [`Score`] type.
168+
type ReadLocked: Deref<Target = Self::Score> + Sized;
169+
170+
/// Returns read locked scorer.
171+
fn read_lock(&'a self) -> Self::ReadLocked;
165172

166-
/// Returns the locked scorer.
167-
fn lock(&'a self) -> Self::Locked;
173+
/// Returns write locked scorer.
174+
fn write_lock(&'a self) -> Self::WriteLocked;
168175
}
169176

170177
/// Refers to a scorer that is accessible under lock and also writeable to disk
@@ -178,20 +185,30 @@ impl<'a, T> WriteableScore<'a> for T where T: LockableScore<'a> + Writeable {}
178185
/// This is not exported to bindings users
179186
impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
180187
type Score = T;
181-
type Locked = MutexGuard<'a, T>;
188+
type WriteLocked = MutexGuard<'a, T>;
189+
type ReadLocked = MutexGuard<'a, T>;
190+
191+
fn read_lock(&'a self) -> Self::ReadLocked {
192+
Mutex::lock(self).unwrap()
193+
}
182194

183-
fn lock(&'a self) -> Self::Locked {
195+
fn write_lock(&'a self) -> Self::WriteLocked {
184196
Mutex::lock(self).unwrap()
185197
}
186198
}
187199

188200
impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
189201
type Score = T;
190-
type Locked = RefMut<'a, T>;
202+
type WriteLocked = RefMut<'a, T>;
203+
type ReadLocked = Ref<'a, T>;
191204

192-
fn lock(&'a self) -> Self::Locked {
205+
fn write_lock(&'a self) -> Self::WriteLocked {
193206
self.borrow_mut()
194207
}
208+
209+
fn read_lock(&'a self) -> Self::ReadLocked {
210+
self.borrow()
211+
}
195212
}
196213

197214
#[cfg(c_bindings)]
@@ -203,17 +220,22 @@ pub struct MultiThreadedLockableScore<T: Score> {
203220
#[cfg(c_bindings)]
204221
impl<'a, T: 'a + Score> LockableScore<'a> for MultiThreadedLockableScore<T> {
205222
type Score = T;
206-
type Locked = MultiThreadedScoreLock<'a, T>;
223+
type ReadLocked = MultiThreadedScoreLock<'a, T>;
224+
type WriteLocked = MultiThreadedScoreLock<'a, T>;
225+
226+
fn read_lock(&'a self) -> Self::WriteLocked {
227+
MultiThreadedScoreLock(Mutex::lock(&self.score).unwrap())
228+
}
207229

208-
fn lock(&'a self) -> Self::Locked {
230+
fn write_lock(&'a self) -> Self::ReadLocked {
209231
MultiThreadedScoreLock(Mutex::lock(&self.score).unwrap())
210232
}
211233
}
212234

213235
#[cfg(c_bindings)]
214236
impl<T: Score> Writeable for MultiThreadedLockableScore<T> {
215237
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
216-
self.lock().write(writer)
238+
self.score.lock().unwrap().write(writer)
217239
}
218240
}
219241

0 commit comments

Comments
 (0)