MythTV  master
signalhandling.h
Go to the documentation of this file.
1 #ifndef SIGNALHANDLING_H_
2 #define SIGNALHANDLING_H_
3 
4 #include <QObject>
5 #include <QSocketNotifier>
6 #include <QMutex>
7 #include <QList>
8 #include <QMap>
9 
10 #include <array>
11 #include <cstdint>
12 #include <csignal>
13 #include <unistd.h>
14 
15 #include "mythbaseexp.h" // MBASE_PUBLIC , etc.
16 
17 #ifdef _WIN32
18 // Quick fix to let this compile for Windows: we have it disabled on the
19 // calling side for Windows anyways, IIRC.
20 using siginfo_t = void;
21 #endif
22 
23 using SigHandlerFunc = void (*)(void);
24 
26 class MBASE_PUBLIC SignalHandler: public QObject
27 {
28  Q_OBJECT
29 
30  public:
31  static void Init(QList<int> &signallist, QObject *parent = nullptr);
32  static void Done(void);
33 
34  static void SetHandler(int signum, SigHandlerFunc handler);
35 
36  static bool IsExiting(void) { return s_exit_program; }
37 
38  // Unix signal handler.
39  // context is of type ucontext_t * cast to void *
40  static void signalHandler(int signum, siginfo_t *info, void *context);
41 
42  public slots:
43  // Qt signal handler.
44  void handleSignal(void);
45 
46  private:
47  SignalHandler(QList<int> &signallist, QObject *parent);
48  ~SignalHandler() override;
49  void SetHandlerPrivate(int signum, SigHandlerFunc handler);
50 
51  static std::array<int,2> s_sigFd;
52  static volatile bool s_exit_program;
53  QSocketNotifier *m_notifier {nullptr};
54  char *m_sigStack {nullptr};
55 
56  QMutex m_sigMapLock;
57  QMap<int, SigHandlerFunc> m_sigMap;
58  static QList<int> s_defaultHandlerList;
59 
60  static QMutex s_singletonLock;
62 };
63 
64 #endif
65 
66 /*
67  * vim:ts=4:sw=4:ai:et:si:sts=4
68  */
SignalHandler::s_sigFd
static std::array< int, 2 > s_sigFd
Definition: signalhandling.h:51
SigHandlerFunc
void(*)(void) SigHandlerFunc
Definition: signalhandling.h:23
SignalHandler
A container object to handle UNIX signals in the Qt space correctly.
Definition: signalhandling.h:27
mythbaseexp.h
SignalHandler::m_sigMapLock
QMutex m_sigMapLock
Definition: signalhandling.h:56
SignalHandler::s_singletonLock
static QMutex s_singletonLock
Definition: signalhandling.h:60
SignalHandler::s_exit_program
static volatile bool s_exit_program
Definition: signalhandling.h:52
SignalHandler::s_defaultHandlerList
static QList< int > s_defaultHandlerList
Definition: signalhandling.h:58
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
SignalHandler::s_singleton
static SignalHandler * s_singleton
Definition: signalhandling.h:61
siginfo_t
void siginfo_t
Definition: signalhandling.h:20
SignalHandler::m_sigMap
QMap< int, SigHandlerFunc > m_sigMap
Definition: signalhandling.h:57
SignalHandler::IsExiting
static bool IsExiting(void)
Definition: signalhandling.h:36