Celeritas  0.5.0-56+6b053cd
Public Member Functions | Static Public Member Functions | List of all members
celeritas::ScopedSignalHandler Class Reference

Catch the given signal type within the scope of the handler. More...

#include <ScopedSignalHandler.hh>

Public Types

Type aliases
using signal_type = int
 

Public Member Functions

 ScopedSignalHandler ()=default
 Default to not handling any signals.
 
 ScopedSignalHandler (signal_type)
 Handle the given signal type.
 
 ScopedSignalHandler (std::initializer_list< signal_type >)
 Handle the given signal type.
 
 ~ScopedSignalHandler ()
 Release the given signal. More...
 
bool operator() () const
 Return whether a signal was intercepted.
 
 operator bool () const
 True if handling a signal.
 
 ScopedSignalHandler (ScopedSignalHandler const &)=delete
 
ScopedSignalHandleroperator= (ScopedSignalHandler const &)=delete
 
 ScopedSignalHandler (ScopedSignalHandler &&) noexcept
 Move construct.
 
ScopedSignalHandleroperator= (ScopedSignalHandler &&) noexcept
 Move assign.
 
void swap (ScopedSignalHandler &other) noexcept
 Swap.
 

Static Public Member Functions

static bool allow_signals ()
 Whether signal handling is enabled.
 
static int raise (signal_type sig)
 Raise a signal visible only to ScopedSignalHandler (for testing). More...
 

Detailed Description

Catch the given signal type within the scope of the handler.

On instantiation with a non-empty argument, this class registers a signal handler for the given signal. A class instance is true if and only if the class is handling a signal. The instance's "call" operator will check and return whether the assigned signal has been caught. The move-assign operator can be used to unregister the handle.

When the class exits scope, the signal for the active type will be cleared.

Signal handling can be disabled by setting the environment variable CELER_DISABLE_SIGNALS to a non-empty value, but hopefully this will not be necessary because signal handling should be used sparingly.

#include <csignal>
int main()
{
ScopedSignalHandler interrupted(SIGINT);
while (true)
{
if (interrupted())
{
CELER_LOG(error) << "Interrupted";
break;
}
if (stop_handling_for_whatever_reason())
{
// Clear handler
interrupted = {};
}
}
return interrupted() ? 1 : 0;
}
int main(int argc, char **argv)
Define main.
Definition: TestMain.hh:13
ScopedSignalHandler()=default
Default to not handling any signals.
#define CELER_LOG(LEVEL)
Return a LogMessage object for streaming into at the given level.
Definition: corecel/io/Logger.hh:48

Constructor & Destructor Documentation

◆ ~ScopedSignalHandler()

celeritas::ScopedSignalHandler::~ScopedSignalHandler ( )

Release the given signal.

This destructor is not thread-safe; it could have a race condition if a signal is sent while our signal bit is being cleared.

Member Function Documentation

◆ raise()

int celeritas::ScopedSignalHandler::raise ( signal_type  sig)
static

Raise a signal visible only to ScopedSignalHandler (for testing).

This function always returns zero, indicating "success", to conform to the C++ standard library function.


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