Struct mio_signals::Signals[][src]

pub struct Signals { /* fields omitted */ }
Expand description

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"),
                        Some(Signal::User1) => println!("Got user signal 1"),
                        Some(Signal::User2) => println!("Got user signal 2"),
                        None => break,
                    }
                },
                _ => println!("Got unexpected event: {:?}", event),
            }
        }
    }
}

Implementations

Create a new signal notifier.

Receive a signal, if any.

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

Trait Implementations

Formats the value using the given formatter. Read more

Register self with the given Registry instance. Read more

Re-register self with the given Registry instance. Read more

Deregister self from the given Registry instance. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.