1 unstable release
new 0.1.0 | Mar 22, 2025 |
---|
#481 in Development tools
13KB
191 lines
simple-sigh
A simple signal handler.
lib.rs
:
A simple signal handler.
On unix, register SIGINT
/SIGTERM
signal handlers that call a closure to
process the signals. On Windows, register "console control" handlers to
call a closure on Ctrl+C, Ctrl+Break and close events.
The closure is called once and it is assumed that it will cause the application to terminate.
Usage
The application must call [init()
] early (before setting up any other
signal handlers or spawning threads). After init()
has been called, it
must call [register()
] to set the callback closure that will be called
when one of the signals of interest is raised.
Waiting for termination requests
Applications that are run on spawned threads can use signaling mechanisms
like a Condvar
or a channel to signal from the signal handler to main()
that the application should be terminated. However, because it is assumed
that this is a somewhat common use-case for simple-sigh
it provides a
pre-baked solution for this pattern: init()
returns a KillWait
object
that has a [KillWait::wait()
] method that will block indefinitely and
wait for the signal handler to trigger.
fn main() {
let kw = simple_sigh::init().unwrap();
// .. spawen application thread here ..
// Set a signal handler callback
simple_sigh::register(move |_| {
println!("Received signal");
// .. signal application runtime to terminate here ..
})
.unwrap();
// Wait for some kind of abortion signal to be received
println!("Wait for interrupt/termination signal..");
kw.wait();
println!("Bye!");
}
Known limitations
simple-sigh
isn't intended for production; it was originally designed for
use in examples, and it may be lossy with regards to error handling.
The general rule is that simple-sigh
belongs in dev-dependencies
rather
than dependencies
.
Dependencies
~1.8–9.5MB
~96K SLoC