@@ -56,26 +56,33 @@ impl Notifier {
56
56
/// Gets a [`Future`] that will get woken up with any waiters
57
57
pub ( crate ) fn get_future ( & self ) -> Future {
58
58
let mut lock = self . notify_pending . lock ( ) . unwrap ( ) ;
59
+ let mut self_idx = 0 ;
59
60
if let Some ( existing_state) = & lock. 1 {
60
- if existing_state. lock ( ) . unwrap ( ) . callbacks_made {
61
+ let mut locked = existing_state. lock ( ) . unwrap ( ) ;
62
+ if locked. callbacks_made {
61
63
// If the existing `FutureState` has completed and actually made callbacks,
62
64
// consider the notification flag to have been cleared and reset the future state.
65
+ mem:: drop ( locked) ;
63
66
lock. 1 . take ( ) ;
64
67
lock. 0 = false ;
68
+ } else {
69
+ self_idx = locked. next_idx ;
70
+ locked. next_idx += 1 ;
65
71
}
66
72
}
67
73
if let Some ( existing_state) = & lock. 1 {
68
- Future { state : Arc :: clone ( & existing_state) }
74
+ Future { state : Arc :: clone ( & existing_state) , self_idx }
69
75
} else {
70
76
let state = Arc :: new ( Mutex :: new ( FutureState {
71
77
callbacks : Vec :: new ( ) ,
72
78
std_future_callbacks : Vec :: new ( ) ,
73
79
callbacks_with_state : Vec :: new ( ) ,
74
80
complete : lock. 0 ,
75
81
callbacks_made : false ,
82
+ next_idx : 1 ,
76
83
} ) ) ;
77
84
lock. 1 = Some ( Arc :: clone ( & state) ) ;
78
- Future { state }
85
+ Future { state, self_idx : 0 }
79
86
}
80
87
}
81
88
@@ -115,10 +122,11 @@ pub(crate) struct FutureState {
115
122
// we only count it after another `poll()` and the second wakes a `Sleeper` which handles
116
123
// setting `callbacks_made` itself).
117
124
callbacks : Vec < Box < dyn FutureCallback > > ,
118
- std_future_callbacks : Vec < StdWaker > ,
125
+ std_future_callbacks : Vec < ( usize , StdWaker ) > ,
119
126
callbacks_with_state : Vec < Box < dyn Fn ( & Arc < Mutex < FutureState > > ) -> ( ) + Send > > ,
120
127
complete : bool ,
121
128
callbacks_made : bool ,
129
+ next_idx : usize ,
122
130
}
123
131
124
132
fn complete_future ( this : & Arc < Mutex < FutureState > > ) -> bool {
@@ -128,7 +136,7 @@ fn complete_future(this: &Arc<Mutex<FutureState>>) -> bool {
128
136
callback. call ( ) ;
129
137
state. callbacks_made = true ;
130
138
}
131
- for waker in state. std_future_callbacks . drain ( ..) {
139
+ for ( _ , waker) in state. std_future_callbacks . drain ( ..) {
132
140
waker. 0 . wake_by_ref ( ) ;
133
141
}
134
142
for callback in state. callbacks_with_state . drain ( ..) {
@@ -139,11 +147,9 @@ fn complete_future(this: &Arc<Mutex<FutureState>>) -> bool {
139
147
}
140
148
141
149
/// A simple future which can complete once, and calls some callback(s) when it does so.
142
- ///
143
- /// Clones can be made and all futures cloned from the same source will complete at the same time.
144
- #[ derive( Clone ) ]
145
150
pub struct Future {
146
151
state : Arc < Mutex < FutureState > > ,
152
+ self_idx : usize ,
147
153
}
148
154
149
155
impl Future {
@@ -210,7 +216,7 @@ impl<'a> StdFuture for Future {
210
216
Poll :: Ready ( ( ) )
211
217
} else {
212
218
let waker = cx. waker ( ) . clone ( ) ;
213
- state. std_future_callbacks . push ( StdWaker ( waker) ) ;
219
+ state. std_future_callbacks . push ( ( self . self_idx , StdWaker ( waker) ) ) ;
214
220
Poll :: Pending
215
221
}
216
222
}
@@ -461,7 +467,9 @@ mod tests {
461
467
callbacks_with_state : Vec :: new ( ) ,
462
468
complete : false ,
463
469
callbacks_made : false ,
464
- } ) )
470
+ next_idx : 1 ,
471
+ } ) ) ,
472
+ self_idx : 0 ,
465
473
} ;
466
474
let callback = Arc :: new ( AtomicBool :: new ( false ) ) ;
467
475
let callback_ref = Arc :: clone ( & callback) ;
@@ -478,10 +486,13 @@ mod tests {
478
486
let future = Future {
479
487
state : Arc :: new ( Mutex :: new ( FutureState {
480
488
callbacks : Vec :: new ( ) ,
489
+ std_future_callbacks : Vec :: new ( ) ,
481
490
callbacks_with_state : Vec :: new ( ) ,
482
491
complete : false ,
483
492
callbacks_made : false ,
484
- } ) )
493
+ next_idx : 1 ,
494
+ } ) ) ,
495
+ self_idx : 0 ,
485
496
} ;
486
497
complete_future ( & future. state ) ;
487
498
@@ -521,9 +532,11 @@ mod tests {
521
532
callbacks_with_state : Vec :: new ( ) ,
522
533
complete : false ,
523
534
callbacks_made : false ,
524
- } ) )
535
+ next_idx : 2 ,
536
+ } ) ) ,
537
+ self_idx : 0 ,
525
538
} ;
526
- let mut second_future = Future { state : Arc :: clone ( & future. state ) } ;
539
+ let mut second_future = Future { state : Arc :: clone ( & future. state ) , self_idx : 1 } ;
527
540
528
541
let ( woken, waker) = create_waker ( ) ;
529
542
assert_eq ! ( Pin :: new( & mut future) . poll( & mut Context :: from_waker( & waker) ) , Poll :: Pending ) ;
0 commit comments