-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ad9afd6
runtime: avoid unnecessary polling of block_on future
zaharidichev 120ec47
add loom test
zaharidichev 37665a1
use two threads for loom test
zaharidichev 1d1d924
add comments, decrease max tasks per tick
zaharidichev 4eff7dc
another test
zaharidichev 6d772e9
fix mod
zaharidichev 7ffe2b0
use swap with AcqRel, remove extra test
zaharidichev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use crate::loom::sync::atomic::AtomicUsize; | ||
use crate::loom::sync::Arc; | ||
use crate::loom::thread; | ||
use crate::runtime::{Builder, Runtime}; | ||
use crate::sync::oneshot::{self, Receiver}; | ||
use crate::task; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::sync::atomic::Ordering::{Acquire, Release}; | ||
use std::task::{Context, Poll}; | ||
|
||
fn assert_at_most_num_polls(rt: Arc<Runtime>, at_most_polls: usize) { | ||
let (tx, rx) = oneshot::channel(); | ||
let num_polls = Arc::new(AtomicUsize::new(0)); | ||
rt.spawn(async move { | ||
for _ in 0..12 { | ||
task::yield_now().await; | ||
} | ||
tx.send(()).unwrap(); | ||
}); | ||
|
||
rt.block_on(async { | ||
BlockedFuture { | ||
rx, | ||
num_polls: num_polls.clone(), | ||
} | ||
.await; | ||
}); | ||
|
||
let polls = num_polls.load(Acquire); | ||
assert!(polls <= at_most_polls); | ||
} | ||
|
||
#[test] | ||
fn block_on_num_polls() { | ||
loom::model(|| { | ||
// we expect at most 3 number of polls because there are | ||
// three points at which we poll the future. At any of these | ||
// points it can be ready: | ||
// | ||
// - when we fail to steal the parker and we block on a | ||
// notification that it is available. | ||
// | ||
// - when we steal the parker and we schedule the future | ||
// | ||
// - when the future is woken up and we have ran the max | ||
// number of tasks for the current tick or there are no | ||
// more tasks to run. | ||
// | ||
let at_most = 3; | ||
|
||
let rt1 = Arc::new(Builder::new_current_thread().build().unwrap()); | ||
let rt2 = rt1.clone(); | ||
let rt3 = rt1.clone(); | ||
|
||
let th1 = thread::spawn(move || assert_at_most_num_polls(rt1, at_most)); | ||
let th2 = thread::spawn(move || assert_at_most_num_polls(rt2, at_most)); | ||
let th3 = thread::spawn(move || assert_at_most_num_polls(rt3, at_most)); | ||
|
||
th1.join().unwrap(); | ||
th2.join().unwrap(); | ||
th3.join().unwrap(); | ||
}); | ||
} | ||
|
||
struct BlockedFuture { | ||
rx: Receiver<()>, | ||
num_polls: Arc<AtomicUsize>, | ||
} | ||
|
||
impl Future for BlockedFuture { | ||
type Output = (); | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
self.num_polls.fetch_add(1, Release); | ||
|
||
match Pin::new(&mut self.rx).poll(cx) { | ||
Poll::Pending => Poll::Pending, | ||
_ => Poll::Ready(()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
cfg_loom! { | ||
mod loom_basic_scheduler; | ||
mod loom_blocking; | ||
mod loom_oneshot; | ||
mod loom_pool; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.