Scarab  3.6.0
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 
19 #ifndef _GNU_SOURCE
20 #define _GNU_SOURCE
21 #endif
22 #ifndef __USE_GNU
23 #define __USE_GNU
24 #endif
25 
26 
27 namespace scarab
28 {
29  class cancelable;
30 
31 
71  {
72  public:
74  virtual ~signal_handler();
75 
77  void add_cancelable( std::shared_ptr< cancelable > a_cancelable );
79  void remove_cancelable( std::shared_ptr< cancelable > a_cancelable );
81  void remove_cancelable( cancelable* a_cancelable );
82 
84  void reset();
85 
87  static void add_cancelable_s( std::shared_ptr< cancelable > a_cancelable );
89  static void remove_cancelable_s( std::shared_ptr< cancelable > a_cancelable );
91  static void remove_cancelable_s( cancelable* a_cancelable );
92 
94  [[noreturn]] static void handle_terminate() noexcept;
95 
97  static void handle_exit_error( int a_sig );
98 
100  static void handle_exit_success( int a_sig );
101 
103  [[noreturn]] static void terminate( int a_code ) noexcept;
104 
106  static void exit( int a_code );
107 
109  static void print_current_exception( bool a_use_logging );
110 
112  static void print_stack_trace( bool a_use_logging );
113 
115  static void cancel_all( int a_code );
116 
117  private:
118 
119  typedef std::weak_ptr< cancelable > cancelable_wptr_t;
120  // technical note: std::map<cancelable*, cancelable_wptr_t> is used instead of std::set<cancelable_wptr_t> because
121  // 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).
122  // the raw pointer is only used as a key here. the weak_ptr is used for accessing the cancelable.
123  typedef std::map< cancelable*, cancelable_wptr_t > cancelers;
124  typedef cancelers::const_iterator cancelers_cit_t;
125  typedef cancelers::iterator cancelers_it_t;
126 
127  static cancelers s_cancelers;
128  static std::recursive_mutex s_mutex;
129 
130  public:
131  mv_accessible_static_noset( bool, exited );
132  mv_accessible_static( int, return_code );
133 
134  mv_accessible_static_noset( bool, handling_sig_abrt );
135  mv_accessible_static_noset( bool, handling_sig_term );
136  mv_accessible_static_noset( bool, handling_sig_int );
137  mv_accessible_static_noset( bool, handling_sig_quit );
138 
139  };
140 
141 } /* namespace scarab */
142 #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