-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignalManager.cpp
More file actions
150 lines (132 loc) · 4.57 KB
/
SignalManager.cpp
File metadata and controls
150 lines (132 loc) · 4.57 KB
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
#include <stdexcept>
#include <unordered_set>
#if defined DEBUG
#include <iostream>
#endif
#include "SignalManager.hpp"
#include "SignalManagerFactory.hpp"
#include "SignalManagerImpl.hpp"
SignalManager* signalmanager_p = 0;
// Used by the operating system to forward signals to the SignalManager,
// assuming the SignalManager is handling a signal. Use
// SignalManager::registerSignal() to have the SignalManager start handling a
// signal.
extern "C" void handle_signal(int sig)
{
if (signalmanager_p)
{
signalmanager_p->signal(sig);
}
}
//==============================================================================
// Attempts to get a platform-specific signal manager from the signal manager
// factory. If this works then we store the reference and use it going forward.
//==============================================================================
SignalManager::SignalManager() :
signal_manager_impl(0)
{
// Set the pointer used to forward signals to this SignalManager by
// handle_signal
signalmanager_p = this;
signal_manager_impl = SignalManagerFactory::createSignalManager();
if (!signal_manager_impl)
{
throw std::runtime_error(
"Platform-specific signal manager could not be created");
}
}
//==============================================================================
// Deletes the platform-specific signal manager acquired in the constructor
//==============================================================================
SignalManager::~SignalManager()
{
delete signal_manager_impl;
}
//==============================================================================
// Registers a special signal handler for this signal which will start recording
// the signal's delivery status to internal storage. Use isSignalDelivered() to
// retrieve the signal's current delivery status.
//==============================================================================
bool SignalManager::registerSignal(int sig)
{
if (signal_manager_impl)
{
return signal_manager_impl->registerSignal(sig);
}
#if defined DEBUG
std::cerr << "Signal handling not available on this platform, ignoring\n";
#endif
return false;
}
//==============================================================================
// THIS FUNCTION IS ASYNC-SIGNAL-SAFE
// Intended to be called from a signal handler for the purpose of delivering a
// signal. Delivery of signals is recorded in the signals map. Use the
// isSignalDelivered() function to query the delivery status of a particular
// signal.
//==============================================================================
void SignalManager::signal(int sig)
{
if (signal_manager_impl)
{
signal_manager_impl->signal(sig);
}
#if defined DEBUG
else
{
std::cerr <<
"Signal handling not available on this platform, ignoring\n";
}
#endif
}
//==============================================================================
// Safely tests and resets the delivery status of a signal. Delivery status of
// the signal is returned and the delivery status itself is reset to indicate
// that the signal is no longer delivered.
//==============================================================================
bool SignalManager::isSignalDelivered(int sig)
{
if (signal_manager_impl)
{
return signal_manager_impl->isSignalDelivered(sig);
}
#if defined DEBUG
std::cerr << "Signal handling not available on this platform, ignoring\n";
#endif
return false;
}
//==============================================================================
// Fills in a user-provided set with the list of signals we know about
//==============================================================================
void
SignalManager::getSupportedSignals(std::unordered_set<int>& supported_signals)
{
if (signal_manager_impl)
{
signal_manager_impl->getSupportedSignals(supported_signals);
}
#if defined DEBUG
else
{
std::cerr <<
"Signal handling not available on this platform, ignoring\n";
}
#endif
}
//==============================================================================
// Returns the name of the given signal
//==============================================================================
void SignalManager::getSignalName(int sig, std::string& signal_name)
{
if (signal_manager_impl)
{
signal_manager_impl->getSignalName(sig, signal_name);
}
#if defined DEBUG
else
{
std::cerr <<
"Signal handling not available on this platform, ignoring\n";
}
#endif
}