PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
signalhandler.h
Go to the documentation of this file.
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 #pragma once
38 
39 // defines NULL
40 #include <stddef.h>
41 #include <csignal>
42 #include <exception>
43 
44 // #include "./pgr_assert.h"
45 
46 class UserQuitException: public std::exception {
47  private:
48  const char *str;
49 
50  public:
51  virtual const char *what() const throw() {
52  return str;
53  }
54 
55  explicit UserQuitException(const char *_str): str(_str) {}
56 };
57 
58 
59 class EventHandler {
60  public:
61  // Hook method for the signal hook method.
62  virtual void handleSignal(int signum) = 0;
63 
64  // ... other hook methods for other types of
65  // events such as timers, I/O, and
66  // synchronization objects.
67 };
68 
69 
71  public:
72  // Entry point.
73  static SignalHandler *instance(void);
74 
75  // Register an event handler <eh> for <signum>
76  // and return a pointer to any existing <EventHandler>
77  // that was previously registered to handle <signum>.
78  EventHandler *registerHandler(int signum, EventHandler *eh);
79 
80  // Remove the <EventHandler> for <signum>
81  // by setting the slot in the <signalHandlers_>
82  // table to NULL.
83  void removeHandler(int signum);
84 
85  private:
86  // Ensure we're a Singleton.
87  SignalHandler(void) {}
88 
89  // Singleton pointer.
91 
92  // Entry point adapter installed into <sigaction>
93  // (must be a static method or a stand-alone
94  // extern "C" function).
95  static void dispatcher(int signum);
96 
97  // Table of pointers to concrete <EventHandler>s
98  // registered by applications. NSIG is the number of
99  // signals defined in </usr/include/sys/signal.h>.
101 };
102 
103 
104 // ---------------------------------------------------------
105 // -- some concrete signal handlers
106 // ---------------------------------------------------------
107 
109  public:
111 
112  // Hook method.
113  virtual void handleSignal(int signum) {
114  if (signum == SIGINT) this->graceful_quit_ = 1;
115  }
116 
117  // Accessor.
118  sig_atomic_t gracefulQuit(void) { return this->graceful_quit_;}
119 
120  private:
121  sig_atomic_t graceful_quit_;
122 };
123 
124 
126  public:
128 
129  // Hook method.
130  virtual void handleSignal(int signum) {
131  if (signum == SIGQUIT) this->abortive_quit_ = 1;
132  }
133 
134  // Accessor.
135  sig_atomic_t abortiveQuit(void) { return this->abortive_quit_;}
136 
137  private:
138  sig_atomic_t abortive_quit_;
139 };
140 
141 
142 #define REG_SIGINT SIGINT_Handler sigint_handler; \
143  SignalHandler::instance()->registerHandler(SIGINT, &sigint_handler);
144 
145 #define REG_SIGQUIT SIGQUIT_Handler sigquit_handler; \
146  SignalHandler::instance()->registerHandler(SIGQUIT, &sigquit_handler);
147 
148 #define THROW_ON_SIGINT do { \
149  if ( sigint_handler.gracefulQuit() == 1 ) \
150  throw(UserQuitException("Abort on User Request!")); \
151  } while (0);
152 
153 #endif // SRC_COMMON_SRC_SIGNALHANDLER_H_
154 
virtual void handleSignal(int signum)=0
static SignalHandler * instance_
Definition: signalhandler.h:90
sig_atomic_t abortive_quit_
sig_atomic_t abortiveQuit(void)
SignalHandler(void)
Definition: signalhandler.h:87
virtual const char * what() const
Definition: signalhandler.h:51
sig_atomic_t gracefulQuit(void)
UserQuitException(const char *_str)
Definition: signalhandler.h:55
EventHandler * registerHandler(int signum, EventHandler *eh)
virtual void handleSignal(int signum)
static EventHandler * signalHandlers_[NSIG]
void removeHandler(int signum)
sig_atomic_t graceful_quit_
virtual void handleSignal(int signum)
static void dispatcher(int signum)
const char * str
str Holds the what() string for the exception.
Definition: signalhandler.h:48
static SignalHandler * instance(void)