/
signal.d
174 lines (158 loc) · 5.31 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
module concurrency.signal;
import concurrency.stoptoken;
shared(StopSource) globalStopSource() @trusted {
import core.atomic : atomicLoad;
static StopSource localSource; // can't be shared else it is global
if (localSource !is null)
return cast(shared)localSource;
if (globalSource.atomicLoad is null) {
auto tmp = new shared StopSource();
if (setGlobalStopSource(tmp)) {
setupCtrlCHandler(tmp);
}
}
localSource = cast()globalSource;
return cast(shared)localSource;
}
/// Returns true if first to set (otherwise it is ignored)
bool setGlobalStopSource(shared StopSource stopSource) @safe {
import core.atomic : atomicExchange;
return atomicExchange(&globalSource, stopSource) is null;
}
/// Sets the stopSource to be called when receiving an interrupt
void setupCtrlCHandler(shared StopSource stopSource) @trusted {
import core.sys.posix.signal;
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 {
static void handleSignal(int s) @trusted {
import core.sys.posix.signal;
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;
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();
}
static int await() nothrow @nogc @trusted {
(cast()event).wait();
return lastSignal.atomicLoad!(MemoryOrder.acq)();
}
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);
}
static int await() nothrow @nogc {
ulong b;
while(read(event, &b, typeof(b).sizeof) != typeof(b).sizeof) {}
return lastSignal.atomicLoad!(MemoryOrder.acq)();
}
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);
}
static int await() nothrow @nogc {
ulong b;
while(read(cast()selfPipe[0], &b, typeof(b).sizeof) != typeof(b).sizeof) {}
return lastSignal.atomicLoad!(MemoryOrder.acq)();
}
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;
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.sys.posix.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);
}
}