Signal

lib/Signal.gob

Waiting for a signal: a task asks for the signals it wants with listen, and waits for the next of them as it waits for anything else, suspended while its worker runs other tasks. No code of the program runs in signal context, so what the task does about a signal -- stop a server, reopen a log, read the terminal's new size -- is ordinary code.

let signals = Signal.listen([Signal.Terminate, Signal.Interrupt])?
let got = Signal.waitUnbounded(signals)
print("stopping on " + show(got))
Server.stop(server, Duration.seconds(10))

Repeats coalesce. A listener holds each kind of signal at most once until a wait takes it, as the kernel holds a pending signal once: three SIGHUPs that arrive before the wait are one Hangup. Different kinds are kept apart, and waits take them in the order they were found; two that arrived between the same two looks come in Signal's declaration order. Every listener that asked for a signal gets it.

While anyone listens, the signal does not do what it did. A program that listens for Terminate is not ended by SIGTERM; it ends when its code decides to. stop the last listener for a signal and the disposition it had before the first listen comes back: the default for most, or ignored for one the program was started with ignored, as nohup starts SIGHUP. A signal that arrives while nobody listens for it is not kept.

Children start clean. A program started while this one listens gets the default disposition for every signal listened for, since exec resets a caught signal, and no signal is blocked. Child exits are not signals here: Process waits for each child of its own.

How it works. The self-pipe trick: a handler counts the signal and writes a byte to a pipe (Turkey.Entry.signalNote), and a waiting task parks on the pipe's read end through the poller. A task that wakes reads the pipe empty, then takes the counts and hands each signal to every listener that asked for it, so whichever task wakes first delivers for all of them, and a listener whose task is not waiting gets its signals when it next waits. Both targets work this way.

type Signal = Hangup | Interrupt | Terminate | User1 | User2 | WindowChange

The signals a program can wait for: SIGHUP, SIGINT, SIGTERM, SIGUSR1, SIGUSR2 and SIGWINCH.

fun number : fun(Signal) -> Int

Returns the signal's number on this target, as kill takes it. Darwin's are <sys/signal.h>, Linux's <bits/signum-arch.h>.

type Listener
fun listen : fun(Array Signal) -> Either IOError Signal.Listener

Returns a listener for signals, which from now on keeps every one of them that arrives until a wait takes it. Fails only if the pipe the signals arrive through cannot be made, the first time.

fun wait : [Timeout t] fun(Signal.Listener, t) -> Option Signal

Returns the next signal l holds, waiting for one until t has passed: None if none came by then, or if l is stopped, before or during the wait.

fun waitUnbounded : fun(Signal.Listener) -> Signal

Returns the next signal l holds, waiting for one for as long as it takes. Panics if l is stopped, before or during the wait.

fun stop : fun(Signal.Listener) -> Unit

Stop l: it holds no more signals, and a wait on it ends. Each signal no listener wants any more gets back the disposition it had before. Stopping a listener twice does nothing the second time.