| 1 |
/*****************************************************************************/ |
| 2 |
// Copyright 2002-2019 Adobe Systems Incorporated |
| 3 |
// All Rights Reserved. |
| 4 |
// |
| 5 |
// NOTICE: Adobe permits you to use, modify, and distribute this file in |
| 6 |
// accordance with the terms of the Adobe license agreement accompanying it. |
| 7 |
/*****************************************************************************/ |
| 8 |
|
| 9 |
#include "dng_pthread.h" |
| 10 |
|
| 11 |
/*****************************************************************************/ |
| 12 |
|
| 13 |
#if qDNGThreadSafe |
| 14 |
|
| 15 |
/*****************************************************************************/ |
| 16 |
|
| 17 |
#include "dng_assertions.h" |
| 18 |
#include "dng_auto_ptr.h" |
| 19 |
|
| 20 |
/*****************************************************************************/ |
| 21 |
|
| 22 |
#if qWinOS |
| 23 |
|
| 24 |
#pragma warning(disable : 4786) |
| 25 |
|
| 26 |
/* Not supporting Win98, check WINVER if this is still needed |
| 27 |
|
| 28 |
// Nothing in this file requires Unicode, |
| 29 |
// However, CreateSemaphore has a path parameter |
| 30 |
// (which is NULL always in this code) and thus |
| 31 |
// does not work on Win98 if UNICODE is defined. |
| 32 |
// So we force it off here. |
| 33 |
|
| 34 |
#undef UNICODE |
| 35 |
#undef _UNICODE |
| 36 |
*/ |
| 37 |
|
| 38 |
#include <windows.h> |
| 39 |
#include <process.h> |
| 40 |
#include <errno.h> |
| 41 |
#include <memory> |
| 42 |
#include <new> |
| 43 |
#include <map> |
| 44 |
|
| 45 |
#else |
| 46 |
|
| 47 |
#include <sys/time.h> |
| 48 |
|
| 49 |
#endif |
| 50 |
|
| 51 |
/*****************************************************************************/ |
| 52 |
|
| 53 |
#if qWinOS |
| 54 |
|
| 55 |
/*****************************************************************************/ |
| 56 |
|
| 57 |
// Turning off qDNGUseConditionVariable because the WakeConditionVariable and |
| 58 |
// WakeAllConditionVariable Microsoft API routines are only available on |
| 59 |
// Vista, and Camera Raw and DNG Converter 8.3 need to continue working on |
| 60 |
// Windows XP. -erichan 2013-11-08. |
| 61 |
|
| 62 |
#define qDNGUseConditionVariable 0 |
| 63 |
|
| 64 |
/*****************************************************************************/ |
| 65 |
|
| 66 |
#ifndef qDNGUseConditionVariable |
| 67 |
#if WINVER >= 0x0600 // Vista introduces a real condition variable support |
| 68 |
#define qDNGUseConditionVariable 1 |
| 69 |
#else |
| 70 |
#define qDNGUseConditionVariable 0 |
| 71 |
#endif |
| 72 |
#endif |
| 73 |
|
| 74 |
/*****************************************************************************/ |
| 75 |
|
| 76 |
#if !qDNGUseConditionVariable |
| 77 |
namespace { |
| 78 |
|
| 79 |
struct waiter { |
| 80 |
struct waiter *prev; |
| 81 |
struct waiter *next; |
| 82 |
HANDLE semaphore; |
| 83 |
bool chosen_by_signal; |
| 84 |
}; |
| 85 |
} |
| 86 |
#endif |
| 87 |
|
| 88 |
/*****************************************************************************/ |
| 89 |
|
| 90 |
struct dng_pthread_mutex_impl |
| 91 |
{ |
| 92 |
CRITICAL_SECTION lock; |
| 93 |
|
| 94 |
dng_pthread_mutex_impl() { ::InitializeCriticalSection(&lock); } |
| 95 |
~dng_pthread_mutex_impl() { ::DeleteCriticalSection(&lock); } |
| 96 |
void Lock() { ::EnterCriticalSection(&lock); } |
| 97 |
void Unlock() { ::LeaveCriticalSection(&lock); } |
| 98 |
private: |
| 99 |
dng_pthread_mutex_impl &operator=(const dng_pthread_mutex_impl &); |
| 100 |
dng_pthread_mutex_impl(const dng_pthread_mutex_impl &); |
| 101 |
}; |
| 102 |
|
| 103 |
/*****************************************************************************/ |
| 104 |
|
| 105 |
struct dng_pthread_cond_impl |
| 106 |
{ |
| 107 |
dng_pthread_mutex_impl lock; // Mutual exclusion on next two variables |
| 108 |
|
| 109 |
#if qDNGUseConditionVariable |
| 110 |
// so much simpler, but Vista+ only |
| 111 |
CONDITION_VARIABLE cond; |
| 112 |
|
| 113 |
dng_pthread_cond_impl() { InitializeConditionVariable(&cond); } |
| 114 |
~dng_pthread_cond_impl() { } // no delete listed |
| 115 |
|
| 116 |
#else |
| 117 |
|
| 118 |
waiter *head_waiter; // List of threads waiting on this condition |
| 119 |
waiter *tail_waiter; // Used to get FIFO, rather than LIFO, behavior for pthread_cond_signal |
| 120 |
unsigned int broadcast_generation; // Used as sort of a separator on broadcasts |
| 121 |
// saves having to walk the waiters list setting |
| 122 |
// each one's "chosen_by_signal" flag while the condition is locked |
| 123 |
|
| 124 |
dng_pthread_cond_impl() : head_waiter(NULL), tail_waiter(NULL), broadcast_generation(0) { } |
| 125 |
~dng_pthread_cond_impl() { } |
| 126 |
#endif |
| 127 |
|
| 128 |
// Non copyable |
| 129 |
private: |
| 130 |
dng_pthread_cond_impl &operator=(const dng_pthread_cond_impl &); |
| 131 |
dng_pthread_cond_impl(const dng_pthread_cond_impl &); |
| 132 |
|
| 133 |
}; |
| 134 |
|
| 135 |
/*****************************************************************************/ |
| 136 |
|
| 137 |
namespace |
| 138 |
{ |
| 139 |
|
| 140 |
struct ScopedLock |
| 141 |
{ |
| 142 |
dng_pthread_mutex_impl *mutex; |
| 143 |
|
| 144 |
ScopedLock(dng_pthread_mutex_impl *arg) : mutex(arg) |
| 145 |
{ |
| 146 |
mutex->Lock(); |
| 147 |
} |
| 148 |
ScopedLock(dng_pthread_mutex_impl &arg) : mutex(&arg) |
| 149 |
{ |
| 150 |
mutex->Lock(); |
| 151 |
} |
| 152 |
~ScopedLock() |
| 153 |
{ |
| 154 |
mutex->Unlock(); |
| 155 |
} |
| 156 |
private: |
| 157 |
ScopedLock &operator=(const ScopedLock &); |
| 158 |
ScopedLock(const ScopedLock &); |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
#if !qDNGUseConditionalVariable |
| 163 |
// DONE: avoid this serialization lock |
| 164 |
// do allocation at init, and then just assert ? |
| 165 |
|
| 166 |
dng_pthread_mutex_impl validationLock; |
| 167 |
|
| 168 |
void ValidateMutex(dng_pthread_mutex_t *mutex) |
| 169 |
{ |
| 170 |
if (*mutex != DNG_PTHREAD_MUTEX_INITIALIZER) |
| 171 |
return; |
| 172 |
|
| 173 |
ScopedLock lock(validationLock); |
| 174 |
|
| 175 |
if (*mutex == DNG_PTHREAD_MUTEX_INITIALIZER) |
| 176 |
dng_pthread_mutex_init(mutex, NULL); |
| 177 |
} |
| 178 |
|
| 179 |
void ValidateCond(dng_pthread_cond_t *cond) |
| 180 |
{ |
| 181 |
if (*cond != DNG_PTHREAD_COND_INITIALIZER) |
| 182 |
return; |
| 183 |
|
| 184 |
ScopedLock lock(validationLock); |
| 185 |
|
| 186 |
if (*cond == DNG_PTHREAD_COND_INITIALIZER) |
| 187 |
dng_pthread_cond_init(cond, NULL); |
| 188 |
} |
| 189 |
#endif |
| 190 |
|
| 191 |
DWORD thread_wait_sema_TLS_index; |
| 192 |
bool thread_wait_sema_inited = false; |
| 193 |
dng_pthread_once_t once_thread_TLS = DNG_PTHREAD_ONCE_INIT; |
| 194 |
|
| 195 |
void init_thread_TLS() |
| 196 |
{ |
| 197 |
thread_wait_sema_TLS_index = ::TlsAlloc(); |
| 198 |
thread_wait_sema_inited = true; |
| 199 |
} |
| 200 |
|
| 201 |
void finalize_thread_TLS() |
| 202 |
{ |
| 203 |
if (thread_wait_sema_inited) |
| 204 |
{ |
| 205 |
::TlsFree(thread_wait_sema_TLS_index); |
| 206 |
thread_wait_sema_inited = false; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
dng_pthread_mutex_impl primaryHandleMapLock; |
| 211 |
|
| 212 |
typedef std::map<DWORD, std::pair<HANDLE, void **> > ThreadMapType; |
| 213 |
|
| 214 |
// A map to make sure handles are freed and to allow returning a pointer sized result |
| 215 |
// even on 64-bit Windows. |
| 216 |
ThreadMapType primaryHandleMap; |
| 217 |
|
| 218 |
HANDLE GetThreadSemaphore() |
| 219 |
{ |
| 220 |
dng_pthread_once(&once_thread_TLS, init_thread_TLS); |
| 221 |
|
| 222 |
HANDLE semaphore = ::TlsGetValue(thread_wait_sema_TLS_index); |
| 223 |
if (semaphore == NULL) |
| 224 |
{ |
| 225 |
semaphore = ::CreateSemaphore(NULL, 0, 1, NULL); |
| 226 |
::TlsSetValue(thread_wait_sema_TLS_index, semaphore); |
| 227 |
} |
| 228 |
|
| 229 |
return semaphore; |
| 230 |
} |
| 231 |
|
| 232 |
void FreeThreadSemaphore() |
| 233 |
{ |
| 234 |
if (thread_wait_sema_inited) |
| 235 |
{ |
| 236 |
HANDLE semaphore = (HANDLE)::TlsGetValue(thread_wait_sema_TLS_index); |
| 237 |
|
| 238 |
if (semaphore != NULL) |
| 239 |
{ |
| 240 |
::TlsSetValue(thread_wait_sema_TLS_index, NULL); |
| 241 |
::CloseHandle(semaphore); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
struct trampoline_args |
| 247 |
{ |
| 248 |
void *(*func)(void *); |
| 249 |
void *arg; |
| 250 |
}; |
| 251 |
|
| 252 |
// This trampoline takes care of the return type being different |
| 253 |
// between pthreads thread funcs and Windows C lib thread funcs |
| 254 |
unsigned __stdcall trampoline(void *arg_arg) |
| 255 |
{ |
| 256 |
trampoline_args *args_ptr = (trampoline_args *)arg_arg; |
| 257 |
trampoline_args args = *args_ptr; |
| 258 |
|
| 259 |
delete args_ptr; |
| 260 |
|
| 261 |
GetThreadSemaphore(); |
| 262 |
|
| 263 |
void *result = args.func(args.arg); |
| 264 |
|
| 265 |
{ |
| 266 |
ScopedLock lockMap(primaryHandleMapLock); |
| 267 |
|
| 268 |
ThreadMapType::iterator iter = primaryHandleMap.find(pthread_self()); |
| 269 |
if (iter != primaryHandleMap.end()) |
| 270 |
*iter->second.second = result; |
| 271 |
} |
| 272 |
|
| 273 |
FreeThreadSemaphore(); |
| 274 |
|
| 275 |
return S_OK; |
| 276 |
} |
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
/*****************************************************************************/ |
| 281 |
|
| 282 |
extern "C" { |
| 283 |
|
| 284 |
/*****************************************************************************/ |
| 285 |
|
| 286 |
struct dng_pthread_attr_impl |
| 287 |
{ |
| 288 |
size_t stacksize; |
| 289 |
}; |
| 290 |
|
| 291 |
/*****************************************************************************/ |
| 292 |
|
| 293 |
int dng_pthread_attr_init(pthread_attr_t *attr) |
| 294 |
{ |
| 295 |
dng_pthread_attr_impl *newAttrs; |
| 296 |
|
| 297 |
newAttrs = new (std::nothrow) dng_pthread_attr_impl; |
| 298 |
if (newAttrs == NULL) |
| 299 |
return -1; // ENOMEM; |
| 300 |
|
| 301 |
newAttrs->stacksize = 0; |
| 302 |
|
| 303 |
*attr = newAttrs; |
| 304 |
|
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
/*****************************************************************************/ |
| 309 |
|
| 310 |
int dng_pthread_attr_destroy(pthread_attr_t *attr) |
| 311 |
{ |
| 312 |
if (*attr == NULL) |
| 313 |
return -1; // EINVAL |
| 314 |
|
| 315 |
delete *attr; |
| 316 |
|
| 317 |
*attr = NULL; |
| 318 |
|
| 319 |
return 0; |
| 320 |
} |
| 321 |
|
| 322 |
/*****************************************************************************/ |
| 323 |
|
| 324 |
int dng_pthread_attr_setstacksize(dng_pthread_attr_t *attr, size_t stacksize) |
| 325 |
{ |
| 326 |
if (attr == NULL || (*attr) == NULL) |
| 327 |
return -1; // EINVAL |
| 328 |
|
| 329 |
(*attr)->stacksize = stacksize; |
| 330 |
|
| 331 |
return 0; |
| 332 |
} |
| 333 |
|
| 334 |
/*****************************************************************************/ |
| 335 |
|
| 336 |
int dng_pthread_attr_getstacksize(const dng_pthread_attr_t *attr, size_t *stacksize) |
| 337 |
{ |
| 338 |
if (attr == NULL || (*attr) == NULL || stacksize == NULL) |
| 339 |
return -1; // EINVAL |
| 340 |
|
| 341 |
*stacksize = (*attr)->stacksize; |
| 342 |
|
| 343 |
return 0; |
| 344 |
} |
| 345 |
|
| 346 |
/*****************************************************************************/ |
| 347 |
|
| 348 |
int dng_pthread_create(dng_pthread_t *thread, const pthread_attr_t *attrs, void * (*func)(void *), void *arg) |
| 349 |
{ |
| 350 |
try |
| 351 |
{ |
| 352 |
uintptr_t result; |
| 353 |
unsigned threadID; |
| 354 |
AutoPtr<trampoline_args> args(new (std::nothrow) trampoline_args); |
| 355 |
AutoPtr<void *> resultHolder(new (std::nothrow) (void *)); |
| 356 |
|
| 357 |
if (args.Get() == NULL || resultHolder.Get () == NULL) |
| 358 |
return -1; // ENOMEM |
| 359 |
|
| 360 |
args->func = func; |
| 361 |
args->arg = arg; |
| 362 |
|
| 363 |
size_t stacksize = 0; |
| 364 |
|
| 365 |
if (attrs != NULL) |
| 366 |
dng_pthread_attr_getstacksize (attrs, &stacksize); |
| 367 |
|
| 368 |
{ |
| 369 |
ScopedLock lockMap(primaryHandleMapLock); |
| 370 |
|
| 371 |
result = _beginthreadex(NULL, (unsigned)stacksize, trampoline, args.Get(), 0, &threadID); |
| 372 |
if (result == NULL) |
| 373 |
return -1; // ENOMEM |
| 374 |
(void) args.Release(); |
| 375 |
|
| 376 |
std::pair<DWORD, std::pair<HANDLE, void **> > newMapEntry(threadID, |
| 377 |
std::pair<HANDLE, void **>((HANDLE)result, resultHolder.Get ())); |
| 378 |
std::pair<ThreadMapType::iterator, bool> insertion = primaryHandleMap.insert(newMapEntry); |
| 379 |
(void) insertion; |
| 380 |
|
| 381 |
// If there is a handle open on the thread, its ID should not be reused so assert that an insertion was made. |
| 382 |
DNG_ASSERT(insertion.second, "pthread emulation logic error"); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
(void) resultHolder.Release (); |
| 387 |
|
| 388 |
*thread = (dng_pthread_t)threadID; |
| 389 |
return 0; |
| 390 |
} |
| 391 |
catch (const std::bad_alloc &) |
| 392 |
{ |
| 393 |
return -1; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
/*****************************************************************************/ |
| 398 |
|
| 399 |
int dng_pthread_detach(dng_pthread_t thread) |
| 400 |
{ |
| 401 |
HANDLE primaryHandle; |
| 402 |
void **resultHolder = NULL; |
| 403 |
|
| 404 |
{ |
| 405 |
ScopedLock lockMap(primaryHandleMapLock); |
| 406 |
|
| 407 |
ThreadMapType::iterator iter = primaryHandleMap.find(thread); |
| 408 |
if (iter == primaryHandleMap.end()) |
| 409 |
return -1; |
| 410 |
|
| 411 |
primaryHandle = iter->second.first; |
| 412 |
|
| 413 |
// A join is waiting on the thread. |
| 414 |
if (primaryHandle == NULL) |
| 415 |
return -1; |
| 416 |
|
| 417 |
resultHolder = iter->second.second; |
| 418 |
|
| 419 |
primaryHandleMap.erase(iter); |
| 420 |
} |
| 421 |
|
| 422 |
delete resultHolder; |
| 423 |
|
| 424 |
#if qWinRT |
| 425 |
if (!::WinRT_CloseThreadHandle(primaryHandle)) |
| 426 |
#else |
| 427 |
if (!::CloseHandle(primaryHandle)) |
| 428 |
#endif |
| 429 |
return -1; |
| 430 |
|
| 431 |
return 0; |
| 432 |
} |
| 433 |
|
| 434 |
/*****************************************************************************/ |
| 435 |
|
| 436 |
int dng_pthread_join(dng_pthread_t thread, void **result) |
| 437 |
{ |
| 438 |
bool found = false; |
| 439 |
HANDLE primaryHandle = NULL; |
| 440 |
void **resultHolder = NULL; |
| 441 |
|
| 442 |
ThreadMapType::iterator iter; |
| 443 |
|
| 444 |
{ |
| 445 |
ScopedLock lockMap(primaryHandleMapLock); |
| 446 |
|
| 447 |
iter = primaryHandleMap.find(thread); |
| 448 |
found = iter != primaryHandleMap.end(); |
| 449 |
if (found) |
| 450 |
{ |
| 451 |
primaryHandle = iter->second.first; |
| 452 |
resultHolder = iter->second.second; |
| 453 |
|
| 454 |
// Set HANDLE to NULL to force any later join or detach to fail. |
| 455 |
iter->second.first = NULL; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
// This case can happens when joining a thread not created with pthread_create, |
| 460 |
// which is a bad idea, but it gets mapped to doing the join, but always returns NULL. |
| 461 |
if (!found) |
| 462 |
primaryHandle = ::OpenThread(SYNCHRONIZE|THREAD_QUERY_INFORMATION, FALSE, thread); |
| 463 |
|
| 464 |
if (primaryHandle == NULL) |
| 465 |
return -1; |
| 466 |
|
| 467 |
if (::WaitForSingleObject(primaryHandle, INFINITE) != WAIT_OBJECT_0) |
| 468 |
{ |
| 469 |
DWORD err = ::GetLastError(); |
| 470 |
(void) err; |
| 471 |
return -1; |
| 472 |
} |
| 473 |
|
| 474 |
{ |
| 475 |
ScopedLock lockMap(primaryHandleMapLock); |
| 476 |
|
| 477 |
if (iter != primaryHandleMap.end()) |
| 478 |
primaryHandleMap.erase(iter); |
| 479 |
} |
| 480 |
|
| 481 |
#if qWinRT |
| 482 |
::WinRT_CloseThreadHandle(primaryHandle); |
| 483 |
#else |
| 484 |
::CloseHandle(primaryHandle); |
| 485 |
#endif |
| 486 |
if (result != NULL && resultHolder != NULL) |
| 487 |
*result = *resultHolder; |
| 488 |
|
| 489 |
delete resultHolder; |
| 490 |
|
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
/*****************************************************************************/ |
| 495 |
|
| 496 |
dng_pthread_t dng_pthread_self() |
| 497 |
{ |
| 498 |
return (dng_pthread_t)::GetCurrentThreadId(); |
| 499 |
} |
| 500 |
|
| 501 |
/*****************************************************************************/ |
| 502 |
|
| 503 |
void dng_pthread_exit(void *result) |
| 504 |
{ |
| 505 |
{ |
| 506 |
ScopedLock lockMap(primaryHandleMapLock); |
| 507 |
|
| 508 |
ThreadMapType::iterator iter = primaryHandleMap.find(pthread_self()); |
| 509 |
if (iter != primaryHandleMap.end()) |
| 510 |
*iter->second.second = result; |
| 511 |
} |
| 512 |
|
| 513 |
FreeThreadSemaphore(); |
| 514 |
|
| 515 |
_endthreadex(S_OK); |
| 516 |
} |
| 517 |
|
| 518 |
/*****************************************************************************/ |
| 519 |
|
| 520 |
int dng_pthread_mutex_init(dng_pthread_mutex_t *mutex, void * /* attrs */) |
| 521 |
{ |
| 522 |
dng_pthread_mutex_t result; |
| 523 |
try { |
| 524 |
result = new(dng_pthread_mutex_impl); |
| 525 |
} catch (const std::bad_alloc &) |
| 526 |
{ |
| 527 |
return -1; |
| 528 |
} |
| 529 |
|
| 530 |
if (result == NULL) |
| 531 |
return -1; |
| 532 |
*mutex = result; |
| 533 |
return 0; |
| 534 |
} |
| 535 |
|
| 536 |
/*****************************************************************************/ |
| 537 |
|
| 538 |
int dng_pthread_mutex_destroy(dng_pthread_mutex_t *mutex) |
| 539 |
{ |
| 540 |
if (*mutex == DNG_PTHREAD_MUTEX_INITIALIZER) |
| 541 |
{ |
| 542 |
*mutex = NULL; |
| 543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
delete *mutex; |
| 547 |
*mutex = NULL; |
| 548 |
return 0; |
| 549 |
} |
| 550 |
|
| 551 |
/*****************************************************************************/ |
| 552 |
|
| 553 |
int dng_pthread_cond_init(dng_pthread_cond_t *cond, void * /* attrs */) |
| 554 |
{ |
| 555 |
dng_pthread_cond_t result; |
| 556 |
try { |
| 557 |
result = new(dng_pthread_cond_impl); |
| 558 |
} catch (const std::bad_alloc &) |
| 559 |
{ |
| 560 |
return -1; |
| 561 |
} |
| 562 |
|
| 563 |
if (result == NULL) |
| 564 |
return -1; |
| 565 |
*cond = result; |
| 566 |
return 0; |
| 567 |
} |
| 568 |
|
| 569 |
/*****************************************************************************/ |
| 570 |
|
| 571 |
int dng_pthread_cond_destroy(dng_pthread_cond_t *cond) |
| 572 |
{ |
| 573 |
if (*cond == DNG_PTHREAD_COND_INITIALIZER) |
| 574 |
{ |
| 575 |
*cond = NULL; |
| 576 |
return 0; |
| 577 |
} |
| 578 |
|
| 579 |
delete *cond; |
| 580 |
*cond = NULL; |
| 581 |
return 0; |
| 582 |
} |
| 583 |
|
| 584 |
/*****************************************************************************/ |
| 585 |
|
| 586 |
int dng_pthread_mutexattr_init(dng_pthread_mutexattr_t* mutexattr) |
| 587 |
{ |
| 588 |
return 0; |
| 589 |
} |
| 590 |
|
| 591 |
/*****************************************************************************/ |
| 592 |
|
| 593 |
int dng_pthread_mutexattr_settype(dng_pthread_mutexattr_t* mutexattr, int type) |
| 594 |
{ |
| 595 |
return 0; |
| 596 |
} |
| 597 |
|
| 598 |
/*****************************************************************************/ |
| 599 |
|
| 600 |
int dng_pthread_mutex_lock(dng_pthread_mutex_t *mutex) |
| 601 |
{ |
| 602 |
ValidateMutex(mutex); |
| 603 |
(*mutex)->Lock(); |
| 604 |
return 0; |
| 605 |
} |
| 606 |
|
| 607 |
/*****************************************************************************/ |
| 608 |
|
| 609 |
int dng_pthread_mutex_unlock(dng_pthread_mutex_t *mutex) |
| 610 |
{ |
| 611 |
ValidateMutex(mutex); |
| 612 |
(*mutex)->Unlock(); |
| 613 |
return 0; |
| 614 |
} |
| 615 |
|
| 616 |
/*****************************************************************************/ |
| 617 |
|
| 618 |
static int cond_wait_internal(dng_pthread_cond_t *cond, dng_pthread_mutex_t *mutex, int timeout_milliseconds) |
| 619 |
{ |
| 620 |
#if qDNGUseConditionVariable |
| 621 |
int result = 0; |
| 622 |
|
| 623 |
BOOL success = SleepConditionVariableCS(&(*cond)->cond, &(*mutex)->lock, timeout_milliseconds); |
| 624 |
if (!success) |
| 625 |
if (GetLastError() == ERROR_TIMEOUT) |
| 626 |
result = DNG_ETIMEDOUT; |
| 627 |
|
| 628 |
return result; |
| 629 |
|
| 630 |
#else |
| 631 |
|
| 632 |
dng_pthread_cond_impl &real_cond = **cond; |
| 633 |
dng_pthread_mutex_impl &real_mutex = **mutex; |
| 634 |
|
| 635 |
waiter this_wait; |
| 636 |
HANDLE semaphore = GetThreadSemaphore(); |
| 637 |
int my_generation; // The broadcast generation this waiter is in |
| 638 |
|
| 639 |
{ |
| 640 |
this_wait.next = NULL; |
| 641 |
this_wait.semaphore = semaphore; |
| 642 |
this_wait.chosen_by_signal = 0; |
| 643 |
|
| 644 |
ScopedLock lock1(real_cond.lock); |
| 645 |
|
| 646 |
// Add this waiter to the end of the list. |
| 647 |
this_wait.prev = real_cond.tail_waiter; |
| 648 |
if (real_cond.tail_waiter != NULL) |
| 649 |
real_cond.tail_waiter->next = &this_wait; |
| 650 |
real_cond.tail_waiter = &this_wait; |
| 651 |
|
| 652 |
// If the list was empty, set the head of the list to this waiter. |
| 653 |
if (real_cond.head_waiter == NULL) |
| 654 |
real_cond.head_waiter = &this_wait; |
| 655 |
|
| 656 |
// Note which broadcast generation this waiter belongs to. |
| 657 |
my_generation = real_cond.broadcast_generation; |
| 658 |
} |
| 659 |
|
| 660 |
real_mutex.Unlock(); |
| 661 |
|
| 662 |
DWORD result = ::WaitForSingleObject(semaphore, timeout_milliseconds); |
| 663 |
|
| 664 |
if (result == WAIT_TIMEOUT) |
| 665 |
{ |
| 666 |
// If the wait timed out, this thread is likely still on the waiters list |
| 667 |
// of the condition. However, there is a race in that the thread may have been |
| 668 |
// signaled or broadcast between when WaitForSingleObject decided |
| 669 |
// we had timed out and this code running. |
| 670 |
|
| 671 |
bool mustConsumeSemaphore = false; |
| 672 |
{ |
| 673 |
ScopedLock lock2(real_cond.lock); |
| 674 |
|
| 675 |
bool chosen_by_signal = this_wait.chosen_by_signal; |
| 676 |
bool chosen_by_broadcast = my_generation != real_cond.broadcast_generation; |
| 677 |
|
| 678 |
if (chosen_by_signal || chosen_by_broadcast) |
| 679 |
mustConsumeSemaphore = true; |
| 680 |
else |
| 681 |
{ |
| 682 |
// Still on waiters list. Remove this waiter from list. |
| 683 |
if (this_wait.next != NULL) |
| 684 |
this_wait.next->prev = this_wait.prev; |
| 685 |
else |
| 686 |
real_cond.tail_waiter = this_wait.prev; |
| 687 |
|
| 688 |
if (this_wait.prev != NULL) |
| 689 |
this_wait.prev->next = this_wait.next; |
| 690 |
else |
| 691 |
real_cond.head_waiter = this_wait.next; |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
if (mustConsumeSemaphore) |
| 696 |
{ |
| 697 |
::WaitForSingleObject(semaphore, INFINITE); |
| 698 |
result = WAIT_OBJECT_0; |
| 699 |
} |
| 700 |
} |
| 701 |
else |
| 702 |
DNG_ASSERT (result == WAIT_OBJECT_0, "pthread emulation logic error"); |
| 703 |
|
| 704 |
// reacquire the mutex |
| 705 |
real_mutex.Lock(); |
| 706 |
|
| 707 |
return (result == WAIT_TIMEOUT) ? DNG_ETIMEDOUT : 0; |
| 708 |
#endif |
| 709 |
} |
| 710 |
|
| 711 |
/*****************************************************************************/ |
| 712 |
|
| 713 |
int dng_pthread_cond_wait(dng_pthread_cond_t *cond, dng_pthread_mutex_t *mutex) |
| 714 |
{ |
| 715 |
ValidateCond(cond); |
| 716 |
|
| 717 |
return cond_wait_internal(cond, mutex, INFINITE); |
| 718 |
} |
| 719 |
|
| 720 |
/*****************************************************************************/ |
| 721 |
|
| 722 |
int dng_pthread_cond_timedwait(dng_pthread_cond_t *cond, dng_pthread_mutex_t *mutex, struct dng_timespec *latest_time) |
| 723 |
{ |
| 724 |
ValidateCond(cond); |
| 725 |
|
| 726 |
struct dng_timespec sys_timespec; |
| 727 |
|
| 728 |
#if defined(_MSC_VER) && _MSC_VER >= 1900 |
| 729 |
|
| 730 |
struct timespec temp; |
| 731 |
dng_pthread_now (&temp); |
| 732 |
|
| 733 |
sys_timespec.tv_sec = (long)temp.tv_sec; |
| 734 |
sys_timespec.tv_nsec = temp.tv_nsec; |
| 735 |
|
| 736 |
#else |
| 737 |
|
| 738 |
dng_pthread_now (&sys_timespec); |
| 739 |
|
| 740 |
#endif |
| 741 |
|
| 742 |
__int64 sys_time = (__int64)sys_timespec.tv_sec * 1000000000 + sys_timespec.tv_nsec; |
| 743 |
__int64 lock_time = (__int64)latest_time->tv_sec * 1000000000 + latest_time->tv_nsec; |
| 744 |
|
| 745 |
int wait_millisecs = (int)((lock_time - sys_time + 500000) / 1000000); |
| 746 |
|
| 747 |
if (wait_millisecs < 0) |
| 748 |
wait_millisecs = 0; |
| 749 |
|
| 750 |
return cond_wait_internal(cond, mutex, wait_millisecs); |
| 751 |
} |
| 752 |
|
| 753 |
/*****************************************************************************/ |
| 754 |
|
| 755 |
int dng_pthread_cond_signal(dng_pthread_cond_t *cond) |
| 756 |
{ |
| 757 |
ValidateCond(cond); |
| 758 |
|
| 759 |
#if qDNGUseConditionVariable |
| 760 |
|
| 761 |
WakeConditionVariable(&(*cond)->cond); |
| 762 |
return 0; |
| 763 |
|
| 764 |
#else |
| 765 |
|
| 766 |
waiter *first; |
| 767 |
dng_pthread_cond_impl &real_cond = **cond; |
| 768 |
|
| 769 |
{ |
| 770 |
ScopedLock lock(real_cond.lock); |
| 771 |
|
| 772 |
first = real_cond.head_waiter; |
| 773 |
if (first != NULL) |
| 774 |
{ |
| 775 |
if (first->next != NULL) |
| 776 |
first->next->prev = NULL; |
| 777 |
else |
| 778 |
real_cond.tail_waiter = NULL; // Or first->prev, which is always NULL in this case |
| 779 |
|
| 780 |
first->chosen_by_signal = true; |
| 781 |
|
| 782 |
real_cond.head_waiter = first->next; |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
if (first != NULL) |
| 787 |
::ReleaseSemaphore(first->semaphore, 1, NULL); |
| 788 |
|
| 789 |
return 0; |
| 790 |
#endif |
| 791 |
} |
| 792 |
|
| 793 |
/*****************************************************************************/ |
| 794 |
|
| 795 |
int dng_pthread_cond_broadcast(dng_pthread_cond_t *cond) |
| 796 |
{ |
| 797 |
ValidateCond(cond); |
| 798 |
|
| 799 |
#if qDNGUseConditionVariable |
| 800 |
|
| 801 |
WakeAllConditionVariable(&(*cond)->cond); |
| 802 |
return 0; |
| 803 |
|
| 804 |
#else |
| 805 |
|
| 806 |
waiter *first; |
| 807 |
dng_pthread_cond_impl &real_cond = **cond; |
| 808 |
|
| 809 |
{ |
| 810 |
ScopedLock lock(real_cond.lock); |
| 811 |
|
| 812 |
first = real_cond.head_waiter; |
| 813 |
real_cond.head_waiter = NULL; |
| 814 |
real_cond.tail_waiter = NULL; |
| 815 |
|
| 816 |
real_cond.broadcast_generation++; |
| 817 |
} |
| 818 |
|
| 819 |
while (first != NULL) |
| 820 |
{ |
| 821 |
waiter *next = first->next; |
| 822 |
::ReleaseSemaphore(first->semaphore, 1, NULL); |
| 823 |
first = next; |
| 824 |
} |
| 825 |
|
| 826 |
return 0; |
| 827 |
#endif |
| 828 |
} |
| 829 |
|
| 830 |
/*****************************************************************************/ |
| 831 |
|
| 832 |
int dng_pthread_once(dng_pthread_once_t *once, void (*init_func)()) |
| 833 |
{ |
| 834 |
if (once == NULL || init_func == NULL) |
| 835 |
return EINVAL; |
| 836 |
|
| 837 |
if (once->inited) |
| 838 |
return 0; |
| 839 |
|
| 840 |
if (::InterlockedIncrement(&once->semaphore) == 0) |
| 841 |
{ |
| 842 |
init_func(); |
| 843 |
once->inited = 1; |
| 844 |
} |
| 845 |
else |
| 846 |
{ |
| 847 |
while (!once->inited) |
| 848 |
Sleep(0); |
| 849 |
} |
| 850 |
|
| 851 |
return 0; |
| 852 |
} |
| 853 |
|
| 854 |
/*****************************************************************************/ |
| 855 |
|
| 856 |
int dng_pthread_key_create(dng_pthread_key_t * key, void (*destructor) (void *)) |
| 857 |
{ |
| 858 |
if (destructor != NULL) |
| 859 |
return -1; |
| 860 |
|
| 861 |
DWORD result = ::TlsAlloc(); |
| 862 |
if (result == TLS_OUT_OF_INDEXES) |
| 863 |
return -1; |
| 864 |
*key = (unsigned long)result; |
| 865 |
return 0; |
| 866 |
} |
| 867 |
|
| 868 |
/*****************************************************************************/ |
| 869 |
|
| 870 |
int dng_pthread_key_delete(dng_pthread_key_t key) |
| 871 |
{ |
| 872 |
if (::TlsFree((DWORD)key)) |
| 873 |
return 0; |
| 874 |
return -1; |
| 875 |
} |
| 876 |
|
| 877 |
/*****************************************************************************/ |
| 878 |
|
| 879 |
int dng_pthread_setspecific(dng_pthread_key_t key, const void *value) |
| 880 |
{ |
| 881 |
if (::TlsSetValue((DWORD)key, const_cast<void *>(value))) |
| 882 |
return 0; |
| 883 |
return -1; |
| 884 |
} |
| 885 |
|
| 886 |
/*****************************************************************************/ |
| 887 |
|
| 888 |
void *dng_pthread_getspecific(dng_pthread_key_t key) |
| 889 |
{ |
| 890 |
return ::TlsGetValue((DWORD)key); |
| 891 |
} |
| 892 |
|
| 893 |
/*****************************************************************************/ |
| 894 |
|
| 895 |
#if !qDNGUseConditionVariable |
| 896 |
|
| 897 |
namespace { |
| 898 |
struct rw_waiter { |
| 899 |
struct rw_waiter *prev; |
| 900 |
struct rw_waiter *next; |
| 901 |
HANDLE semaphore; |
| 902 |
bool is_writer; |
| 903 |
}; |
| 904 |
} |
| 905 |
|
| 906 |
#endif |
| 907 |
|
| 908 |
struct dng_pthread_rwlock_impl |
| 909 |
{ |
| 910 |
|
| 911 |
|
| 912 |
#if qDNGUseConditionVariable |
| 913 |
SRWLOCK rwlock; |
| 914 |
bool fWriteLockExclusive; |
| 915 |
|
| 916 |
dng_pthread_rwlock_impl () { InitializeSRWLock(&rwlock); } |
| 917 |
~dng_pthread_rwlock_impl () { } // no delete listed |
| 918 |
|
| 919 |
|
| 920 |
#else |
| 921 |
dng_pthread_mutex_impl mutex; |
| 922 |
|
| 923 |
rw_waiter *head_waiter; |
| 924 |
rw_waiter *tail_waiter; |
| 925 |
|
| 926 |
unsigned long readers_active; |
| 927 |
unsigned long writers_waiting; |
| 928 |
bool writer_active; |
| 929 |
|
| 930 |
dng_pthread_cond_impl read_wait; |
| 931 |
dng_pthread_cond_impl write_wait; |
| 932 |
|
| 933 |
dng_pthread_rwlock_impl () |
| 934 |
: mutex () |
| 935 |
, head_waiter (NULL) |
| 936 |
, tail_waiter (NULL) |
| 937 |
, readers_active (0) |
| 938 |
, writers_waiting (0) |
| 939 |
, writer_active (false) |
| 940 |
, read_wait () |
| 941 |
, write_wait () |
| 942 |
{ |
| 943 |
} |
| 944 |
|
| 945 |
~dng_pthread_rwlock_impl () |
| 946 |
{ |
| 947 |
} |
| 948 |
|
| 949 |
void WakeHeadWaiter () |
| 950 |
{ |
| 951 |
HANDLE semaphore = head_waiter->semaphore; |
| 952 |
|
| 953 |
head_waiter = head_waiter->next; |
| 954 |
if (head_waiter == NULL) |
| 955 |
tail_waiter = NULL; |
| 956 |
|
| 957 |
::ReleaseSemaphore(semaphore, 1, NULL); |
| 958 |
} |
| 959 |
#endif |
| 960 |
|
| 961 |
// Non copyable |
| 962 |
private: |
| 963 |
|
| 964 |
dng_pthread_rwlock_impl &operator=(const dng_pthread_rwlock_impl &); |
| 965 |
dng_pthread_rwlock_impl (const dng_pthread_rwlock_impl &); |
| 966 |
|
| 967 |
}; |
| 968 |
|
| 969 |
/*****************************************************************************/ |
| 970 |
|
| 971 |
int dng_pthread_rwlock_init(dng_pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attrs) |
| 972 |
{ |
| 973 |
dng_pthread_rwlock_impl *newRWLock; |
| 974 |
|
| 975 |
newRWLock = new (std::nothrow) dng_pthread_rwlock_impl; |
| 976 |
if (newRWLock == NULL) |
| 977 |
return -1; // ENOMEM; |
| 978 |
|
| 979 |
*rwlock = newRWLock; |
| 980 |
|
| 981 |
return 0; |
| 982 |
} |
| 983 |
|
| 984 |
/*****************************************************************************/ |
| 985 |
|
| 986 |
int dng_pthread_rwlock_destroy(dng_pthread_rwlock_t *rwlock) |
| 987 |
{ |
| 988 |
dng_pthread_rwlock_impl &real_rwlock = **rwlock; |
| 989 |
|
| 990 |
#if !qDNGUseConditionVariable |
| 991 |
|
| 992 |
{ |
| 993 |
ScopedLock lock (real_rwlock.mutex); |
| 994 |
|
| 995 |
if (real_rwlock.head_waiter != NULL || |
| 996 |
real_rwlock.readers_active != 0 || |
| 997 |
real_rwlock.writers_waiting != 0 || |
| 998 |
real_rwlock.writer_active) |
| 999 |
return -1; // EBUSY |
| 1000 |
} |
| 1001 |
#endif |
| 1002 |
|
| 1003 |
delete *rwlock; |
| 1004 |
*rwlock = NULL; |
| 1005 |
return 0; |
| 1006 |
} |
| 1007 |
|
| 1008 |
/*****************************************************************************/ |
| 1009 |
|
| 1010 |
#if !qDNGUseConditionVariable |
| 1011 |
|
| 1012 |
#define CHECK_RWLOCK_STATE(real_rwlock) \ |
| 1013 |
DNG_ASSERT (!real_rwlock.writer_active || real_rwlock.readers_active == 0, "dng_pthread_rwlock_t logic error") |
| 1014 |
|
| 1015 |
#endif |
| 1016 |
|
| 1017 |
/*****************************************************************************/ |
| 1018 |
|
| 1019 |
int dng_pthread_rwlock_rdlock(dng_pthread_rwlock_t *rwlock) |
| 1020 |
{ |
| 1021 |
#if qDNGUseConditionVariable |
| 1022 |
// Note: Acquire cannot be called resursively from same thread, once acquired or deadlock will occur |
| 1023 |
|
| 1024 |
AcquireSRWLockShared(&(*rwlock)->rwlock); |
| 1025 |
(*rwlock)->fWriteLockExclusive = false; |
| 1026 |
|
| 1027 |
return 0; |
| 1028 |
|
| 1029 |
#else |
| 1030 |
|
| 1031 |
dng_pthread_rwlock_impl &real_rwlock = **rwlock; |
| 1032 |
|
| 1033 |
struct rw_waiter this_wait; |
| 1034 |
bool doWait = false;; |
| 1035 |
int result = 0; |
| 1036 |
HANDLE semaphore=NULL; |
| 1037 |
|
| 1038 |
{ |
| 1039 |
|
| 1040 |
ScopedLock lock (real_rwlock.mutex); |
| 1041 |
|
| 1042 |
CHECK_RWLOCK_STATE (real_rwlock); |
| 1043 |
|
| 1044 |
if (real_rwlock.writers_waiting > 0 || real_rwlock.writer_active) |
| 1045 |
{ |
| 1046 |
semaphore = GetThreadSemaphore(); |
| 1047 |
|
| 1048 |
this_wait.next = NULL; |
| 1049 |
this_wait.semaphore = semaphore; |
| 1050 |
this_wait.is_writer = false; |
| 1051 |
|
| 1052 |
// Add this waiter to the end of the list. |
| 1053 |
this_wait.prev = real_rwlock.tail_waiter; |
| 1054 |
if (real_rwlock.tail_waiter != NULL) |
| 1055 |
real_rwlock.tail_waiter->next = &this_wait; |
| 1056 |
real_rwlock.tail_waiter = &this_wait; |
| 1057 |
|
| 1058 |
// If the list was empty, set the head of the list to this waiter. |
| 1059 |
if (real_rwlock.head_waiter == NULL) |
| 1060 |
real_rwlock.head_waiter = &this_wait; |
| 1061 |
|
| 1062 |
doWait = true; |
| 1063 |
} |
| 1064 |
else |
| 1065 |
real_rwlock.readers_active++; |
| 1066 |
} |
| 1067 |
|
| 1068 |
if (result == 0 && doWait) |
| 1069 |
result = (WaitForSingleObject(semaphore, INFINITE) == WAIT_OBJECT_0) ? 0 : -1; |
| 1070 |
|
| 1071 |
return result; |
| 1072 |
#endif |
| 1073 |
} |
| 1074 |
|
| 1075 |
/*****************************************************************************/ |
| 1076 |
|
| 1077 |
int dng_pthread_rwlock_tryrdlock(dng_pthread_rwlock_t *rwlock) |
| 1078 |
{ |
| 1079 |
#if qDNGUseConditionVariable |
| 1080 |
|
| 1081 |
if (TryAcquireSRWLockExclusive(&(*rwlock)->rwlock) == 0) |
| 1082 |
return 0; |
| 1083 |
|
| 1084 |
(*rwlock)->fWriteLockExclusive = false; |
| 1085 |
return -1; |
| 1086 |
|
| 1087 |
#else |
| 1088 |
dng_pthread_rwlock_impl &real_rwlock = **rwlock; |
| 1089 |
|
| 1090 |
ScopedLock lock (real_rwlock.mutex); |
| 1091 |
|
| 1092 |
CHECK_RWLOCK_STATE (real_rwlock); |
| 1093 |
|
| 1094 |
if (real_rwlock.writers_waiting == 0 && !real_rwlock.writer_active) |
| 1095 |
{ |
| 1096 |
real_rwlock.readers_active++; |
| 1097 |
return 0; |
| 1098 |
} |
| 1099 |
|
| 1100 |
return -1; |
| 1101 |
#endif |
| 1102 |
} |
| 1103 |
|
| 1104 |
/*****************************************************************************/ |
| 1105 |
|
| 1106 |
int dng_pthread_rwlock_trywrlock(dng_pthread_rwlock_t *rwlock) |
| 1107 |
{ |
| 1108 |
#if qDNGUseConditionVariable |
| 1109 |
|
| 1110 |
if (TryAcquireSRWLockShared(&(*rwlock)->rwlock) == 0) |
| 1111 |
return 0; |
| 1112 |
|
| 1113 |
(*rwlock)->fWriteLockExclusive = true; |
| 1114 |
return -1; |
| 1115 |
|
| 1116 |
#else |
| 1117 |
dng_pthread_rwlock_impl &real_rwlock = **rwlock; |
| 1118 |
|
| 1119 |
ScopedLock lock (real_rwlock.mutex); |
| 1120 |
|
| 1121 |
CHECK_RWLOCK_STATE (real_rwlock); |
| 1122 |
|
| 1123 |
if (real_rwlock.readers_active == 0 && |
| 1124 |
real_rwlock.writers_waiting == 0 && |
| 1125 |
!real_rwlock.writer_active) |
| 1126 |
{ |
| 1127 |
real_rwlock.writer_active = true; |
| 1128 |
return 0; |
| 1129 |
} |
| 1130 |
|
| 1131 |
return -1; |
| 1132 |
#endif |
| 1133 |
} |
| 1134 |
|
| 1135 |
/*****************************************************************************/ |
| 1136 |
|
| 1137 |
int dng_pthread_rwlock_unlock(dng_pthread_rwlock_t *rwlock) |
| 1138 |
{ |
| 1139 |
#if qDNGUseConditionVariable |
| 1140 |
|
| 1141 |
if ((*rwlock)->fWriteLockExclusive) |
| 1142 |
ReleaseSRWLockExclusive(&(*rwlock)->rwlock); |
| 1143 |
else |
| 1144 |
ReleaseSRWLockShared(&(*rwlock)->rwlock); |
| 1145 |
|
| 1146 |
return 0; |
| 1147 |
|
| 1148 |
#else |
| 1149 |
dng_pthread_rwlock_impl &real_rwlock = **rwlock; |
| 1150 |
|
| 1151 |
int result = 0; |
| 1152 |
|
| 1153 |
ScopedLock lock (real_rwlock.mutex); |
| 1154 |
|
| 1155 |
CHECK_RWLOCK_STATE (real_rwlock); |
| 1156 |
|
| 1157 |
if (real_rwlock.readers_active > 0) |
| 1158 |
--real_rwlock.readers_active; |
| 1159 |
else |
| 1160 |
real_rwlock.writer_active = false; |
| 1161 |
|
| 1162 |
while (real_rwlock.head_waiter != NULL) |
| 1163 |
{ |
| 1164 |
if (real_rwlock.head_waiter->is_writer) |
| 1165 |
{ |
| 1166 |
if (real_rwlock.readers_active == 0) |
| 1167 |
{ |
| 1168 |
real_rwlock.writers_waiting--; |
| 1169 |
real_rwlock.writer_active = true; |
| 1170 |
real_rwlock.WakeHeadWaiter (); |
| 1171 |
} |
| 1172 |
|
| 1173 |
break; |
| 1174 |
} |
| 1175 |
else |
| 1176 |
{ |
| 1177 |
++real_rwlock.readers_active; |
| 1178 |
real_rwlock.WakeHeadWaiter (); |
| 1179 |
} |
| 1180 |
} |
| 1181 |
|
| 1182 |
return result; |
| 1183 |
#endif |
| 1184 |
} |
| 1185 |
|
| 1186 |
/*****************************************************************************/ |
| 1187 |
|
| 1188 |
int dng_pthread_rwlock_wrlock(dng_pthread_rwlock_t *rwlock) |
| 1189 |
{ |
| 1190 |
#if qDNGUseConditionVariable |
| 1191 |
|
| 1192 |
AcquireSRWLockExclusive(&(*rwlock)->rwlock); |
| 1193 |
(*rwlock)->fWriteLockExclusive = true; |
| 1194 |
|
| 1195 |
return 0; |
| 1196 |
|
| 1197 |
#else |
| 1198 |
dng_pthread_rwlock_impl &real_rwlock = **rwlock; |
| 1199 |
|
| 1200 |
int result = 0; |
| 1201 |
struct rw_waiter this_wait; |
| 1202 |
HANDLE semaphore=NULL; |
| 1203 |
bool doWait = false; |
| 1204 |
|
| 1205 |
{ |
| 1206 |
ScopedLock lock (real_rwlock.mutex); |
| 1207 |
|
| 1208 |
CHECK_RWLOCK_STATE (real_rwlock); |
| 1209 |
|
| 1210 |
if (real_rwlock.readers_active || |
| 1211 |
real_rwlock.writers_waiting || |
| 1212 |
real_rwlock.writer_active) |
| 1213 |
{ |
| 1214 |
semaphore = GetThreadSemaphore(); |
| 1215 |
|
| 1216 |
this_wait.next = NULL; |
| 1217 |
this_wait.semaphore = semaphore; |
| 1218 |
this_wait.is_writer = true; |
| 1219 |
|
| 1220 |
// Add this waiter to the end of the list. |
| 1221 |
this_wait.prev = real_rwlock.tail_waiter; |
| 1222 |
if (real_rwlock.tail_waiter != NULL) |
| 1223 |
real_rwlock.tail_waiter->next = &this_wait; |
| 1224 |
real_rwlock.tail_waiter = &this_wait; |
| 1225 |
|
| 1226 |
// If the list was empty, set the head of the list to this waiter. |
| 1227 |
if (real_rwlock.head_waiter == NULL) |
| 1228 |
real_rwlock.head_waiter = &this_wait; |
| 1229 |
|
| 1230 |
real_rwlock.writers_waiting++; |
| 1231 |
|
| 1232 |
doWait = true; |
| 1233 |
} |
| 1234 |
else |
| 1235 |
real_rwlock.writer_active = true; |
| 1236 |
} |
| 1237 |
|
| 1238 |
if (result == 0 && doWait) |
| 1239 |
result = (WaitForSingleObject(semaphore, INFINITE) == WAIT_OBJECT_0) ? 0 : -1; |
| 1240 |
|
| 1241 |
return result; |
| 1242 |
#endif |
| 1243 |
} |
| 1244 |
|
| 1245 |
/*****************************************************************************/ |
| 1246 |
|
| 1247 |
void dng_pthread_disassociate() |
| 1248 |
{ |
| 1249 |
FreeThreadSemaphore(); |
| 1250 |
} |
| 1251 |
|
| 1252 |
void dng_pthread_terminate() |
| 1253 |
{ |
| 1254 |
finalize_thread_TLS(); |
| 1255 |
} |
| 1256 |
|
| 1257 |
/*****************************************************************************/ |
| 1258 |
|
| 1259 |
} // extern "C" |
| 1260 |
|
| 1261 |
/*****************************************************************************/ |
| 1262 |
|
| 1263 |
#endif |
| 1264 |
|
| 1265 |
/*****************************************************************************/ |
| 1266 |
|
| 1267 |
int dng_pthread_now (struct timespec *now) |
| 1268 |
{ |
| 1269 |
|
| 1270 |
if (now == NULL) |
| 1271 |
return -1; // EINVAL |
| 1272 |
|
| 1273 |
#if qWinOS |
| 1274 |
|
| 1275 |
FILETIME ft; |
| 1276 |
::GetSystemTimeAsFileTime(&ft); |
| 1277 |
|
| 1278 |
__int64 sys_time = ((__int64)ft.dwHighDateTime << 32) + ft.dwLowDateTime; |
| 1279 |
|
| 1280 |
#define SecsFrom1601To1970 11644473600 |
| 1281 |
|
| 1282 |
sys_time -= SecsFrom1601To1970 * 10000000LL; |
| 1283 |
|
| 1284 |
sys_time *= 100; // Convert from 100ns to 1ns units |
| 1285 |
|
| 1286 |
now->tv_sec = (long)(sys_time / 1000000000); |
| 1287 |
now->tv_nsec = (long)(sys_time % 1000000000); |
| 1288 |
|
| 1289 |
#else |
| 1290 |
|
| 1291 |
struct timeval tv; |
| 1292 |
|
| 1293 |
if (gettimeofday (&tv, NULL) != 0) |
| 1294 |
return errno; |
| 1295 |
|
| 1296 |
now->tv_sec = tv.tv_sec; |
| 1297 |
now->tv_nsec = tv.tv_usec * 1000; |
| 1298 |
|
| 1299 |
#endif |
| 1300 |
|
| 1301 |
return 0; |
| 1302 |
|
| 1303 |
} |
| 1304 |
|
| 1305 |
/*****************************************************************************/ |
| 1306 |
|
| 1307 |
#endif // qDNGThreadSafe |
| 1308 |
|
| 1309 |
/*****************************************************************************/ |