summaryrefslogtreecommitdiffstats
path: root/tokio/src/task
diff options
context:
space:
mode:
authorLucio Franco <luciofranco14@gmail.com>2020-10-12 13:44:54 -0400
committerGitHub <noreply@github.com>2020-10-12 13:44:54 -0400
commit8880222036f37c6204c8466f25e828447f16dacb (patch)
treefd623afc20f73bbce65746a3d1b1b2731ecf30a5 /tokio/src/task
parent0893841f31542b2b04c5050a8a4a3c45cf867e55 (diff)
rt: Remove `threaded_scheduler()` and `basic_scheduler()` (#2876)
Co-authored-by: Alice Ryhl <alice@ryhl.io> Co-authored-by: Carl Lerche <me@carllerche.com>
Diffstat (limited to 'tokio/src/task')
-rw-r--r--tokio/src/task/blocking.rs153
-rw-r--r--tokio/src/task/local.rs2
-rw-r--r--tokio/src/task/mod.rs19
-rw-r--r--tokio/src/task/yield_now.rs2
4 files changed, 87 insertions, 89 deletions
diff --git a/tokio/src/task/blocking.rs b/tokio/src/task/blocking.rs
index ed60f4c4..5f9d8af7 100644
--- a/tokio/src/task/blocking.rs
+++ b/tokio/src/task/blocking.rs
@@ -17,7 +17,7 @@ cfg_rt_threaded! {
/// using the [`join!`] macro. To avoid this issue, use [`spawn_blocking`]
/// instead.
///
- /// Note that this function can only be used on the [threaded scheduler].
+ /// Note that this function can only be used when using the `multi_thread` runtime.
///
/// Code running behind `block_in_place` cannot be cancelled. When you shut
/// down the executor, it will wait indefinitely for all blocking operations
@@ -27,7 +27,6 @@ cfg_rt_threaded! {
/// returns.
///
/// [blocking]: ../index.html#cpu-bound-tasks-and-blocking-code
- /// [threaded scheduler]: fn@crate::runtime::Builder::threaded_scheduler
/// [`spawn_blocking`]: fn@crate::task::spawn_blocking
/// [`join!`]: macro@join
/// [`thread::spawn`]: fn@std::thread::spawn
@@ -44,7 +43,6 @@ cfg_rt_threaded! {
/// });
/// # }
/// ```
- #[cfg_attr(docsrs, doc(cfg(feature = "blocking")))]
pub fn block_in_place<F, R>(f: F) -> R
where
F: FnOnce() -> R,
@@ -53,80 +51,77 @@ cfg_rt_threaded! {
}
}
-cfg_blocking! {
- /// Runs the provided closure on a thread where blocking is acceptable.
- ///
- /// In general, issuing a blocking call or performing a lot of compute in a
- /// future without yielding is not okay, as it may prevent the executor from
- /// driving other futures forward. This function runs the provided closure
- /// on a thread dedicated to blocking operations. See the [CPU-bound tasks
- /// and blocking code][blocking] section for more information.
- ///
- /// Tokio will spawn more blocking threads when they are requested through
- /// this function until the upper limit configured on the [`Builder`] is
- /// reached. This limit is very large by default, because `spawn_blocking` is
- /// often used for various kinds of IO operations that cannot be performed
- /// asynchronously. When you run CPU-bound code using `spawn_blocking`, you
- /// should keep this large upper limit in mind; to run your CPU-bound
- /// computations on only a few threads, you should use a separate thread
- /// pool such as [rayon] rather than configuring the number of blocking
- /// threads.
- ///
- /// This function is intended for non-async operations that eventually
- /// finish on their own. If you want to spawn an ordinary thread, you should
- /// use [`thread::spawn`] instead.
- ///
- /// Closures spawned using `spawn_blocking` cannot be cancelled. When you
- /// shut down the executor, it will wait indefinitely for all blocking
- /// operations to finish. You can use [`shutdown_timeout`] to stop waiting
- /// for them after a certain timeout. Be aware that this will still not
- /// cancel the tasks — they are simply allowed to keep running after the
- /// method returns.
- ///
- /// Note that if you are using the [basic scheduler], this function will
- /// still spawn additional threads for blocking operations. The basic
- /// scheduler's single thread is only used for asynchronous code.
- ///
- /// [`Builder`]: struct@crate::runtime::Builder
- /// [blocking]: ../index.html#cpu-bound-tasks-and-blocking-code
- /// [rayon]: https://docs.rs/rayon
- /// [basic scheduler]: fn@crate::runtime::Builder::basic_scheduler
- /// [`thread::spawn`]: fn@std::thread::spawn
- /// [`shutdown_timeout`]: fn@crate::runtime::Runtime::shutdown_timeout
- ///
- /// # Examples
- ///
- /// ```
- /// use tokio::task;
- ///
- /// # async fn docs() -> Result<(), Box<dyn std::error::Error>>{
- /// let res = task::spawn_blocking(move || {
- /// // do some compute-heavy work or call synchronous code
- /// "done computing"
- /// }).await?;
- ///
- /// assert_eq!(res, "done computing");
- /// # Ok(())
- /// # }
- /// ```
- pub fn spawn_blocking<F, R>(f: F) -> JoinHandle<R>
- where
- F: FnOnce() -> R + Send + 'static,
- R: Send + 'static,
- {
- #[cfg(feature = "tracing")]
- let f = {
- let span = tracing::trace_span!(
- target: "tokio::task",
- "task",
- kind = %"blocking",
- function = %std::any::type_name::<F>(),
- );
- move || {
- let _g = span.enter();
- f()
- }
- };
- crate::runtime::spawn_blocking(f)
- }
+/// Runs the provided closure on a thread where blocking is acceptable.
+///
+/// In general, issuing a blocking call or performing a lot of compute in a
+/// future without yielding is not okay, as it may prevent the executor from
+/// driving other futures forward. This function runs the provided closure
+/// on a thread dedicated to blocking operations. See the [CPU-bound tasks
+/// and blocking code][blocking] section for more information.
+///
+/// Tokio will spawn more blocking threads when they are requested through
+/// this function until the upper limit configured on the [`Builder`] is
+/// reached. This limit is very large by default, because `spawn_blocking` is
+/// often used for various kinds of IO operations that cannot be performed
+/// asynchronously. When you run CPU-bound code using `spawn_blocking`, you
+/// should keep this large upper limit in mind; to run your CPU-bound
+/// computations on only a few threads, you should use a separate thread
+/// pool such as [rayon] rather than configuring the number of blocking
+/// threads.
+///
+/// This function is intended for non-async operations that eventually
+/// finish on their own. If you want to spawn an ordinary thread, you should
+/// use [`thread::spawn`] instead.
+///
+/// Closures spawned using `spawn_blocking` cannot be cancelled. When you
+/// shut down the executor, it will wait indefinitely for all blocking
+/// operations to finish. You can use [`shutdown_timeout`] to stop waiting
+/// for them after a certain timeout. Be aware that this will still not
+/// cancel the tasks — they are simply allowed to keep running after the
+/// method returns.
+///
+/// Note that if you are using the single threaded runtime, this function will
+/// still spawn additional threads for blocking operations. The basic
+/// scheduler's single thread is only used for asynchronous code.
+///
+/// [`Builder`]: struct@crate::runtime::Builder
+/// [blocking]: ../index.html#cpu-bound-tasks-and-blocking-code
+/// [rayon]: https://docs.rs/rayon
+/// [`thread::spawn`]: fn@std::thread::spawn
+/// [`shutdown_timeout`]: fn@crate::runtime::Runtime::shutdown_timeout
+///
+/// # Examples
+///
+/// ```
+/// use tokio::task;
+///
+/// # async fn docs() -> Result<(), Box<dyn std::error::Error>>{
+/// let res = task::spawn_blocking(move || {
+/// // do some compute-heavy work or call synchronous code
+/// "done computing"
+/// }).await?;
+///
+/// assert_eq!(res, "done computing");
+/// # Ok(())
+/// # }
+/// ```
+pub fn spawn_blocking<F, R>(f: F) -> JoinHandle<R>
+where
+ F: FnOnce() -> R + Send + 'static,
+ R: Send + 'static,
+{
+ #[cfg(feature = "tracing")]
+ let f = {
+ let span = tracing::trace_span!(
+ target: "tokio::task",
+ "task",
+ kind = %"blocking",
+ function = %std::any::type_name::<F>(),
+ );
+ move || {
+ let _g = span.enter();
+ f()
+ }
+ };
+ crate::runtime::spawn_blocking(f)
}
diff --git a/tokio/src/task/local.rs b/tokio/src/task/local.rs
index ccb9201e..e19cbe63 100644
--- a/tokio/src/task/local.rs
+++ b/tokio/src/task/local.rs
@@ -346,6 +346,8 @@ impl LocalSet {
/// [`Runtime::block_on`]: method@crate::runtime::Runtime::block_on
/// [in-place blocking]: fn@crate::task::block_in_place
/// [`spawn_blocking`]: fn@crate::task::spawn_blocking
+ #[cfg(feature = "rt-core")]
+ #[cfg_attr(docsrs, doc(cfg(feature = "rt-core")))]
pub fn block_on<F>(&self, rt: &crate::runtime::Runtime, future: F) -> F::Output
where
F: Future,
diff --git a/tokio/src/task/mod.rs b/tokio/src/task/mod.rs
index 5c89393a..860c8929 100644
--- a/tokio/src/task/mod.rs
+++ b/tokio/src/task/mod.rs
@@ -214,26 +214,27 @@
//! [rt-threaded]: ../runtime/index.html#threaded-scheduler
//! [`task::yield_now`]: crate::task::yield_now()
//! [`thread::yield_now`]: std::thread::yield_now
-cfg_blocking! {
- mod blocking;
- pub use blocking::spawn_blocking;
- cfg_rt_threaded! {
- pub use blocking::block_in_place;
- }
+cfg_task! {
+ pub use crate::runtime::task::{JoinError, JoinHandle};
}
cfg_rt_core! {
- pub use crate::runtime::task::{JoinError, JoinHandle};
+ mod blocking;
+ pub use blocking::spawn_blocking;
mod spawn;
pub use spawn::spawn;
- mod yield_now;
- pub use yield_now::yield_now;
+ cfg_rt_threaded! {
+ pub use blocking::block_in_place;
+ }
}
cfg_rt_util! {
+ mod yield_now;
+ pub use yield_now::yield_now;
+
mod local;
pub use local::{spawn_local, LocalSet};
diff --git a/tokio/src/task/yield_now.rs b/tokio/src/task/yield_now.rs
index e0e20841..97e2db2c 100644
--- a/tokio/src/task/yield_now.rs
+++ b/tokio/src/task/yield_now.rs
@@ -2,7 +2,7 @@ use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
-doc_rt_core! {
+cfg_rt_util! {
/// Yields execution back to the Tokio runtime.
///
/// A task yields by awaiting on `yield_now()`, and may resume when that