-
Notifications
You must be signed in to change notification settings - Fork 9
/
signal.d
216 lines (194 loc) · 6.91 KB
/
signal.d
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
module concurrency.signal;
import concurrency.stoptoken;
shared(StopSource) globalStopSource() @trusted {
import core.atomic : atomicLoad, cas;
if (globalSource.atomicLoad is null) {
import concurrency.utils : dynamicLoad;
auto ptr = getGlobalStopSourcePointer();
if (auto source = (*ptr).atomicLoad) {
globalSource = source;
return globalSource;
}
auto tmp = new shared StopSource();
if (ptr.cas(cast(shared StopSource)null, tmp)) {
setupCtrlCHandler(tmp);
globalSource = tmp;
} else
globalSource = (*ptr).atomicLoad;
}
return globalSource;
}
/// Returns true if first to set (otherwise it is ignored)
bool setGlobalStopSource(shared StopSource stopSource) @safe {
import core.atomic : cas;
auto ptr = getGlobalStopSourcePointer();
if (!ptr.cas(cast(shared StopSource)null, stopSource))
return false;
globalSource = stopSource;
return true;
}
/// Sets the stopSource to be called when receiving an interrupt
void setupCtrlCHandler(shared StopSource stopSource) @trusted {
import core.atomic;
if (stopSource is null)
return;
auto old = atomicExchange(&SignalHandler.signalStopSource, stopSource);
if (old !is null)
return;
SignalHandler.setup();
SignalHandler.launchHandlerThread();
version (Windows) {
import core.sys.windows.windows;
SetConsoleCtrlHandler(&signalHandler, true);
} else {
import core.sys.posix.signal;
static void handleSignal(int s) @trusted {
sigaction_t old;
sigset_t sigset;
sigemptyset(&sigset);
sigaction_t siginfo;
siginfo.sa_handler = &signalHandler;
siginfo.sa_mask = sigset;
siginfo.sa_flags = SA_RESTART;
sigaction(s, &siginfo, &old);
// TODO: what to do with old?
}
handleSignal(SIGINT);
handleSignal(SIGTERM);
}
}
private static shared StopSource globalSource;
// a mixin for OS-specific visibility
private mixin template globalStopSourcePointerImpl() {
// should not be called directly by usercode, call `getGlobalStopSourcePointer` instead
pragma(inline, false)
extern(C) shared(StopSource*) concurrency_globalStopSourcePointer() @safe {
return &globalSource;
}
}
// We need to make sure all binaries (executable and shared libraries) in the
// process share a single StopSource instance.
version(Windows) {
// On Windows, the executable can export `concurrency_globalStopSourcePointer`
// explicitly via linker flag `/EXPORT:concurrency_globalStopSourcePointer`.
// DLLs containing `concurrency` use the executable's then, falling back to
// their own definition.
private mixin globalStopSourcePointerImpl;
private shared(StopSource*) getGlobalStopSourcePointer() @safe {
import concurrency.utils : dynamicLoad;
return dynamicLoad!concurrency_globalStopSourcePointer()();
}
} else {
// Make sure the `concurrency_globalStopSourcePointer` function gets public
// visibility; coupled with the `--export-dynamic-symbol=…` linker flag in
// dub.sdl, the symbol is then exported as dynamic symbol from every binary
// containing this `concurrency` library, and the dynamic loader uniques the
// symbol across the whole process for us.
export mixin globalStopSourcePointerImpl;
alias getGlobalStopSourcePointer = concurrency_globalStopSourcePointer;
}
struct SignalHandler {
import core.atomic : atomicStore, atomicLoad, MemoryOrder, atomicExchange;
import core.thread : Thread;
static shared int lastSignal; // last signal received
enum int ABORT = -1;
version (Windows) {
import core.sync.event : Event;
private static shared Event event; // used to notify the dedicated thread to shutdown
static void notify(int num) nothrow @nogc @trusted {
lastSignal.atomicStore!(MemoryOrder.rel)(num);
(cast()event).set();
}
private static int await() nothrow @nogc @trusted {
(cast()event).wait();
return lastSignal.atomicLoad!(MemoryOrder.acq)();
}
private static void setup() @trusted {
(cast()event).initialize(false, false);
}
} else version (linux) {
import core.sys.posix.unistd : write, read;
private static shared int event; // eventfd to notify dedicated thread
static void notify(int num) nothrow @nogc {
lastSignal.atomicStore!(MemoryOrder.rel)(num);
ulong b = 1;
write(event, &b, typeof(b).sizeof);
}
private static int await() nothrow @nogc {
ulong b;
while(read(event, &b, typeof(b).sizeof) != typeof(b).sizeof) {}
return lastSignal.atomicLoad!(MemoryOrder.acq)();
}
private static void setup() {
import core.sys.linux.sys.eventfd;
event = eventfd(0, EFD_CLOEXEC);
}
} else version (Posix) {
import core.sys.posix.unistd : write, read, pipe;
private static shared int[2] selfPipe; // self pipe to notify dedicated thread
static void notify(int num) nothrow @nogc {
lastSignal.atomicStore!(MemoryOrder.rel)(num);
ulong b = 1;
write(selfPipe[1], &b, typeof(b).sizeof);
}
private static int await() nothrow @nogc {
ulong b;
while(read(cast()selfPipe[0], &b, typeof(b).sizeof) != typeof(b).sizeof) {}
return lastSignal.atomicLoad!(MemoryOrder.acq)();
}
private static void setup() {
import std.exception : ErrnoException;
if (pipe(cast(int[2])selfPipe) == -1)
throw new ErrnoException("Failed to create self-pipe");
}
}
private static void shutdown() {
if (atomicLoad!(MemoryOrder.acq)(signalStopSource) !is null)
SignalHandler.notify(ABORT);
}
private static shared StopSource signalStopSource;
private static shared Thread handlerThread;
private static void launchHandlerThread() {
if (handlerThread.atomicLoad !is null)
return;
auto thread = new Thread((){
for(;;) {
if (SignalHandler.await() == ABORT) {
return;
}
signalStopSource.stop();
}
});
// This has to be a daemon thread otherwise the runtime will wait on it before calling the shared module destructor that stops it.
thread.isDaemon = true;
if (atomicExchange(&handlerThread, cast(shared)thread) !is null)
return; // someone beat us to it
thread.start();
}
}
/// This is required to properly shutdown in the presence of sanitizers
shared static ~this() {
import core.atomic : atomicExchange;
import core.thread : Thread;
SignalHandler.shutdown();
if (auto thread = atomicExchange(&SignalHandler.handlerThread, null))
(cast()thread).join();
}
version (Windows) {
import core.sys.windows.windows;
extern (Windows) static BOOL signalHandler(DWORD dwCtrlType) nothrow @system {
import core.stdc.signal;
if (dwCtrlType == CTRL_C_EVENT ||
dwCtrlType == CTRL_BREAK_EVENT ||
dwCtrlType == CTRL_CLOSE_EVENT ||
dwCtrlType == CTRL_SHUTDOWN_EVENT) {
SignalHandler.notify(SIGINT);
return TRUE;
}
return FALSE;
}
} else {
extern (C) static void signalHandler(int i) nothrow @nogc {
SignalHandler.notify(i);
}
}