Scarab  v3.9.1
Project 8 C++ Utility Library
signal_handler.hh
Go to the documentation of this file.
1 /*
2  * signal_handler.hh
3  *
4  * Created on: Dec 3, 2013
5  * Author: N.S. Oblath
6  */
7 
8 #ifndef SCARAB_SIGNAL_HANDLER_HH_
9 #define SCARAB_SIGNAL_HANDLER_HH_
10 
11 #include "scarab_api.hh"
12 #include "member_variables.hh"
13 
14 #include <memory>
15 #include <mutex>
16 #include <map>
17 
18 #ifndef _WIN32
19 #include <signal.h> // for struct sigaction, which is in signal.h but not csignal
20 #endif
21 
22 #ifndef _GNU_SOURCE
23 #define _GNU_SOURCE
24 #endif
25 #ifndef __USE_GNU
26 #define __USE_GNU
27 #endif
28 
29 
30 namespace scarab
31 {
32  class cancelable;
33 
34 
115  {
116  public:
117  signal_handler();
118  virtual ~signal_handler();
119 
121  static void add_cancelable( std::shared_ptr< cancelable > a_cancelable );
123  static void remove_cancelable( std::shared_ptr< cancelable > a_cancelable );
125  static void remove_cancelable( cancelable* a_cancelable );
126 
128  static void reset();
129 
131  static void handle_signals();
133  static void unhandle_signals();
135  static bool is_handling( int a_signal );
136 
138  [[noreturn]] static void handle_terminate() noexcept;
139 
141  static void handle_exit_error( int a_sig );
142 
144  static void handle_exit_success( int a_sig );
145 
147  [[noreturn]] static void terminate( int a_code ) noexcept;
148 
150  static void exit( int a_code );
151 
153  static void print_current_exception( bool a_use_logging );
154 
156  static void print_stack_trace( bool a_use_logging );
157 
159  static void cancel_all( int a_code );
160 
161  private:
162 
163  typedef std::weak_ptr< cancelable > cancelable_wptr_t;
164  // technical note: std::map<cancelable*, cancelable_wptr_t> is used instead of std::set<cancelable_wptr_t> because
165  // weak_ptr cannot be used as a key because weak_ptrs are not immutable (see, e.g. https://stackoverflow.com/a/32668849 and one of the comments therein).
166  // the raw pointer is only used as a key here. the weak_ptr is used for accessing the cancelable.
167  typedef std::map< cancelable*, cancelable_wptr_t > cancelers;
168  typedef cancelers::const_iterator cancelers_cit_t;
169  typedef cancelers::iterator cancelers_it_t;
170 
171  static cancelers s_cancelers;
172  static std::recursive_mutex s_mutex;
173 
174  public:
175  mv_accessible_static_noset( int, ref_count );
176 
177  mv_accessible_static_noset( bool, exited );
178  mv_accessible_static( int, return_code );
179 
180  mv_accessible_static_noset( bool, handling_sig_abrt );
181  mv_accessible_static_noset( bool, handling_sig_term );
182  mv_accessible_static_noset( bool, handling_sig_int );
183  mv_accessible_static_noset( bool, handling_sig_quit );
184 
185 #ifndef _WIN32
186  mv_referrable_static( struct sigaction, old_sig_abrt_action );
187  mv_referrable_static( struct sigaction, old_sig_term_action );
188  mv_referrable_static( struct sigaction, old_sig_int_action );
189  mv_referrable_static( struct sigaction, old_sig_quit_action );
190 #else // _WIN32
191  typedef void (*handler_t)(int);
192  mv_referrable_static( handler_t, old_sig_abrt_handler );
193  mv_referrable_static( handler_t, old_sig_term_handler );
194  mv_referrable_static( handler_t, old_sig_int_handler );
195  mv_referrable_static( handler_t, old_sig_quit_handler );
196 #endif
197  };
198 
199 } /* namespace scarab */
200 #endif /* SCARAB_SIGNAL_HANDLER_HH_ */
std::weak_ptr< cancelable > cancelable_wptr_t
#define mv_accessible_static_noset
cancelers::iterator cancelers_it_t
static cancelers s_cancelers
cancelers::const_iterator cancelers_cit_t
#define SCARAB_API
Definition: scarab_api.hh:24
std::map< cancelable *, cancelable_wptr_t > cancelers
Deals with cleanly exiting an application, and includes signal and std::terminate handler functions...
#define mv_accessible_static
static std::recursive_mutex s_mutex
Base class for a cancelable object (i.e. an object that can be canceled by scarab::signal_handler or ...
Definition: cancelable.hh:51
#define mv_referrable_static