| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#ifdef ECL_THREADS |
| 15 |
|
| 16 |
#ifndef __sun__ |
| 17 |
# if _XOPEN_SOURCE < 600 |
| 18 |
# ifdef _XOPEN_SOURCE |
| 19 |
# undef _XOPEN_SOURCE |
| 20 |
# endif |
| 21 |
# define _XOPEN_SOURCE 600 |
| 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 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
add_timeout_delta(&ts, seconds, CLOCK_REALTIME); |
| 141 |
return pthread_mutex_timedlock(mutex, &ts); |
| 142 |
#else |
| 143 |
|
| 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 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 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 |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 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 |
|
| 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 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 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 |
|
| 551 |
do { |
| 552 |
|
| 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 |
|
| 568 |
do { |
| 569 |
|
| 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); |
| 589 |
cv->signal_event = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 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 |
|
| 615 |
ecl_cond_var_decrement_wait_count(cv); |
| 616 |
|
| 617 |
ecl_enable_interrupts_env(env); |
| 618 |
ecl_check_pending_interrupts(env); |
| 619 |
ecl_disable_interrupts_env(env); |
| 620 |
|
| 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 |
|
| 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 |
| 736 |
|
| 737 |
#endif |
| 738 |
|
| 739 |
#endif |