Tomographer  v2.0
Tomographer C++ Framework Documentation
signal_handler.h
Go to the documentation of this file.
1 /* This file is part of the Tomographer project, which is distributed under the
2  * terms of the MIT license.
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2016 ETH Zurich, Institute for Theoretical Physics, Philippe Faist
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
27 
28 #ifndef TOMOGRAPHER_TOOLS_SIGNAL_HANDLER
29 #define TOMOGRAPHER_TOOLS_SIGNAL_HANDLER
30 
31 
38 #include <signal.h>
39 
40 #include <cstdio>
41 #include <ctime>
42 
43 #include <chrono>
44 
45 
46 namespace Tomographer
47 {
48 namespace Tools
49 {
50 
57 {
58  SignalHandler() { }
59  virtual ~SignalHandler() { }
60 
65  virtual void handleSignal(int sig_num) = 0;
66 };
67 
68 
69 
70 #ifndef TOMOGRAPHER_SIG_HANDLER_REPEAT_EXIT_DELAY
71 #define TOMOGRAPHER_SIG_HANDLER_REPEAT_EXIT_DELAY 2
72 #endif
73 
74 namespace tomo_internal {
75  static std::time_t last_sig_hit_time[NSIG] = { 0 };
76  static SignalHandler * signal_handler[NSIG] = { NULL };
77 
78  void signal_dispatch_fn(int signum)
79  {
80  assert(signum >= 0 && signum < NSIG && "signum out of range 0...NSIG !") ;
81 
82 #if defined(__MINGW32__) || defined(__MINGW64__)
83  // re-attach handler---seems like it's needed on Windows---why??
84  signal(signum, tomo_internal::signal_dispatch_fn);
85 #endif
86 
87  std::fprintf(stderr, "\n*** interrupt (%d)\n", signum);
88  std::time_t now;
89 
90  time(&now);
91 
92  if ( (now - tomo_internal::last_sig_hit_time[signum]) < TOMOGRAPHER_SIG_HANDLER_REPEAT_EXIT_DELAY ) {
93  // two interrupts within two seconds --> exit
94  std::fprintf(stderr, "\n*** Exit\n");
95  ::exit(1);
96  return;
97  }
98 
99  tomo_internal::last_sig_hit_time[signum] = now;
100 
101  if (signal_handler[signum] != NULL) {
102  signal_handler[signum]->handleSignal(signum);
103  } else {
104  std::fprintf(stderr, "Warning: sig_handle: no signal handler set (got signal %d)\n", signum);
105  }
106 
107  }
108 }
109 
110 
116 void installSignalHandler(int signum, SignalHandler * sobj)
117 {
118  tomo_internal::signal_handler[signum] = sobj;
119  signal(signum, tomo_internal::signal_dispatch_fn);
120 }
121 
122 
123 } // namespace Tools
124 } // namespace Tomographer
125 
126 
127 #endif
Base namespace for the Tomographer project.
Definition: densellh.h:44
An abstract signal handler (C++ interface)
void installSignalHandler(int signum, SignalHandler *sobj)
Installs the given signal handler to catch the signal signum.
virtual void handleSignal(int sig_num)=0
Perform some action in reaction to a signal.
T fprintf(T...args)