@@ -56,26 +56,33 @@ impl Notifier {
5656 /// Gets a [`Future`] that will get woken up with any waiters
5757 pub ( crate ) fn get_future ( & self ) -> Future {
5858 let mut lock = self . notify_pending . lock ( ) . unwrap ( ) ;
59+ let mut self_idx = 0 ;
5960 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 {
6163 // If the existing `FutureState` has completed and actually made callbacks,
6264 // consider the notification flag to have been cleared and reset the future state.
65+ mem:: drop ( locked) ;
6366 lock. 1 . take ( ) ;
6467 lock. 0 = false ;
68+ } else {
69+ self_idx = locked. next_idx ;
70+ locked. next_idx += 1 ;
6571 }
6672 }
6773 if let Some ( existing_state) = & lock. 1 {
68- Future { state : Arc :: clone ( & existing_state) }
74+ Future { state : Arc :: clone ( & existing_state) , self_idx }
6975 } else {
7076 let state = Arc :: new ( Mutex :: new ( FutureState {
7177 callbacks : Vec :: new ( ) ,
7278 std_future_callbacks : Vec :: new ( ) ,
7379 callbacks_with_state : Vec :: new ( ) ,
7480 complete : lock. 0 ,
7581 callbacks_made : false ,
82+ next_idx : 1 ,
7683 } ) ) ;
7784 lock. 1 = Some ( Arc :: clone ( & state) ) ;
78- Future { state }
85+ Future { state, self_idx : 0 }
7986 }
8087 }
8188
@@ -114,10 +121,11 @@ pub(crate) struct FutureState {
114121 // first bool - set to false if we're just calling a Waker, and true if we're calling an actual
115122 // user-provided function.
116123 callbacks : Vec < Box < dyn FutureCallback > > ,
117- std_future_callbacks : Vec < StdWaker > ,
124+ std_future_callbacks : Vec < ( usize , StdWaker ) > ,
118125 callbacks_with_state : Vec < ( bool , Box < dyn Fn ( & Arc < Mutex < FutureState > > ) -> ( ) + Send > ) > ,
119126 complete : bool ,
120127 callbacks_made : bool ,
128+ next_idx : usize ,
121129}
122130
123131fn complete_future ( this : & Arc < Mutex < FutureState > > ) -> bool {
@@ -127,7 +135,7 @@ fn complete_future(this: &Arc<Mutex<FutureState>>) -> bool {
127135 callback. call ( ) ;
128136 state. callbacks_made = true ;
129137 }
130- for waker in state. std_future_callbacks . drain ( ..) {
138+ for ( _ , waker) in state. std_future_callbacks . drain ( ..) {
131139 waker. 0 . wake_by_ref ( ) ;
132140 }
133141 for ( counts_as_call, callback) in state. callbacks_with_state . drain ( ..) {
@@ -139,11 +147,9 @@ fn complete_future(this: &Arc<Mutex<FutureState>>) -> bool {
139147}
140148
141149/// 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 ) ]
145150pub struct Future {
146151 state : Arc < Mutex < FutureState > > ,
152+ self_idx : usize ,
147153}
148154
149155impl Future {
@@ -210,7 +216,7 @@ impl<'a> StdFuture for Future {
210216 Poll :: Ready ( ( ) )
211217 } else {
212218 let waker = cx. waker ( ) . clone ( ) ;
213- state. std_future_callbacks . push ( StdWaker ( waker) ) ;
219+ state. std_future_callbacks . push ( ( self . self_idx , StdWaker ( waker) ) ) ;
214220 Poll :: Pending
215221 }
216222 }
@@ -461,7 +467,9 @@ mod tests {
461467 callbacks_with_state : Vec :: new ( ) ,
462468 complete : false ,
463469 callbacks_made : false ,
464- } ) )
470+ next_idx : 1 ,
471+ } ) ) ,
472+ self_idx : 0 ,
465473 } ;
466474 let callback = Arc :: new ( AtomicBool :: new ( false ) ) ;
467475 let callback_ref = Arc :: clone ( & callback) ;
@@ -478,10 +486,13 @@ mod tests {
478486 let future = Future {
479487 state : Arc :: new ( Mutex :: new ( FutureState {
480488 callbacks : Vec :: new ( ) ,
489+ std_future_callbacks : Vec :: new ( ) ,
481490 callbacks_with_state : Vec :: new ( ) ,
482491 complete : false ,
483492 callbacks_made : false ,
484- } ) )
493+ next_idx : 1 ,
494+ } ) ) ,
495+ self_idx : 0 ,
485496 } ;
486497 complete_future ( & future. state ) ;
487498
@@ -521,9 +532,11 @@ mod tests {
521532 callbacks_with_state : Vec :: new ( ) ,
522533 complete : false ,
523534 callbacks_made : false ,
524- } ) )
535+ next_idx : 2 ,
536+ } ) ) ,
537+ self_idx : 0 ,
525538 } ;
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 } ;
527540
528541 let ( woken, waker) = create_waker ( ) ;
529542 assert_eq ! ( Pin :: new( & mut future) . poll( & mut Context :: from_waker( & waker) ) , Poll :: Pending ) ;
0 commit comments