DataStorm  0.1
Data Distribution Service
Public Member Functions | Static Public Member Functions | List of all members
DataStorm::CtrlCHandler Class Reference

Provides a portable way to handle Ctrl-C and Ctrl-C like signals. More...

#include <DataStorm/DataStorm.h>

Public Member Functions

 CtrlCHandler (CtrlCHandlerCallback cb=nullptr)
 Register a function that handles Ctrl-C like signals. More...
 
 ~CtrlCHandler ()
 Unregister the callback function. More...
 
CtrlCHandlerCallback setCallback (CtrlCHandlerCallback cb) noexcept
 Replace the signal callback. More...
 
CtrlCHandlerCallback getCallback () const noexcept
 Obtain the signal callback. More...
 

Static Public Member Functions

static void maskSignals () noexcept
 Linux and macOS: mask the SIGHUP, SIGINT and SIGTERM signals. More...
 

Detailed Description

Provides a portable way to handle Ctrl-C and Ctrl-C like signals.

On Linux and macOS, the CtrlCHandler handles SIGHUP, SIGINT and SIGTERM. On Windows, it is essentially a wrapper for SetConsoleCtrlHandler().

Constructor & Destructor Documentation

◆ CtrlCHandler()

DataStorm::CtrlCHandler::CtrlCHandler ( CtrlCHandlerCallback  cb = nullptr)
explicit

Register a function that handles Ctrl-C like signals.

This constructor first calls maskSignals if it was not already called. On Linux and macOS, it creates a thread that waits on SIGHUP, SIGINT and SIGTERM using sigwait. Only a single CtrlCHandler object can exist in a process at a give time.

Parameters
cbThe callback function to invoke when a signal is received.

◆ ~CtrlCHandler()

DataStorm::CtrlCHandler::~CtrlCHandler ( )

Unregister the callback function.

This destructor does not "unmask" SIGHUP, SIGINT and SIGTERM or unregister the handler routine on Windows. As a result, Ctrl-C and similar signals are just ignored after this destructor completes.

Member Function Documentation

◆ getCallback()

CtrlCHandlerCallback DataStorm::CtrlCHandler::getCallback ( ) const
noexcept

Obtain the signal callback.

Returns
The callback

◆ maskSignals()

static void DataStorm::CtrlCHandler::maskSignals ( )
staticnoexcept

Linux and macOS: mask the SIGHUP, SIGINT and SIGTERM signals.

It is essential to call maskSignals before creating any thread. Threads created later on will inherit this signal mask. Windows: call SetConsoleCtrlCHandler to register a handler routine that ignores signals.

◆ setCallback()

CtrlCHandlerCallback DataStorm::CtrlCHandler::setCallback ( CtrlCHandlerCallback  cb)
noexcept

Replace the signal callback.

Parameters
cbThe new callback.
Returns
The old callback

The documentation for this class was generated from the following file: