...
Compliant Solution (Windows)
There are two classes of signals in the Microsoft Visual Studio implementation:
SIGSEGV
,SIGILL
, andSIGFPE
have per-thread handlers, so each thread may register its own handler for these signals.SIGABRT
,SIGBREAK
,SIGTERM
, andSIGINT
have global handlers and access to these handlers is synchronized via a global lock.
There is no safe way to implement persistent signal-handler behavior on Windows platforms, and it should not be attempted. If a design depends on this behavior, and the design cannot be altered, it may be necessary to claim a deviation from this rule after completing an appropriate risk analysis.
The reason for this is that Windows is a nonpersistent platform as discussed above. Just before calling the current handler function, Windows resets the handler for the next occurrence of the same signal to SIG_DFL
. If the handler calls signal()
to reinstall itself, there is still a race window. A signal might occur between the start of the handler and the call to signal()
, which would invoke the default behavior instead of the desired handlerFor the signals with global handlers, the handler is reset to SIG_DFL
and the handler is called under a lock, so there is no race if signal()
is called again from the handler to reassert itself as the handler. For the signals with per-thread handlers, the state is local to the thread and resultantly there is no opportunity for a race.
Exceptions
SIG34-C-EX1: For implementations with persistent signal handlers, it is safe for a handler to modify the behavior of its own signal. Behavior modifications include ignoring the signal, resetting to the default behavior, and having the signal handled by a different handler. A handler reasserting its binding is also safe but unnecessary.
...