Skip to content

Commit 7ffdf5b

Browse files
authored
m: Replace unnecessary atomics with non-atomic operations
1 parent 0baba46 commit 7ffdf5b

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

src/lib.rs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use std::future::Future;
3838
use std::marker::PhantomData;
3939
use std::panic::{RefUnwindSafe, UnwindSafe};
4040
use std::rc::Rc;
41-
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
41+
use std::sync::atomic::{AtomicBool, Ordering};
4242
use std::sync::{Arc, Mutex, RwLock, TryLockError};
4343
use std::task::{Poll, Waker};
4444

@@ -243,7 +243,7 @@ impl<'a> Executor<'a> {
243243
/// assert_eq!(res, 6);
244244
/// ```
245245
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());
247247
let mut rng = fastrand::Rng::new();
248248

249249
// A future that runs tasks forever.
@@ -639,29 +639,26 @@ struct Ticker<'a> {
639639
/// 1) Woken.
640640
/// 2a) Sleeping and unnotified.
641641
/// 2b) Sleeping and notified.
642-
sleeping: AtomicUsize,
642+
sleeping: usize,
643643
}
644644

645645
impl Ticker<'_> {
646646
/// Creates a ticker.
647647
fn new(state: &State) -> Ticker<'_> {
648-
Ticker {
649-
state,
650-
sleeping: AtomicUsize::new(0),
651-
}
648+
Ticker { state, sleeping: 0 }
652649
}
653650

654651
/// Moves the ticker into sleeping and unnotified state.
655652
///
656653
/// 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 {
658655
let mut sleepers = self.state.sleepers.lock().unwrap();
659656

660-
match self.sleeping.load(Ordering::SeqCst) {
657+
match self.sleeping {
661658
// 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+
}
665662

666663
// Already sleeping, check if notified.
667664
id => {
@@ -679,25 +676,25 @@ impl Ticker<'_> {
679676
}
680677

681678
/// 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 {
685681
let mut sleepers = self.state.sleepers.lock().unwrap();
686-
sleepers.remove(id);
682+
sleepers.remove(self.sleeping);
687683

688684
self.state
689685
.notified
690686
.swap(sleepers.is_notified(), Ordering::SeqCst);
691687
}
688+
self.sleeping = 0;
692689
}
693690

694691
/// Waits for the next runnable task to run.
695-
async fn runnable(&self) -> Runnable {
692+
async fn runnable(&mut self) -> Runnable {
696693
self.runnable_with(|| self.state.queue.pop().ok()).await
697694
}
698695

699696
/// 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 {
701698
future::poll_fn(|cx| {
702699
loop {
703700
match search() {
@@ -728,10 +725,9 @@ impl Ticker<'_> {
728725
impl Drop for Ticker<'_> {
729726
fn drop(&mut self) {
730727
// 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 {
733729
let mut sleepers = self.state.sleepers.lock().unwrap();
734-
let notified = sleepers.remove(id);
730+
let notified = sleepers.remove(self.sleeping);
735731

736732
self.state
737733
.notified
@@ -760,7 +756,7 @@ struct Runner<'a> {
760756
local: Arc<ConcurrentQueue<Runnable>>,
761757

762758
/// Bumped every time a runnable task is found.
763-
ticks: AtomicUsize,
759+
ticks: usize,
764760
}
765761

766762
impl Runner<'_> {
@@ -770,7 +766,7 @@ impl Runner<'_> {
770766
state,
771767
ticker: Ticker::new(state),
772768
local: Arc::new(ConcurrentQueue::bounded(512)),
773-
ticks: AtomicUsize::new(0),
769+
ticks: 0,
774770
};
775771
state
776772
.local_queues
@@ -781,7 +777,7 @@ impl Runner<'_> {
781777
}
782778

783779
/// 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 {
785781
let runnable = self
786782
.ticker
787783
.runnable_with(|| {
@@ -824,9 +820,9 @@ impl Runner<'_> {
824820
.await;
825821

826822
// Bump the tick counter.
827-
let ticks = self.ticks.fetch_add(1, Ordering::SeqCst);
823+
self.ticks += 1;
828824

829-
if ticks % 64 == 0 {
825+
if self.ticks % 64 == 0 {
830826
// Steal tasks from the global queue to ensure fair task scheduling.
831827
steal(&self.state.queue, &self.local);
832828
}

0 commit comments

Comments
 (0)