Debian Codegraph @

src/h/threads.h

raw symbols SCIP
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * threads.h - wrapper for mutex and condition variable operating
6 * system primitives
7 *
8 * Copyright (c) 2020, Marius Gerbershagen
9 *
10 * See file 'LICENSE' for the copyright details.
11 *
12 */
13
14 #ifdef ECL_THREADS
15
16 #ifndef __sun__ /* See unixinit.d for this */
17 # if _XOPEN_SOURCE < 600
18 # ifdef _XOPEN_SOURCE
19 # undef _XOPEN_SOURCE
20 # endif
21 # define _XOPEN_SOURCE 600 /* For pthread mutex attributes */
22 # endif
23 #endif
24
25 #ifndef ECL_MUTEX_H
26 #define ECL_MUTEX_H
27
28 #include <errno.h>
29 #ifdef ECL_WINDOWS_THREADS
30 # include <windows.h>
31 # include <synchapi.h>
32 #else
33 # include <pthread.h>
34 #endif
35 #include <time.h>
36 #ifdef HAVE_GETTIMEOFDAY
37 # include <sys/time.h>
38 #endif
39 #include <math.h>
40
41 #if !defined(ECL_WINDOWS_THREADS)
42
43 #define ECL_MUTEX_SUCCESS 0
44 #define ECL_MUTEX_LOCKED EBUSY
45 #define ECL_MUTEX_NOT_OWNED EPERM
46 #define ECL_MUTEX_TIMEOUT ETIMEDOUT
47 #define ECL_MUTEX_DEADLOCK EDEADLK
48
49 #if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
50 # define COND_VAR_CLOCK CLOCK_MONOTONIC
51 #else
52 # define COND_VAR_CLOCK CLOCK_REALTIME
53 #endif
54
55 /* MUTEX */
56
57 /* Non-recursive locks are only provided as an optimization; on
58 * Windows locks are always recursive. If ECL_MUTEX_DEADLOCK is
59 * undefined, recursive locks are not available. */
60
61 static inline void
62 ecl_mutex_init(ecl_mutex_t *mutex, bool recursive)
63 {
64 pthread_mutexattr_t mutexattr[1];
65 pthread_mutexattr_init(mutexattr);
66 #ifdef __COSMOPOLITAN__
67 pthread_mutexattr_settype(mutexattr, PTHREAD_MUTEX_NORMAL);
68 #else
69 if (recursive) {
70 pthread_mutexattr_settype(mutexattr, PTHREAD_MUTEX_RECURSIVE);
71 } else {
72 pthread_mutexattr_settype(mutexattr, PTHREAD_MUTEX_ERRORCHECK);
73 }
74 #endif
75 pthread_mutex_init(mutex, mutexattr);
76 }
77
78 static inline void
79 ecl_mutex_destroy(ecl_mutex_t *mutex)
80 {
81 pthread_mutex_destroy(mutex);
82 }
83
84 static inline int
85 ecl_mutex_unlock(ecl_mutex_t *mutex)
86 {
87 return pthread_mutex_unlock(mutex);
88 }
89
90 static inline int
91 ecl_mutex_trylock(ecl_mutex_t *mutex)
92 {
93 return pthread_mutex_trylock(mutex);
94 }
95
96 static inline int
97 ecl_mutex_lock(ecl_mutex_t *mutex)
98 {
99 return pthread_mutex_lock(mutex);
100 }
101
102 /* CONDITION VARIABLE */
103
104 static inline void
105 add_timeout_delta(struct timespec *ts, double seconds, clockid_t preferred_clock)
106 {
107 #if defined(HAVE_CLOCK_GETTIME)
108 clock_gettime(preferred_clock, ts);
109 #elif defined(HAVE_GETTIMEOFDAY)
110 struct timeval tp;
111
112 gettimeofday(&tp, NULL);
113 /* Convert from timeval to timespec */
114 ts->tv_sec = tp.tv_sec;
115 ts->tv_nsec = tp.tv_usec * 1000;
116 #else
117 ts->tv_sec = time(NULL);
118 ts->tv_nsec = 0;
119 #endif
120
121 /* Add `seconds' delta */
122 ts->tv_sec += (time_t)floor(seconds);
123 ts->tv_nsec += (long)((seconds - floor(seconds)) * 1e9);
124 if (ts->tv_nsec >= 1e9) {
125 ts->tv_nsec -= 1e9;
126 ts->tv_sec++;
127 }
128 }
129
130 static inline int
131 ecl_mutex_timedlock(ecl_mutex_t *mutex, double seconds)
132 {
133 #if defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK)
134 struct timespec ts;
135 /* Ideally we would like to use CLOCK_MONOTONIC here to prevent
136 wrong timeouts when the system clock is adjusted while we are
137 waiting. However the posix spec says that pthread_mutex_timedlock
138 always uses CLOCK_REALTIME and does not define a way to set the
139 clock for mutexes. */
140 add_timeout_delta(&ts, seconds, CLOCK_REALTIME);
141 return pthread_mutex_timedlock(mutex, &ts);
142 #else
143 /* Not implemented, see mutex.d for alternative implementation using interrupts */
144 return -1;
145 #endif
146 }
147
148 static inline void
149 ecl_cond_var_init(ecl_cond_var_t *cv)
150 {
151 pthread_condattr_t attr;
152 pthread_condattr_init(&attr);
153 #if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK)
154 pthread_condattr_setclock(&attr, COND_VAR_CLOCK);
155 #endif
156 pthread_cond_init(cv, &attr);
157 pthread_condattr_destroy(&attr);
158 }
159
160 static inline void
161 ecl_cond_var_destroy(ecl_cond_var_t *cv)
162 {
163 pthread_cond_destroy(cv);
164 }
165
166 static inline int
167 ecl_cond_var_wait(ecl_cond_var_t *cv, ecl_mutex_t *mutex)
168 {
169 return pthread_cond_wait(cv, mutex);
170 }
171
172 static inline int
173 ecl_cond_var_timedwait(ecl_cond_var_t *cv, ecl_mutex_t *mutex, double seconds)
174 {
175 struct timespec ts;
176 add_timeout_delta(&ts, seconds, COND_VAR_CLOCK);
177 return pthread_cond_timedwait(cv, mutex, &ts);
178 }
179
180 static inline int
181 ecl_cond_var_signal(ecl_cond_var_t *cv)
182 {
183 return pthread_cond_signal(cv);
184 }
185
186 static inline int
187 ecl_cond_var_broadcast(ecl_cond_var_t *cv)
188 {
189 return pthread_cond_broadcast(cv);
190 }
191
192 /* READ-WRITE LOCK */
193
194 /* If posix rwlocks are not available, we provide our own fallback
195 * implementation. Note that for reasons of simplicity and performance
196 * this implementation is a) not interrupt safe, b) will not signal
197 * errors on unlocking a mutex which is owned by a different thread
198 * and c) allows for writer starvation, i.e. as long as readers are
199 * active, the writer will never obtain the lock. */
200
201 static inline void
202 ecl_rwlock_init(ecl_rwlock_t *rwlock)
203 {
204 #if defined(HAVE_POSIX_RWLOCK)
205 pthread_rwlock_init(rwlock, NULL);
206 #else
207 pthread_mutex_init(&rwlock->mutex, NULL);
208 pthread_cond_init(&rwlock->reader_cv, NULL);
209 pthread_cond_init(&rwlock->writer_cv, NULL);
210 rwlock->reader_count = 0;
211 #endif
212 }
213
214 static inline void
215 ecl_rwlock_destroy(ecl_rwlock_t *rwlock)
216 {
217 #if defined(HAVE_POSIX_RWLOCK)
218 pthread_rwlock_destroy(rwlock);
219 #else
220 pthread_mutex_destroy(&rwlock->mutex);
221 pthread_cond_destroy(&rwlock->reader_cv);
222 pthread_cond_destroy(&rwlock->writer_cv);
223 #endif
224 }
225
226 static inline int
227 ecl_rwlock_unlock_read(ecl_rwlock_t *rwlock)
228 {
229 #if defined(HAVE_POSIX_RWLOCK)
230 return pthread_rwlock_unlock(rwlock);
231 #else
232 int rc;
233 pthread_mutex_lock(&rwlock->mutex);
234 if (rwlock->reader_count <= 0) {
235 rc = ECL_MUTEX_NOT_OWNED;
236 } else {
237 if (--rwlock->reader_count == 0) {
238 pthread_cond_signal(&rwlock->writer_cv);
239 }
240 rc = ECL_MUTEX_SUCCESS;
241 }
242 pthread_mutex_unlock(&rwlock->mutex);
243 return rc;
244 #endif
245 }
246
247 static inline int
248 ecl_rwlock_unlock_write(ecl_rwlock_t *rwlock)
249 {
250 #if defined(HAVE_POSIX_RWLOCK)
251 return pthread_rwlock_unlock(rwlock);
252 #else
253 int rc;
254 pthread_mutex_lock(&rwlock->mutex);
255 if (rwlock->reader_count >= 0) {
256 rc = ECL_MUTEX_NOT_OWNED;
257 } else {
258 rwlock->reader_count = 0;
259 pthread_cond_signal(&rwlock->writer_cv);
260 pthread_cond_broadcast(&rwlock->reader_cv);
261 rc = ECL_MUTEX_SUCCESS;
262 }
263 pthread_mutex_unlock(&rwlock->mutex);
264 return rc;
265 #endif
266 }
267
268 static inline int
269 ecl_rwlock_trylock_read(ecl_rwlock_t *rwlock)
270 {
271 #if defined(HAVE_POSIX_RWLOCK)
272 return pthread_rwlock_tryrdlock(rwlock);
273 #else
274 int rc;
275 pthread_mutex_lock(&rwlock->mutex);
276 if (rwlock->reader_count == -1) {
277 rc = ECL_MUTEX_LOCKED;
278 } else {
279 ++rwlock->reader_count;
280 rc = ECL_MUTEX_SUCCESS;
281 }
282 pthread_mutex_unlock(&rwlock->mutex);
283 return rc;
284 #endif
285 }
286
287 static inline int
288 ecl_rwlock_trylock_write(ecl_rwlock_t *rwlock)
289 {
290 #if defined(HAVE_POSIX_RWLOCK)
291 return pthread_rwlock_trywrlock(rwlock);
292 #else
293 int rc;
294 pthread_mutex_lock(&rwlock->mutex);
295 if (rwlock->reader_count != 0) {
296 rc = ECL_MUTEX_LOCKED;
297 } else {
298 rwlock->reader_count = -1;
299 rc = ECL_MUTEX_SUCCESS;
300 }
301 pthread_mutex_unlock(&rwlock->mutex);
302 return rc;
303 #endif
304 }
305
306 static inline int
307 ecl_rwlock_lock_read(ecl_rwlock_t *rwlock)
308 {
309 #if defined(HAVE_POSIX_RWLOCK)
310 return pthread_rwlock_rdlock(rwlock);
311 #else
312 pthread_mutex_lock(&rwlock->mutex);
313 while (rwlock->reader_count == -1) {
314 pthread_cond_wait(&rwlock->reader_cv, &rwlock->mutex);
315 }
316 ++rwlock->reader_count;
317 pthread_mutex_unlock(&rwlock->mutex);
318 return ECL_MUTEX_SUCCESS;
319 #endif
320 }
321
322 static inline int
323 ecl_rwlock_lock_write(ecl_rwlock_t *rwlock)
324 {
325 #if defined(HAVE_POSIX_RWLOCK)
326 return pthread_rwlock_wrlock(rwlock);
327 #else
328 pthread_mutex_lock(&rwlock->mutex);
329 while (rwlock->reader_count != 0) {
330 pthread_cond_wait(&rwlock->writer_cv, &rwlock->mutex);
331 }
332 rwlock->reader_count = -1;
333 pthread_mutex_unlock(&rwlock->mutex);
334 return ECL_MUTEX_SUCCESS;
335 #endif
336 }
337
338 #else /* ECL_WINDOWS_THREADS */
339
340 /* To allow for timed wait operations on locks and for interrupting
341 * deadlocked threads, we use Windows mutexes instead of critical
342 * section objects (which would serve the same purpose and be slightly
343 * faster). This also requires implementing our own version of
344 * condition variables, since the ones provided by Windows only work
345 * together with critical sections. */
346
347 #define ECL_MUTEX_SUCCESS 0
348 #define ECL_MUTEX_LOCKED -1
349 #define ECL_MUTEX_NOT_OWNED ERROR_NOT_OWNER
350 #define ECL_MUTEX_TIMEOUT ERROR_TIMEOUT
351 #undef ECL_MUTEX_DEADLOCK
352
353 /* MUTEX */
354
355 static inline void
356 ecl_mutex_init(ecl_mutex_t *mutex, bool recursive)
357 {
358 *mutex = CreateMutex(NULL, FALSE, NULL);
359 }
360
361 static inline void
362 ecl_mutex_destroy(ecl_mutex_t *mutex)
363 {
364 CloseHandle(*mutex);
365 }
366
367 static inline int
368 ecl_mutex_unlock(ecl_mutex_t *mutex)
369 {
370 return ReleaseMutex(*mutex) ? ECL_MUTEX_SUCCESS : GetLastError();
371 }
372
373 static inline int
374 ecl_mutex_trylock(ecl_mutex_t *mutex)
375 {
376 switch (WaitForSingleObject(*mutex, 0)) {
377 case WAIT_OBJECT_0:
378 case WAIT_ABANDONED:
379 return ECL_MUTEX_SUCCESS;
380 case WAIT_TIMEOUT:
381 return ECL_MUTEX_LOCKED;
382 default:
383 return GetLastError();
384 }
385 }
386
387 static inline int
388 ecl_mutex_lock(ecl_mutex_t *mutex)
389 {
390 AGAIN:
391 switch (WaitForSingleObjectEx(*mutex, INFINITE, TRUE)) {
392 case WAIT_OBJECT_0:
393 case WAIT_ABANDONED:
394 return ECL_MUTEX_SUCCESS;
395 case WAIT_IO_COMPLETION:
396 goto AGAIN;
397 case WAIT_TIMEOUT:
398 return ECL_MUTEX_LOCKED;
399 default:
400 return GetLastError();
401 }
402 }
403
404 static inline DWORD
405 remaining_milliseconds(double seconds, DWORD start_ticks)
406 {
407 DWORD ret = ((DWORD) seconds * 1000.0) - (GetTickCount() - start_ticks);
408 return (ret < 0) ? 0 : ret;
409 }
410
411 static inline int
412 ecl_mutex_timedlock(ecl_mutex_t *mutex, double seconds)
413 {
414 DWORD start_ticks = GetTickCount();
415 AGAIN:
416 switch (WaitForSingleObjectEx(*mutex, remaining_milliseconds(seconds, start_ticks), TRUE)) {
417 case WAIT_OBJECT_0:
418 case WAIT_ABANDONED:
419 return ECL_MUTEX_SUCCESS;
420 case WAIT_IO_COMPLETION:
421 goto AGAIN;
422 case WAIT_TIMEOUT:
423 return ECL_MUTEX_TIMEOUT;
424 default:
425 return GetLastError();
426 }
427 }
428
429 /* CONDITION VARIABLE */
430
431 /* IMPLEMENTATION
432 * --------------
433 * Condition variables are implemented on top of the event
434 * synchronization objects provided by the Windows operating
435 * system.
436 *
437 * Event objects allow a thread to wait until a certain event is
438 * signaled. If the event has already been signaled before the thread
439 * checks for the event, no wait is performed and WaitForSingleObject,
440 * WaitForMultipleObjects returns immediately. Event objects can be of
441 * the manual reset flavor which stays signaled until explicitely
442 * reset and auto reset which wakes up only one thread and then resets
443 * itself automatically. We use a manual reset event for broadcasting
444 * (waking up all threads waiting on the condition variable) and an
445 * auto reset event for signaling (waking up only a single thread).
446 *
447 * The complete state of the condition variable (how many threads are
448 * waiting and whether we are waiting, waking up threads or reseting
449 * the broadcasting event) is stored in an integer which is
450 * manipulated using atomic operations.
451 *
452 * RACE CONDITION SAFETY
453 * ---------------------
454 * To prevent wakeup signals from getting lost, condition variables
455 * are required to atomically unlock the associated mutex when
456 * starting a wait operation. While it is not possible to atomically
457 * unlock a mutex and wait for an event on Windows, the implementation
458 * is nevertheless race condition free in this regard.
459 *
460 * Consider two threads, thread A which starts a wait operation on a
461 * condition variable and thread B which wakes up the same condition
462 * variable. A increases the wait count, releases the mutex but
463 * doesn't wait for a wakeup event yet. Thread B acquires the mutex,
464 * and signals an event (broadcast or signal). Since the event is
465 * reset only after a waiting thread has been released, at the time
466 * that thread A starts waiting the event is still signaled and so the
467 * wakeup event is not lost. Note that for this to work, it is crucial
468 * that the wait count is increased before the mutex is released in
469 * order for thread B to know that a thread is about to start waiting.
470 *
471 * Further race conditions between multiple broadcasting/signaling
472 * threads threads are prevented by allowing only one operation
473 * (signaling or broadcasting) at a time to happen. The case in which
474 * threads start waiting during a wakeup operation is treated as if
475 * the waiting thread had arrived before the wakeup. Race conditions
476 * between threads which start waiting while the broadcast event is
477 * reset are prevented by letting the about to be waiting threads spin
478 * until the reset has finished.
479 *
480 * INTERRUPT SAFETY
481 * ----------------
482 * INV: On Windows, interrupts happen only when accessing the thread
483 * local environment or when entering an alertable wait state. In the
484 * latter case, an arriving interrupt causes the wait function to
485 * return WAIT_IO_COMPLETION.
486 *
487 * This allows us to provide an interrupt safe implementation by
488 * queueing the interrupt, fixing up the wait count of the condition
489 * variable and then executing the interrupt. If the interrupt doesn't
490 * execute a non-local jump, we resume waiting again.
491 *
492 * Note that while this prevents the state of the condition variable
493 * from getting corrupted, it doesn't prevent wakup events which
494 * arrive while the interrupt is executing from getting lost. It is up
495 * to the user to prevent this type of logical bugs.
496 */
497
498 #define ECL_COND_VAR_BROADCAST 1
499 #define ECL_COND_VAR_SIGNAL 2
500 #define ECL_COND_VAR_RESET 3
501
502 static inline cl_index
503 ecl_cond_var_wait_count(cl_index state)
504 {
505 return state >> 2;
506 }
507
508 static inline cl_index
509 ecl_cond_var_status(cl_index state)
510 {
511 return state & 3;
512 }
513
514 static inline cl_index
515 ecl_cond_var_make_state(cl_index wait_count, cl_index status)
516 {
517 return (wait_count << 2) | status;
518 }
519
520 static inline void
521 ecl_cond_var_increment_wait_count(ecl_cond_var_t *cv)
522 {
523 cl_index old_state, new_state;
524 do {
525 old_state = AO_load_acquire((AO_t*)&cv->state);
526 new_state = ecl_cond_var_make_state(ecl_cond_var_wait_count(old_state) + 1,
527 ecl_cond_var_status(old_state));
528 } while (ecl_cond_var_status(old_state) == ECL_COND_VAR_RESET ||
529 !AO_compare_and_swap_full((AO_t*)&cv->state, (AO_t)old_state, (AO_t)new_state));
530 }
531
532 static inline void
533 ecl_cond_var_decrement_wait_count(ecl_cond_var_t *cv)
534 {
535 cl_index old_state, new_state;
536 do {
537 old_state = AO_load_acquire((AO_t*)&cv->state);
538 new_state = ecl_cond_var_make_state(ecl_cond_var_wait_count(old_state) - 1,
539 ecl_cond_var_status(old_state));
540 } while (!AO_compare_and_swap_full((AO_t*)&cv->state, (AO_t)old_state, (AO_t)new_state));
541 }
542
543 static inline int
544 ecl_cond_var_handle_event(DWORD rc, ecl_cond_var_t *cv, ecl_mutex_t *mutex)
545 {
546 cl_index old_state, new_state, wait_count;
547 switch (rc) {
548 case WAIT_OBJECT_0:
549 case WAIT_ABANDONED:
550 /* broadcast event */
551 do {
552 /* INV: ecl_cond_var_status(old_state) == ECL_COND_VAR_BROADCAST */
553 old_state = AO_load_acquire((AO_t*)&cv->state);
554 wait_count = ecl_cond_var_wait_count(old_state) - 1;
555 if (wait_count == 0) {
556 new_state = ecl_cond_var_make_state(0, ECL_COND_VAR_RESET);
557 while (!AO_compare_and_swap_full((AO_t*)&cv->state, (AO_t)old_state, (AO_t)new_state));
558 ResetEvent(cv->broadcast_event);
559 AO_store_release((AO_t*)&cv->state, 0);
560 break;
561 }
562 new_state = ecl_cond_var_make_state(wait_count, ECL_COND_VAR_BROADCAST);
563 } while(!AO_compare_and_swap_full((AO_t*)&cv->state, (AO_t)old_state, (AO_t)new_state));
564 return ecl_mutex_lock(mutex);
565 case WAIT_OBJECT_0 + 1:
566 case WAIT_ABANDONED + 1:
567 /* signal event */
568 do {
569 /* INV: ecl_cond_var_status(old_state) == ECL_COND_VAR_SIGNAL */
570 old_state = AO_load_acquire((AO_t*)&cv->state);
571 wait_count = ecl_cond_var_wait_count(old_state) - 1;
572 new_state = ecl_cond_var_make_state(wait_count, 0);
573 } while(!AO_compare_and_swap_full((AO_t*)&cv->state, (AO_t)old_state, (AO_t)new_state));
574 return ecl_mutex_lock(mutex);
575 case WAIT_TIMEOUT:
576 ecl_cond_var_decrement_wait_count(cv);
577 rc = ecl_mutex_lock(mutex);
578 return (rc == ECL_MUTEX_SUCCESS) ? ECL_MUTEX_TIMEOUT : rc;
579 default:
580 ecl_cond_var_decrement_wait_count(cv);
581 return GetLastError();
582 }
583 }
584
585 static inline void
586 ecl_cond_var_init(ecl_cond_var_t *cv)
587 {
588 cv->broadcast_event = CreateEvent(NULL, TRUE, FALSE, NULL); /* manual reset event */
589 cv->signal_event = CreateEvent(NULL, FALSE, FALSE, NULL); /* auto reset event */
590 cv->state = 0;
591 }
592
593 static inline void
594 ecl_cond_var_destroy(ecl_cond_var_t *cv)
595 {
596 CloseHandle(cv->broadcast_event);
597 CloseHandle(cv->signal_event);
598 }
599
600 static inline int
601 ecl_cond_var_wait(ecl_cond_var_t *cv, ecl_mutex_t *mutex)
602 {
603 cl_env_ptr env = ecl_process_env();
604 DWORD rc;
605 HANDLE events[2];
606 events[0] = cv->broadcast_event;
607 events[1] = cv->signal_event;
608 ecl_disable_interrupts_env(env);
609 ecl_cond_var_increment_wait_count(cv);
610 if (!ReleaseMutex(*mutex)) {
611 return GetLastError();
612 }
613 while ((rc = WaitForMultipleObjectsEx(2, events, FALSE, INFINITE, TRUE)) == WAIT_IO_COMPLETION) {
614 /* we got an interrupt; first fix up the state of the condition variable, ... */
615 ecl_cond_var_decrement_wait_count(cv);
616 /* ... then handle the interrupt ... */
617 ecl_enable_interrupts_env(env);
618 ecl_check_pending_interrupts(env);
619 ecl_disable_interrupts_env(env);
620 /* ... and start waiting again ... */
621 ecl_cond_var_increment_wait_count(cv);
622 }
623 return ecl_cond_var_handle_event(rc, cv, mutex);
624 }
625
626 static inline int
627 ecl_cond_var_timedwait(ecl_cond_var_t *cv, ecl_mutex_t *mutex, double seconds)
628 {
629 cl_env_ptr env = ecl_process_env();
630 DWORD rc;
631 DWORD start_ticks = GetTickCount();
632 HANDLE events[2];
633 events[0] = cv->broadcast_event;
634 events[1] = cv->signal_event;
635 ecl_disable_interrupts_env(env);
636 ecl_cond_var_increment_wait_count(cv);
637 if (!ReleaseMutex(*mutex)) {
638 return GetLastError();
639 }
640 while ((rc = WaitForMultipleObjectsEx(2, events, FALSE, remaining_milliseconds(seconds, start_ticks), TRUE)) == WAIT_IO_COMPLETION) {
641 ecl_cond_var_decrement_wait_count(cv);
642 ecl_enable_interrupts_env(env);
643 ecl_check_pending_interrupts(env);
644 ecl_disable_interrupts_env(env);
645 ecl_cond_var_increment_wait_count(cv);
646 }
647 return ecl_cond_var_handle_event(rc, cv, mutex);
648 }
649
650 static inline int
651 ecl_cond_var_signal(ecl_cond_var_t *cv)
652 {
653 cl_index old_state, new_state, wait_count, status;
654 do {
655 old_state = AO_load_acquire((AO_t*)&cv->state);
656 wait_count = ecl_cond_var_wait_count(old_state);
657 status = ecl_cond_var_status(old_state);
658 if (wait_count == 0 || status == ECL_COND_VAR_BROADCAST) {
659 return ECL_MUTEX_SUCCESS;
660 }
661 new_state = ecl_cond_var_make_state(wait_count, ECL_COND_VAR_SIGNAL);
662 } while(status != 0 ||
663 !AO_compare_and_swap_full((AO_t*)&cv->state, (AO_t)old_state, (AO_t)new_state));
664 return SetEvent(cv->signal_event) ? ECL_MUTEX_SUCCESS : GetLastError();
665 }
666
667 static inline int
668 ecl_cond_var_broadcast(ecl_cond_var_t *cv)
669 {
670 cl_index old_state, new_state, wait_count, status;
671 do {
672 old_state = AO_load_acquire((AO_t*)&cv->state);
673 wait_count = ecl_cond_var_wait_count(old_state);
674 status = ecl_cond_var_status(old_state);
675 if (wait_count == 0 || status == ECL_COND_VAR_BROADCAST) {
676 return ECL_MUTEX_SUCCESS;
677 }
678 new_state = ecl_cond_var_make_state(wait_count, ECL_COND_VAR_BROADCAST);
679 } while(status != 0 ||
680 !AO_compare_and_swap_full((AO_t*)&cv->state, (AO_t)old_state, (AO_t)new_state));
681 return SetEvent(cv->broadcast_event) ? ECL_MUTEX_SUCCESS : GetLastError();
682 }
683
684 /* READ-WRITE LOCK */
685
686 static inline void
687 ecl_rwlock_init(ecl_rwlock_t *rwlock)
688 {
689 InitializeSRWLock(rwlock);
690 }
691
692 static inline void
693 ecl_rwlock_destroy(ecl_rwlock_t *rwlock) { }
694
695 static inline int
696 ecl_rwlock_unlock_read(ecl_rwlock_t *rwlock)
697 {
698 ReleaseSRWLockShared(rwlock);
699 return ECL_MUTEX_SUCCESS;
700 }
701
702 static inline int
703 ecl_rwlock_unlock_write(ecl_rwlock_t *rwlock)
704 {
705 ReleaseSRWLockExclusive(rwlock);
706 return ECL_MUTEX_SUCCESS;
707 }
708
709 static inline int
710 ecl_rwlock_trylock_read(ecl_rwlock_t *rwlock)
711 {
712 return (TryAcquireSRWLockShared(rwlock) == 0) ? ECL_MUTEX_LOCKED : ECL_MUTEX_SUCCESS;
713 }
714
715 static inline int
716 ecl_rwlock_trylock_write(ecl_rwlock_t *rwlock)
717 {
718 return (TryAcquireSRWLockExclusive(rwlock) == 0) ? ECL_MUTEX_LOCKED : ECL_MUTEX_SUCCESS;
719 }
720
721 static inline int
722 ecl_rwlock_lock_read(ecl_rwlock_t *rwlock)
723 {
724 AcquireSRWLockShared(rwlock);
725 return ECL_MUTEX_SUCCESS;
726 }
727
728 static inline int
729 ecl_rwlock_lock_write(ecl_rwlock_t *rwlock)
730 {
731 AcquireSRWLockExclusive(rwlock);
732 return ECL_MUTEX_SUCCESS;
733 }
734
735 #endif /* ECL_WINDOWS_THREADS */
736
737 #endif /* ECL_MUTEX_H */
738
739 #endif /* ECL_THREADS */