| 1 |
/** |
| 2 |
* The event module provides a primitive for lightweight signaling of other threads |
| 3 |
* (emulating Windows events on Posix) |
| 4 |
* |
| 5 |
* Copyright: Copyright (c) 2019 D Language Foundation |
| 6 |
* License: Distributed under the |
| 7 |
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0). |
| 8 |
* (See accompanying file LICENSE) |
| 9 |
* Authors: Rainer Schuetze |
| 10 |
* Source: $(DRUNTIMESRC core/sync/event.d) |
| 11 |
*/ |
| 12 |
module core.sync.event; |
| 13 |
|
| 14 |
version (Windows) |
| 15 |
{ |
| 16 |
import core.sys.windows.basetsd /+: HANDLE +/; |
| 17 |
import core.sys.windows.winerror /+: WAIT_TIMEOUT +/; |
| 18 |
import core.sys.windows.winbase /+: CreateEvent, CloseHandle, SetEvent, ResetEvent, |
| 19 |
WaitForSingleObject, INFINITE, WAIT_OBJECT_0+/; |
| 20 |
} |
| 21 |
else version (Posix) |
| 22 |
{ |
| 23 |
import core.sys.posix.pthread : pthread_cond_broadcast, pthread_cond_destroy, pthread_cond_init, |
| 24 |
pthread_cond_timedwait, pthread_cond_wait, pthread_mutex_destroy, pthread_mutex_init, pthread_mutex_lock, |
| 25 |
pthread_mutex_unlock; |
| 26 |
import core.sys.posix.sys.types : pthread_cond_t, pthread_mutex_t; |
| 27 |
import core.sys.posix.time : timespec; |
| 28 |
} |
| 29 |
else |
| 30 |
{ |
| 31 |
static assert(false, "Platform not supported"); |
| 32 |
} |
| 33 |
|
| 34 |
import core.internal.abort : abort; |
| 35 |
import core.time; |
| 36 |
|
| 37 |
/** |
| 38 |
* represents an event. Clients of an event are suspended while waiting |
| 39 |
* for the event to be "signaled". |
| 40 |
* |
| 41 |
* Implemented using `pthread_mutex` and `pthread_condition` on Posix and |
| 42 |
* `CreateEvent` and `SetEvent` on Windows. |
| 43 |
--- |
| 44 |
import core.sync.event, core.thread, std.file; |
| 45 |
|
| 46 |
struct ProcessFile |
| 47 |
{ |
| 48 |
ThreadGroup group; |
| 49 |
Event event; |
| 50 |
void[] buffer; |
| 51 |
|
| 52 |
void doProcess() |
| 53 |
{ |
| 54 |
event.wait(); |
| 55 |
// process buffer |
| 56 |
} |
| 57 |
|
| 58 |
void process(string filename) |
| 59 |
{ |
| 60 |
event.initialize(true, false); |
| 61 |
group = new ThreadGroup; |
| 62 |
for (int i = 0; i < 10; ++i) |
| 63 |
group.create(&doProcess); |
| 64 |
|
| 65 |
buffer = std.file.read(filename); |
| 66 |
event.setIfInitialized(); |
| 67 |
group.joinAll(); |
| 68 |
event.terminate(); |
| 69 |
} |
| 70 |
} |
| 71 |
--- |
| 72 |
*/ |
| 73 |
struct Event |
| 74 |
{ |
| 75 |
nothrow @nogc: |
| 76 |
/** |
| 77 |
* Creates an event object. |
| 78 |
* |
| 79 |
* Params: |
| 80 |
* manualReset = the state of the event is not reset automatically after resuming waiting clients |
| 81 |
* initialState = initial state of the signal |
| 82 |
*/ |
| 83 |
this(bool manualReset, bool initialState) |
| 84 |
{ |
| 85 |
initialize(manualReset, initialState); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Initializes an event object. Does nothing if the event is already initialized. |
| 90 |
* |
| 91 |
* Params: |
| 92 |
* manualReset = the state of the event is not reset automatically after resuming waiting clients |
| 93 |
* initialState = initial state of the signal |
| 94 |
*/ |
| 95 |
void initialize(bool manualReset, bool initialState) |
| 96 |
{ |
| 97 |
version (Windows) |
| 98 |
{ |
| 99 |
if (m_event) |
| 100 |
return; |
| 101 |
m_event = CreateEvent(null, manualReset, initialState, null); |
| 102 |
m_event || abort("Error: CreateEvent failed."); |
| 103 |
} |
| 104 |
else version (Posix) |
| 105 |
{ |
| 106 |
if (m_initalized) |
| 107 |
return; |
| 108 |
pthread_mutex_init(cast(pthread_mutex_t*) &m_mutex, null) == 0 || |
| 109 |
abort("Error: pthread_mutex_init failed."); |
| 110 |
|
| 111 |
static if ( is( typeof( imported!"core.sys.posix.pthread".pthread_condattr_setclock ) ) ) |
| 112 |
{ |
| 113 |
import core.sys.posix.pthread : CLOCK_MONOTONIC, pthread_condattr_destroy, pthread_condattr_init, |
| 114 |
pthread_condattr_setclock; |
| 115 |
import core.sys.posix.sys.types : pthread_condattr_t; |
| 116 |
|
| 117 |
pthread_condattr_t attr = void; |
| 118 |
pthread_condattr_init(&attr) == 0 || |
| 119 |
abort("Error: pthread_condattr_init failed."); |
| 120 |
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) == 0 || |
| 121 |
abort("Error: pthread_condattr_setclock failed."); |
| 122 |
pthread_cond_init(&m_cond, &attr) == 0 || |
| 123 |
abort("Error: pthread_cond_init failed."); |
| 124 |
pthread_condattr_destroy(&attr) == 0 || |
| 125 |
abort("Error: pthread_condattr_destroy failed."); |
| 126 |
} |
| 127 |
else |
| 128 |
{ |
| 129 |
pthread_cond_init(&m_cond, null) == 0 || |
| 130 |
abort("Error: pthread_cond_init failed."); |
| 131 |
} |
| 132 |
m_state = initialState; |
| 133 |
m_manualReset = manualReset; |
| 134 |
m_initalized = true; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// copying not allowed, can produce resource leaks |
| 139 |
@disable this(this); |
| 140 |
@disable void opAssign(Event); |
| 141 |
|
| 142 |
~this() |
| 143 |
{ |
| 144 |
terminate(); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* deinitialize event. Does nothing if the event is not initialized. There must not be |
| 149 |
* threads currently waiting for the event to be signaled. |
| 150 |
*/ |
| 151 |
void terminate() |
| 152 |
{ |
| 153 |
version (Windows) |
| 154 |
{ |
| 155 |
if (m_event) |
| 156 |
CloseHandle(m_event); |
| 157 |
m_event = null; |
| 158 |
} |
| 159 |
else version (Posix) |
| 160 |
{ |
| 161 |
if (m_initalized) |
| 162 |
{ |
| 163 |
pthread_mutex_destroy(&m_mutex) == 0 || |
| 164 |
abort("Error: pthread_mutex_destroy failed."); |
| 165 |
pthread_cond_destroy(&m_cond) == 0 || |
| 166 |
abort("Error: pthread_cond_destroy failed."); |
| 167 |
m_initalized = false; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
deprecated ("Use setIfInitialized() instead") void set() |
| 173 |
{ |
| 174 |
setIfInitialized(); |
| 175 |
} |
| 176 |
|
| 177 |
/// Set the event to "signaled", so that waiting clients are resumed |
| 178 |
void setIfInitialized() |
| 179 |
{ |
| 180 |
version (Windows) |
| 181 |
{ |
| 182 |
if (m_event) |
| 183 |
SetEvent(m_event); |
| 184 |
} |
| 185 |
else version (Posix) |
| 186 |
{ |
| 187 |
if (m_initalized) |
| 188 |
{ |
| 189 |
pthread_mutex_lock(&m_mutex); |
| 190 |
m_state = true; |
| 191 |
pthread_cond_broadcast(&m_cond); |
| 192 |
pthread_mutex_unlock(&m_mutex); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/// Reset the event manually |
| 198 |
void reset() |
| 199 |
{ |
| 200 |
version (Windows) |
| 201 |
{ |
| 202 |
if (m_event) |
| 203 |
ResetEvent(m_event); |
| 204 |
} |
| 205 |
else version (Posix) |
| 206 |
{ |
| 207 |
if (m_initalized) |
| 208 |
{ |
| 209 |
pthread_mutex_lock(&m_mutex); |
| 210 |
m_state = false; |
| 211 |
pthread_mutex_unlock(&m_mutex); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Wait for the event to be signaled without timeout. |
| 218 |
* |
| 219 |
* Returns: |
| 220 |
* `true` if the event is in signaled state, `false` if the event is uninitialized or another error occured |
| 221 |
*/ |
| 222 |
bool wait() |
| 223 |
{ |
| 224 |
version (Windows) |
| 225 |
{ |
| 226 |
return m_event && WaitForSingleObject(m_event, INFINITE) == WAIT_OBJECT_0; |
| 227 |
} |
| 228 |
else version (Posix) |
| 229 |
{ |
| 230 |
return wait(Duration.max); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Wait for the event to be signaled with timeout. |
| 236 |
* |
| 237 |
* Params: |
| 238 |
* tmout = the maximum time to wait |
| 239 |
* Returns: |
| 240 |
* `true` if the event is in signaled state, `false` if the event was nonsignaled for the given time or |
| 241 |
* the event is uninitialized or another error occured |
| 242 |
*/ |
| 243 |
bool wait(Duration tmout) |
| 244 |
{ |
| 245 |
version (Windows) |
| 246 |
{ |
| 247 |
if (!m_event) |
| 248 |
return false; |
| 249 |
|
| 250 |
auto maxWaitMillis = dur!("msecs")(uint.max - 1); |
| 251 |
|
| 252 |
while (tmout > maxWaitMillis) |
| 253 |
{ |
| 254 |
auto res = WaitForSingleObject(m_event, uint.max - 1); |
| 255 |
if (res != WAIT_TIMEOUT) |
| 256 |
return res == WAIT_OBJECT_0; |
| 257 |
tmout -= maxWaitMillis; |
| 258 |
} |
| 259 |
auto ms = cast(uint)(tmout.total!"msecs"); |
| 260 |
return WaitForSingleObject(m_event, ms) == WAIT_OBJECT_0; |
| 261 |
} |
| 262 |
else version (Posix) |
| 263 |
{ |
| 264 |
if (!m_initalized) |
| 265 |
return false; |
| 266 |
|
| 267 |
pthread_mutex_lock(&m_mutex); |
| 268 |
|
| 269 |
int result = 0; |
| 270 |
if (!m_state) |
| 271 |
{ |
| 272 |
if (tmout == Duration.max) |
| 273 |
{ |
| 274 |
result = pthread_cond_wait(&m_cond, &m_mutex); |
| 275 |
} |
| 276 |
else |
| 277 |
{ |
| 278 |
import core.sync.config; |
| 279 |
|
| 280 |
timespec t = void; |
| 281 |
mktspec(t, tmout); |
| 282 |
|
| 283 |
result = pthread_cond_timedwait(&m_cond, &m_mutex, &t); |
| 284 |
} |
| 285 |
} |
| 286 |
if (result == 0 && !m_manualReset) |
| 287 |
m_state = false; |
| 288 |
|
| 289 |
pthread_mutex_unlock(&m_mutex); |
| 290 |
|
| 291 |
return result == 0; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
private: |
| 296 |
version (Windows) |
| 297 |
{ |
| 298 |
HANDLE m_event; |
| 299 |
} |
| 300 |
else version (Posix) |
| 301 |
{ |
| 302 |
pthread_mutex_t m_mutex; |
| 303 |
pthread_cond_t m_cond; |
| 304 |
bool m_initalized; |
| 305 |
bool m_state; |
| 306 |
bool m_manualReset; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
// Test single-thread (non-shared) use. |
| 311 |
@nogc nothrow unittest |
| 312 |
{ |
| 313 |
// auto-reset, initial state false |
| 314 |
Event ev1 = Event(false, false); |
| 315 |
assert(!ev1.wait(1.dur!"msecs")); |
| 316 |
ev1.setIfInitialized(); |
| 317 |
assert(ev1.wait()); |
| 318 |
assert(!ev1.wait(1.dur!"msecs")); |
| 319 |
|
| 320 |
// manual-reset, initial state true |
| 321 |
Event ev2 = Event(true, true); |
| 322 |
assert(ev2.wait()); |
| 323 |
assert(ev2.wait()); |
| 324 |
ev2.reset(); |
| 325 |
assert(!ev2.wait(1.dur!"msecs")); |
| 326 |
} |
| 327 |
|
| 328 |
unittest |
| 329 |
{ |
| 330 |
import core.atomic; |
| 331 |
import core.thread; |
| 332 |
|
| 333 |
scope event = new Event(true, false); |
| 334 |
int numThreads = 10; |
| 335 |
shared int numRunning = 0; |
| 336 |
|
| 337 |
void testFn() |
| 338 |
{ |
| 339 |
event.wait(8.dur!"seconds"); // timeout below limit for druntime test_runner |
| 340 |
numRunning.atomicOp!"+="(1); |
| 341 |
} |
| 342 |
|
| 343 |
auto group = new ThreadGroup; |
| 344 |
|
| 345 |
for (int i = 0; i < numThreads; ++i) |
| 346 |
group.create(&testFn); |
| 347 |
|
| 348 |
auto start = MonoTime.currTime; |
| 349 |
assert(numRunning == 0); |
| 350 |
|
| 351 |
event.setIfInitialized(); |
| 352 |
group.joinAll(); |
| 353 |
|
| 354 |
assert(numRunning == numThreads); |
| 355 |
|
| 356 |
assert(MonoTime.currTime - start < 5.dur!"seconds"); |
| 357 |
} |