-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rt: yield_now defers task until after driver poll (#5223)
Previously, calling `task::yield_now().await` would yield the current task to the scheduler, but the scheduler would poll it again before polling the resource drivers. This behavior can result in starving the resource drivers. This patch creates a queue tracking yielded tasks. The scheduler notifies those tasks **after** polling the resource drivers. Refs: #5209
- Loading branch information
1 parent
993a60b
commit 2286273
Showing
11 changed files
with
240 additions
and
7 deletions.
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
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,27 @@ | ||
use std::task::Waker; | ||
|
||
pub(crate) struct Defer { | ||
deferred: Vec<Waker>, | ||
} | ||
|
||
impl Defer { | ||
pub(crate) fn new() -> Defer { | ||
Defer { | ||
deferred: Default::default(), | ||
} | ||
} | ||
|
||
pub(crate) fn defer(&mut self, waker: Waker) { | ||
self.deferred.push(waker); | ||
} | ||
|
||
pub(crate) fn is_empty(&self) -> bool { | ||
self.deferred.is_empty() | ||
} | ||
|
||
pub(crate) fn wake(&mut self) { | ||
for waker in self.deferred.drain(..) { | ||
waker.wake(); | ||
} | ||
} | ||
} |
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
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
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,37 @@ | ||
use crate::runtime::park; | ||
use crate::runtime::tests::loom_oneshot as oneshot; | ||
use crate::runtime::{self, Runtime}; | ||
|
||
#[test] | ||
fn yield_calls_park_before_scheduling_again() { | ||
// Don't need to check all permutations | ||
let mut loom = loom::model::Builder::default(); | ||
loom.max_permutations = Some(1); | ||
loom.check(|| { | ||
let rt = mk_runtime(2); | ||
let (tx, rx) = oneshot::channel::<()>(); | ||
|
||
rt.spawn(async { | ||
let tid = loom::thread::current().id(); | ||
let park_count = park::current_thread_park_count(); | ||
|
||
crate::task::yield_now().await; | ||
|
||
if tid == loom::thread::current().id() { | ||
let new_park_count = park::current_thread_park_count(); | ||
assert_eq!(park_count + 1, new_park_count); | ||
} | ||
|
||
tx.send(()); | ||
}); | ||
|
||
rx.recv(); | ||
}); | ||
} | ||
|
||
fn mk_runtime(num_threads: usize) -> Runtime { | ||
runtime::Builder::new_multi_thread() | ||
.worker_threads(num_threads) | ||
.build() | ||
.unwrap() | ||
} |
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
Oops, something went wrong.