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