1 #![allow(
2     clippy::cognitive_complexity,
3     clippy::large_enum_variant,
4     clippy::needless_doctest_main
5 )]
6 #![warn(
7     missing_debug_implementations,
8     missing_docs,
9     rust_2018_idioms,
10     unreachable_pub
11 )]
12 #![deny(unused_must_use)]
13 #![cfg_attr(docsrs, deny(rustdoc::broken_intra_doc_links))]
14 #![doc(test(
15     no_crate_inject,
16     attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
17 ))]
18 #![cfg_attr(docsrs, feature(doc_cfg))]
19 #![cfg_attr(docsrs, feature(doc_cfg_hide))]
20 #![cfg_attr(docsrs, doc(cfg_hide(docsrs)))]
21 #![cfg_attr(docsrs, doc(cfg_hide(loom)))]
22 #![cfg_attr(docsrs, doc(cfg_hide(not(loom))))]
23 #![cfg_attr(docsrs, allow(unused_attributes))]
24 
25 //! A runtime for writing reliable network applications without compromising speed.
26 //!
27 //! Tokio is an event-driven, non-blocking I/O platform for writing asynchronous
28 //! applications with the Rust programming language. At a high level, it
29 //! provides a few major components:
30 //!
31 //! * Tools for [working with asynchronous tasks][tasks], including
32 //!   [synchronization primitives and channels][sync] and [timeouts, sleeps, and
33 //!   intervals][time].
34 //! * APIs for [performing asynchronous I/O][io], including [TCP and UDP][net] sockets,
35 //!   [filesystem][fs] operations, and [process] and [signal] management.
36 //! * A [runtime] for executing asynchronous code, including a task scheduler,
37 //!   an I/O driver backed by the operating system's event queue (epoll, kqueue,
38 //!   IOCP, etc...), and a high performance timer.
39 //!
40 //! Guide level documentation is found on the [website].
41 //!
42 //! [tasks]: #working-with-tasks
43 //! [sync]: crate::sync
44 //! [time]: crate::time
45 //! [io]: #asynchronous-io
46 //! [net]: crate::net
47 //! [fs]: crate::fs
48 //! [process]: crate::process
49 //! [signal]: crate::signal
50 //! [fs]: crate::fs
51 //! [runtime]: crate::runtime
52 //! [website]: https://tokio.rs/tokio/tutorial
53 //!
54 //! # A Tour of Tokio
55 //!
56 //! Tokio consists of a number of modules that provide a range of functionality
57 //! essential for implementing asynchronous applications in Rust. In this
58 //! section, we will take a brief tour of Tokio, summarizing the major APIs and
59 //! their uses.
60 //!
61 //! The easiest way to get started is to enable all features. Do this by
62 //! enabling the `full` feature flag:
63 //!
64 //! ```toml
65 //! tokio = { version = "1", features = ["full"] }
66 //! ```
67 //!
68 //! ### Authoring applications
69 //!
70 //! Tokio is great for writing applications and most users in this case shouldn't
71 //! worry too much about what features they should pick. If you're unsure, we suggest
72 //! going with `full` to ensure that you don't run into any road blocks while you're
73 //! building your application.
74 //!
75 //! #### Example
76 //!
77 //! This example shows the quickest way to get started with Tokio.
78 //!
79 //! ```toml
80 //! tokio = { version = "1", features = ["full"] }
81 //! ```
82 //!
83 //! ### Authoring libraries
84 //!
85 //! As a library author your goal should be to provide the lightest weight crate
86 //! that is based on Tokio. To achieve this you should ensure that you only enable
87 //! the features you need. This allows users to pick up your crate without having
88 //! to enable unnecessary features.
89 //!
90 //! #### Example
91 //!
92 //! This example shows how you may want to import features for a library that just
93 //! needs to `tokio::spawn` and use a `TcpStream`.
94 //!
95 //! ```toml
96 //! tokio = { version = "1", features = ["rt", "net"] }
97 //! ```
98 //!
99 //! ## Working With Tasks
100 //!
101 //! Asynchronous programs in Rust are based around lightweight, non-blocking
102 //! units of execution called [_tasks_][tasks]. The [`tokio::task`] module provides
103 //! important tools for working with tasks:
104 //!
105 //! * The [`spawn`] function and [`JoinHandle`] type, for scheduling a new task
106 //!   on the Tokio runtime and awaiting the output of a spawned task, respectively,
107 //! * Functions for [running blocking operations][blocking] in an asynchronous
108 //!   task context.
109 //!
110 //! The [`tokio::task`] module is present only when the "rt" feature flag
111 //! is enabled.
112 //!
113 //! [tasks]: task/index.html#what-are-tasks
114 //! [`tokio::task`]: crate::task
115 //! [`spawn`]: crate::task::spawn()
116 //! [`JoinHandle`]: crate::task::JoinHandle
117 //! [blocking]: task/index.html#blocking-and-yielding
118 //!
119 //! The [`tokio::sync`] module contains synchronization primitives to use when
120 //! needing to communicate or share data. These include:
121 //!
122 //! * channels ([`oneshot`], [`mpsc`], and [`watch`]), for sending values
123 //!   between tasks,
124 //! * a non-blocking [`Mutex`], for controlling access to a shared, mutable
125 //!   value,
126 //! * an asynchronous [`Barrier`] type, for multiple tasks to synchronize before
127 //!   beginning a computation.
128 //!
129 //! The `tokio::sync` module is present only when the "sync" feature flag is
130 //! enabled.
131 //!
132 //! [`tokio::sync`]: crate::sync
133 //! [`Mutex`]: crate::sync::Mutex
134 //! [`Barrier`]: crate::sync::Barrier
135 //! [`oneshot`]: crate::sync::oneshot
136 //! [`mpsc`]: crate::sync::mpsc
137 //! [`watch`]: crate::sync::watch
138 //!
139 //! The [`tokio::time`] module provides utilities for tracking time and
140 //! scheduling work. This includes functions for setting [timeouts][timeout] for
141 //! tasks, [sleeping][sleep] work to run in the future, or [repeating an operation at an
142 //! interval][interval].
143 //!
144 //! In order to use `tokio::time`, the "time" feature flag must be enabled.
145 //!
146 //! [`tokio::time`]: crate::time
147 //! [sleep]: crate::time::sleep()
148 //! [interval]: crate::time::interval()
149 //! [timeout]: crate::time::timeout()
150 //!
151 //! Finally, Tokio provides a _runtime_ for executing asynchronous tasks. Most
152 //! applications can use the [`#[tokio::main]`][main] macro to run their code on the
153 //! Tokio runtime. However, this macro provides only basic configuration options. As
154 //! an alternative, the [`tokio::runtime`] module provides more powerful APIs for configuring
155 //! and managing runtimes. You should use that module if the `#[tokio::main]` macro doesn't
156 //! provide the functionality you need.
157 //!
158 //! Using the runtime requires the "rt" or "rt-multi-thread" feature flags, to
159 //! enable the basic [single-threaded scheduler][rt] and the [thread-pool
160 //! scheduler][rt-multi-thread], respectively. See the [`runtime` module
161 //! documentation][rt-features] for details. In addition, the "macros" feature
162 //! flag enables the `#[tokio::main]` and `#[tokio::test]` attributes.
163 //!
164 //! [main]: attr.main.html
165 //! [`tokio::runtime`]: crate::runtime
166 //! [`Builder`]: crate::runtime::Builder
167 //! [`Runtime`]: crate::runtime::Runtime
168 //! [rt]: runtime/index.html#current-thread-scheduler
169 //! [rt-multi-thread]: runtime/index.html#multi-thread-scheduler
170 //! [rt-features]: runtime/index.html#runtime-scheduler
171 //!
172 //! ## CPU-bound tasks and blocking code
173 //!
174 //! Tokio is able to concurrently run many tasks on a few threads by repeatedly
175 //! swapping the currently running task on each thread. However, this kind of
176 //! swapping can only happen at `.await` points, so code that spends a long time
177 //! without reaching an `.await` will prevent other tasks from running. To
178 //! combat this, Tokio provides two kinds of threads: Core threads and blocking
179 //! threads. The core threads are where all asynchronous code runs, and Tokio
180 //! will by default spawn one for each CPU core. The blocking threads are
181 //! spawned on demand, can be used to run blocking code that would otherwise
182 //! block other tasks from running and are kept alive when not used for a certain
183 //! amount of time which can be configured with [`thread_keep_alive`].
184 //! Since it is not possible for Tokio to swap out blocking tasks, like it
185 //! can do with asynchronous code, the upper limit on the number of blocking
186 //! threads is very large. These limits can be configured on the [`Builder`].
187 //!
188 //! To spawn a blocking task, you should use the [`spawn_blocking`] function.
189 //!
190 //! [`Builder`]: crate::runtime::Builder
191 //! [`spawn_blocking`]: crate::task::spawn_blocking()
192 //! [`thread_keep_alive`]: crate::runtime::Builder::thread_keep_alive()
193 //!
194 //! ```
195 //! #[tokio::main]
196 //! async fn main() {
197 //!     // This is running on a core thread.
198 //!
199 //!     let blocking_task = tokio::task::spawn_blocking(|| {
200 //!         // This is running on a blocking thread.
201 //!         // Blocking here is ok.
202 //!     });
203 //!
204 //!     // We can wait for the blocking task like this:
205 //!     // If the blocking task panics, the unwrap below will propagate the
206 //!     // panic.
207 //!     blocking_task.await.unwrap();
208 //! }
209 //! ```
210 //!
211 //! If your code is CPU-bound and you wish to limit the number of threads used
212 //! to run it, you should use a separate thread pool dedicated to CPU bound tasks.
213 //! For example, you could consider using the [rayon] library for CPU-bound
214 //! tasks. It is also possible to create an extra Tokio runtime dedicated to
215 //! CPU-bound tasks, but if you do this, you should be careful that the extra
216 //! runtime runs _only_ CPU-bound tasks, as IO-bound tasks on that runtime
217 //! will behave poorly.
218 //!
219 //! Hint: If using rayon, you can use a [`oneshot`] channel to send the result back
220 //! to Tokio when the rayon task finishes.
221 //!
222 //! [rayon]: https://docs.rs/rayon
223 //! [`oneshot`]: crate::sync::oneshot
224 //!
225 //! ## Asynchronous IO
226 //!
227 //! As well as scheduling and running tasks, Tokio provides everything you need
228 //! to perform input and output asynchronously.
229 //!
230 //! The [`tokio::io`] module provides Tokio's asynchronous core I/O primitives,
231 //! the [`AsyncRead`], [`AsyncWrite`], and [`AsyncBufRead`] traits. In addition,
232 //! when the "io-util" feature flag is enabled, it also provides combinators and
233 //! functions for working with these traits, forming as an asynchronous
234 //! counterpart to [`std::io`].
235 //!
236 //! Tokio also includes APIs for performing various kinds of I/O and interacting
237 //! with the operating system asynchronously. These include:
238 //!
239 //! * [`tokio::net`], which contains non-blocking versions of [TCP], [UDP], and
240 //!   [Unix Domain Sockets][UDS] (enabled by the "net" feature flag),
241 //! * [`tokio::fs`], similar to [`std::fs`] but for performing filesystem I/O
242 //!   asynchronously (enabled by the "fs" feature flag),
243 //! * [`tokio::signal`], for asynchronously handling Unix and Windows OS signals
244 //!   (enabled by the "signal" feature flag),
245 //! * [`tokio::process`], for spawning and managing child processes (enabled by
246 //!   the "process" feature flag).
247 //!
248 //! [`tokio::io`]: crate::io
249 //! [`AsyncRead`]: crate::io::AsyncRead
250 //! [`AsyncWrite`]: crate::io::AsyncWrite
251 //! [`AsyncBufRead`]: crate::io::AsyncBufRead
252 //! [`std::io`]: std::io
253 //! [`tokio::net`]: crate::net
254 //! [TCP]: crate::net::tcp
255 //! [UDP]: crate::net::UdpSocket
256 //! [UDS]: crate::net::unix
257 //! [`tokio::fs`]: crate::fs
258 //! [`std::fs`]: std::fs
259 //! [`tokio::signal`]: crate::signal
260 //! [`tokio::process`]: crate::process
261 //!
262 //! # Examples
263 //!
264 //! A simple TCP echo server:
265 //!
266 //! ```no_run
267 //! use tokio::net::TcpListener;
268 //! use tokio::io::{AsyncReadExt, AsyncWriteExt};
269 //!
270 //! #[tokio::main]
271 //! async fn main() -> Result<(), Box<dyn std::error::Error>> {
272 //!     let listener = TcpListener::bind("127.0.0.1:8080").await?;
273 //!
274 //!     loop {
275 //!         let (mut socket, _) = listener.accept().await?;
276 //!
277 //!         tokio::spawn(async move {
278 //!             let mut buf = [0; 1024];
279 //!
280 //!             // In a loop, read data from the socket and write the data back.
281 //!             loop {
282 //!                 let n = match socket.read(&mut buf).await {
283 //!                     // socket closed
284 //!                     Ok(n) if n == 0 => return,
285 //!                     Ok(n) => n,
286 //!                     Err(e) => {
287 //!                         eprintln!("failed to read from socket; err = {:?}", e);
288 //!                         return;
289 //!                     }
290 //!                 };
291 //!
292 //!                 // Write the data back
293 //!                 if let Err(e) = socket.write_all(&buf[0..n]).await {
294 //!                     eprintln!("failed to write to socket; err = {:?}", e);
295 //!                     return;
296 //!                 }
297 //!             }
298 //!         });
299 //!     }
300 //! }
301 //! ```
302 //!
303 //! ## Feature flags
304 //!
305 //! Tokio uses a set of [feature flags] to reduce the amount of compiled code. It
306 //! is possible to just enable certain features over others. By default, Tokio
307 //! does not enable any features but allows one to enable a subset for their use
308 //! case. Below is a list of the available feature flags. You may also notice
309 //! above each function, struct and trait there is listed one or more feature flags
310 //! that are required for that item to be used. If you are new to Tokio it is
311 //! recommended that you use the `full` feature flag which will enable all public APIs.
312 //! Beware though that this will pull in many extra dependencies that you may not
313 //! need.
314 //!
315 //! - `full`: Enables all Tokio public API features listed below except `test-util`.
316 //! - `rt`: Enables `tokio::spawn`, the basic (current thread) scheduler,
317 //!         and non-scheduler utilities.
318 //! - `rt-multi-thread`: Enables the heavier, multi-threaded, work-stealing scheduler.
319 //! - `io-util`: Enables the IO based `Ext` traits.
320 //! - `io-std`: Enable `Stdout`, `Stdin` and `Stderr` types.
321 //! - `net`: Enables `tokio::net` types such as `TcpStream`, `UnixStream` and
322 //!          `UdpSocket`, as well as (on Unix-like systems) `AsyncFd` and (on
323 //!          FreeBSD) `PollAio`.
324 //! - `time`: Enables `tokio::time` types and allows the schedulers to enable
325 //!           the built in timer.
326 //! - `process`: Enables `tokio::process` types.
327 //! - `macros`: Enables `#[tokio::main]` and `#[tokio::test]` macros.
328 //! - `sync`: Enables all `tokio::sync` types.
329 //! - `signal`: Enables all `tokio::signal` types.
330 //! - `fs`: Enables `tokio::fs` types.
331 //! - `test-util`: Enables testing based infrastructure for the Tokio runtime.
332 //!
333 //! _Note: `AsyncRead` and `AsyncWrite` traits do not require any features and are
334 //! always available._
335 //!
336 //! ### Internal features
337 //!
338 //! These features do not expose any new API, but influence internal
339 //! implementation aspects of Tokio, and can pull in additional
340 //! dependencies.
341 //!
342 //! - `parking_lot`: As a potential optimization, use the _parking_lot_ crate's
343 //! synchronization primitives internally. MSRV may increase according to the
344 //! _parking_lot_ release in use.
345 //!
346 //! ### Unstable features
347 //!
348 //! These feature flags enable **unstable** features. The public API may break in 1.x
349 //! releases. To enable these features, the `--cfg tokio_unstable` must be passed to
350 //! `rustc` when compiling. This is easiest done using the `RUSTFLAGS` env variable:
351 //! `RUSTFLAGS="--cfg tokio_unstable"`.
352 //!
353 //! - `tracing`: Enables tracing events.
354 //!
355 //! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
356 
357 // Test that pointer width is compatible. This asserts that e.g. usize is at
358 // least 32 bits, which a lot of components in Tokio currently assumes.
359 //
360 // TODO: improve once we have MSRV access to const eval to make more flexible.
361 #[cfg(not(any(
362     target_pointer_width = "32",
363     target_pointer_width = "64",
364     target_pointer_width = "128"
365 )))]
366 compile_error! {
367     "Tokio requires the platform pointer width to be 32, 64, or 128 bits"
368 }
369 
370 // Includes re-exports used by macros.
371 //
372 // This module is not intended to be part of the public API. In general, any
373 // `doc(hidden)` code is not part of Tokio's public and stable API.
374 #[macro_use]
375 #[doc(hidden)]
376 pub mod macros;
377 
378 cfg_fs! {
379     pub mod fs;
380 }
381 
382 mod future;
383 
384 pub mod io;
385 pub mod net;
386 
387 mod loom;
388 mod park;
389 
390 cfg_process! {
391     pub mod process;
392 }
393 
394 #[cfg(any(feature = "net", feature = "fs", feature = "io-std"))]
395 mod blocking;
396 
397 cfg_rt! {
398     pub mod runtime;
399 }
400 
401 pub(crate) mod coop;
402 
403 cfg_signal! {
404     pub mod signal;
405 }
406 
407 cfg_signal_internal! {
408     #[cfg(not(feature = "signal"))]
409     #[allow(dead_code)]
410     #[allow(unreachable_pub)]
411     pub(crate) mod signal;
412 }
413 
414 cfg_sync! {
415     pub mod sync;
416 }
417 cfg_not_sync! {
418     mod sync;
419 }
420 
421 pub mod task;
422 cfg_rt! {
423     pub use task::spawn;
424 }
425 
426 cfg_time! {
427     pub mod time;
428 }
429 
430 mod util;
431 
432 /// Due to the `Stream` trait's inclusion in `std` landing later than Tokio's 1.0
433 /// release, most of the Tokio stream utilities have been moved into the [`tokio-stream`]
434 /// crate.
435 ///
436 /// # Why was `Stream` not included in Tokio 1.0?
437 ///
438 /// Originally, we had planned to ship Tokio 1.0 with a stable `Stream` type
439 /// but unfortunately the [RFC] had not been merged in time for `Stream` to
440 /// reach `std` on a stable compiler in time for the 1.0 release of Tokio. For
441 /// this reason, the team has decided to move all `Stream` based utilities to
442 /// the [`tokio-stream`] crate. While this is not ideal, once `Stream` has made
443 /// it into the standard library and the MSRV period has passed, we will implement
444 /// stream for our different types.
445 ///
446 /// While this may seem unfortunate, not all is lost as you can get much of the
447 /// `Stream` support with `async/await` and `while let` loops. It is also possible
448 /// to create a `impl Stream` from `async fn` using the [`async-stream`] crate.
449 ///
450 /// [`tokio-stream`]: https://docs.rs/tokio-stream
451 /// [`async-stream`]: https://docs.rs/async-stream
452 /// [RFC]: https://github.com/rust-lang/rfcs/pull/2996
453 ///
454 /// # Example
455 ///
456 /// Convert a [`sync::mpsc::Receiver`] to an `impl Stream`.
457 ///
458 /// ```rust,no_run
459 /// use tokio::sync::mpsc;
460 ///
461 /// let (tx, mut rx) = mpsc::channel::<usize>(16);
462 ///
463 /// let stream = async_stream::stream! {
464 ///     while let Some(item) = rx.recv().await {
465 ///         yield item;
466 ///     }
467 /// };
468 /// ```
469 pub mod stream {}
470 
471 // local re-exports of platform specific things, allowing for decent
472 // documentation to be shimmed in on docs.rs
473 
474 #[cfg(docsrs)]
475 pub mod doc;
476 
477 #[cfg(docsrs)]
478 #[allow(unused)]
479 pub(crate) use self::doc::os;
480 
481 #[cfg(not(docsrs))]
482 #[allow(unused)]
483 pub(crate) use std::os;
484 
485 #[cfg(docsrs)]
486 #[allow(unused)]
487 pub(crate) use self::doc::winapi;
488 
489 #[cfg(all(not(docsrs), windows, feature = "net"))]
490 #[allow(unused)]
491 pub(crate) use ::winapi;
492 
493 cfg_macros! {
494     /// Implementation detail of the `select!` macro. This macro is **not**
495     /// intended to be used as part of the public API and is permitted to
496     /// change.
497     #[doc(hidden)]
498     pub use tokio_macros::select_priv_declare_output_enum;
499 
500     /// Implementation detail of the `select!` macro. This macro is **not**
501     /// intended to be used as part of the public API and is permitted to
502     /// change.
503     #[doc(hidden)]
504     pub use tokio_macros::select_priv_clean_pattern;
505 
506     cfg_rt! {
507         #[cfg(feature = "rt-multi-thread")]
508         #[cfg(not(test))] // Work around for rust-lang/rust#62127
509         #[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
510         #[doc(inline)]
511         pub use tokio_macros::main;
512 
513         #[cfg(feature = "rt-multi-thread")]
514         #[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
515         #[doc(inline)]
516         pub use tokio_macros::test;
517 
518         cfg_not_rt_multi_thread! {
519             #[cfg(not(test))] // Work around for rust-lang/rust#62127
520             #[doc(inline)]
521             pub use tokio_macros::main_rt as main;
522 
523             #[doc(inline)]
524             pub use tokio_macros::test_rt as test;
525         }
526     }
527 
528     // Always fail if rt is not enabled.
529     cfg_not_rt! {
530         #[cfg(not(test))]
531         #[doc(inline)]
532         pub use tokio_macros::main_fail as main;
533 
534         #[doc(inline)]
535         pub use tokio_macros::test_fail as test;
536     }
537 }
538 
539 // TODO: rm
540 #[cfg(feature = "io-util")]
541 #[cfg(test)]
is_unpin<T: Unpin>()542 fn is_unpin<T: Unpin>() {}
543