#signal #signal-handler #future #monoio #tokio #glommio #compio

signalfut

A future similar to tokio::signal::unix::Signal, but can be used with all the runtimes

1 unstable release

0.1.0 Jun 5, 2024

#777 in Asynchronous

Download history 4/week @ 2024-08-06 48/week @ 2024-08-13 14/week @ 2024-08-20 12/week @ 2024-08-27 2/week @ 2024-09-10 10/week @ 2024-09-17 37/week @ 2024-09-24 13/week @ 2024-10-01 3/week @ 2024-10-08

60 downloads per month

Apache-2.0

23KB
372 lines

signalfut

crates.io docs.rs msrv BUILD

A future similar to tokio::signal::unix::Signal, but can be used with:

  • Tokio
  • async-std
  • futures
  • smol
  • Monoio
  • Glommio
  • Compio

Supported platforms

Currently, only Linux and macOS are supported. Windows will also be supported in the future.

Examples

Greet when receive either SIGINT or SIGQUIT:

use signalfut::ctrl_c;
use signalfut::Signal;
use signalfut::SignalFut;

#[tokio::main]
async fn main() {
    tokio::select! {
        _ = ctrl_c() => {},
        _ = SignalFut::new(Signal::SIGQUIT) => {},
    }
    println!("Greeting!");
}

Let multiple tasks wait for the same signal:

use signalfut::ctrl_c;

#[tokio::main]
async fn main() {
    let fut1 = ctrl_c();
    let fut2 = ctrl_c();

    tokio::join!(fut1, fut2);
}

Signal handler

This crate disposes a signal handler for the signals you want to watch, since signal handler is shared by the whole process, don't use other crates that also do this, they will clash.

After disposition, the default signal handler will not be reset.

Helper thread and Signal Safety

This crate creates a helper thread to execute the code that is not signal-safe, the signal handler and the helper thread communicate through an OS pipe, the only thing that the signal handler does is write(2), which is signal-safe.

Dependencies

~2.5MB
~58K SLoC