diff options
author | Carl Lerche <me@carllerche.com> | 2019-08-15 15:04:21 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-15 15:04:21 -0700 |
commit | f1f61a3b15d17767b12bfd8c0c5712db1b089b0b (patch) | |
tree | dd9cd51fee4b0b33bcbd079d1e96b4009e8f5fe8 /tokio | |
parent | d0a8e5d6f2921fadc51a9612f6fe558e4213560f (diff) |
net: reorganize crate in anticipation of #1264 (#1453)
Space is made to add `tcp`, `udp`, `uds`, ... modules.
Diffstat (limited to 'tokio')
-rw-r--r-- | tokio/src/lib.rs | 2 | ||||
-rw-r--r-- | tokio/src/reactor.rs | 134 | ||||
-rw-r--r-- | tokio/src/runtime/current_thread/builder.rs | 2 | ||||
-rw-r--r-- | tokio/src/runtime/current_thread/runtime.rs | 8 | ||||
-rw-r--r-- | tokio/src/runtime/threadpool/background.rs | 6 | ||||
-rw-r--r-- | tokio/src/runtime/threadpool/builder.rs | 4 | ||||
-rw-r--r-- | tokio/src/runtime/threadpool/mod.rs | 3 | ||||
-rw-r--r-- | tokio/tests/drop-core.rs | 2 | ||||
-rw-r--r-- | tokio/tests/reactor.rs | 4 |
9 files changed, 15 insertions, 150 deletions
diff --git a/tokio/src/lib.rs b/tokio/src/lib.rs index 81747bae..ec90d5ec 100644 --- a/tokio/src/lib.rs +++ b/tokio/src/lib.rs @@ -88,8 +88,6 @@ pub mod io; #[cfg(any(feature = "tcp", feature = "udp", feature = "uds"))] pub mod net; pub mod prelude; -#[cfg(feature = "tokio-net")] -pub mod reactor; pub mod stream; #[cfg(feature = "sync")] pub mod sync; diff --git a/tokio/src/reactor.rs b/tokio/src/reactor.rs deleted file mode 100644 index 53351e3e..00000000 --- a/tokio/src/reactor.rs +++ /dev/null @@ -1,134 +0,0 @@ -//! Event loop that drives Tokio I/O resources. -//! -//! This module contains [`Reactor`], which is the event loop that drives all -//! Tokio I/O resources. It is the reactor's job to receive events from the -//! operating system ([epoll], [kqueue], [IOCP], etc...) and forward them to -//! waiting tasks. It is the bridge between operating system and the futures -//! model. -//! -//! # Overview -//! -//! When using Tokio, all operations are asynchronous and represented by -//! futures. These futures, representing the application logic, are scheduled by -//! an executor (see [runtime model] for more details). Executors wait for -//! notifications before scheduling the future for execution time, i.e., nothing -//! happens until an event is received indicating that the task can make -//! progress. -//! -//! The reactor receives events from the operating system and notifies the -//! executor. -//! -//! Let's start with a basic example, establishing a TCP connection. -//! -//! ``` -//! #![feature(async_await)] -//! -//! use tokio::net::TcpStream; -//! -//! # async fn process<T>(t: T) {} -//! # async fn dox() -> Result<(), Box<dyn std::error::Error>> { -//! let addr = "93.184.216.34:9243".parse()?; -//! -//! let stream = TcpStream::connect(&addr).await?; -//! -//! println!("successfully connected"); -//! -//! process(stream).await; -//! # Ok(()) -//! # } -//! ``` -//! -//! Establishing a TCP connection usually cannot be completed immediately. -//! [`TcpStream::connect`] does not block the current thread. Instead, it -//! returns a [future][connect-future] that resolves once the TCP connection has -//! been established. The connect future itself has no way of knowing when the -//! TCP connection has been established. -//! -//! Before returning the future, [`TcpStream::connect`] registers the socket -//! with a reactor. This registration process, handled by [`Registration`], is -//! what links the [`TcpStream`] with the [`Reactor`] instance. At this point, -//! the reactor starts listening for connection events from the operating system -//! for that socket. -//! -//! Once the connect future is passed to [`tokio::run`], it is spawned onto a -//! thread pool. The thread pool waits until it is notified that the connection -//! has completed. -//! -//! When the TCP connection is established, the reactor receives an event from -//! the operating system. It then notifies the thread pool, telling it that the -//! connect future can complete. At this point, the thread pool will schedule -//! the task to run on one of its worker threads. This results in the `and_then` -//! closure to get executed. -//! -//! ## Lazy registration -//! -//! Notice how the snippet above does not explicitly reference a reactor. When -//! [`TcpStream::connect`] is called, it registers the socket with a reactor, -//! but no reactor is specified. This works because the registration process -//! mentioned above is actually lazy. It doesn't *actually* happen in the -//! [`connect`] function. Instead, the registration is established the first -//! time that the task is polled (again, see [runtime model]). -//! -//! A reactor instance is automatically made available when using the Tokio -//! [runtime], which is done using [`tokio::run`]. The Tokio runtime's executor -//! sets a thread-local variable referencing the associated [`Reactor`] instance -//! and [`Handle::current`] (used by [`Registration`]) returns the reference. -//! -//! ## Implementation -//! -//! The reactor implementation uses [`mio`] to interface with the operating -//! system's event queue. A call to [`Reactor::poll`] results in a single -//! call to [`Poll::poll`] which in turn results in a single call to the -//! operating system's selector. -//! -//! The reactor maintains state for each registered I/O resource. This tracks -//! the executor task to notify when events are provided by the operating -//! system's selector. This state is stored in a `Sync` data structure and -//! referenced by [`Registration`]. When the [`Registration`] instance is -//! dropped, this state is cleaned up. Because the state is stored in a `Sync` -//! data structure, the [`Registration`] instance is able to be moved to other -//! threads. -//! -//! By default, a runtime's default reactor runs on a background thread. This -//! ensures that application code cannot significantly impact the reactor's -//! responsiveness. -//! -//! ## Integrating with the reactor -//! -//! Tokio comes with a number of I/O resources, like TCP and UDP sockets, that -//! automatically integrate with the reactor. However, library authors or -//! applications may wish to implement their own resources that are also backed -//! by the reactor. -//! -//! There are a couple of ways to do this. -//! -//! If the custom I/O resource implements [`mio::Evented`] and implements -//! [`std::io::Read`] and / or [`std::io::Write`], then [`PollEvented`] is the -//! most suited. -//! -//! Otherwise, [`Registration`] can be used directly. This provides the lowest -//! level primitive needed for integrating with the reactor: a stream of -//! readiness events. -//! -//! [`Reactor`]: struct.Reactor.html -//! [`Registration`]: struct.Registration.html -//! [runtime model]: https://tokio.rs/docs/internals/runtime-model/ -//! [epoll]: http://man7.org/linux/man-pages/man7/epoll.7.html -//! [kqueue]: https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2 -//! [IOCP]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365198(v=vs.85).aspx -//! [`TcpStream::connect`]: ../net/struct.TcpStream.html#method.connect -//! [`connect`]: ../net/struct.TcpStream.html#method.connect -//! [connect-future]: ../net/struct.ConnectFuture.html -//! [`tokio::run`]: ../runtime/fn.run.html -//! [`TcpStream`]: ../net/struct.TcpStream.html -//! [runtime]: ../runtime -//! [`Handle::current`]: struct.Handle.html#method.current -//! [`mio`]: https://github.com/carllerche/mio -//! [`Reactor::poll`]: struct.Reactor.html#method.poll -//! [`Poll::poll`]: https://docs.rs/mio/0.6/mio/struct.Poll.html#method.poll -//! [`mio::Evented`]: https://docs.rs/mio/0.6/mio/trait.Evented.html -//! [`PollEvented`]: struct.PollEvented.html -//! [`std::io::Read`]: https://doc.rust-lang.org/std/io/trait.Read.html -//! [`std::io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html - -pub use tokio_net::{Handle, PollEvented, Reactor, Registration, Turn}; diff --git a/tokio/src/runtime/current_thread/builder.rs b/tokio/src/runtime/current_thread/builder.rs index 7c9cd797..d48136d4 100644 --- a/tokio/src/runtime/current_thread/builder.rs +++ b/tokio/src/runtime/current_thread/builder.rs @@ -1,7 +1,7 @@ use crate::runtime::current_thread::Runtime; use tokio_executor::current_thread::CurrentThread; -use tokio_net::Reactor; +use tokio_net::driver::Reactor; use tokio_timer::clock::Clock; use tokio_timer::timer::Timer; diff --git a/tokio/src/runtime/current_thread/runtime.rs b/tokio/src/runtime/current_thread/runtime.rs index 8482ecad..da3e6770 100644 --- a/tokio/src/runtime/current_thread/runtime.rs +++ b/tokio/src/runtime/current_thread/runtime.rs @@ -2,7 +2,7 @@ use crate::runtime::current_thread::Builder; use tokio_executor::current_thread::Handle as ExecutorHandle; use tokio_executor::current_thread::{self, CurrentThread}; -use tokio_net::{self, Reactor}; +use tokio_net::driver::{self, Reactor}; use tokio_timer::clock::{self, Clock}; use tokio_timer::timer::{self, Timer}; @@ -19,7 +19,7 @@ use std::io; /// [mod]: index.html #[derive(Debug)] pub struct Runtime { - reactor_handle: tokio_net::Handle, + reactor_handle: driver::Handle, timer_handle: timer::Handle, clock: Clock, executor: CurrentThread<Parker>, @@ -93,7 +93,7 @@ impl Runtime { } pub(super) fn new2( - reactor_handle: tokio_net::Handle, + reactor_handle: driver::Handle, timer_handle: timer::Handle, clock: Clock, executor: CurrentThread<Parker>, @@ -197,7 +197,7 @@ impl Runtime { // This will set the default handle and timer to use inside the closure // and run the future. - let _reactor = tokio_net::set_default(&reactor_handle); + let _reactor = driver::set_default(&reactor_handle); clock::with_default(clock, || { let _timer = timer::set_default(&timer_handle); // The TaskExecutor is a fake executor that looks into the diff --git a/tokio/src/runtime/threadpool/background.rs b/tokio/src/runtime/threadpool/background.rs index a65e0250..3d884118 100644 --- a/tokio/src/runtime/threadpool/background.rs +++ b/tokio/src/runtime/threadpool/background.rs @@ -2,7 +2,7 @@ //! `block_on` work. use tokio_executor::current_thread::CurrentThread; -use tokio_net::Reactor; +use tokio_net::driver::{self, Reactor}; use tokio_sync::oneshot; use tokio_timer::clock::Clock; use tokio_timer::timer::{self, Timer}; @@ -11,7 +11,7 @@ use std::{io, thread}; #[derive(Debug)] pub(crate) struct Background { - reactor_handle: tokio_net::Handle, + reactor_handle: driver::Handle, timer_handle: timer::Handle, shutdown_tx: Option<oneshot::Sender<()>>, thread: Option<thread::JoinHandle<()>>, @@ -44,7 +44,7 @@ pub(crate) fn spawn(clock: &Clock) -> io::Result<Background> { } impl Background { - pub(super) fn reactor(&self) -> &tokio_net::Handle { + pub(super) fn reactor(&self) -> &driver::Handle { &self.reactor_handle } diff --git a/tokio/src/runtime/threadpool/builder.rs b/tokio/src/runtime/threadpool/builder.rs index 10beff32..d045c398 100644 --- a/tokio/src/runtime/threadpool/builder.rs +++ b/tokio/src/runtime/threadpool/builder.rs @@ -1,7 +1,7 @@ use super::{background, Inner, Runtime}; -use crate::reactor::Reactor; use tokio_executor::threadpool; +use tokio_net::driver::{self, Reactor}; use tokio_timer::clock::{self, Clock}; use tokio_timer::timer::{self, Timer}; @@ -343,7 +343,7 @@ impl Builder { .around_worker(move |w| { let index = w.id().to_usize(); - let _reactor = tokio_net::set_default(&reactor_handles[index]); + let _reactor = driver::set_default(&reactor_handles[index]); clock::with_default(&clock, || { let _timer = timer::set_default(&timer_handles[index]); trace::dispatcher::with_default(&dispatch, || { diff --git a/tokio/src/runtime/threadpool/mod.rs b/tokio/src/runtime/threadpool/mod.rs index 19bcdf07..b9de101a 100644 --- a/tokio/src/runtime/threadpool/mod.rs +++ b/tokio/src/runtime/threadpool/mod.rs @@ -10,6 +10,7 @@ use background::Background; use tokio_executor::enter; use tokio_executor::threadpool::ThreadPool; +use tokio_net::driver; use tokio_timer::timer; use tracing_core as trace; @@ -174,7 +175,7 @@ impl Runtime { let trace = &self.inner().trace; tokio_executor::with_default(&mut self.inner().pool.sender(), || { - let _reactor = tokio_net::set_default(bg.reactor()); + let _reactor = driver::set_default(bg.reactor()); let _timer = timer::set_default(bg.timer()); trace::dispatcher::with_default(trace, || { entered.block_on(future) diff --git a/tokio/tests/drop-core.rs b/tokio/tests/drop-core.rs index 32fb2444..bb41c12e 100644 --- a/tokio/tests/drop-core.rs +++ b/tokio/tests/drop-core.rs @@ -3,7 +3,7 @@ #![cfg(feature = "default")] use tokio::net::TcpListener; -use tokio::reactor::Reactor; +use tokio_net::driver::Reactor; use tokio_test::{assert_err, assert_pending, assert_ready, task}; #[test] diff --git a/tokio/tests/reactor.rs b/tokio/tests/reactor.rs index 04107ccb..8e93bd48 100644 --- a/tokio/tests/reactor.rs +++ b/tokio/tests/reactor.rs @@ -2,7 +2,7 @@ #![warn(rust_2018_idioms)] #![cfg(feature = "default")] -use tokio_net::Reactor; +use tokio_net::driver::Reactor; use tokio_tcp::TcpListener; use tokio_test::{assert_ok, assert_pending}; @@ -68,7 +68,7 @@ fn test_drop_on_notify() { { let handle = reactor.handle(); - let _reactor = tokio_net::set_default(&handle); + let _reactor = tokio_net::driver::set_default(&handle); let waker = waker_ref(&task); let mut cx = Context::from_waker(&waker); assert_pending!(task.future.lock().unwrap().as_mut().poll(&mut cx)); |