harn-stdlib 0.8.26

Embedded Harn standard library source catalog
Documentation
// std/signal — cooperative process interruption helpers.
/**
 * Register a process interrupt handler.
 *
 * @effects: []
 * @allocation: heap
 * @errors: []
 * @api_stability: stable
 * @example: on_interrupt(handler, options)
 */
pub fn on_interrupt(handler, options = nil) {
  return __signal_on_interrupt(handler, options)
}

/**
 * Remove an interrupt handler returned by `on_interrupt`.
 *
 * @effects: []
 * @allocation: heap
 * @errors: []
 * @api_stability: stable
 * @example: off_interrupt(handle)
 */
pub fn off_interrupt(handle) {
  return __signal_off_interrupt(handle)
}

/**
 * Return true after the current VM observes an interrupt.
 *
 * @effects: []
 * @allocation: stack-only
 * @errors: []
 * @api_stability: stable
 * @example: interrupted()
 */
pub fn interrupted() -> bool {
  return __signal_interrupted()
}

/**
 * Run `body` with an interrupt handler that is always unregistered afterward.
 *
 * @effects: []
 * @allocation: heap
 * @errors: []
 * @api_stability: stable
 * @example: with_interrupt(handler, body, options)
 */
pub fn with_interrupt(handler, body, options = nil) {
  let registration = on_interrupt(handler, options)
  try {
    let result = body()
    off_interrupt(registration)
    return result
  } catch (e) {
    off_interrupt(registration)
    throw e
  }
}