diff --git a/tokio/src/io/poll_evented.rs b/tokio/src/io/poll_evented.rs index 785968f43f8..9054c3b8733 100644 --- a/tokio/src/io/poll_evented.rs +++ b/tokio/src/io/poll_evented.rs @@ -173,7 +173,7 @@ where /// /// The runtime is usually set implicitly when this function is called /// from a future driven by a tokio runtime, otherwise runtime can be set - /// explicitly with [`Handle::enter`](crate::runtime::Handle::enter) function. + /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. pub fn new(io: E) -> io::Result { PollEvented::new_with_ready(io, mio::Ready::all()) } @@ -201,7 +201,7 @@ where /// /// The runtime is usually set implicitly when this function is called /// from a future driven by a tokio runtime, otherwise runtime can be set - /// explicitly with [`Handle::enter`](crate::runtime::Handle::enter) function. + /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. pub fn new_with_ready(io: E, ready: mio::Ready) -> io::Result { let registration = Registration::new_with_ready(&io, ready)?; Ok(Self { diff --git a/tokio/src/io/registration.rs b/tokio/src/io/registration.rs index 63aaff56d86..8206507280d 100644 --- a/tokio/src/io/registration.rs +++ b/tokio/src/io/registration.rs @@ -67,7 +67,7 @@ impl Registration { /// /// The runtime is usually set implicitly when this function is called /// from a future driven by a tokio runtime, otherwise runtime can be set - /// explicitly with [`Handle::enter`](crate::runtime::Handle::enter) function. + /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. pub fn new(io: &T) -> io::Result where T: Evented, @@ -104,7 +104,7 @@ impl Registration { /// /// The runtime is usually set implicitly when this function is called /// from a future driven by a tokio runtime, otherwise runtime can be set - /// explicitly with [`Handle::enter`](crate::runtime::Handle::enter) function. + /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. pub fn new_with_ready(io: &T, ready: mio::Ready) -> io::Result where T: Evented, diff --git a/tokio/src/net/tcp/listener.rs b/tokio/src/net/tcp/listener.rs index fd79b259b92..44945e387b4 100644 --- a/tokio/src/net/tcp/listener.rs +++ b/tokio/src/net/tcp/listener.rs @@ -262,7 +262,7 @@ impl TcpListener { /// /// The runtime is usually set implicitly when this function is called /// from a future driven by a tokio runtime, otherwise runtime can be set - /// explicitly with [`Handle::enter`](crate::runtime::Handle::enter) function. + /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. pub fn from_std(listener: net::TcpListener) -> io::Result { let io = mio::net::TcpListener::from_std(listener)?; let io = PollEvented::new(io)?; diff --git a/tokio/src/net/tcp/stream.rs b/tokio/src/net/tcp/stream.rs index e624fb9d954..e0348724cff 100644 --- a/tokio/src/net/tcp/stream.rs +++ b/tokio/src/net/tcp/stream.rs @@ -187,7 +187,7 @@ impl TcpStream { /// /// The runtime is usually set implicitly when this function is called /// from a future driven by a tokio runtime, otherwise runtime can be set - /// explicitly with [`Handle::enter`](crate::runtime::Handle::enter) function. + /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. pub fn from_std(stream: net::TcpStream) -> io::Result { let io = mio::net::TcpStream::from_stream(stream)?; let io = PollEvented::new(io)?; diff --git a/tokio/src/net/udp/socket.rs b/tokio/src/net/udp/socket.rs index 16e537739d4..f9d88372035 100644 --- a/tokio/src/net/udp/socket.rs +++ b/tokio/src/net/udp/socket.rs @@ -64,7 +64,7 @@ impl UdpSocket { /// /// The runtime is usually set implicitly when this function is called /// from a future driven by a tokio runtime, otherwise runtime can be set - /// explicitly with [`Handle::enter`](crate::runtime::Handle::enter) function. + /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. pub fn from_std(socket: net::UdpSocket) -> io::Result { let io = mio::net::UdpSocket::from_socket(socket)?; let io = PollEvented::new(io)?; diff --git a/tokio/src/net/unix/datagram/socket.rs b/tokio/src/net/unix/datagram/socket.rs index 2282f96a3f6..ba3a10c4882 100644 --- a/tokio/src/net/unix/datagram/socket.rs +++ b/tokio/src/net/unix/datagram/socket.rs @@ -164,7 +164,7 @@ impl UnixDatagram { /// /// The runtime is usually set implicitly when this function is called /// from a future driven by a Tokio runtime, otherwise runtime can be set - /// explicitly with [`Handle::enter`](crate::runtime::Handle::enter) function. + /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. /// # Examples /// ``` /// # use std::error::Error; diff --git a/tokio/src/net/unix/listener.rs b/tokio/src/net/unix/listener.rs index 9b76cb01fd7..119dc6fb41c 100644 --- a/tokio/src/net/unix/listener.rs +++ b/tokio/src/net/unix/listener.rs @@ -60,7 +60,7 @@ impl UnixListener { /// /// The runtime is usually set implicitly when this function is called /// from a future driven by a tokio runtime, otherwise runtime can be set - /// explicitly with [`Handle::enter`](crate::runtime::Handle::enter) function. + /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. pub fn bind

(path: P) -> io::Result where P: AsRef, @@ -82,7 +82,7 @@ impl UnixListener { /// /// The runtime is usually set implicitly when this function is called /// from a future driven by a tokio runtime, otherwise runtime can be set - /// explicitly with [`Handle::enter`](crate::runtime::Handle::enter) function. + /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. pub fn from_std(listener: net::UnixListener) -> io::Result { let listener = mio_uds::UnixListener::from_listener(listener)?; let io = PollEvented::new(listener)?; diff --git a/tokio/src/net/unix/stream.rs b/tokio/src/net/unix/stream.rs index 559fe02a625..6f49849aef3 100644 --- a/tokio/src/net/unix/stream.rs +++ b/tokio/src/net/unix/stream.rs @@ -54,7 +54,7 @@ impl UnixStream { /// /// The runtime is usually set implicitly when this function is called /// from a future driven by a tokio runtime, otherwise runtime can be set - /// explicitly with [`Handle::enter`](crate::runtime::Handle::enter) function. + /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. pub fn from_std(stream: net::UnixStream) -> io::Result { let stream = mio_uds::UnixStream::from_stream(stream)?; let io = PollEvented::new(stream)?; diff --git a/tokio/src/runtime/blocking/pool.rs b/tokio/src/runtime/blocking/pool.rs index 5ad5f5f8421..47895fcf477 100644 --- a/tokio/src/runtime/blocking/pool.rs +++ b/tokio/src/runtime/blocking/pool.rs @@ -6,6 +6,7 @@ use crate::runtime::blocking::schedule::NoopSchedule; use crate::runtime::blocking::shutdown; use crate::runtime::blocking::task::BlockingTask; use crate::runtime::builder::ThreadNameFn; +use crate::runtime::context; use crate::runtime::task::{self, JoinHandle}; use crate::runtime::{Builder, Callback, Handle}; @@ -67,7 +68,7 @@ pub(crate) fn spawn_blocking(func: F) -> JoinHandle where F: FnOnce() -> R + Send + 'static, { - let rt = Handle::current(); + let rt = context::current().expect("not currently running on the Tokio runtime."); let (task, handle) = task::joinable(BlockingTask::new(func)); let _ = rt.blocking_spawner.spawn(task, &rt); @@ -79,7 +80,7 @@ pub(crate) fn try_spawn_blocking(func: F) -> Result<(), ()> where F: FnOnce() -> R + Send + 'static, { - let rt = Handle::current(); + let rt = context::current().expect("not currently running on the Tokio runtime."); let (task, _handle) = task::joinable(BlockingTask::new(func)); rt.blocking_spawner.spawn(task, &rt) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index d6e2fe2f0af..1b52a14970e 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -67,7 +67,7 @@ pub struct Builder { pub(super) before_stop: Option, } -pub(crate) type ThreadNameFn = Arc String + Send + Sync + 'static>; +pub(crate) type ThreadNameFn = std::sync::Arc String + Send + Sync + 'static>; #[derive(Debug, Clone, Copy)] enum Kind { @@ -100,7 +100,7 @@ impl Builder { max_threads: 512, // Default thread name - thread_name: Arc::new(|| "tokio-runtime-worker".into()), + thread_name: std::sync::Arc::new(|| "tokio-runtime-worker".into()), // Do not set a stack size by default thread_stack_size: None, @@ -212,7 +212,7 @@ impl Builder { /// ``` pub fn thread_name(&mut self, val: impl Into) -> &mut Self { let val = val.into(); - self.thread_name = Arc::new(move || val.clone()); + self.thread_name = std::sync::Arc::new(move || val.clone()); self } @@ -240,7 +240,7 @@ impl Builder { where F: Fn() -> String + Send + Sync + 'static, { - self.thread_name = Arc::new(f); + self.thread_name = std::sync::Arc::new(f); self } diff --git a/tokio/src/runtime/context.rs b/tokio/src/runtime/context.rs index 1b267f481e2..c42b3432dea 100644 --- a/tokio/src/runtime/context.rs +++ b/tokio/src/runtime/context.rs @@ -7,8 +7,10 @@ thread_local! { static CONTEXT: RefCell> = RefCell::new(None) } -pub(crate) fn current() -> Option { - CONTEXT.with(|ctx| ctx.borrow().clone()) +cfg_blocking_impl! { + pub(crate) fn current() -> Option { + CONTEXT.with(|ctx| ctx.borrow().clone()) + } } cfg_io_driver! { diff --git a/tokio/src/runtime/handle.rs b/tokio/src/runtime/handle.rs index 837fd72edc1..516ad4b3aad 100644 --- a/tokio/src/runtime/handle.rs +++ b/tokio/src/runtime/handle.rs @@ -33,15 +33,4 @@ impl Handle { { context::enter(self.clone(), f) } - - /// Returns a `Handle` view over the currently running `Runtime` - /// - /// # Panic - /// - /// This will panic if called outside the context of a Tokio runtime. That means that you must - /// call this on one of the threads **being run by the runtime**. Calling this from within a - /// thread created by `std::thread::spawn` (for example) will cause a panic. - pub(crate) fn current() -> Self { - context::current().expect("not currently running on the Tokio runtime.") - } } diff --git a/tokio/src/runtime/mod.rs b/tokio/src/runtime/mod.rs index 7d4e41b8665..9d430cfaf43 100644 --- a/tokio/src/runtime/mod.rs +++ b/tokio/src/runtime/mod.rs @@ -409,10 +409,10 @@ impl Runtime { /// complete, and yielding its resolved result. Any tasks or timers which /// the future spawns internally will be executed on the runtime. /// - /// `&mut` is required as calling `block_on` **may** result in advancing the - /// state of the runtime. The details depend on how the runtime is - /// configured. [`runtime::Handle::block_on`][handle] provides a version - /// that takes `&self`. + /// When this runtime is configured with `core_threads = 0` only the first call + /// to `block_on` will run the io/timer driver. All other calls _before_ the first + /// `block_on` completes will just hook into that driver. This means the driver + /// may be passed around from thread to thread manually by the user. /// /// This method may not be called from an asynchronous context. /// @@ -477,11 +477,8 @@ impl Runtime { /// have an executor available on creation such as [`Delay`] or [`TcpStream`]. /// It will also allow you to call methods such as [`tokio::spawn`]. /// - /// This function is also available as [`Handle::enter`]. - /// /// [`Delay`]: struct@crate::time::Delay /// [`TcpStream`]: struct@crate::net::TcpStream - /// [`Handle::enter`]: fn@crate::runtime::Handle::enter /// [`tokio::spawn`]: fn@crate::spawn /// /// # Example diff --git a/tokio/src/task/spawn.rs b/tokio/src/task/spawn.rs index d6e771184f2..280e90ead04 100644 --- a/tokio/src/task/spawn.rs +++ b/tokio/src/task/spawn.rs @@ -18,7 +18,7 @@ doc_rt_core! { /// /// This function must be called from the context of a Tokio runtime. Tasks running on /// the Tokio runtime are always inside its context, but you can also enter the context - /// using the [`Handle::enter`](crate::runtime::Handle::enter()) method. + /// using the [`Runtime::enter`](crate::runtime::Runtime::enter()) method. /// /// # Examples ///