pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
signalhandler.h
1 /*PGR-GNU*****************************************************************
2 
3 Copyright (c) 2015 pgRouting developers
4 Mail: project@pgrouting.org
5 
6 ------
7 
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 
22 ********************************************************************PGR-GNU*/
23 /*PGR-MIT******************************************************************
24  *
25  * file signalhandler.h
26  *
27  * Copyright 2014 Stephen Woodbridge <woodbri@imaptools.com>
28  * Copyright 2014 Vicky Vergara <vicky_vergara@hotmail.com>
29  *
30  * This is free software; you can redistribute and/or modify it under
31  * the terms of the MIT License. Please file MIT-LICENSE for details.
32  *
33  *****************************************************************PGR-MIT*/
34 
35 #ifndef SRC_COMMON_SRC_SIGNALHANDLER_H_
36 #define SRC_COMMON_SRC_SIGNALHANDLER_H_
37 
38 #include <stddef.h> // defines NULL
39 #include <csignal>
40 #include <exception>
41 
42 // #include "./pgr_assert.h"
43 
44 class UserQuitException: public std::exception {
45  private:
46  const char *str;
47 
48  public:
49  virtual const char *what() const throw() {
50  return str;
51  }
52 
53  explicit UserQuitException(const char *_str): str(_str) {}
54 };
55 
56 
57 class EventHandler {
58  public:
59  // Hook method for the signal hook method.
60  virtual void handleSignal(int signum) = 0;
61 
62  // ... other hook methods for other types of
63  // events such as timers, I/O, and
64  // synchronization objects.
65 };
66 
67 
69  public:
70  // Entry point.
71  static SignalHandler *instance(void);
72 
73  // Register an event handler <eh> for <signum>
74  // and return a pointer to any existing <EventHandler>
75  // that was previously registered to handle <signum>.
76  EventHandler *registerHandler(int signum, EventHandler *eh);
77 
78  // Remove the <EventHandler> for <signum>
79  // by setting the slot in the <signalHandlers_>
80  // table to NULL.
81  void removeHandler(int signum);
82 
83  private:
84  // Ensure we're a Singleton.
85  SignalHandler(void) {}
86 
87  // Singleton pointer.
88  static SignalHandler *instance_;
89 
90  // Entry point adapter installed into <sigaction>
91  // (must be a static method or a stand-alone
92  // extern "C" function).
93  static void dispatcher(int signum);
94 
95  // Table of pointers to concrete <EventHandler>s
96  // registered by applications. NSIG is the number of
97  // signals defined in </usr/include/sys/signal.h>.
98  static EventHandler *signalHandlers_[NSIG];
99 };
100 
101 
102 // ---------------------------------------------------------
103 // -- some concrete signal handlers
104 // ---------------------------------------------------------
105 
107  public:
108  SIGINT_Handler(void): graceful_quit_(0) {}
109 
110  // Hook method.
111  virtual void handleSignal(int signum) {
112  if (signum == SIGINT) this->graceful_quit_ = 1;
113  }
114 
115  // Accessor.
116  sig_atomic_t gracefulQuit(void) { return this->graceful_quit_;}
117 
118  private:
119  sig_atomic_t graceful_quit_;
120 };
121 
122 
124  public:
125  SIGQUIT_Handler(void): abortive_quit_(0) {}
126 
127  // Hook method.
128  virtual void handleSignal(int signum) {
129  if (signum == SIGQUIT) this->abortive_quit_ = 1;
130  }
131 
132  // Accessor.
133  sig_atomic_t abortiveQuit(void) { return this->abortive_quit_;}
134 
135  private:
136  sig_atomic_t abortive_quit_;
137 };
138 
139 
140 #define REG_SIGINT SIGINT_Handler sigint_handler; \
141  SignalHandler::instance()->registerHandler(SIGINT, &sigint_handler);
142 
143 #define REG_SIGQUIT SIGQUIT_Handler sigquit_handler; \
144  SignalHandler::instance()->registerHandler(SIGQUIT, &sigquit_handler);
145 
146 #define THROW_ON_SIGINT do { \
147  if ( sigint_handler.gracefulQuit() == 1 ) \
148  throw(UserQuitException("Abort on User Request!")); \
149  } while (0);
150 
151 #endif // SRC_COMMON_SRC_SIGNALHANDLER_H_
152 
const char * str
str Holds the what() string for the exception.
Definition: signalhandler.h:46