summaryrefslogtreecommitdiffstats
path: root/tokio/src/runtime/mod.rs
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2019-11-21 23:28:39 -0800
committerGitHub <noreply@github.com>2019-11-21 23:28:39 -0800
commit8546ff826db8dba1e39b4119ad909fb6cab2492a (patch)
tree0c1cdd36aaf9d732079a4ff7a71e5c6b138e7d42 /tokio/src/runtime/mod.rs
parent6866fe426cfab0e4da3e88c673f7bef141259bb6 (diff)
runtime: cleanup and add config options (#1807)
* runtime: cleanup and add config options This patch finishes the cleanup as part of the transition to Tokio 0.2. A number of changes were made to take advantage of having all Tokio types in a single crate. Also, fixes using Tokio types from `spawn_blocking`. * Many threads, one resource driver Previously, in the threaded scheduler, a resource driver (mio::Poll / timer combo) was created per thread. This was more or less fine, except it required balancing across the available drivers. When using a resource driver from **outside** of the thread pool, balancing is tricky. The change was original done to avoid having a dedicated driver thread. Now, instead of creating many resource drivers, a single resource driver is used. Each scheduler thread will attempt to "lock" the resource driver before parking on it. If the resource driver is already locked, the thread uses a condition variable to park. Contention should remain low as, under load, the scheduler avoids using the drivers. * Add configuration options to enable I/O / time New configuration options are added to `runtime::Builder` to allow enabling I/O and time drivers on a runtime instance basis. This is useful when wanting to create lightweight runtime instances to execute compute only tasks. * Bug fixes The condition variable parker is updated to the same algorithm used in `std`. This is motivated by some potential deadlock cases discovered by `loom`. The basic scheduler is fixed to fairly schedule tasks. `push_front` was accidentally used instead of `push_back`. I/O, time, and spawning now work from within `spawn_blocking` closures. * Misc cleanup The threaded scheduler is no longer generic over `P :Park`. Instead, it is hard coded to a specific parker. Tests, including loom tests, are updated to use `Runtime` directly. This provides greater coverage. The `blocking` module is moved back into `runtime` as all usage is within `runtime` itself.
Diffstat (limited to 'tokio/src/runtime/mod.rs')
-rw-r--r--tokio/src/runtime/mod.rs33
1 files changed, 28 insertions, 5 deletions
diff --git a/tokio/src/runtime/mod.rs b/tokio/src/runtime/mod.rs
index eef4872e..7225ea8d 100644
--- a/tokio/src/runtime/mod.rs
+++ b/tokio/src/runtime/mod.rs
@@ -153,6 +153,14 @@
//! Most applications should use the threaded scheduler, except in some niche
//! use-cases, such as when running only a single thread is required.
//!
+//! #### Resource drivers
+//!
+//! When configuring a runtime by hand, no resource drivers are enabled by
+//! default. In this case, attempting to use networking types or time types will
+//! fail. In order to enable these types, the resource drivers must be enabled.
+//! This is done with [`Builder::enable_io`] and [`Builder::enable_time`]. As a
+//! shorthand, [`Builder::enable_all`] enables both resource drivers.
+//!
//! [tasks]: crate::task
//! [driver]: crate::io::driver
//! [executor]: https://tokio.rs/docs/internals/runtime-model/#executors
@@ -165,6 +173,9 @@
//! [`Runtime::new`]: crate::runtime::Runtime::new
//! [`Builder::basic_scheduler`]: crate::runtime::Builder::basic_scheduler
//! [`Builder::threaded_scheduler`]: crate::runtime::Builder::threaded_scheduler
+//! [`Builder::enable_io`]: crate::runtime::Builder::enable_io
+//! [`Builder::enable_time`]: crate::runtime::Builder::enable_time
+//! [`Builder::enable_all`]: crate::runtime::Builder::enable_all
// At the top due to macros
#[cfg(test)]
@@ -179,6 +190,10 @@ cfg_rt_core! {
mod blocking;
use blocking::BlockingPool;
+cfg_blocking_impl! {
+ pub(crate) use blocking::spawn_blocking;
+}
+
mod builder;
pub use self::builder::Builder;
@@ -195,12 +210,17 @@ pub use self::handle::Handle;
mod io;
-mod park;
-pub use self::park::{Park, Unpark};
+cfg_rt_threaded! {
+ mod park;
+ use park::{Parker, Unparker};
+}
mod shell;
use self::shell::Shell;
+mod spawner;
+use self::spawner::Spawner;
+
mod time;
cfg_rt_threaded! {
@@ -271,6 +291,9 @@ enum Kind {
ThreadPool(ThreadPool),
}
+/// After thread starts / before thread stops
+type Callback = ::std::sync::Arc<dyn Fn() + Send + Sync>;
+
impl Runtime {
/// Create a new runtime instance with default configuration values.
///
@@ -309,13 +332,13 @@ impl Runtime {
/// [runtime builder]: crate::runtime::Builder
pub fn new() -> io::Result<Self> {
#[cfg(feature = "rt-threaded")]
- let ret = Builder::new().threaded_scheduler().build();
+ let ret = Builder::new().threaded_scheduler().enable_all().build();
#[cfg(all(not(feature = "rt-threaded"), feature = "rt-core"))]
- let ret = Builder::new().basic_scheduler().build();
+ let ret = Builder::new().basic_scheduler().enable_all().build();
#[cfg(not(feature = "rt-core"))]
- let ret = Builder::new().build();
+ let ret = Builder::new().enable_all().build();
ret
}