hmbdc
simplify-high-performance-messaging-programming
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 using namespace std;
13 
14 /**
15  * @brief provides functions to handle signals
16  */
17 struct
19  /**
20  * @brief specfy what to do when SIGTERM or SIGINT is received
21  * @details will install signal handlers - might make previous installed
22  * handlers not working, so don't call this more than once
23  *
24  * @param doThis a function<void()> or more lkely a lambda specifying
25  * what to do
26  */
27  static
28  void
29  onTermIntDo(function<void()> doThis) {
30  onTermInt_s = doThis;
31 
32  struct sigaction act;
33  memset(&act, 0, sizeof(act));
34  act.sa_sigaction = handler;
35  act.sa_flags = SA_SIGINFO;
36 
37  if (sigaction(SIGTERM, &act, NULL) ||
38  sigaction(SIGINT, &act, NULL)) {
39  HMBDC_THROW(runtime_error, "cannot install signal handler");
40  }
41  }
42 
43 private:
44  static function<void()> onTermInt_s;
45  static
46  void
47  handler(int signum, siginfo_t *, void *) {
48  onTermInt_s();
49  }
50 };
51 
52 }}
53 
Definition: TypedString.hpp:74
provides functions to handle signals
Definition: Signals.hpp:17
static void onTermIntDo(function< void()> doThis)
specfy what to do when SIGTERM or SIGINT is received
Definition: Signals.hpp:29
Definition: Client.hpp:11