From 56b07e52aabdaca521987e765f1fa864966a5d49 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 19 Sep 2022 09:11:11 +0000 Subject: [PATCH] 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`. --- lightning/src/routing/scoring.rs | 35 ++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/lightning/src/routing/scoring.rs b/lightning/src/routing/scoring.rs index 4f980825d..66900f2ba 100644 --- a/lightning/src/routing/scoring.rs +++ b/lightning/src/routing/scoring.rs @@ -188,12 +188,39 @@ pub struct MultiThreadedLockableScore { score: Mutex, } #[cfg(c_bindings)] -/// (C-not exported) +/// A locked `MultiThreadedLockableScore`. +pub struct MultiThreadedLockableScoreLock<'a, S: Score>(MutexGuard<'a, S>); +#[cfg(c_bindings)] +impl<'a, T: Score + 'a> Score for MultiThreadedLockableScoreLock<'a, T> { + fn channel_penalty_msat(&self, scid: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage) -> u64 { + self.0.channel_penalty_msat(scid, source, target, usage) + } + fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) { + self.0.payment_path_failed(path, short_channel_id) + } + fn payment_path_successful(&mut self, path: &[&RouteHop]) { + self.0.payment_path_successful(path) + } + fn probe_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) { + self.0.probe_failed(path, short_channel_id) + } + fn probe_successful(&mut self, path: &[&RouteHop]) { + self.0.probe_successful(path) + } +} +#[cfg(c_bindings)] +impl<'a, T: Score + 'a> Writeable for MultiThreadedLockableScoreLock<'a, T> { + fn write(&self, writer: &mut W) -> Result<(), io::Error> { + self.0.write(writer) + } +} + +#[cfg(c_bindings)] impl<'a, T: Score + 'a> LockableScore<'a> for MultiThreadedLockableScore { - type Locked = MutexGuard<'a, T>; + type Locked = MultiThreadedLockableScoreLock<'a, T>; - fn lock(&'a self) -> MutexGuard<'a, T> { - Mutex::lock(&self.score).unwrap() + fn lock(&'a self) -> MultiThreadedLockableScoreLock<'a, T> { + MultiThreadedLockableScoreLock(Mutex::lock(&self.score).unwrap()) } } -- 2.39.5