summaryrefslogtreecommitdiffstats
path: root/tokio/src/coop.rs
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2020-03-28 13:55:12 -0700
committerGitHub <noreply@github.com>2020-03-28 13:55:12 -0700
commitcaa7e180e43fdf914774de86f01f88e6b41f4a32 (patch)
treeacd63c2a01e11f2c728f2d7527efafbc99c66132 /tokio/src/coop.rs
parent7b2438e7441e98b2a3f72eb239b1c51489b7d9b8 (diff)
rt: cap fifo scheduler slot to avoid starvation (#2349)
The work-stealing scheduler includes an optimization where each worker includes a single slot to store the **last** scheduled task. Tasks in scheduler's LIFO slot are executed next. This speeds up and reduces latency with message passing patterns. Previously, this optimization was susceptible to starving other tasks in certain cases. If two tasks ping-ping between each other without ever yielding, the worker would never execute other tasks. An early PR (#2160) introduced a form of pre-emption. Each task is allocated a per-poll operation budget. Tokio resources will return ready until the budget is depleted, at which point, Tokio resources will always return `Pending`. This patch leverages the operation budget to limit the LIFO scheduler optimization. When executing tasks from the LIFO slot, the budget is **not** reset. Once the budget goes to zero, the task in the LIFO slot is pushed to the back of the queue.
Diffstat (limited to 'tokio/src/coop.rs')
-rw-r--r--tokio/src/coop.rs7
1 files changed, 7 insertions, 0 deletions
diff --git a/tokio/src/coop.rs b/tokio/src/coop.rs
index 19302559..78905e3c 100644
--- a/tokio/src/coop.rs
+++ b/tokio/src/coop.rs
@@ -98,6 +98,13 @@ where
})
}
+cfg_rt_threaded! {
+ #[inline(always)]
+ pub(crate) fn has_budget_remaining() -> bool {
+ HITS.with(|hits| hits.get() > 0)
+ }
+}
+
cfg_blocking_impl! {
/// Forcibly remove the budgeting constraints early.
pub(crate) fn stop() {