@@ -54,6 +54,8 @@ use chain::keysinterface::{Sign, KeysInterface, KeysManager, InMemorySigner, Rec
54
54
use util:: config:: { UserConfig , ChannelConfig } ;
55
55
use util:: events:: { EventHandler , EventsProvider , MessageSendEvent , MessageSendEventsProvider , ClosureReason , HTLCDestination } ;
56
56
use util:: { byte_utils, events} ;
57
+ use util:: crypto:: sign;
58
+ use util:: wakers:: PersistenceNotifier ;
57
59
use util:: scid_utils:: fake_scid;
58
60
use util:: ser:: { BigSize , FixedLengthReader , Readable , ReadableArgs , MaybeReadable , Writeable , Writer , VecWriter } ;
59
61
use util:: logger:: { Level , Logger } ;
@@ -64,15 +66,11 @@ use prelude::*;
64
66
use core:: { cmp, mem} ;
65
67
use core:: cell:: RefCell ;
66
68
use io:: Read ;
67
- use sync:: { Arc , Condvar , Mutex , MutexGuard , RwLock , RwLockReadGuard } ;
69
+ use sync:: { Arc , Mutex , MutexGuard , RwLock , RwLockReadGuard } ;
68
70
use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
69
71
use core:: time:: Duration ;
70
72
use core:: ops:: Deref ;
71
73
72
- #[ cfg( any( test, feature = "std" ) ) ]
73
- use std:: time:: Instant ;
74
- use util:: crypto:: sign;
75
-
76
74
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
77
75
//
78
76
// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
@@ -5992,10 +5990,7 @@ where
5992
5990
5993
5991
#[ cfg( any( test, feature = "_test_utils" ) ) ]
5994
5992
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 ( )
5999
5994
}
6000
5995
6001
5996
/// 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 >
6237
6232
}
6238
6233
}
6239
6234
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
-
6311
6235
const SERIALIZATION_VERSION : u8 = 1 ;
6312
6236
const MIN_SERIALIZATION_VERSION : u8 = 1 ;
6313
6237
@@ -7355,54 +7279,6 @@ mod tests {
7355
7279
use util:: test_utils;
7356
7280
use chain:: keysinterface:: KeysInterface ;
7357
7281
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
-
7406
7282
#[ test]
7407
7283
fn test_notify_limits ( ) {
7408
7284
// Check that a few cases which don't require the persistence of a new ChannelManager,
0 commit comments