// std/signal — cooperative process interruption helpers.
/**
* Register a process interrupt handler.
*
* @effects: []
* @errors: []
*/
pub type InterruptOptions = {
signals?: string | list<string>,
once?: bool,
graceful_timeout_ms?: int,
}
pub type InterruptRegistration = {handle: int, signals: list<string>, once: bool}
pub fn on_interrupt(
handler: fn() -> unknown,
options: InterruptOptions? = nil,
) -> InterruptRegistration {
const registration = __signal_on_interrupt(handler, options)
return {handle: registration.handle, signals: registration.signals, once: registration.once}
}
/**
* Remove an interrupt handler returned by `on_interrupt`.
*
* @effects: []
* @errors: []
*/
pub fn off_interrupt(handle: int | InterruptRegistration) -> nil {
return __signal_off_interrupt(handle)
}
/**
* Return true after the current VM observes an interrupt.
*
* @effects: []
* @errors: []
*/
pub fn interrupted() -> bool {
return __signal_interrupted()
}
/**
* Run `body` with an interrupt handler that is always unregistered afterward.
*
* @effects: []
* @errors: []
*/
pub fn with_interrupt<T>(
handler: fn() -> unknown,
body: fn() -> T,
options: InterruptOptions? = nil,
) -> T {
const registration = on_interrupt(handler, options)
try {
const result = body()
off_interrupt(registration)
return result
} catch (e) {
off_interrupt(registration)
throw e
}
}