34 #if defined(_TTHREAD_POSIX_) 40 #elif defined(_TTHREAD_WIN32_) 42 #include <sys/timeb.h> 58 #if defined(_TTHREAD_WIN32_) 59 mtx->mAlreadyLocked =
FALSE;
61 InitializeCriticalSection(&mtx->mHandle);
65 pthread_mutexattr_t attr;
66 pthread_mutexattr_init(&attr);
67 if (type & mtx_recursive)
69 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
71 ret = pthread_mutex_init(mtx, &attr);
72 pthread_mutexattr_destroy(&attr);
79 #if defined(_TTHREAD_WIN32_) 80 DeleteCriticalSection(&mtx->mHandle);
82 pthread_mutex_destroy(mtx);
88 #if defined(_TTHREAD_WIN32_) 89 EnterCriticalSection(&mtx->mHandle);
92 while(mtx->mAlreadyLocked) Sleep(1000);
93 mtx->mAlreadyLocked =
TRUE;
111 #if defined(_TTHREAD_WIN32_) 113 if ((!mtx->mRecursive) && (ret ==
thrd_success) && mtx->mAlreadyLocked)
115 LeaveCriticalSection(&mtx->mHandle);
126 #if defined(_TTHREAD_WIN32_) 127 mtx->mAlreadyLocked =
FALSE;
128 LeaveCriticalSection(&mtx->mHandle);
135 #if defined(_TTHREAD_WIN32_) 136 #define _CONDITION_EVENT_ONE 0 137 #define _CONDITION_EVENT_ALL 1 142 #if defined(_TTHREAD_WIN32_) 143 cond->mWaitersCount = 0;
146 InitializeCriticalSection(&cond->mWaitersCountLock);
150 if (cond->mEvents[_CONDITION_EVENT_ONE] ==
NULL)
152 cond->mEvents[_CONDITION_EVENT_ALL] =
NULL;
156 if (cond->mEvents[_CONDITION_EVENT_ALL] ==
NULL)
158 CloseHandle(cond->mEvents[_CONDITION_EVENT_ONE]);
159 cond->mEvents[_CONDITION_EVENT_ONE] =
NULL;
171 #if defined(_TTHREAD_WIN32_) 172 if (cond->mEvents[_CONDITION_EVENT_ONE] !=
NULL)
174 CloseHandle(cond->mEvents[_CONDITION_EVENT_ONE]);
176 if (cond->mEvents[_CONDITION_EVENT_ALL] !=
NULL)
178 CloseHandle(cond->mEvents[_CONDITION_EVENT_ALL]);
180 DeleteCriticalSection(&cond->mWaitersCountLock);
182 pthread_cond_destroy(cond);
188 #if defined(_TTHREAD_WIN32_) 192 EnterCriticalSection(&cond->mWaitersCountLock);
193 haveWaiters = (cond->mWaitersCount > 0);
194 LeaveCriticalSection(&cond->mWaitersCountLock);
199 if (SetEvent(cond->mEvents[_CONDITION_EVENT_ONE]) == 0)
213 #if defined(_TTHREAD_WIN32_) 217 EnterCriticalSection(&cond->mWaitersCountLock);
218 haveWaiters = (cond->mWaitersCount > 0);
219 LeaveCriticalSection(&cond->mWaitersCountLock);
224 if (SetEvent(cond->mEvents[_CONDITION_EVENT_ALL]) == 0)
236 #if defined(_TTHREAD_WIN32_) 242 EnterCriticalSection(&cond->mWaitersCountLock);
243 ++ cond->mWaitersCount;
244 LeaveCriticalSection(&cond->mWaitersCountLock);
252 result = WaitForMultipleObjects(2, cond->mEvents,
FALSE, timeout);
253 if (result == WAIT_TIMEOUT)
257 else if (result == (
int)WAIT_FAILED)
263 EnterCriticalSection(&cond->mWaitersCountLock);
264 -- cond->mWaitersCount;
265 lastWaiter = (result == (WAIT_OBJECT_0 + _CONDITION_EVENT_ALL)) &&
266 (cond->mWaitersCount == 0);
267 LeaveCriticalSection(&cond->mWaitersCountLock);
272 if (ResetEvent(cond->mEvents[_CONDITION_EVENT_ALL]) == 0)
287 #if defined(_TTHREAD_WIN32_) 288 return _cnd_timedwait_win32(cond, mtx, INFINITE);
296 #if defined(_TTHREAD_WIN32_) 298 if (clock_gettime(CLOCK_REALTIME, &now) == 0)
300 DWORD delta = (DWORD) ((ts->tv_sec - now.tv_sec) * 1000 +
301 (ts->tv_nsec - now.tv_nsec + 500000) / 1000000);
302 return _cnd_timedwait_win32(cond, mtx, delta);
308 ret = pthread_cond_timedwait(cond, mtx, ts);
309 if (ret == ETIMEDOUT)
325 #if defined(_TTHREAD_WIN32_) 327 #elif defined(_TTHREAD_POSIX_) 334 #if defined(_TTHREAD_POSIX_) 349 #if defined(_TTHREAD_WIN32_) 352 pres = malloc(
sizeof(
int));
374 #if defined(_TTHREAD_WIN32_) 376 #elif defined(_TTHREAD_POSIX_) 395 #if defined(_TTHREAD_WIN32_) 396 return GetCurrentThread();
398 return pthread_self();
411 #if defined(_TTHREAD_WIN32_) 414 return pthread_equal(thr0, thr1);
420 #if defined(_TTHREAD_WIN32_) 423 void *pres = malloc(
sizeof(
int));
434 #if defined(_TTHREAD_WIN32_) 435 if (WaitForSingleObject(thr, INFINITE) == WAIT_FAILED)
442 GetExitCodeThread(thr, &dwRes);
445 #elif defined(_TTHREAD_POSIX_) 448 if (pthread_join(thr, &pres) != 0)
465 int thrd_sleep(
const struct timespec *time_point,
struct timespec *remaining)
468 #if defined(_TTHREAD_WIN32_) 475 if (clock_gettime(CLOCK_REALTIME, &now) != 0)
478 #if defined(_TTHREAD_WIN32_) 480 delta = (DWORD) ((time_point->tv_sec - now.tv_sec) * 1000 +
481 (time_point->tv_nsec - now.tv_nsec + 500000) / 1000000);
488 delta = (time_point->tv_sec - now.tv_sec) * 1000000L +
489 (time_point->tv_nsec - now.tv_nsec + 500L) / 1000L;
492 while (delta > 999999L)
499 usleep((useconds_t)delta);
506 remaining->tv_sec = 0;
507 remaining->tv_nsec = 0;
514 #if defined(_TTHREAD_WIN32_) 523 #if defined(_TTHREAD_WIN32_) 530 if (*key == TLS_OUT_OF_INDEXES)
535 if (pthread_key_create(key, dtor) != 0)
545 #if defined(_TTHREAD_WIN32_) 548 pthread_key_delete(key);
554 #if defined(_TTHREAD_WIN32_) 555 return TlsGetValue(key);
557 return pthread_getspecific(key);
563 #if defined(_TTHREAD_WIN32_) 564 if (TlsSetValue(key, val) == 0)
569 if (pthread_setspecific(key, val) != 0)
577 #if defined(_TTHREAD_EMULATE_CLOCK_GETTIME_) 578 int _tthread_clock_gettime(clockid_t clk_id,
struct timespec *ts)
580 #if defined(_TTHREAD_WIN32_) 583 ts->tv_sec = (time_t)tb.time;
584 ts->tv_nsec = 1000000L * (
long)tb.millitm;
587 gettimeofday(&tv,
NULL);
588 ts->tv_sec = (time_t)tv.tv_sec;
589 ts->tv_nsec = 1000L * (
long)tv.tv_usec;
593 #endif // _TTHREAD_EMULATE_CLOCK_GETTIME_
typedef void(APIENTRY *GLDEBUGPROC)(GLenum source
int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts)
int mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
int thrd_equal(thrd_t thr0, thrd_t thr1)
int mtx_init(mtx_t *mtx, int type)
int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
int(* thrd_start_t)(void *arg)
void tss_delete(tss_t key)
int mtx_unlock(mtx_t *mtx)
int tss_set(tss_t key, void *val)
int tss_create(tss_t *key, tss_dtor_t dtor)
int cnd_init(cnd_t *cond)
int cnd_wait(cnd_t *cond, mtx_t *mtx)
void * tss_get(tss_t key)
void mtx_destroy(mtx_t *mtx)
void(* tss_dtor_t)(void *val)
int thrd_join(thrd_t thr, int *res)
int cnd_broadcast(cnd_t *cond)
void cnd_destroy(cnd_t *cond)
static void * _thrd_wrapper_function(void *aArg)
GLbitfield GLuint64 timeout
int cnd_signal(cnd_t *cond)
thrd_t thrd_current(void)
int thrd_sleep(const struct timespec *time_point, struct timespec *remaining)
int mtx_trylock(mtx_t *mtx)
int thrd_detach(thrd_t thr)