@@ -2,7 +2,7 @@ use std::cell::Cell;
22use std:: io;
33use std:: iter;
44use std:: sync:: atomic:: { self , Ordering } ;
5- use std:: sync:: Arc ;
5+ use std:: sync:: { Arc , Mutex } ;
66use std:: thread;
77use std:: time:: Duration ;
88
@@ -22,6 +22,11 @@ thread_local! {
2222 static YIELD_NOW : Cell <bool > = Cell :: new( false ) ;
2323}
2424
25+ struct Scheduler {
26+ /// Set to `true` while a machine is polling the reactor.
27+ polling : bool ,
28+ }
29+
2530/// An async runtime.
2631pub struct Runtime {
2732 /// The reactor.
@@ -33,7 +38,11 @@ pub struct Runtime {
3338 /// Handles to local queues for stealing work.
3439 stealers : Vec < Stealer < Runnable > > ,
3540
41+ /// Machines to start
3642 machines : Vec < Arc < Machine > > ,
43+
44+ /// The scheduler state.
45+ sched : Mutex < Scheduler > ,
3746}
3847
3948impl Runtime {
@@ -57,6 +66,7 @@ impl Runtime {
5766 injector : Injector :: new ( ) ,
5867 stealers,
5968 machines,
69+ sched : Mutex :: new ( Scheduler { polling : false } ) ,
6070 }
6171 }
6272
@@ -116,7 +126,25 @@ impl Runtime {
116126 /// This function might not poll the reactor at all so do not rely on it doing anything. Only
117127 /// use for optimization.
118128 fn quick_poll ( & self ) -> io:: Result < bool > {
119- return self . reactor . poll ( Some ( Duration :: from_secs ( 0 ) ) ) ;
129+ if let Ok ( sched) = self . sched . try_lock ( ) {
130+ if !sched. polling {
131+ return self . reactor . poll ( Some ( Duration :: from_secs ( 0 ) ) ) ;
132+ }
133+ }
134+ Ok ( false )
135+ }
136+
137+ fn poll ( & self ) -> io:: Result < bool > {
138+ let mut sched = self . sched . lock ( ) . unwrap ( ) ;
139+ sched. polling = true ;
140+ drop ( sched) ;
141+
142+ let result = self . reactor . poll ( None ) ;
143+
144+ let mut sched = self . sched . lock ( ) . unwrap ( ) ;
145+ sched. polling = false ;
146+
147+ result
120148 }
121149}
122150
@@ -242,7 +270,7 @@ impl Machine {
242270 continue ;
243271 }
244272
245- rt. reactor . poll ( None ) . unwrap ( ) ;
273+ rt. poll ( ) . unwrap ( ) ;
246274
247275 runs = 0 ;
248276 fails = 0 ;
0 commit comments