@@ -38,7 +38,7 @@ use std::future::Future;
38
38
use std:: marker:: PhantomData ;
39
39
use std:: panic:: { RefUnwindSafe , UnwindSafe } ;
40
40
use std:: rc:: Rc ;
41
- use std:: sync:: atomic:: { AtomicBool , AtomicUsize , Ordering } ;
41
+ use std:: sync:: atomic:: { AtomicBool , Ordering } ;
42
42
use std:: sync:: { Arc , Mutex , RwLock , TryLockError } ;
43
43
use std:: task:: { Poll , Waker } ;
44
44
@@ -243,7 +243,7 @@ impl<'a> Executor<'a> {
243
243
/// assert_eq!(res, 6);
244
244
/// ```
245
245
pub async fn run < T > ( & self , future : impl Future < Output = T > ) -> T {
246
- let runner = Runner :: new ( self . state ( ) ) ;
246
+ let mut runner = Runner :: new ( self . state ( ) ) ;
247
247
let mut rng = fastrand:: Rng :: new ( ) ;
248
248
249
249
// A future that runs tasks forever.
@@ -639,29 +639,26 @@ struct Ticker<'a> {
639
639
/// 1) Woken.
640
640
/// 2a) Sleeping and unnotified.
641
641
/// 2b) Sleeping and notified.
642
- sleeping : AtomicUsize ,
642
+ sleeping : usize ,
643
643
}
644
644
645
645
impl Ticker < ' _ > {
646
646
/// Creates a ticker.
647
647
fn new ( state : & State ) -> Ticker < ' _ > {
648
- Ticker {
649
- state,
650
- sleeping : AtomicUsize :: new ( 0 ) ,
651
- }
648
+ Ticker { state, sleeping : 0 }
652
649
}
653
650
654
651
/// Moves the ticker into sleeping and unnotified state.
655
652
///
656
653
/// Returns `false` if the ticker was already sleeping and unnotified.
657
- fn sleep ( & self , waker : & Waker ) -> bool {
654
+ fn sleep ( & mut self , waker : & Waker ) -> bool {
658
655
let mut sleepers = self . state . sleepers . lock ( ) . unwrap ( ) ;
659
656
660
- match self . sleeping . load ( Ordering :: SeqCst ) {
657
+ match self . sleeping {
661
658
// Move to sleeping state.
662
- 0 => self
663
- . sleeping
664
- . store ( sleepers . insert ( waker ) , Ordering :: SeqCst ) ,
659
+ 0 => {
660
+ self . sleeping = sleepers . insert ( waker ) ;
661
+ }
665
662
666
663
// Already sleeping, check if notified.
667
664
id => {
@@ -679,25 +676,25 @@ impl Ticker<'_> {
679
676
}
680
677
681
678
/// Moves the ticker into woken state.
682
- fn wake ( & self ) {
683
- let id = self . sleeping . swap ( 0 , Ordering :: SeqCst ) ;
684
- if id != 0 {
679
+ fn wake ( & mut self ) {
680
+ if self . sleeping != 0 {
685
681
let mut sleepers = self . state . sleepers . lock ( ) . unwrap ( ) ;
686
- sleepers. remove ( id ) ;
682
+ sleepers. remove ( self . sleeping ) ;
687
683
688
684
self . state
689
685
. notified
690
686
. swap ( sleepers. is_notified ( ) , Ordering :: SeqCst ) ;
691
687
}
688
+ self . sleeping = 0 ;
692
689
}
693
690
694
691
/// Waits for the next runnable task to run.
695
- async fn runnable ( & self ) -> Runnable {
692
+ async fn runnable ( & mut self ) -> Runnable {
696
693
self . runnable_with ( || self . state . queue . pop ( ) . ok ( ) ) . await
697
694
}
698
695
699
696
/// Waits for the next runnable task to run, given a function that searches for a task.
700
- async fn runnable_with ( & self , mut search : impl FnMut ( ) -> Option < Runnable > ) -> Runnable {
697
+ async fn runnable_with ( & mut self , mut search : impl FnMut ( ) -> Option < Runnable > ) -> Runnable {
701
698
future:: poll_fn ( |cx| {
702
699
loop {
703
700
match search ( ) {
@@ -728,10 +725,9 @@ impl Ticker<'_> {
728
725
impl Drop for Ticker < ' _ > {
729
726
fn drop ( & mut self ) {
730
727
// If this ticker is in sleeping state, it must be removed from the sleepers list.
731
- let id = self . sleeping . swap ( 0 , Ordering :: SeqCst ) ;
732
- if id != 0 {
728
+ if self . sleeping != 0 {
733
729
let mut sleepers = self . state . sleepers . lock ( ) . unwrap ( ) ;
734
- let notified = sleepers. remove ( id ) ;
730
+ let notified = sleepers. remove ( self . sleeping ) ;
735
731
736
732
self . state
737
733
. notified
@@ -760,7 +756,7 @@ struct Runner<'a> {
760
756
local : Arc < ConcurrentQueue < Runnable > > ,
761
757
762
758
/// Bumped every time a runnable task is found.
763
- ticks : AtomicUsize ,
759
+ ticks : usize ,
764
760
}
765
761
766
762
impl Runner < ' _ > {
@@ -770,7 +766,7 @@ impl Runner<'_> {
770
766
state,
771
767
ticker : Ticker :: new ( state) ,
772
768
local : Arc :: new ( ConcurrentQueue :: bounded ( 512 ) ) ,
773
- ticks : AtomicUsize :: new ( 0 ) ,
769
+ ticks : 0 ,
774
770
} ;
775
771
state
776
772
. local_queues
@@ -781,7 +777,7 @@ impl Runner<'_> {
781
777
}
782
778
783
779
/// Waits for the next runnable task to run.
784
- async fn runnable ( & self , rng : & mut fastrand:: Rng ) -> Runnable {
780
+ async fn runnable ( & mut self , rng : & mut fastrand:: Rng ) -> Runnable {
785
781
let runnable = self
786
782
. ticker
787
783
. runnable_with ( || {
@@ -824,9 +820,9 @@ impl Runner<'_> {
824
820
. await ;
825
821
826
822
// Bump the tick counter.
827
- let ticks = self . ticks . fetch_add ( 1 , Ordering :: SeqCst ) ;
823
+ self . ticks += 1 ;
828
824
829
- if ticks % 64 == 0 {
825
+ if self . ticks % 64 == 0 {
830
826
// Steal tasks from the global queue to ensure fair task scheduling.
831
827
steal ( & self . state . queue , & self . local ) ;
832
828
}
0 commit comments