summaryrefslogtreecommitdiffstats
path: root/tokio/src/task
diff options
context:
space:
mode:
Diffstat (limited to 'tokio/src/task')
-rw-r--r--tokio/src/task/blocking.rs113
-rw-r--r--tokio/src/task/mod.rs52
-rw-r--r--tokio/src/task/raw.rs19
-rw-r--r--tokio/src/task/state.rs2
4 files changed, 98 insertions, 88 deletions
diff --git a/tokio/src/task/blocking.rs b/tokio/src/task/blocking.rs
index e0c12d4a..4445bb72 100644
--- a/tokio/src/task/blocking.rs
+++ b/tokio/src/task/blocking.rs
@@ -1,62 +1,65 @@
use crate::blocking;
use crate::task::JoinHandle;
-/// Run the provided blocking function without blocking the executor.
-///
-/// 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. If you run a closure through this method,
-/// the current executor thread will relegate all its executor duties to another
-/// (possibly new) thread, and only then poll the task. Note that this requires
-/// additional synchronization.
-///
-/// # Examples
-///
-/// ```
-/// use tokio::task;
-///
-/// # async fn docs() {
-/// task::block_in_place(move || {
-/// // do some compute-heavy work or call synchronous code
-/// });
-/// # }
-/// ```
-#[cfg(feature = "rt-full")]
-pub fn block_in_place<F, R>(f: F) -> R
-where
- F: FnOnce() -> R,
-{
- use crate::runtime::{enter, thread_pool};
+cfg_rt_threaded! {
+ /// Run the provided blocking function without blocking the executor.
+ ///
+ /// 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. If you run a closure through this method,
+ /// the current executor thread will relegate all its executor duties to another
+ /// (possibly new) thread, and only then poll the task. Note that this requires
+ /// additional synchronization.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use tokio::task;
+ ///
+ /// # async fn docs() {
+ /// task::block_in_place(move || {
+ /// // do some compute-heavy work or call synchronous code
+ /// });
+ /// # }
+ /// ```
+ pub fn block_in_place<F, R>(f: F) -> R
+ where
+ F: FnOnce() -> R,
+ {
+ use crate::runtime::{enter, thread_pool};
- enter::exit(|| thread_pool::block_in_place(f))
+ enter::exit(|| thread_pool::block_in_place(f))
+ }
}
-/// Run 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.
-/// A closure that is run through this method will instead be run on a dedicated thread pool for
-/// such blocking tasks without holding up the main futures executor.
-///
-/// # 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,
-{
- blocking::spawn_blocking(f)
+cfg_blocking! {
+ /// Run 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.
+ /// A closure that is run through this method will instead be run on a dedicated thread pool for
+ /// such blocking tasks without holding up the main futures executor.
+ ///
+ /// # 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,
+ {
+ blocking::spawn_blocking(f)
+ }
}
diff --git a/tokio/src/task/mod.rs b/tokio/src/task/mod.rs
index d2025330..7b27ff1b 100644
--- a/tokio/src/task/mod.rs
+++ b/tokio/src/task/mod.rs
@@ -1,11 +1,13 @@
//! Asynchronous green-threads.
-#[cfg(feature = "blocking")]
-mod blocking;
-#[cfg(feature = "rt-full")]
-pub use blocking::block_in_place;
-#[cfg(feature = "blocking")]
-pub use blocking::spawn_blocking;
+cfg_blocking! {
+ mod blocking;
+ pub use blocking::spawn_blocking;
+
+ cfg_rt_threaded! {
+ pub use blocking::block_in_place;
+ }
+}
mod core;
use self::core::Cell;
@@ -17,10 +19,11 @@ pub use self::error::JoinError;
mod harness;
use self::harness::Harness;
-mod join;
-#[cfg(feature = "rt-core")]
-#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
-pub use self::join::JoinHandle;
+cfg_rt_core! {
+ mod join;
+ #[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
+ pub use self::join::JoinHandle;
+}
mod list;
pub(crate) use self::list::OwnedList;
@@ -28,10 +31,10 @@ pub(crate) use self::list::OwnedList;
mod raw;
use self::raw::RawTask;
-#[cfg(feature = "rt-core")]
-mod spawn;
-#[cfg(feature = "rt-core")]
-pub use spawn::spawn;
+cfg_rt_core! {
+ mod spawn;
+ pub use spawn::spawn;
+}
mod stack;
pub(crate) use self::stack::TransferStack;
@@ -81,16 +84,17 @@ pub(crate) trait Schedule: Send + Sync + Sized + 'static {
fn schedule(&self, task: Task<Self>);
}
-/// Create a new task without an associated join handle
-#[cfg(feature = "rt-full")]
-pub(crate) fn background<T, S>(task: T) -> Task<S>
-where
- T: Future + Send + 'static,
- S: Schedule,
-{
- Task {
- raw: RawTask::new_background::<_, S>(task),
- _p: PhantomData,
+cfg_rt_threaded! {
+ /// Create a new task without an associated join handle
+ pub(crate) fn background<T, S>(task: T) -> Task<S>
+ where
+ T: Future + Send + 'static,
+ S: Schedule,
+ {
+ Task {
+ raw: RawTask::new_background::<_, S>(task),
+ _p: PhantomData,
+ }
}
}
diff --git a/tokio/src/task/raw.rs b/tokio/src/task/raw.rs
index 899e5aa3..c1879344 100644
--- a/tokio/src/task/raw.rs
+++ b/tokio/src/task/raw.rs
@@ -54,16 +54,19 @@ pub(super) fn vtable<T: Future, S: Schedule>() -> &'static Vtable {
}
}
-impl RawTask {
- #[cfg(feature = "rt-full")]
- pub(super) fn new_background<T, S>(task: T) -> RawTask
- where
- T: Future + Send + 'static,
- S: Schedule,
- {
- RawTask::new::<_, S>(task, State::new_background())
+cfg_rt_threaded! {
+ impl RawTask {
+ pub(super) fn new_background<T, S>(task: T) -> RawTask
+ where
+ T: Future + Send + 'static,
+ S: Schedule,
+ {
+ RawTask::new::<_, S>(task, State::new_background())
+ }
}
+}
+impl RawTask {
pub(super) fn new_joinable<T, S>(task: T) -> RawTask
where
T: Future + Send + 'static,
diff --git a/tokio/src/task/state.rs b/tokio/src/task/state.rs
index e10f0601..a9284ac9 100644
--- a/tokio/src/task/state.rs
+++ b/tokio/src/task/state.rs
@@ -58,7 +58,7 @@ const INITIAL_STATE: usize = NOTIFIED;
/// unambiguous modification order.
impl State {
/// Starts with a ref count of 1
- #[cfg(feature = "rt-full")]
+ #[cfg(feature = "rt-threaded")]
pub(super) fn new_background() -> State {
State {
val: AtomicUsize::new(INITIAL_STATE),