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

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

#include "cppzmqzoltanext/interrupt.h"
#include <signal.h>
#include <atomic>
Include dependency graph for interrupt.cpp:

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.

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.cpp.

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.