Scoped host signals

(sigil signals) provides call-with-signal-handlers on POSIX hosts. Supply a list of (signal . procedure) pairs and a thunk. The supported signals are int, quit, term and hup. Each callback takes no arguments.

(import (sigil signals))

(let ((interrupted? #f))
  (call-with-signal-handlers
    (list (cons 'int (lambda () (set! interrupted? #t))))
    (lambda ()
      (check-signals!)
      interrupted?)))

The scheduler dispatches callbacks at its signal service points. Synchronous code can call check-signals! explicitly. Each pending signal is coalesced until dispatch, so callbacks should tolerate more than one OS delivery.

When the thunk returns, raises, or leaves through a continuation, the scope restores the previous OS dispositions and Scheme callbacks for its signals. Nested scopes restore the enclosing scopes in turn. A continuation that reenters the scope installs its handlers again for the new dynamic extent. on-signal also accepts quit for persistent registration.