Signal handlers

#include <x/sighandler.H>

class myhandlerObj : public x::sighandlerObj {

public:

// ...

	void signal(int signum);

};

x::ref<myhandlerObj> handler=x::ref<myhandlerObj>::create();

// ...

x::ref<x::obj> mcguffin=x::sighandler::install(SIGHUP, handler);

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 class derives from x::sighandlerObj, and gets installed by x::sighandler::install(). The same signal handler object may be installed for different signals. More than one handler may be installed for the same signal. Upon receipt of a signal, each registered handler gets invoked by a separate thread. There's a single thread that implements signal processing, so the signal handler should not implement time-consuming tasks.

x::sighandler::install() 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 there are no other references to the signal handler object, it gets destroyed. If the mcguffin goes out of scope, there are no other references to the handler, but the signal thread is currently executing the handler due to a receipt of a signal, the signal handler object gets destroyed 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.