PGROUTING  3.2
signalhandler.h
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 Copyright (c) 2015 pgRouting developers
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 <[email protected]>
28  * Copyright 2014 Vicky Vergara <[email protected]>
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 INCLUDE_CPP_COMMON_SIGNALHANDLER_H_
36 #define INCLUDE_CPP_COMMON_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 // INCLUDE_CPP_COMMON_SIGNALHANDLER_H_
154 
SIGINT_Handler
Definition: signalhandler.h:108
SignalHandler
Definition: signalhandler.h:70
SIGINT_Handler::SIGINT_Handler
SIGINT_Handler(void)
Definition: signalhandler.h:110
SIGQUIT_Handler::handleSignal
virtual void handleSignal(int signum)
Definition: signalhandler.h:130
SignalHandler::SignalHandler
SignalHandler(void)
Definition: signalhandler.h:87
SignalHandler::dispatcher
static void dispatcher(int signum)
Definition: signalhandler.cpp:73
SignalHandler::signalHandlers_
static EventHandler * signalHandlers_[NSIG]
Definition: signalhandler.h:100
EventHandler::handleSignal
virtual void handleSignal(int signum)=0
UserQuitException
Definition: signalhandler.h:46
SIGINT_Handler::gracefulQuit
sig_atomic_t gracefulQuit(void)
Definition: signalhandler.h:118
SIGINT_Handler::handleSignal
virtual void handleSignal(int signum)
Definition: signalhandler.h:113
SIGQUIT_Handler
Definition: signalhandler.h:125
UserQuitException::UserQuitException
UserQuitException(const char *_str)
Definition: signalhandler.h:55
SIGQUIT_Handler::abortiveQuit
sig_atomic_t abortiveQuit(void)
Definition: signalhandler.h:135
SIGQUIT_Handler::abortive_quit_
sig_atomic_t abortive_quit_
Definition: signalhandler.h:138
SignalHandler::instance_
static SignalHandler * instance_
Definition: signalhandler.h:90
SignalHandler::registerHandler
EventHandler * registerHandler(int signum, EventHandler *eh)
Definition: signalhandler.cpp:47
SignalHandler::instance
static SignalHandler * instance(void)
Definition: signalhandler.cpp:38
SignalHandler::removeHandler
void removeHandler(int signum)
Definition: signalhandler.cpp:67
EventHandler
Definition: signalhandler.h:59
SIGINT_Handler::graceful_quit_
sig_atomic_t graceful_quit_
Definition: signalhandler.h:121
SIGQUIT_Handler::SIGQUIT_Handler
SIGQUIT_Handler(void)
Definition: signalhandler.h:127
UserQuitException::what
virtual const char * what() const
Definition: signalhandler.h:51
UserQuitException::str
const char * str
str Holds the what() string for the exception.
Definition: signalhandler.h:48