@@ -54,6 +54,8 @@ use chain::keysinterface::{Sign, KeysInterface, KeysManager, InMemorySigner, Rec
5454use util:: config:: { UserConfig , ChannelConfig } ;
5555use util:: events:: { EventHandler , EventsProvider , MessageSendEvent , MessageSendEventsProvider , ClosureReason , HTLCDestination } ;
5656use util:: { byte_utils, events} ;
57+ use util:: crypto:: sign;
58+ use util:: wakers:: PersistenceNotifier ;
5759use util:: scid_utils:: fake_scid;
5860use util:: ser:: { BigSize , FixedLengthReader , Readable , ReadableArgs , MaybeReadable , Writeable , Writer , VecWriter } ;
5961use util:: logger:: { Level , Logger } ;
@@ -64,15 +66,11 @@ use prelude::*;
6466use core:: { cmp, mem} ;
6567use core:: cell:: RefCell ;
6668use io:: Read ;
67- use sync:: { Arc , Condvar , Mutex , MutexGuard , RwLock , RwLockReadGuard } ;
69+ use sync:: { Arc , Mutex , MutexGuard , RwLock , RwLockReadGuard } ;
6870use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
6971use core:: time:: Duration ;
7072use core:: ops:: Deref ;
7173
72- #[ cfg( any( test, feature = "std" ) ) ]
73- use std:: time:: Instant ;
74- use util:: crypto:: sign;
75-
7674// We hold various information about HTLC relay in the HTLC objects in Channel itself:
7775//
7876// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
@@ -5992,10 +5990,7 @@ where
59925990
59935991 #[ cfg( any( test, feature = "_test_utils" ) ) ]
59945992 pub fn get_persistence_condvar_value ( & self ) -> bool {
5995- let mutcond = & self . persistence_notifier . persistence_lock ;
5996- let & ( ref mtx, _) = mutcond;
5997- let guard = mtx. lock ( ) . unwrap ( ) ;
5998- * guard
5993+ self . persistence_notifier . needs_persist ( )
59995994 }
60005995
60015996 /// Gets the latest best block which was connected either via the [`chain::Listen`] or
@@ -6237,77 +6232,6 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
62376232 }
62386233}
62396234
6240- /// Used to signal to the ChannelManager persister that the manager needs to be re-persisted to
6241- /// disk/backups, through `await_persistable_update_timeout` and `await_persistable_update`.
6242- struct PersistenceNotifier {
6243- /// Users won't access the persistence_lock directly, but rather wait on its bool using
6244- /// `wait_timeout` and `wait`.
6245- persistence_lock : ( Mutex < bool > , Condvar ) ,
6246- }
6247-
6248- impl PersistenceNotifier {
6249- fn new ( ) -> Self {
6250- Self {
6251- persistence_lock : ( Mutex :: new ( false ) , Condvar :: new ( ) ) ,
6252- }
6253- }
6254-
6255- fn wait ( & self ) {
6256- loop {
6257- let & ( ref mtx, ref cvar) = & self . persistence_lock ;
6258- let mut guard = mtx. lock ( ) . unwrap ( ) ;
6259- if * guard {
6260- * guard = false ;
6261- return ;
6262- }
6263- guard = cvar. wait ( guard) . unwrap ( ) ;
6264- let result = * guard;
6265- if result {
6266- * guard = false ;
6267- return
6268- }
6269- }
6270- }
6271-
6272- #[ cfg( any( test, feature = "std" ) ) ]
6273- fn wait_timeout ( & self , max_wait : Duration ) -> bool {
6274- let current_time = Instant :: now ( ) ;
6275- loop {
6276- let & ( ref mtx, ref cvar) = & self . persistence_lock ;
6277- let mut guard = mtx. lock ( ) . unwrap ( ) ;
6278- if * guard {
6279- * guard = false ;
6280- return true ;
6281- }
6282- guard = cvar. wait_timeout ( guard, max_wait) . unwrap ( ) . 0 ;
6283- // Due to spurious wakeups that can happen on `wait_timeout`, here we need to check if the
6284- // desired wait time has actually passed, and if not then restart the loop with a reduced wait
6285- // time. Note that this logic can be highly simplified through the use of
6286- // `Condvar::wait_while` and `Condvar::wait_timeout_while`, if and when our MSRV is raised to
6287- // 1.42.0.
6288- let elapsed = current_time. elapsed ( ) ;
6289- let result = * guard;
6290- if result || elapsed >= max_wait {
6291- * guard = false ;
6292- return result;
6293- }
6294- match max_wait. checked_sub ( elapsed) {
6295- None => return result,
6296- Some ( _) => continue
6297- }
6298- }
6299- }
6300-
6301- // Signal to the ChannelManager persister that there are updates necessitating persisting to disk.
6302- fn notify ( & self ) {
6303- let & ( ref persist_mtx, ref cnd) = & self . persistence_lock ;
6304- let mut persistence_lock = persist_mtx. lock ( ) . unwrap ( ) ;
6305- * persistence_lock = true ;
6306- mem:: drop ( persistence_lock) ;
6307- cnd. notify_all ( ) ;
6308- }
6309- }
6310-
63116235const SERIALIZATION_VERSION : u8 = 1 ;
63126236const MIN_SERIALIZATION_VERSION : u8 = 1 ;
63136237
@@ -7355,54 +7279,6 @@ mod tests {
73557279 use util:: test_utils;
73567280 use chain:: keysinterface:: KeysInterface ;
73577281
7358- #[ cfg( feature = "std" ) ]
7359- #[ test]
7360- fn test_wait_timeout ( ) {
7361- use ln:: channelmanager:: PersistenceNotifier ;
7362- use sync:: Arc ;
7363- use core:: sync:: atomic:: AtomicBool ;
7364- use std:: thread;
7365-
7366- let persistence_notifier = Arc :: new ( PersistenceNotifier :: new ( ) ) ;
7367- let thread_notifier = Arc :: clone ( & persistence_notifier) ;
7368-
7369- let exit_thread = Arc :: new ( AtomicBool :: new ( false ) ) ;
7370- let exit_thread_clone = exit_thread. clone ( ) ;
7371- thread:: spawn ( move || {
7372- loop {
7373- let & ( ref persist_mtx, ref cnd) = & thread_notifier. persistence_lock ;
7374- let mut persistence_lock = persist_mtx. lock ( ) . unwrap ( ) ;
7375- * persistence_lock = true ;
7376- cnd. notify_all ( ) ;
7377-
7378- if exit_thread_clone. load ( Ordering :: SeqCst ) {
7379- break
7380- }
7381- }
7382- } ) ;
7383-
7384- // Check that we can block indefinitely until updates are available.
7385- let _ = persistence_notifier. wait ( ) ;
7386-
7387- // Check that the PersistenceNotifier will return after the given duration if updates are
7388- // available.
7389- loop {
7390- if persistence_notifier. wait_timeout ( Duration :: from_millis ( 100 ) ) {
7391- break
7392- }
7393- }
7394-
7395- exit_thread. store ( true , Ordering :: SeqCst ) ;
7396-
7397- // Check that the PersistenceNotifier will return after the given duration even if no updates
7398- // are available.
7399- loop {
7400- if !persistence_notifier. wait_timeout ( Duration :: from_millis ( 100 ) ) {
7401- break
7402- }
7403- }
7404- }
7405-
74067282 #[ test]
74077283 fn test_notify_limits ( ) {
74087284 // Check that a few cases which don't require the persistence of a new ChannelManager,
0 commit comments