This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 424
/
event.d
345 lines (309 loc) · 8.9 KB
/
event.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* The event module provides a primitive for lightweight signaling of other threads
* (emulating Windows events on Posix)
*
* Copyright: Copyright (c) 2019 D Language Foundation
* License: Distributed under the
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
* (See accompanying file LICENSE)
* Authors: Rainer Schuetze
* Source: $(DRUNTIMESRC core/sync/event.d)
*/
module core.sync.event;
version (Windows)
{
import core.sys.windows.basetsd /+: HANDLE +/;
import core.sys.windows.winerror /+: WAIT_TIMEOUT +/;
import core.sys.windows.winbase /+: CreateEvent, CloseHandle, SetEvent, ResetEvent,
WaitForSingleObject, INFINITE, WAIT_OBJECT_0+/;
}
else version (Posix)
{
import core.sys.posix.pthread;
import core.sys.posix.sys.types;
import core.sys.posix.time;
}
else
{
static assert(false, "Platform not supported");
}
import core.time;
import core.internal.abort : abort;
/**
* represents an event. Clients of an event are suspended while waiting
* for the event to be "signaled".
*
* Implemented using `pthread_mutex` and `pthread_condition` on Posix and
* `CreateEvent` and `SetEvent` on Windows.
---
import core.sync.event, core.thread, std.file;
struct ProcessFile
{
ThreadGroup group;
Event event;
void[] buffer;
void doProcess()
{
event.wait();
// process buffer
}
void process(string filename)
{
event.initialize(true, false);
group = new ThreadGroup;
for (int i = 0; i < 10; ++i)
group.create(&doProcess);
buffer = std.file.read(filename);
event.set();
group.joinAll();
event.terminate();
}
}
---
*/
struct Event
{
nothrow @nogc:
/**
* Creates an event object.
*
* Params:
* manualReset = the state of the event is not reset automatically after resuming waiting clients
* initialState = initial state of the signal
*/
this(bool manualReset, bool initialState)
{
initialize(manualReset, initialState);
}
/**
* Initializes an event object. Does nothing if the event is already initialized.
*
* Params:
* manualReset = the state of the event is not reset automatically after resuming waiting clients
* initialState = initial state of the signal
*/
void initialize(bool manualReset, bool initialState)
{
version (Windows)
{
if (m_event)
return;
m_event = CreateEvent(null, manualReset, initialState, null);
m_event || abort("Error: CreateEvent failed.");
}
else version (Posix)
{
if (m_initalized)
return;
pthread_mutex_init(cast(pthread_mutex_t*) &m_mutex, null) == 0 ||
abort("Error: pthread_mutex_init failed.");
static if ( is( typeof( pthread_condattr_setclock ) ) )
{
pthread_condattr_t attr = void;
pthread_condattr_init(&attr) == 0 ||
abort("Error: pthread_condattr_init failed.");
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) == 0 ||
abort("Error: pthread_condattr_setclock failed.");
pthread_cond_init(&m_cond, &attr) == 0 ||
abort("Error: pthread_cond_init failed.");
pthread_condattr_destroy(&attr) == 0 ||
abort("Error: pthread_condattr_destroy failed.");
}
else
{
pthread_cond_init(&m_cond, null) == 0 ||
abort("Error: pthread_cond_init failed.");
}
m_state = initialState;
m_manualReset = manualReset;
m_initalized = true;
}
}
// copying not allowed, can produce resource leaks
@disable this(this);
@disable void opAssign(Event);
~this()
{
terminate();
}
/**
* deinitialize event. Does nothing if the event is not initialized. There must not be
* threads currently waiting for the event to be signaled.
*/
void terminate()
{
version (Windows)
{
if (m_event)
CloseHandle(m_event);
m_event = null;
}
else version (Posix)
{
if (m_initalized)
{
pthread_mutex_destroy(&m_mutex) == 0 ||
abort("Error: pthread_mutex_destroy failed.");
pthread_cond_destroy(&m_cond) == 0 ||
abort("Error: pthread_cond_destroy failed.");
m_initalized = false;
}
}
}
/// Set the event to "signaled", so that waiting clients are resumed
void set()
{
version (Windows)
{
if (m_event)
SetEvent(m_event);
}
else version (Posix)
{
if (m_initalized)
{
pthread_mutex_lock(&m_mutex);
m_state = true;
pthread_cond_broadcast(&m_cond);
pthread_mutex_unlock(&m_mutex);
}
}
}
/// Reset the event manually
void reset()
{
version (Windows)
{
if (m_event)
ResetEvent(m_event);
}
else version (Posix)
{
if (m_initalized)
{
pthread_mutex_lock(&m_mutex);
m_state = false;
pthread_mutex_unlock(&m_mutex);
}
}
}
/**
* Wait for the event to be signaled without timeout.
*
* Returns:
* `true` if the event is in signaled state, `false` if the event is uninitialized or another error occured
*/
bool wait()
{
version (Windows)
{
return m_event && WaitForSingleObject(m_event, INFINITE) == WAIT_OBJECT_0;
}
else version (Posix)
{
return wait(Duration.max);
}
}
/**
* Wait for the event to be signaled with timeout.
*
* Params:
* tmout = the maximum time to wait
* Returns:
* `true` if the event is in signaled state, `false` if the event was nonsignaled for the given time or
* the event is uninitialized or another error occured
*/
bool wait(Duration tmout)
{
version (Windows)
{
if (!m_event)
return false;
auto maxWaitMillis = dur!("msecs")(uint.max - 1);
while (tmout > maxWaitMillis)
{
auto res = WaitForSingleObject(m_event, uint.max - 1);
if (res != WAIT_TIMEOUT)
return res == WAIT_OBJECT_0;
tmout -= maxWaitMillis;
}
auto ms = cast(uint)(tmout.total!"msecs");
return WaitForSingleObject(m_event, ms) == WAIT_OBJECT_0;
}
else version (Posix)
{
if (!m_initalized)
return false;
pthread_mutex_lock(&m_mutex);
int result = 0;
if (!m_state)
{
if (tmout == Duration.max)
{
result = pthread_cond_wait(&m_cond, &m_mutex);
}
else
{
import core.sync.config;
timespec t = void;
mktspec(t, tmout);
result = pthread_cond_timedwait(&m_cond, &m_mutex, &t);
}
}
if (result == 0 && !m_manualReset)
m_state = false;
pthread_mutex_unlock(&m_mutex);
return result == 0;
}
}
private:
version (Windows)
{
HANDLE m_event;
}
else version (Posix)
{
pthread_mutex_t m_mutex;
pthread_cond_t m_cond;
bool m_initalized;
bool m_state;
bool m_manualReset;
}
}
// Test single-thread (non-shared) use.
@nogc nothrow unittest
{
// auto-reset, initial state false
Event ev1 = Event(false, false);
assert(!ev1.wait(1.dur!"msecs"));
ev1.set();
assert(ev1.wait());
assert(!ev1.wait(1.dur!"msecs"));
// manual-reset, initial state true
Event ev2 = Event(true, true);
assert(ev2.wait());
assert(ev2.wait());
ev2.reset();
assert(!ev2.wait(1.dur!"msecs"));
}
unittest
{
import core.thread, core.atomic;
scope event = new Event(true, false);
int numThreads = 10;
shared int numRunning = 0;
void testFn()
{
event.wait(8.dur!"seconds"); // timeout below limit for druntime test_runner
numRunning.atomicOp!"+="(1);
}
auto group = new ThreadGroup;
for (int i = 0; i < numThreads; ++i)
group.create(&testFn);
auto start = MonoTime.currTime;
assert(numRunning == 0);
event.set();
group.joinAll();
assert(numRunning == numThreads);
assert(MonoTime.currTime - start < 5.dur!"seconds");
}