Skip to content

Commit 56b07e5

Browse files
committed
Add a MutexGuard wrapper for the bindings-only LockableScore
In the bindings, we don't directly export any `std` types. Instead, we provide trivial wrappers around them which expose only a bindings-compatible API (and which is code in-crate, where the bindings generator can see it). We never quite finished this for `MultiThreadedLockableScore` - due to some limitations in the bindings generator and the way the scores are used in `lightning-invoice`, the scoring API ended up being further concretized in patches for the bindings. Luckily the scoring interface has been rewritten somewhat, and it so happens that we can now generate bindings with the upstream code. The final piece of the puzzle is done here, where we add a struct which wraps `MutexGuard` so that we can expose the lock for `MultiThreadedLockableScore`.
1 parent dc28f9b commit 56b07e5

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

lightning/src/routing/scoring.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,39 @@ pub struct MultiThreadedLockableScore<S: Score> {
188188
score: Mutex<S>,
189189
}
190190
#[cfg(c_bindings)]
191-
/// (C-not exported)
191+
/// A locked `MultiThreadedLockableScore`.
192+
pub struct MultiThreadedLockableScoreLock<'a, S: Score>(MutexGuard<'a, S>);
193+
#[cfg(c_bindings)]
194+
impl<'a, T: Score + 'a> Score for MultiThreadedLockableScoreLock<'a, T> {
195+
fn channel_penalty_msat(&self, scid: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage) -> u64 {
196+
self.0.channel_penalty_msat(scid, source, target, usage)
197+
}
198+
fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
199+
self.0.payment_path_failed(path, short_channel_id)
200+
}
201+
fn payment_path_successful(&mut self, path: &[&RouteHop]) {
202+
self.0.payment_path_successful(path)
203+
}
204+
fn probe_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
205+
self.0.probe_failed(path, short_channel_id)
206+
}
207+
fn probe_successful(&mut self, path: &[&RouteHop]) {
208+
self.0.probe_successful(path)
209+
}
210+
}
211+
#[cfg(c_bindings)]
212+
impl<'a, T: Score + 'a> Writeable for MultiThreadedLockableScoreLock<'a, T> {
213+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
214+
self.0.write(writer)
215+
}
216+
}
217+
218+
#[cfg(c_bindings)]
192219
impl<'a, T: Score + 'a> LockableScore<'a> for MultiThreadedLockableScore<T> {
193-
type Locked = MutexGuard<'a, T>;
220+
type Locked = MultiThreadedLockableScoreLock<'a, T>;
194221

195-
fn lock(&'a self) -> MutexGuard<'a, T> {
196-
Mutex::lock(&self.score).unwrap()
222+
fn lock(&'a self) -> MultiThreadedLockableScoreLock<'a, T> {
223+
MultiThreadedLockableScoreLock(Mutex::lock(&self.score).unwrap())
197224
}
198225
}
199226

0 commit comments

Comments
 (0)