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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#ifdef __sun
# define _POSIX_PTHREAD_SEMANTICS
#endif
#include <IceUtil/CtrlCHandler.h>
#include <IceUtil/MutexPtrLock.h>
#include <IceUtil/Mutex.h>
#ifndef _WIN32
# include <signal.h>
#endif
using namespace std;
using namespace IceUtil;
namespace
{
CtrlCHandlerCallback _callback = 0;
const CtrlCHandler* _handler = 0;
IceUtil::Mutex* globalMutex = 0;
class Init
{
public:
Init()
{
globalMutex = new IceUtil::Mutex;
}
~Init()
{
delete globalMutex;
globalMutex = 0;
}
};
Init init;
}
CtrlCHandlerException::CtrlCHandlerException(const char* file, int line) :
Exception(file, line)
{
}
static const char* ctrlCHandlerName = "IceUtil::CtrlCHandlerException";
string
CtrlCHandlerException::ice_name() const
{
return ctrlCHandlerName;
}
Exception*
CtrlCHandlerException::ice_clone() const
{
return new CtrlCHandlerException(*this);
}
void
CtrlCHandlerException::ice_throw() const
{
throw *this;
}
void
CtrlCHandler::setCallback(CtrlCHandlerCallback callback)
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(globalMutex);
_callback = callback;
}
CtrlCHandlerCallback
CtrlCHandler::getCallback() const
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(globalMutex);
return _callback;
}
#ifdef _WIN32
static BOOL WINAPI handlerRoutine(DWORD dwCtrlType)
{
CtrlCHandlerCallback callback = _handler->getCallback();
if(callback != 0)
{
callback(dwCtrlType);
}
return TRUE;
}
CtrlCHandler::CtrlCHandler(CtrlCHandlerCallback callback)
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(globalMutex);
bool handler = _handler != 0;
if(handler)
{
throw CtrlCHandlerException(__FILE__, __LINE__);
}
else
{
_callback = callback;
_handler = this;
lock.release();
SetConsoleCtrlHandler(handlerRoutine, TRUE);
}
}
CtrlCHandler::~CtrlCHandler()
{
SetConsoleCtrlHandler(handlerRoutine, FALSE);
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(globalMutex);
_handler = 0;
}
}
#else
extern "C"
{
static void*
sigwaitThread(void*)
{
sigset_t ctrlCLikeSignals;
sigemptyset(&ctrlCLikeSignals);
sigaddset(&ctrlCLikeSignals, SIGHUP);
sigaddset(&ctrlCLikeSignals, SIGINT);
sigaddset(&ctrlCLikeSignals, SIGTERM);
//
// Run until I'm cancelled (in sigwait())
//
for(;;)
{
int signal = 0;
int rc = sigwait(&ctrlCLikeSignals, &signal);
#if defined(__APPLE__)
//
// WORKAROUND: sigwait is not a cancelation point on MacOS X. To cancel this thread, the
// destructor cancels the thread and send a signal to the thread to unblock sigwait, then
// we explicitly test for cancellation.
//
pthread_testcancel();
#endif
//
// Some sigwait() implementations incorrectly return EINTR
// when interrupted by an unblocked caught signal
//
if(rc == EINTR)
{
continue;
}
assert(rc == 0);
rc = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
assert(rc == 0);
CtrlCHandlerCallback callback = _handler->getCallback();
if(callback != 0)
{
callback(signal);
}
rc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
assert(rc == 0);
}
return 0;
}
}
namespace
{
pthread_t _tid;
}
CtrlCHandler::CtrlCHandler(CtrlCHandlerCallback callback)
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(globalMutex);
bool handler = _handler != 0;
if(handler)
{
throw CtrlCHandlerException(__FILE__, __LINE__);
}
else
{
_callback = callback;
_handler = this;
lock.release();
// We block these CTRL+C like signals in the main thread,
// and by default all other threads will inherit this signal
// mask.
sigset_t ctrlCLikeSignals;
sigemptyset(&ctrlCLikeSignals);
sigaddset(&ctrlCLikeSignals, SIGHUP);
sigaddset(&ctrlCLikeSignals, SIGINT);
sigaddset(&ctrlCLikeSignals, SIGTERM);
int rc = pthread_sigmask(SIG_BLOCK, &ctrlCLikeSignals, 0);
assert(rc == 0);
// Joinable thread
rc = pthread_create(&_tid, 0, sigwaitThread, 0);
assert(rc == 0);
}
}
CtrlCHandler::~CtrlCHandler()
{
int rc = pthread_cancel(_tid);
assert(rc == 0);
#if defined(__APPLE__)
//
// WORKAROUND: sigwait isn't a cancellation point on MacOS X, see
// comment in sigwaitThread
//
rc = pthread_kill(_tid, SIGTERM);
//assert(rc == 0); For some reaosns, this assert is sometime triggered
#endif
void* status = 0;
rc = pthread_join(_tid, &status);
assert(rc == 0);
#if !defined(__APPLE__)
assert(status == PTHREAD_CANCELED);
#endif
{
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(globalMutex);
_handler = 0;
}
}
#endif
|