Struct signal_hook::iterator::Signals [−][src]
pub struct Signals { /* fields omitted */ }
The main structure of the module, representing interest in some signals.
Unlike the helpers in other modules, this registers the signals when created and unregisters them on drop. It provides the pending signals during its lifetime, either in batches or as an infinite iterator.
Multiple consumers
You may have noticed this structure can be used simultaneously by multiple threads. If it is done, a signal arrives to one of the threads (on the first come, first serve basis). The signal is not broadcasted to all currently active threads.
A similar thing applies to cloning the structure ‒ at least one of the copies gets the signal, but it is not broadcasted to all of them.
If you need multiple recipients, you can create multiple independent instances (not by cloning, but by the constructor).
Examples
let signals = signal_hook::iterator::Signals::new(&[libc::SIGUSR1, libc::SIGUSR2])?; thread::spawn(move || { for signal in &signals { match signal { libc::SIGUSR1 => {}, libc::SIGUSR2 => {}, _ => unreachable!(), } } });
mio
support
If the crate is compiled with the mio-support
flag, the Signals
becomes pluggable into
mio
(it implements the Evented
trait). If it becomes readable, there may be new signals to
pick up. The structure is expected to be registered with level triggered mode.
tokio
support
If the crate is compiled with the tokio-support
flag, the into_async
method becomes available. This method turns the iterator into an asynchronous stream of
received signals.
Methods
impl Signals
[src]
impl Signals
pub fn into_async(self) -> Result<Async, Error>
[src]
pub fn into_async(self) -> Result<Async, Error>
Turns the iterator into an asynchronous stream.
This allows getting the signals in asynchronous way in a tokio event loop. Available
only if compiled with the tokio-support
feature enabled.
Examples
extern crate libc; extern crate signal_hook; extern crate tokio; use std::io::Error; use signal_hook::iterator::Signals; use tokio::prelude::*; fn main() -> Result<(), Error> { let wait_signal = Signals::new(&[libc::SIGUSR1])? .into_async()? .into_future() .map(|sig| assert_eq!(sig.0.unwrap(), libc::SIGUSR1)) .map_err(|e| panic!("{}", e.0)); unsafe { libc::kill(libc::getpid(), libc::SIGUSR1) }; tokio::run(wait_signal); Ok(()) }
pub fn into_async_with_handle(self, handle: &Handle) -> Result<Async, Error>
[src]
pub fn into_async_with_handle(self, handle: &Handle) -> Result<Async, Error>
Turns the iterator into a stream, tied into a specific tokio reactor.
impl Signals
[src]
impl Signals
pub fn new<I, S>(signals: I) -> Result<Self, Error> where
I: IntoIterator<Item = S>,
S: Borrow<c_int>,
[src]
pub fn new<I, S>(signals: I) -> Result<Self, Error> where
I: IntoIterator<Item = S>,
S: Borrow<c_int>,
Creates the Signals
structure.
This registers all the signals listed. The same restrictions (panics, errors) apply as with
register
.
ⓘImportant traits for Pending<'a>pub fn pending(&self) -> Pending
[src]
pub fn pending(&self) -> Pending
Returns an iterator of already received signals.
This returns an iterator over all the signal numbers of the signals received since last
time they were read (out of the set registered by this Signals
instance). Note that they
are returned in arbitrary order and a signal number is returned only once even if it was
received multiple times.
This method returns immediately (does not block) and may produce an empty iterator if there are no signals ready.
ⓘImportant traits for Pending<'a>pub fn wait(&self) -> Pending
[src]
pub fn wait(&self) -> Pending
Waits for some signals to be available and returns an iterator.
This is similar to pending
. If there are no signals available, it
tries to wait for some to arrive. However, due to implementation details, this still can
produce an empty iterator.
This can block for arbitrary long time.
Note that the blocking is done in this method, not in the iterator.
ⓘImportant traits for Forever<'a>pub fn forever(&self) -> Forever
[src]
pub fn forever(&self) -> Forever
Returns an infinite iterator over arriving signals.
The iterator's next()
blocks as necessary to wait for signals to arrive. This is adequate
if you want to designate a thread solely to handling signals. If multiple signals come at
the same time (between two values produced by the iterator), they will be returned in
arbitrary order. Multiple instances of the same signal may be collated.
This is also the iterator returned by IntoIterator
implementation on &Signals
.
Examples
let signals = signal_hook::iterator::Signals::new(&[libc::SIGUSR1, libc::SIGUSR2])?; thread::spawn(move || { for signal in signals.forever() { match signal { libc::SIGUSR1 => {}, libc::SIGUSR2 => {}, _ => unreachable!(), } } });
Trait Implementations
impl Evented for Signals
[src]
impl Evented for Signals
fn register(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt
) -> Result<(), Error>
[src]
fn register(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt
) -> Result<(), Error>
Register self
with the given Poll
instance. Read more
fn reregister(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt
) -> Result<(), Error>
[src]
fn reregister(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt
) -> Result<(), Error>
Re-register self
with the given Poll
instance. Read more
fn deregister(&self, poll: &Poll) -> Result<(), Error>
[src]
fn deregister(&self, poll: &Poll) -> Result<(), Error>
Deregister self
from the given Poll
instance Read more
impl Clone for Signals
[src]
impl Clone for Signals
fn clone(&self) -> Signals
[src]
fn clone(&self) -> Signals
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl Debug for Signals
[src]
impl Debug for Signals
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl Drop for Signals
[src]
impl Drop for Signals
impl<'a> IntoIterator for &'a Signals
[src]
impl<'a> IntoIterator for &'a Signals