File: signalhandler.h

package info (click to toggle)
pgrouting 3.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 16,520 kB
  • sloc: sql: 38,763; cpp: 21,049; ansic: 13,171; perl: 1,781; sh: 804; xml: 182; makefile: 48
file content (154 lines) | stat: -rw-r--r-- 4,370 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*PGR-GNU*****************************************************************

Copyright (c) 2015 pgRouting developers
Mail: project@pgrouting.org

------

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 ********************************************************************PGR-GNU*/
/*PGR-MIT******************************************************************
 *
 * file signalhandler.h
 *
 * Copyright 2014 Stephen Woodbridge <woodbri@imaptools.com>
 * Copyright 2014 Vicky Vergara <vicky_vergara@hotmail.com>
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the MIT License. Please file MIT-LICENSE for details.
 *
 *****************************************************************PGR-MIT*/

#ifndef INCLUDE_CPP_COMMON_SIGNALHANDLER_H_
#define INCLUDE_CPP_COMMON_SIGNALHANDLER_H_
#pragma once

// defines NULL
#include <stddef.h>
#include <csignal>
#include <exception>

// #include "./pgr_assert.h"

class UserQuitException: public std::exception {
 private:
  const char *str;    ///< str Holds the what() string for the exception.

 public:
  virtual const char *what() const throw() {
    return str;
  }

  explicit UserQuitException(const char *_str): str(_str) {}
};


class EventHandler {
 public:
  // Hook method for the signal hook method.
  virtual void handleSignal(int signum) = 0;

  // ... other hook methods for other types of
  // events such as timers, I/O, and
  // synchronization objects.
};


class SignalHandler {
 public:
  // Entry point.
  static SignalHandler *instance(void);

  // Register an event handler <eh> for <signum>
  // and return a pointer to any existing <EventHandler>
  // that was previously registered to handle <signum>.
  EventHandler *registerHandler(int signum, EventHandler *eh);

  // Remove the <EventHandler> for <signum>
  // by setting the slot in the <signalHandlers_>
  // table to NULL.
  void removeHandler(int signum);

 private:
  // Ensure we're a Singleton.
  SignalHandler(void) {}

  // Singleton pointer.
  static SignalHandler *instance_;

  // Entry point adapter installed into <sigaction>
  // (must be a static method or a stand-alone
  // extern "C" function).
  static void dispatcher(int signum);

  // Table of pointers to concrete <EventHandler>s
  // registered by applications.  NSIG is the number of
  // signals defined in </usr/include/sys/signal.h>.
  static EventHandler *signalHandlers_[NSIG];
};


// ---------------------------------------------------------
// -- some concrete signal handlers
// ---------------------------------------------------------

class SIGINT_Handler: public EventHandler {
 public:
  SIGINT_Handler(void): graceful_quit_(0) {}

  // Hook method.
  virtual void handleSignal(int signum) {
    if (signum == SIGINT) this->graceful_quit_ =  1;
  }

  // Accessor.
  sig_atomic_t gracefulQuit(void) { return this->graceful_quit_;}

 private:
  sig_atomic_t graceful_quit_;
};


class SIGQUIT_Handler: public EventHandler {
 public:
  SIGQUIT_Handler(void): abortive_quit_(0) {}

  // Hook method.
  virtual void handleSignal(int signum) {
    if (signum == SIGQUIT) this->abortive_quit_ =  1;
  }

  // Accessor.
  sig_atomic_t abortiveQuit(void) { return this->abortive_quit_;}

 private:
  sig_atomic_t abortive_quit_;
};


#define REG_SIGINT SIGINT_Handler sigint_handler; \
    SignalHandler::instance()->registerHandler(SIGINT, &sigint_handler);

#define REG_SIGQUIT SIGQUIT_Handler sigquit_handler; \
    SignalHandler::instance()->registerHandler(SIGQUIT, &sigquit_handler);

#define THROW_ON_SIGINT do { \
    if ( sigint_handler.gracefulQuit() == 1 ) \
        throw(UserQuitException("Abort on User Request!")); \
    } while (0);

#endif  // INCLUDE_CPP_COMMON_SIGNALHANDLER_H_