Signal handlers

#include <x/sighandler.H>


// ...

x::sigset signals;

signals += SIGHUP;

signals.block();
x::ref<x::obj> handler=x::install_sighandler(
    SIGHUP,
    []
    (int signum)
    {
    });

Signal handlers provide a higher level interface for handling signals. Installing a signal handler starts a background thread that creates a signal file descriptor and invokes the installed handler upon receipt of a signal.

A signal handler is a callable object with one parameter, the signal number, and gets installed by x::install_sighandler(). The same signal handler object may be installed for different signals, and receives the signal number as a parameter, indicating the received signal. More than one handler may be installed for the same signal. Each installed handler gets invoked by the received signal. There's a single thread that implements signal processing, so the signal handler should not implement time-consuming tasks.

x::install_sighandler() returns a mcguffin. The signal handler remains installed until the mcguffin goes out of scope and gets destroyed, at which time it gets uninstalled, and will no longer handle signals. if the signal thread is currently executing the handler, the signal handler gets uninstalled when it returns.

When all signal handlers are uninstalled, the signal handler thread gets stopped and the signal file descriptor is closed. The next installation of a signal handler restarts the thread and recreates the signal file descriptor.

As noted, applications must block individual signals in order to effect signal handling.