hmbdc
simplify-high-performance-messaging-programming
 All Classes Namespaces Functions Variables Friends Pages
Signals.hpp
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 #include "hmbdc/Exception.hpp"
5 
6 #include <functional>
7 #include <signal.h>
8 #include <memory.h>
9 
10 namespace hmbdc { namespace os {
11 
12 /**
13  * @brief provides functions to handle signals
14  */
15 struct
17  /**
18  * @brief specfy what to do when SIGTERM or SIGINT is received
19  * @details will install signal handlers - might make previous installed
20  * handlers not working, so don't call this more than once
21  *
22  * @param doThis a function<void()> or more lkely a lambda specifying
23  * what to do
24  */
25  static
26  void
27  onTermIntDo(std::function<void()> doThis) {
28  onTermInt_s = doThis;
29 
30  struct sigaction act;
31  memset(&act, 0, sizeof(act));
32  act.sa_sigaction = handler;
33  act.sa_flags = SA_SIGINFO;
34 
35  if (sigaction(SIGTERM, &act, NULL) ||
36  sigaction(SIGINT, &act, NULL)) {
37  HMBDC_THROW(std::runtime_error, "cannot install signal handler");
38  }
39  }
40 
41 private:
42  static std::function<void()> onTermInt_s;
43  static
44  void
45  handler(int signum, siginfo_t *, void *) {
46  onTermInt_s();
47  }
48 };
49 
50 }}
51 
static void onTermIntDo(std::function< void()> doThis)
specfy what to do when SIGTERM or SIGINT is received
Definition: Signals.hpp:27
provides functions to handle signals
Definition: Signals.hpp:15