From 467adec4e16bdf1b5461e77d87d1d56b4a29f001 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 2 Nov 2022 14:37:44 -0700 Subject: [PATCH] rt: move park logic into runtime module (#5158) The runtime is the primary user of parking. Moving park into runtime will help future cleanups around combining context entering w/ block_on calls. --- tokio/src/future/block_on.rs | 2 +- tokio/src/lib.rs | 1 - tokio/src/park/mod.rs | 37 ------------------- tokio/src/runtime/driver.rs | 2 +- tokio/src/runtime/enter.rs | 4 +- tokio/src/runtime/mod.rs | 2 + tokio/src/{park/thread.rs => runtime/park.rs} | 0 tokio/src/sync/mpsc/chan.rs | 2 +- 8 files changed, 7 insertions(+), 43 deletions(-) delete mode 100644 tokio/src/park/mod.rs rename tokio/src/{park/thread.rs => runtime/park.rs} (100%) diff --git a/tokio/src/future/block_on.rs b/tokio/src/future/block_on.rs index 8e1e6957eee..a624db53538 100644 --- a/tokio/src/future/block_on.rs +++ b/tokio/src/future/block_on.rs @@ -11,7 +11,7 @@ cfg_rt! { cfg_not_rt! { #[track_caller] pub(crate) fn block_on(f: F) -> F::Output { - let mut park = crate::park::thread::CachedParkThread::new(); + let mut park = crate::runtime::park::CachedParkThread::new(); park.block_on(f).unwrap() } } diff --git a/tokio/src/lib.rs b/tokio/src/lib.rs index 95947148ef3..f95786b81bf 100644 --- a/tokio/src/lib.rs +++ b/tokio/src/lib.rs @@ -479,7 +479,6 @@ pub mod io; pub mod net; mod loom; -mod park; cfg_process! { pub mod process; diff --git a/tokio/src/park/mod.rs b/tokio/src/park/mod.rs deleted file mode 100644 index a88b33ac15b..00000000000 --- a/tokio/src/park/mod.rs +++ /dev/null @@ -1,37 +0,0 @@ -//! Abstraction over blocking and unblocking the current thread. -//! -//! Provides an abstraction over blocking the current thread. This is similar to -//! the park / unpark constructs provided by `std` but made generic. This allows -//! embedding custom functionality to perform when the thread is blocked. -//! -//! A blocked `Park` instance is unblocked by calling `unpark` on its -//! `Unpark` handle. -//! -//! The `ParkThread` struct implements `Park` using `thread::park` to put the -//! thread to sleep. The Tokio reactor also implements park, but uses -//! `mio::Poll` to block the thread instead. -//! -//! The `Park` trait is composable. A timer implementation might decorate a -//! `Park` implementation by checking if any timeouts have elapsed after the -//! inner `Park` implementation unblocks. -//! -//! # Model -//! -//! Conceptually, each `Park` instance has an associated token, which is -//! initially not present: -//! -//! * The `park` method blocks the current thread unless or until the token is -//! available, at which point it atomically consumes the token. -//! * The `unpark` method atomically makes the token available if it wasn't -//! already. -//! -//! Some things to note: -//! -//! * If `unpark` is called before `park`, the next call to `park` will -//! **not** block the thread. -//! * **Spurious** wakeups are permitted, i.e., the `park` method may unblock -//! even if `unpark` was not called. -//! * `park_timeout` does the same as `park` but allows specifying a maximum -//! time to block the thread for. - -pub(crate) mod thread; diff --git a/tokio/src/runtime/driver.rs b/tokio/src/runtime/driver.rs index 895b97902c1..8f9c5122b85 100644 --- a/tokio/src/runtime/driver.rs +++ b/tokio/src/runtime/driver.rs @@ -4,7 +4,7 @@ // don't need to worry much about dead code with certain feature permutations. #![cfg_attr(not(feature = "full"), allow(dead_code))] -use crate::park::thread::{ParkThread, UnparkThread}; +use crate::runtime::park::{ParkThread, UnparkThread}; use std::io; use std::time::Duration; diff --git a/tokio/src/runtime/enter.rs b/tokio/src/runtime/enter.rs index fec5887eb84..6089b42c831 100644 --- a/tokio/src/runtime/enter.rs +++ b/tokio/src/runtime/enter.rs @@ -146,7 +146,7 @@ cfg_rt! { where F: std::future::Future, { - use crate::park::thread::CachedParkThread; + use crate::runtime::park::CachedParkThread; let mut park = CachedParkThread::new(); park.block_on(f) @@ -160,7 +160,7 @@ cfg_rt! { where F: std::future::Future, { - use crate::park::thread::CachedParkThread; + use crate::runtime::park::CachedParkThread; use std::task::Context; use std::task::Poll::Ready; use std::time::Instant; diff --git a/tokio/src/runtime/mod.rs b/tokio/src/runtime/mod.rs index aef12998e68..97971ae2762 100644 --- a/tokio/src/runtime/mod.rs +++ b/tokio/src/runtime/mod.rs @@ -181,6 +181,8 @@ pub(crate) mod context; pub(crate) mod coop; +pub(crate) mod park; + mod driver; pub(crate) mod scheduler; diff --git a/tokio/src/park/thread.rs b/tokio/src/runtime/park.rs similarity index 100% rename from tokio/src/park/thread.rs rename to tokio/src/runtime/park.rs diff --git a/tokio/src/sync/mpsc/chan.rs b/tokio/src/sync/mpsc/chan.rs index 92f057b068f..edd3e9527b0 100644 --- a/tokio/src/sync/mpsc/chan.rs +++ b/tokio/src/sync/mpsc/chan.rs @@ -2,7 +2,7 @@ use crate::loom::cell::UnsafeCell; use crate::loom::future::AtomicWaker; use crate::loom::sync::atomic::AtomicUsize; use crate::loom::sync::Arc; -use crate::park::thread::CachedParkThread; +use crate::runtime::park::CachedParkThread; use crate::sync::mpsc::error::TryRecvError; use crate::sync::mpsc::{bounded, list, unbounded}; use crate::sync::notify::Notify;