|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
RAII wrapper for POSIX signal handler registration. More...
#include <ah-signal.H>
Classes | |
| struct | NoAbortTag |
Public Types | |
| using | Handler = void(*)(int) |
| Type alias for signal handler function. | |
Public Member Functions | |
| Signal (int signo, Handler func, bool restart_calls=true) | |
| Installs a signal handler. | |
| ~Signal () | |
| Restores the previous signal handler. | |
| void | release () noexcept |
| Releases ownership of the signal handler. | |
| int | signal_number () const noexcept |
| Returns the signal number being handled. | |
| bool | restarts_calls () const noexcept |
| Returns whether SA_RESTART is enabled for this handler. | |
| bool | is_active () const noexcept |
| Returns whether this object will restore the handler on destruction. | |
| Handler | previous_handler () const noexcept |
| Returns the previous handler that will be restored. | |
| const struct sigaction & | previous_action () const noexcept |
| Returns the previous sigaction structure. | |
| Signal (const Signal &)=delete | |
| Signal & | operator= (const Signal &)=delete |
| Signal (Signal &&other) noexcept | |
| Move constructor. | |
| Signal & | operator= (Signal &&other) noexcept |
| Move assignment operator. | |
Static Public Member Functions | |
| static Signal | create (int signo, Handler func, bool restart_calls=true) |
| Creates a Signal object or throws on failure. | |
| static Signal | try_create (int signo, Handler func, bool restart_calls, int &error_out) |
| Attempts to create a Signal object. | |
Private Member Functions | |
| Signal (NoAbortTag, int signo, Handler func, bool restart_calls, int &error_out) | |
Private Attributes | |
| struct sigaction | old_action_ |
| int | signal_number_ |
| bool | restart_calls_ |
| bool | active_ = true |
RAII wrapper for POSIX signal handler registration.
Installs a signal handler on construction and automatically restores the previous handler when the object is destroyed. This ensures proper cleanup even when exceptions are thrown.
For SIGALRM, the SA_INTERRUPT flag is used (if available) instead of SA_RESTART to ensure that blocking calls (like sleep(), read()) are interrupted by the alarm.
By default, the constructor calls abort() if sigaction() fails (for backward compatibility). Use the throwing constructor overload or Signal::try_create() for exception-based error handling.
Definition at line 353 of file ah-signal.H.
| using Signal::Handler = void (*)(int) |
Type alias for signal handler function.
Definition at line 357 of file ah-signal.H.
|
inlineprivate |
Definition at line 368 of file ah-signal.H.
References active_, old_action_, and restart_calls_.
|
inline |
Installs a signal handler.
This constructor maintains backward compatibility by calling abort() if sigaction() fails. For exception-based error handling, use Signal::try_create() or the throwing overload.
| signo | The signal number to handle (e.g., SIGINT, SIGTERM). |
| func | The handler function to install. Can be a function pointer, SIG_IGN (to ignore), or SIG_DFL (for default action). |
| restart_calls | If true (default), interrupted system calls are automatically restarted (SA_RESTART flag). Ignored for SIGALRM. |
abort() if sigaction fails. Use try_create() for exception-based error handling. Definition at line 416 of file ah-signal.H.
References old_action_, and restart_calls_.
|
inline |
Restores the previous signal handler.
Unless release() was called, the destructor restores the signal handler that was in effect before this Signal object was constructed.
abort(). This maintains backward compatibility but may not be ideal in all contexts. Consider calling release() if you don't need automatic restoration. Definition at line 520 of file ah-signal.H.
References active_, old_action_, and signal_number_.
|
delete |
|
inlinenoexcept |
Move constructor.
Transfers ownership of signal handler management. The moved-from object will not restore the previous handler.
Definition at line 583 of file ah-signal.H.
Creates a Signal object or throws on failure.
Unlike the regular constructor, this factory method throws a SignalError exception instead of calling abort() on failure.
| signo | The signal number to handle. |
| func | The handler function to install. |
| restart_calls | Whether to restart interrupted system calls. |
| SignalError | if sigaction fails. |
Definition at line 470 of file ah-signal.H.
|
inlinenoexcept |
Move assignment operator.
If this object is active, restores its handler before taking ownership from the other object.
Definition at line 598 of file ah-signal.H.
References active_, old_action_, restart_calls_, and signal_number_.
|
inlinenoexcept |
Returns the previous sigaction structure.
Definition at line 568 of file ah-signal.H.
References old_action_.
|
inlinenoexcept |
Returns the previous handler that will be restored.
Definition at line 562 of file ah-signal.H.
References old_action_.
Referenced by TEST_F().
|
inlinenoexcept |
Releases ownership of the signal handler.
After calling this method, the destructor will not restore the previous handler. This is useful when you want to permanently change a signal handler.
Definition at line 550 of file ah-signal.H.
References active_.
Referenced by TEST_F().
|
inlinenoexcept |
Returns whether SA_RESTART is enabled for this handler.
Definition at line 556 of file ah-signal.H.
References restart_calls_.
|
inlinenoexcept |
Returns the signal number being handled.
Definition at line 553 of file ah-signal.H.
References signal_number_.
|
inlinestatic |
Attempts to create a Signal object.
Non-throwing version that returns success status through an output parameter.
| signo | The signal number to handle. | |
| func | The handler function to install. | |
| restart_calls | Whether to restart interrupted system calls. | |
| [out] | error_out | Set to errno value on failure, 0 on success. |
Definition at line 503 of file ah-signal.H.
Referenced by TEST_F().
|
private |
Definition at line 363 of file ah-signal.H.
Referenced by Signal(), ~Signal(), is_active(), operator=(), and release().
|
private |
Definition at line 360 of file ah-signal.H.
Referenced by Signal(), Signal(), ~Signal(), operator=(), previous_action(), and previous_handler().
|
private |
Definition at line 362 of file ah-signal.H.
Referenced by Signal(), Signal(), operator=(), and restarts_calls().
|
private |
Definition at line 361 of file ah-signal.H.
Referenced by ~Signal(), operator=(), and signal_number().