| 1 |
#![allow(unreachable_pub)] |
| 2 |
use crate::{ |
| 3 |
runtime::{Handle, BOX_FUTURE_THRESHOLD}, |
| 4 |
task::{JoinHandle, LocalSet}, |
| 5 |
}; |
| 6 |
use std::{future::Future, io}; |
| 7 |
|
| 8 |
/// Factory which is used to configure the properties of a new task. |
| 9 |
/// |
| 10 |
/// **Note**: This is an [unstable API][unstable]. The public API of this type |
| 11 |
/// may break in 1.x releases. See [the documentation on unstable |
| 12 |
/// features][unstable] for details. |
| 13 |
/// |
| 14 |
/// Methods can be chained in order to configure it. |
| 15 |
/// |
| 16 |
/// Currently, there is only one configuration option: |
| 17 |
/// |
| 18 |
/// - [`name`], which specifies an associated name for |
| 19 |
/// the task |
| 20 |
/// |
| 21 |
/// There are three types of task that can be spawned from a Builder: |
| 22 |
/// - [`spawn_local`] for executing futures on the current thread |
| 23 |
/// - [`spawn`] for executing [`Send`] futures on the runtime |
| 24 |
/// - [`spawn_blocking`] for executing blocking code in the |
| 25 |
/// blocking thread pool. |
| 26 |
/// |
| 27 |
/// ## Example |
| 28 |
/// |
| 29 |
/// ```no_run |
| 30 |
/// use tokio::net::{TcpListener, TcpStream}; |
| 31 |
/// |
| 32 |
/// use std::io; |
| 33 |
/// |
| 34 |
/// async fn process(socket: TcpStream) { |
| 35 |
/// // ... |
| 36 |
/// # drop(socket); |
| 37 |
/// } |
| 38 |
/// |
| 39 |
/// #[tokio::main] |
| 40 |
/// async fn main() -> io::Result<()> { |
| 41 |
/// let listener = TcpListener::bind("127.0.0.1:8080").await?; |
| 42 |
/// |
| 43 |
/// loop { |
| 44 |
/// let (socket, _) = listener.accept().await?; |
| 45 |
/// |
| 46 |
/// tokio::task::Builder::new() |
| 47 |
/// .name("tcp connection handler") |
| 48 |
/// .spawn(async move { |
| 49 |
/// // Process each socket concurrently. |
| 50 |
/// process(socket).await |
| 51 |
/// })?; |
| 52 |
/// } |
| 53 |
/// } |
| 54 |
/// ``` |
| 55 |
/// [unstable]: crate#unstable-features |
| 56 |
/// [`name`]: Builder::name |
| 57 |
/// [`spawn_local`]: Builder::spawn_local |
| 58 |
/// [`spawn`]: Builder::spawn |
| 59 |
/// [`spawn_blocking`]: Builder::spawn_blocking |
| 60 |
#[derive(Default, Debug)] |
| 61 |
#[cfg_attr(docsrs, doc(cfg(all(tokio_unstable, feature = "tracing"))))] |
| 62 |
pub struct Builder<'a> { |
| 63 |
name: Option<&'a str>, |
| 64 |
} |
| 65 |
|
| 66 |
impl<'a> Builder<'a> { |
| 67 |
/// Creates a new task builder. |
| 68 |
pub fn new() -> Self { |
| 69 |
Self::default() |
| 70 |
} |
| 71 |
|
| 72 |
/// Assigns a name to the task which will be spawned. |
| 73 |
pub fn name(&self, name: &'a str) -> Self { |
| 74 |
Self { name: Some(name) } |
| 75 |
} |
| 76 |
|
| 77 |
/// Spawns a task with this builder's settings on the current runtime. |
| 78 |
/// |
| 79 |
/// # Panics |
| 80 |
/// |
| 81 |
/// This method panics if called outside of a Tokio runtime. |
| 82 |
/// |
| 83 |
/// See [`task::spawn`](crate::task::spawn()) for |
| 84 |
/// more details. |
| 85 |
#[track_caller] |
| 86 |
pub fn spawn<Fut>(self, future: Fut) -> io::Result<JoinHandle<Fut::Output>> |
| 87 |
where |
| 88 |
Fut: Future + Send + 'static, |
| 89 |
Fut::Output: Send + 'static, |
| 90 |
{ |
| 91 |
Ok( |
| 92 |
if cfg!(debug_assertions) && std::mem::size_of::<Fut>() > BOX_FUTURE_THRESHOLD { |
| 93 |
super::spawn::spawn_inner(Box::pin(future), self.name) |
| 94 |
} else { |
| 95 |
super::spawn::spawn_inner(future, self.name) |
| 96 |
}, |
| 97 |
) |
| 98 |
} |
| 99 |
|
| 100 |
/// Spawn a task with this builder's settings on the provided [runtime |
| 101 |
/// handle]. |
| 102 |
/// |
| 103 |
/// See [`Handle::spawn`] for more details. |
| 104 |
/// |
| 105 |
/// [runtime handle]: crate::runtime::Handle |
| 106 |
/// [`Handle::spawn`]: crate::runtime::Handle::spawn |
| 107 |
#[track_caller] |
| 108 |
pub fn spawn_on<Fut>(self, future: Fut, handle: &Handle) -> io::Result<JoinHandle<Fut::Output>> |
| 109 |
where |
| 110 |
Fut: Future + Send + 'static, |
| 111 |
Fut::Output: Send + 'static, |
| 112 |
{ |
| 113 |
Ok( |
| 114 |
if cfg!(debug_assertions) && std::mem::size_of::<Fut>() > BOX_FUTURE_THRESHOLD { |
| 115 |
handle.spawn_named(Box::pin(future), self.name) |
| 116 |
} else { |
| 117 |
handle.spawn_named(future, self.name) |
| 118 |
}, |
| 119 |
) |
| 120 |
} |
| 121 |
|
| 122 |
/// Spawns `!Send` a task on the current [`LocalSet`] with this builder's |
| 123 |
/// settings. |
| 124 |
/// |
| 125 |
/// The spawned future will be run on the same thread that called `spawn_local`. |
| 126 |
/// This may only be called from the context of a [local task set][`LocalSet`]. |
| 127 |
/// |
| 128 |
/// # Panics |
| 129 |
/// |
| 130 |
/// This function panics if called outside of a [local task set][`LocalSet`]. |
| 131 |
/// |
| 132 |
/// See [`task::spawn_local`] for more details. |
| 133 |
/// |
| 134 |
/// [`task::spawn_local`]: crate::task::spawn_local |
| 135 |
/// [`LocalSet`]: crate::task::LocalSet |
| 136 |
#[track_caller] |
| 137 |
pub fn spawn_local<Fut>(self, future: Fut) -> io::Result<JoinHandle<Fut::Output>> |
| 138 |
where |
| 139 |
Fut: Future + 'static, |
| 140 |
Fut::Output: 'static, |
| 141 |
{ |
| 142 |
Ok( |
| 143 |
if cfg!(debug_assertions) && std::mem::size_of::<Fut>() > BOX_FUTURE_THRESHOLD { |
| 144 |
super::local::spawn_local_inner(Box::pin(future), self.name) |
| 145 |
} else { |
| 146 |
super::local::spawn_local_inner(future, self.name) |
| 147 |
}, |
| 148 |
) |
| 149 |
} |
| 150 |
|
| 151 |
/// Spawns `!Send` a task on the provided [`LocalSet`] with this builder's |
| 152 |
/// settings. |
| 153 |
/// |
| 154 |
/// See [`LocalSet::spawn_local`] for more details. |
| 155 |
/// |
| 156 |
/// [`LocalSet::spawn_local`]: crate::task::LocalSet::spawn_local |
| 157 |
/// [`LocalSet`]: crate::task::LocalSet |
| 158 |
#[track_caller] |
| 159 |
pub fn spawn_local_on<Fut>( |
| 160 |
self, |
| 161 |
future: Fut, |
| 162 |
local_set: &LocalSet, |
| 163 |
) -> io::Result<JoinHandle<Fut::Output>> |
| 164 |
where |
| 165 |
Fut: Future + 'static, |
| 166 |
Fut::Output: 'static, |
| 167 |
{ |
| 168 |
Ok(local_set.spawn_named(future, self.name)) |
| 169 |
} |
| 170 |
|
| 171 |
/// Spawns blocking code on the blocking threadpool. |
| 172 |
/// |
| 173 |
/// # Panics |
| 174 |
/// |
| 175 |
/// This method panics if called outside of a Tokio runtime. |
| 176 |
/// |
| 177 |
/// See [`task::spawn_blocking`](crate::task::spawn_blocking) |
| 178 |
/// for more details. |
| 179 |
#[track_caller] |
| 180 |
pub fn spawn_blocking<Function, Output>( |
| 181 |
self, |
| 182 |
function: Function, |
| 183 |
) -> io::Result<JoinHandle<Output>> |
| 184 |
where |
| 185 |
Function: FnOnce() -> Output + Send + 'static, |
| 186 |
Output: Send + 'static, |
| 187 |
{ |
| 188 |
let handle = Handle::current(); |
| 189 |
self.spawn_blocking_on(function, &handle) |
| 190 |
} |
| 191 |
|
| 192 |
/// Spawns blocking code on the provided [runtime handle]'s blocking threadpool. |
| 193 |
/// |
| 194 |
/// See [`Handle::spawn_blocking`] for more details. |
| 195 |
/// |
| 196 |
/// [runtime handle]: crate::runtime::Handle |
| 197 |
/// [`Handle::spawn_blocking`]: crate::runtime::Handle::spawn_blocking |
| 198 |
#[track_caller] |
| 199 |
pub fn spawn_blocking_on<Function, Output>( |
| 200 |
self, |
| 201 |
function: Function, |
| 202 |
handle: &Handle, |
| 203 |
) -> io::Result<JoinHandle<Output>> |
| 204 |
where |
| 205 |
Function: FnOnce() -> Output + Send + 'static, |
| 206 |
Output: Send + 'static, |
| 207 |
{ |
| 208 |
use crate::runtime::Mandatory; |
| 209 |
let (join_handle, spawn_result) = |
| 210 |
if cfg!(debug_assertions) && std::mem::size_of::<Function>() > BOX_FUTURE_THRESHOLD { |
| 211 |
handle.inner.blocking_spawner().spawn_blocking_inner( |
| 212 |
Box::new(function), |
| 213 |
Mandatory::NonMandatory, |
| 214 |
self.name, |
| 215 |
handle, |
| 216 |
) |
| 217 |
} else { |
| 218 |
handle.inner.blocking_spawner().spawn_blocking_inner( |
| 219 |
function, |
| 220 |
Mandatory::NonMandatory, |
| 221 |
self.name, |
| 222 |
handle, |
| 223 |
) |
| 224 |
}; |
| 225 |
|
| 226 |
spawn_result?; |
| 227 |
Ok(join_handle) |
| 228 |
} |
| 229 |
} |