.. _program_listing_file_src_common_signal_handling.cpp: Program Listing for File signal_handling.cpp ============================================ |exhale_lsh| :ref:`Return to documentation for file ` (``src/common/signal_handling.cpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #include "common/logging.h" #include "signal_handling.h" // The simplest (and recommended) way to handle signals is to simply set a flag // in the signal handler and check that flag later. // // We provide setSignalFlag as the most generic signal handler. This handler uses a // single sig_atomic_t as a bit field. On Linux, sig_atomic_t is equivalent to a signed int, // theoretically providing 32 binary flags; in practice, most likely signals for which we may // want to install signal handlers are // - SIGTERM (15): which by default signals the request for a graceful shutdown // - SIGUSR1 (10): intended for custom use, default action in Linux is termination // - SIGUSR2 (12): intended for custom use, default action in Linux is termination // - SIGINT (2): interrupt from the console // Just to be safe, we accommodate signals up to signal No. 30. // In addition, we also provide requestSaveAndExit() and saveAndExit() as a signal // handler/checker for graceful shutdown requests during training. constexpr int maxSignalForSetSignalFlag{30}; // Make sure sig_atomic_t is large enough as a bit field for our purposes. // That said, I'm not aware of any platform where this would be a problem. static_assert(SIG_ATOMIC_MAX > (1U< maxSignalForSetSignalFlag, "Signal out of range (must be < {}, is {}).", maxSignalForSetSignalFlag, sig); // Do bitwise AND between sigflags_ and an int value that has exactly one bit set that // corresponds to the signal in question. If the bit is set (see setSignalFlag above), // the bitwise AND will return a non-zero integer, if it is not set, the result will // be zero. return (sigflags_ & (1<