Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: avoid unnecessary polling of block_on future #3582

Merged
merged 7 commits into from
Mar 16, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions tokio/src/runtime/basic_scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use std::cell::RefCell;
use std::collections::VecDeque;
use std::fmt;
use std::future::Future;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::{Acquire, Release};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use crate::loom::sync::atomic::AtomicBool here?

use std::sync::Arc;
use std::task::Poll::{Pending, Ready};
use std::time::Duration;
Expand Down Expand Up @@ -70,6 +72,9 @@ struct Shared {

/// Unpark the blocked thread
unpark: Box<dyn Unpark>,

// indicates whether the blocked on thread was woken
woken: AtomicBool,
Comment on lines +76 to +77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if we have multiple calls to block_on?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Darksonn the field is set to false in Spawner::waker_ref, Wouldn't that work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you implement the loom test I have mentioned below, that will answer this question.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is additional synchronization that ensures only one concurrent blocker hits this bool.

}

/// Thread-local context.
Expand Down Expand Up @@ -101,6 +106,7 @@ impl<P: Park> BasicScheduler<P> {
shared: Arc::new(Shared {
queue: Mutex::new(VecDeque::with_capacity(INITIAL_CAPACITY)),
unpark: unpark as Box<dyn Unpark>,
woken: AtomicBool::new(false),
}),
};

Expand Down Expand Up @@ -210,8 +216,10 @@ impl<P: Park> Inner<P> {
// Park until the thread is signaled
scheduler.park.park().ok().expect("failed to park");

// Try polling the `block_on` future next
continue 'outer;
if scheduler.spawner.was_woken() {
// Try polling the `block_on` future next if it was woken
continue 'outer;
}
}
}
}
Expand Down Expand Up @@ -329,8 +337,14 @@ impl Spawner {
}

fn waker_ref(&self) -> WakerRef<'_> {
// clear the woken bit
self.shared.woken.store(false, Release);
waker_ref(&self.shared)
}

fn was_woken(&self) -> bool {
self.shared.woken.load(Acquire)
}
}

impl fmt::Debug for Spawner {
Expand Down Expand Up @@ -384,6 +398,7 @@ impl Wake for Shared {

/// Wake by reference
fn wake_by_ref(arc_self: &Arc<Self>) {
arc_self.woken.store(true, Release);
arc_self.unpark.unpark();
}
}
Expand Down