CppZmqZoltanExt 0.0.1
Loading...
Searching...
No Matches
interrupt.h File Reference

Signal interrupt handling for graceful application shutdown. More...

#include <atomic>
#include "cppzmqzoltanext/czze_export.h"
Include dependency graph for interrupt.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

CZZE_EXPORT void zmqzext::install_interrupt_handler () noexcept
 Install signal handlers for SIGINT and SIGTERM.
 
CZZE_EXPORT void zmqzext::restore_interrupt_handler () noexcept
 Restore the previously stored signal handlers.
 
CZZE_EXPORT bool zmqzext::is_interrupted () noexcept
 Check if a program interrupt was received.
 
CZZE_EXPORT void zmqzext::reset_interrupted () noexcept
 Reset the interrupt flag to false.
 

Detailed Description

Signal interrupt handling for graceful application shutdown.

This header provides signal handling utilities for managing SIGINT (Ctrl+C) and SIGTERM signals. It establishes a mechanism for detecting and responding to interrupt signals, enabling graceful shutdown of applications.

The module manages signal handlers through a pair of installation and restoration functions, allowing applications to safely install the signal handler provided by this module and restore previous handlers when needed.

The signal handler provided by this module sets an atomic flag that tracks the interrupt state, which can be checked by the application to determine when shutdown should be initiated.

The atomic flag is monitored by the poller_t and loop_t classes to allow them to detect interrupt conditions and return early from polling or loop operations.

Typically, an application will call install_interrupt_handler() during application initialization and perform a clean shutdown when its main poller_t or loop_t instance indicates that an interrupt has occurred.

Note
If no signal handlers are installed and the application receives a SIINT or SIGTERM signal, any waiting call to a ZMQ function will be interrupted and the application will terminate abruptly. On the other hand, if the signal handlers are installed using this module, the waiting calls to ZMQ functions will be interrupted and return EINTR, allowing the application to handle the interrupt.
On Windows, the waiting calls to ZMQ functions do not return early on interrupt signals, no matter if the signal handlers are installed or not. Still, the interrupt flag is set and can be checked by the application after the ZMQ wait operation. Then, it is very important to set appropriate timeouts on all ZMQ calls.

Key features:

  • Atomic flag for thread-safe interrupt detection
  • Automatic storage and restoration of previous signal handlers
  • Support for SIGINT (Ctrl+C) and SIGTERM signals
  • Non-blocking interrupt checking
  • Manual interrupt flag reset capability
Authors
Luan Young (luanp.nosp@m.y@gm.nosp@m.ail.c.nosp@m.om)

Distributed under the MIT License (MIT) (See accompanying file LICENSE or copy at http://opensource.org/licenses/MIT)

Definition in file interrupt.h.

Function Documentation

◆ install_interrupt_handler()

void zmqzext::install_interrupt_handler ( )
noexcept

Install signal handlers for SIGINT and SIGTERM.

Installs a signal handler for SIGINT (Ctrl+C) and SIGTERM signals. When either signal is received, a global atomic flag is set to true, indicating that an interrupt has been requested. Other parts of the application can check this flag via is_interrupted() to implement graceful shutdown logic.

The function preserves the current signal handlers before installing new ones on the first call or after a call to restore_interrupt_handler(). This allows these original handlers to be restored later via restore_interrupt_handler().

Note
This function is not thread-safe; it should be called during application initialization before multiple threads are spawned.
Multiple calls without an intervening call to restore_interrupt_handler() will not save additional handler states.
See also
restore_interrupt_handler()
is_interrupted()
reset_interrupted()

Definition at line 82 of file interrupt.cpp.

◆ is_interrupted()

bool zmqzext::is_interrupted ( )
noexcept

Check if a program interrupt was received.

Checks whether a SIGINT (Ctrl+C) or SIGTERM signals have been received since the interrupt handlers were installed or since the last call to reset_interrupted().

Returns
true if an interrupt signal has been received, false otherwise
Note
This function is thread-safe and non-blocking.
See also
reset_interrupted()
install_interrupt_handler()

Definition at line 118 of file interrupt.cpp.

◆ reset_interrupted()

void zmqzext::reset_interrupted ( )
noexcept

Reset the interrupt flag to false.

Resets the atomic interrupt flag to false, allowing the application to continue monitoring for new interrupt signals. This function is useful when you want to handle an interrupt and then continue execution while maintaining the ability to detect future interrupts.

Note
This function is thread-safe.
Resetting the flag does not affect the signal handlers.
See also
is_interrupted()
install_interrupt_handler()

Definition at line 120 of file interrupt.cpp.

◆ restore_interrupt_handler()

void zmqzext::restore_interrupt_handler ( )
noexcept

Restore the previously stored signal handlers.

Restores the signal handlers that were active before the first call to install_interrupt_handler(). This function does nothing if install_interrupt_handler() was never called or if the handlers were already restored.

After calling this function, a subsequent call to install_interrupt_handler() will save the current handlers (which are now the original handlers) before installing new custom signal handlers.

Note
This function is not thread-safe.
The interrupt flag state is not affected by this operation.
See also
install_interrupt_handler()

Definition at line 90 of file interrupt.cpp.