Add a C-bindings-compatible read lock type for NetworkGraph

In order to calculate a route, it is likely that users need to take
a read()-lock on NetGraphMsgHandler::network_graph. This is not
possible naively from C bindings, as Rust's native RwLock is not
exposed.

Thus, we provide a simple wrapper around the RwLockReadGuard and
expose simple accessor methods.
This commit is contained in:
Matt Corallo 2020-08-24 14:14:05 -04:00
parent c6bae1fdb0
commit d224c1def4

View file

@ -27,7 +27,7 @@ use util::ser::{Writeable, Readable, Writer};
use util::logger::Logger;
use std::{cmp, fmt};
use std::sync::RwLock;
use std::sync::{RwLock, RwLockReadGuard};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::collections::BTreeMap;
use std::collections::btree_map::Entry as BtreeEntry;
@ -41,6 +41,11 @@ pub struct NetworkGraph {
nodes: BTreeMap<PublicKey, NodeInfo>,
}
/// A simple newtype for RwLockReadGuard<'a, NetworkGraph>.
/// This exists only to make accessing a RwLock<NetworkGraph> possible from
/// the C bindings, as it can be done directly in Rust code.
pub struct LockedNetworkGraph<'a>(pub RwLockReadGuard<'a, NetworkGraph>);
/// Receives and validates network updates from peers,
/// stores authentic and relevant data as a network graph.
/// This network graph is then used for routing payments.
@ -85,6 +90,21 @@ impl<C: Deref, L: Deref> NetGraphMsgHandler<C, L> where C::Target: ChainWatchInt
logger,
}
}
/// Take a read lock on the network_graph and return it in the C-bindings
/// newtype helper. This is likely only useful when called via the C
/// bindings as you can call `self.network_graph.read().unwrap()` in Rust
/// yourself.
pub fn read_locked_graph<'a>(&'a self) -> LockedNetworkGraph<'a> {
LockedNetworkGraph(self.network_graph.read().unwrap())
}
}
impl<'a> LockedNetworkGraph<'a> {
/// Get a reference to the NetworkGraph which this read-lock contains.
pub fn graph(&self) -> &NetworkGraph {
&*self.0
}
}