summaryrefslogtreecommitdiffstats
path: root/tokio-net
diff options
context:
space:
mode:
authorIvan Petkov <ivanppetkov@gmail.com>2019-08-18 14:22:09 -0700
committerGitHub <noreply@github.com>2019-08-18 14:22:09 -0700
commit08b07afbd9beb8d92c7aeb0cf07e56d065a73726 (patch)
tree002036c50ef256b6d80cdcff6d7092d16f8ffefd /tokio-net
parent7b0c60849cefa9e73b0fec9c16da2f7dc5edb082 (diff)
signal: remove `new()` constructors in favor of free functions (#1472)
* Also removed any `*_with_handle` related methods in favor of always using the default reactor
Diffstat (limited to 'tokio-net')
-rw-r--r--tokio-net/src/signal/ctrl_c.rs27
-rw-r--r--tokio-net/src/signal/mod.rs12
-rw-r--r--tokio-net/src/signal/unix.rs109
-rw-r--r--tokio-net/src/signal/windows.rs49
-rw-r--r--tokio-net/tests/signal_drop_multi_loop.rs4
-rw-r--r--tokio-net/tests/signal_drop_then_get_a_signal.rs8
-rw-r--r--tokio-net/tests/signal_dropping_does_not_deregister_other_instances.rs10
-rw-r--r--tokio-net/tests/signal_multi_loop.rs2
-rw-r--r--tokio-net/tests/signal_notify_both.rs4
-rw-r--r--tokio-net/tests/signal_simple.rs6
-rw-r--r--tokio-net/tests/signal_support/mod.rs2
-rw-r--r--tokio-net/tests/signal_twice.rs6
12 files changed, 90 insertions, 149 deletions
diff --git a/tokio-net/src/signal/ctrl_c.rs b/tokio-net/src/signal/ctrl_c.rs
index c1fb54b7..f9dd4679 100644
--- a/tokio-net/src/signal/ctrl_c.rs
+++ b/tokio-net/src/signal/ctrl_c.rs
@@ -1,8 +1,7 @@
#[cfg(unix)]
-use super::unix::Signal as Inner;
+use super::unix::{self as os_impl, Signal as Inner};
#[cfg(windows)]
-use super::windows::Event as Inner;
-use crate::driver::Handle;
+use super::windows::{self as os_impl, Event as Inner};
use futures_core::stream::Stream;
use std::io;
@@ -30,22 +29,12 @@ pub struct CtrlC {
inner: Inner,
}
-impl CtrlC {
- /// Creates a new stream which receives "ctrl-c" notifications sent to the
- /// process.
- ///
- /// This function binds to the default reactor.
- pub fn new() -> io::Result<Self> {
- Self::with_handle(&Handle::default())
- }
-
- /// Creates a new stream which receives "ctrl-c" notifications sent to the
- /// process.
- ///
- /// This function binds to reactor specified by `handle`.
- pub fn with_handle(handle: &Handle) -> io::Result<Self> {
- Inner::ctrl_c(handle).map(|inner| Self { inner })
- }
+/// Creates a new stream which receives "ctrl-c" notifications sent to the
+/// process.
+///
+/// This function binds to the default reactor.
+pub fn ctrl_c() -> io::Result<CtrlC> {
+ os_impl::ctrl_c().map(|inner| CtrlC { inner })
}
impl Stream for CtrlC {
diff --git a/tokio-net/src/signal/mod.rs b/tokio-net/src/signal/mod.rs
index 8c05cb60..b591c5c9 100644
--- a/tokio-net/src/signal/mod.rs
+++ b/tokio-net/src/signal/mod.rs
@@ -29,7 +29,7 @@
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create an infinite stream of "Ctrl+C" notifications. Each item received
//! // on this stream may represent multiple ctrl-c signals.
-//! let ctrl_c = signal::CtrlC::new()?;
+//! let ctrl_c = signal::ctrl_c()?;
//!
//! // Process each ctrl-c as it comes in
//! let prog = ctrl_c.for_each(|_| {
@@ -49,7 +49,7 @@
//! #![feature(async_await)]
//! # #[cfg(unix)] {
//!
-//! use tokio_net::signal::{self, unix::{Signal, SignalKind}};
+//! use tokio_net::signal::{self, unix::{signal, SignalKind}};
//!
//! use futures_util::future;
//! use futures_util::stream::StreamExt;
@@ -58,7 +58,7 @@
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create an infinite stream of "Ctrl+C" notifications. Each item received
//! // on this stream may represent multiple ctrl-c signals.
-//! let ctrl_c = signal::CtrlC::new()?;
+//! let ctrl_c = signal::ctrl_c()?;
//!
//! // Process each ctrl-c as it comes in
//! let prog = ctrl_c.for_each(|_| {
@@ -70,10 +70,10 @@
//!
//! // Like the previous example, this is an infinite stream of signals
//! // being received, and signals may be coalesced while pending.
-//! let stream = Signal::new(SignalKind::hangup())?;
+//! let stream = signal(SignalKind::hangup())?;
//!
//! // Convert out stream into a future and block the program
-//! let (signal, _signal) = stream.into_future().await;
+//! let (signal, _stream) = stream.into_future().await;
//! println!("got signal {:?}", signal);
//! Ok(())
//! }
@@ -93,4 +93,4 @@ mod os {
pub mod unix;
pub mod windows;
-pub use self::ctrl_c::CtrlC;
+pub use self::ctrl_c::{ctrl_c, CtrlC};
diff --git a/tokio-net/src/signal/unix.rs b/tokio-net/src/signal/unix.rs
index 49251a6e..f485b4e9 100644
--- a/tokio-net/src/signal/unix.rs
+++ b/tokio-net/src/signal/unix.rs
@@ -6,7 +6,6 @@
#![cfg(unix)]
use super::registry::{globals, EventId, EventInfo, Globals, Init, Storage};
-use crate::driver::Handle;
use crate::util::PollEvented;
use tokio_io::AsyncRead;
@@ -280,7 +279,7 @@ impl Future for Driver {
}
impl Driver {
- fn new(handle: &Handle) -> io::Result<Driver> {
+ fn new() -> io::Result<Driver> {
// NB: We give each driver a "fresh" reciever file descriptor to avoid
// the issues described in alexcrichton/tokio-process#42.
//
@@ -295,7 +294,7 @@ impl Driver {
// either, since we can't compare Handles or assume they will always
// point to the exact same reactor.
let stream = globals().receiver.try_clone()?;
- let wakeup = PollEvented::new_with_handle(stream, handle)?;
+ let wakeup = PollEvented::new(stream);
Ok(Driver { wakeup })
}
@@ -359,70 +358,48 @@ pub struct Signal {
rx: Receiver<()>,
}
-impl Signal {
- /// Creates a new stream which will receive notifications when the current
- /// process receives the signal `signal`.
- ///
- /// This function will create a new stream which binds to the default reactor.
- /// The `Signal` stream is an infinite stream which will receive
- /// notifications whenever a signal is received. More documentation can be
- /// found on `Signal` itself, but to reiterate:
- ///
- /// * Signals may be coalesced beyond what the kernel already does.
- /// * Once a signal handler is registered with the process the underlying
- /// libc signal handler is never unregistered.
- ///
- /// A `Signal` stream can be created for a particular signal number
- /// multiple times. When a signal is received then all the associated
- /// channels will receive the signal notification.
- ///
- /// # Errors
- ///
- /// * If the lower-level C functions fail for some reason.
- /// * If the previous initialization of this specific signal failed.
- /// * If the signal is one of
- /// [`signal_hook::FORBIDDEN`](https://docs.rs/signal-hook/*/signal_hook/fn.register.html#panics)
- pub fn new(kind: SignalKind) -> io::Result<Self> {
- Signal::with_handle(kind, &Handle::default())
- }
-
- /// Creates a new stream which will receive notifications when the current
- /// process receives the signal `signal`.
- ///
- /// This function will create a new stream which may be based on the
- /// provided reactor handle.
- /// The `Signal` stream is an infinite stream which will receive
- /// notifications whenever a signal is received. More documentation can be
- /// found on `Signal` itself, but to reiterate:
- ///
- /// * Signals may be coalesced beyond what the kernel already does.
- /// * Once a signal handler is registered with the process the underlying
- /// libc signal handler is never unregistered.
- ///
- /// A `Signal` stream can be created for a particular signal number
- /// multiple times. When a signal is received then all the associated
- /// channels will receive the signal notification.
- pub fn with_handle(kind: SignalKind, handle: &Handle) -> io::Result<Self> {
- let signal = kind.0;
-
- // Turn the signal delivery on once we are ready for it
- signal_enable(signal)?;
-
- // Ensure there's a driver for our associated event loop processing
- // signals.
- let driver = Driver::new(&handle)?;
-
- // One wakeup in a queue is enough, no need for us to buffer up any
- // more.
- let (tx, rx) = channel(1);
- globals().register_listener(signal as EventId, tx);
-
- Ok(Signal { driver, rx })
- }
+/// Creates a new stream which will receive notifications when the current
+/// process receives the signal `signal`.
+///
+/// This function will create a new stream which binds to the default reactor.
+/// The `Signal` stream is an infinite stream which will receive
+/// notifications whenever a signal is received. More documentation can be
+/// found on `Signal` itself, but to reiterate:
+///
+/// * Signals may be coalesced beyond what the kernel already does.
+/// * Once a signal handler is registered with the process the underlying
+/// libc signal handler is never unregistered.
+///
+/// A `Signal` stream can be created for a particular signal number
+/// multiple times. When a signal is received then all the associated
+/// channels will receive the signal notification.
+///
+/// # Errors
+///
+/// * If the lower-level C functions fail for some reason.
+/// * If the previous initialization of this specific signal failed.
+/// * If the signal is one of
+/// [`signal_hook::FORBIDDEN`](https://docs.rs/signal-hook/*/signal_hook/fn.register.html#panics)
+pub fn signal(kind: SignalKind) -> io::Result<Signal> {
+ let signal = kind.0;
+
+ // Turn the signal delivery on once we are ready for it
+ signal_enable(signal)?;
+
+ // Ensure there's a driver for our associated event loop processing
+ // signals.
+ let driver = Driver::new()?;
+
+ // One wakeup in a queue is enough, no need for us to buffer up any
+ // more.
+ let (tx, rx) = channel(1);
+ globals().register_listener(signal as EventId, tx);
+
+ Ok(Signal { driver, rx })
+}
- pub(crate) fn ctrl_c(handle: &Handle) -> io::Result<Self> {
- Self::with_handle(SignalKind::interrupt(), handle)
- }
+pub(crate) fn ctrl_c() -> io::Result<Signal> {
+ signal(SignalKind::interrupt())
}
impl Stream for Signal {
diff --git a/tokio-net/src/signal/windows.rs b/tokio-net/src/signal/windows.rs
index 959ebefa..c9ee05a9 100644
--- a/tokio-net/src/signal/windows.rs
+++ b/tokio-net/src/signal/windows.rs
@@ -8,7 +8,6 @@
#![cfg(windows)]
use super::registry::{globals, EventId, EventInfo, Init, Storage};
-use crate::driver::Handle;
use tokio_sync::mpsc::{channel, Receiver};
@@ -85,23 +84,7 @@ pub(crate) struct Event {
}
impl Event {
- /// Creates a new stream listening for the `CTRL_C_EVENT` events.
- ///
- /// This function will register a handler via `SetConsoleCtrlHandler` and
- /// deliver notifications to the returned stream.
- pub(crate) fn ctrl_c(handle: &Handle) -> io::Result<Self> {
- Event::new(CTRL_C_EVENT, handle)
- }
-
- /// Creates a new stream listening for the `CTRL_BREAK_EVENT` events.
- ///
- /// This function will register a handler via `SetConsoleCtrlHandler` and
- /// deliver notifications to the returned stream.
- fn ctrl_break_handle(handle: &Handle) -> io::Result<Self> {
- Event::new(CTRL_BREAK_EVENT, handle)
- }
-
- fn new(signum: DWORD, _handle: &Handle) -> io::Result<Self> {
+ fn new(signum: DWORD) -> io::Result<Self> {
global_init()?;
let (tx, rx) = channel(1);
@@ -111,6 +94,10 @@ impl Event {
}
}
+pub(crate) fn ctrl_c() -> io::Result<Event> {
+ Event::new(CTRL_C_EVENT)
+}
+
impl Stream for Event {
type Item = ();
@@ -167,22 +154,12 @@ pub struct CtrlBreak {
inner: Event,
}
-impl CtrlBreak {
- /// Creates a new stream which receives "ctrl-break" notifications sent to the
- /// process.
- ///
- /// This function binds to the default reactor.
- pub fn new() -> io::Result<Self> {
- Self::with_handle(&Handle::default())
- }
-
- /// Creates a new stream which receives "ctrl-break" notifications sent to the
- /// process.
- ///
- /// This function binds to reactor specified by `handle`.
- pub fn with_handle(handle: &Handle) -> io::Result<Self> {
- Event::ctrl_break_handle(handle).map(|inner| Self { inner })
- }
+/// Creates a new stream which receives "ctrl-break" notifications sent to the
+/// process.
+///
+/// This function binds to the default reactor.
+pub fn ctrl_break() -> io::Result<CtrlBreak> {
+ Event::new(CTRL_BREAK_EVENT).map(|inner| CtrlBreak { inner })
}
impl Stream for CtrlBreak {
@@ -212,7 +189,7 @@ mod tests {
#[tokio::test]
async fn ctrl_c() {
- let ctrl_c = crate::signal::CtrlC::new().expect("failed to create CtrlC");
+ let ctrl_c = crate::signal::ctrl_c().expect("failed to create CtrlC");
// Windows doesn't have a good programmatic way of sending events
// like sending signals on Unix, so we'll stub out the actual OS
@@ -226,7 +203,7 @@ mod tests {
#[tokio::test]
async fn ctrl_break() {
- let ctrl_break = super::CtrlBreak::new().expect("failed to create CtrlC");
+ let ctrl_break = super::ctrl_break().expect("failed to create CtrlC");
// Windows doesn't have a good programmatic way of sending events
// like sending signals on Unix, so we'll stub out the actual OS
diff --git a/tokio-net/tests/signal_drop_multi_loop.rs b/tokio-net/tests/signal_drop_multi_loop.rs
index d91cb96b..bfd38533 100644
--- a/tokio-net/tests/signal_drop_multi_loop.rs
+++ b/tokio-net/tests/signal_drop_multi_loop.rs
@@ -11,10 +11,10 @@ fn dropping_loops_does_not_cause_starvation() {
let kind = SignalKind::user_defined1();
let mut first_rt = CurrentThreadRuntime::new().expect("failed to init first runtime");
- let mut first_signal = Signal::new(kind).expect("failed to register first signal");
+ let mut first_signal = signal(kind).expect("failed to register first signal");
let mut second_rt = CurrentThreadRuntime::new().expect("failed to init second runtime");
- let mut second_signal = Signal::new(kind).expect("failed to register second signal");
+ let mut second_signal = signal(kind).expect("failed to register second signal");
send_signal(libc::SIGUSR1);
diff --git a/tokio-net/tests/signal_drop_then_get_a_signal.rs b/tokio-net/tests/signal_drop_then_get_a_signal.rs
index 21b77cc8..daaff600 100644
--- a/tokio-net/tests/signal_drop_then_get_a_signal.rs
+++ b/tokio-net/tests/signal_drop_then_get_a_signal.rs
@@ -9,11 +9,11 @@ use crate::signal_support::*;
#[tokio::test]
async fn drop_then_get_a_signal() {
let kind = SignalKind::user_defined1();
- let signal = Signal::new(kind).expect("failed to create first signal");
- drop(signal);
+ let sig = signal(kind).expect("failed to create first signal");
+ drop(sig);
send_signal(libc::SIGUSR1);
- let signal = Signal::new(kind).expect("failed to create second signal");
+ let sig = signal(kind).expect("failed to create second signal");
- let _ = with_timeout(signal.into_future()).await;
+ let _ = with_timeout(sig.into_future()).await;
}
diff --git a/tokio-net/tests/signal_dropping_does_not_deregister_other_instances.rs b/tokio-net/tests/signal_dropping_does_not_deregister_other_instances.rs
index e446cb04..47367622 100644
--- a/tokio-net/tests/signal_dropping_does_not_deregister_other_instances.rs
+++ b/tokio-net/tests/signal_dropping_does_not_deregister_other_instances.rs
@@ -12,15 +12,13 @@ async fn dropping_signal_does_not_deregister_any_other_instances() {
// NB: Testing for issue alexcrichton/tokio-signal#38:
// signals should not starve based on ordering
- let first_duplicate_signal =
- Signal::new(kind).expect("failed to register first duplicate signal");
- let signal = Signal::new(kind).expect("failed to register signal");
- let second_duplicate_signal =
- Signal::new(kind).expect("failed to register second duplicate signal");
+ let first_duplicate_signal = signal(kind).expect("failed to register first duplicate signal");
+ let sig = signal(kind).expect("failed to register signal");
+ let second_duplicate_signal = signal(kind).expect("failed to register second duplicate signal");
drop(first_duplicate_signal);
drop(second_duplicate_signal);
send_signal(libc::SIGUSR1);
- let _ = with_timeout(signal.into_future()).await;
+ let _ = with_timeout(sig.into_future()).await;
}
diff --git a/tokio-net/tests/signal_multi_loop.rs b/tokio-net/tests/signal_multi_loop.rs
index 2f535797..22f58625 100644
--- a/tokio-net/tests/signal_multi_loop.rs
+++ b/tokio-net/tests/signal_multi_loop.rs
@@ -20,7 +20,7 @@ fn multi_loop() {
let sender = sender.clone();
thread::spawn(move || {
let mut rt = CurrentThreadRuntime::new().unwrap();
- let signal = Signal::new(SignalKind::hangup()).unwrap();
+ let signal = signal(SignalKind::hangup()).unwrap();
sender.send(()).unwrap();
let _ = run_with_timeout(&mut rt, signal.into_future());
})
diff --git a/tokio-net/tests/signal_notify_both.rs b/tokio-net/tests/signal_notify_both.rs
index 138c1d4d..c26aaf60 100644
--- a/tokio-net/tests/signal_notify_both.rs
+++ b/tokio-net/tests/signal_notify_both.rs
@@ -9,9 +9,9 @@ use crate::signal_support::*;
#[tokio::test]
async fn notify_both() {
let kind = SignalKind::user_defined2();
- let signal1 = Signal::new(kind).expect("failed to create signal1");
+ let signal1 = signal(kind).expect("failed to create signal1");
- let signal2 = Signal::new(kind).expect("failed to create signal2");
+ let signal2 = signal(kind).expect("failed to create signal2");
send_signal(libc::SIGUSR2);
let _ = with_timeout(future::join(signal1.into_future(), signal2.into_future())).await;
diff --git a/tokio-net/tests/signal_simple.rs b/tokio-net/tests/signal_simple.rs
index 923eb940..fa5640e7 100644
--- a/tokio-net/tests/signal_simple.rs
+++ b/tokio-net/tests/signal_simple.rs
@@ -8,7 +8,7 @@ use crate::signal_support::*;
#[tokio::test]
async fn simple() {
- let signal = Signal::new(SignalKind::user_defined1()).expect("failed to create signal");
+ let signal = signal(SignalKind::user_defined1()).expect("failed to create signal");
send_signal(libc::SIGUSR1);
@@ -19,9 +19,9 @@ async fn simple() {
#[cfg(unix)]
async fn ctrl_c() {
use tokio::sync::oneshot;
- use tokio_net::signal::CtrlC;
+ use tokio_net::signal::ctrl_c;
- let ctrl_c = CtrlC::new().expect("failed to init ctrl_c");
+ let ctrl_c = ctrl_c().expect("failed to init ctrl_c");
let (fire, wait) = oneshot::channel();
diff --git a/tokio-net/tests/signal_support/mod.rs b/tokio-net/tests/signal_support/mod.rs
index 69a120e3..5d15ccf4 100644
--- a/tokio-net/tests/signal_support/mod.rs
+++ b/tokio-net/tests/signal_support/mod.rs
@@ -3,7 +3,7 @@
pub use tokio::runtime::current_thread::{self, Runtime as CurrentThreadRuntime};
use tokio::timer::Timeout;
-pub use tokio_net::signal::unix::{Signal, SignalKind};
+pub use tokio_net::signal::unix::{signal, SignalKind};
pub use futures_util::future;
use futures_util::future::FutureExt;
diff --git a/tokio-net/tests/signal_twice.rs b/tokio-net/tests/signal_twice.rs
index 3375990d..ed575355 100644
--- a/tokio-net/tests/signal_twice.rs
+++ b/tokio-net/tests/signal_twice.rs
@@ -9,14 +9,14 @@ use crate::signal_support::*;
#[tokio::test]
async fn twice() {
let kind = SignalKind::user_defined1();
- let mut signal = Signal::new(kind).expect("failed to get signal");
+ let mut sig = signal(kind).expect("failed to get signal");
for _ in 0..2 {
send_signal(libc::SIGUSR1);
- let (item, sig) = with_timeout(signal.into_future()).await;
+ let (item, sig_next) = with_timeout(sig.into_future()).await;
assert_eq!(item, Some(()));
- signal = sig;
+ sig = sig_next;
}
}