Struct mio_signals::Signals[][src]

pub struct Signals { /* fields omitted */ }

Notification of process signals.

Multithreaded process

For Signals to function correctly in multithreaded processes it must be created on the main thread before spawning any threads. This is due to an implementation detail where the spawned threads must inherit various signal related thread options from the parent thread (mainly the blocked signals on Linux).

Any threads spawned before calling Signals::new will experience the default process signals behaviour, i.e. sending it a signal will stop it.

Notes

On Android and Linux this will block all signals in the signal set given when creating Signals, using pthread_sigmask(3). This means that the thread in which Signals was created is not interrupted, or in any way notified of signal until the assiocated Poll is polled.

On platforms that support kqueue(2) the signal handler action is set to SIG_IGN using sigaction(2), meaning that all signals will be ignored. Same as on Linux based systems; the program is not interrupted, or in any way notified of signal until the assiocated Poll is polled.

Implementation notes

On platforms that support kqueue(2) this will use the EVFILT_SIGNAL event filter. On Android and Linux it uses signalfd(2).

Examples

use std::io;

use mio::{Poll, Events, Interest, Token};
use mio_signals::{Signals, Signal, SignalSet};

const SIGNAL: Token = Token(10);

fn main() -> io::Result<()> {
    let mut poll = Poll::new()?;
    let mut events = Events::with_capacity(128);

    // Create a `Signals` instance that will catch signals for us.
    let mut signals = Signals::new(SignalSet::all())?;
    // And register it with our `Poll` instance.
    poll.registry().register(&mut signals, SIGNAL, Interest::READABLE)?;

    loop {
        poll.poll(&mut events, None)?;

        for event in events.iter() {
            match event.token() {
                // Because we're using edge triggers (default in Mio) we need
                // to keep calling `receive` until it returns `Ok(None)`.
                SIGNAL => loop {
                    match signals.receive()? {
                        Some(Signal::Interrupt) => println!("Got interrupt signal"),
                        Some(Signal::Terminate) => println!("Got terminate signal"),
                        Some(Signal::Quit) => println!("Got quit signal"),
                        None => break,
                    }
                },
                _ => println!("Got unexpected event: {:?}", event),
            }
        }
    }
}

Implementations

impl Signals[src]

pub fn new(signals: SignalSet) -> Result<Signals>[src]

Create a new signal notifier.

pub fn receive(&mut self) -> Result<Option<Signal>>[src]

Receive a signal, if any.

If no signal is available this returns Ok(None).

Trait Implementations

impl Debug for Signals[src]

impl Source for Signals[src]

Auto Trait Implementations

impl RefUnwindSafe for Signals

impl Send for Signals

impl Sync for Signals

impl Unpin for Signals

impl UnwindSafe for Signals

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.