int cnd_timedwait_ms(cnd_t *cnd, mtx_t *mtx, int timeout_ms) { #if defined(_TTHREAD_WIN32_) return _cnd_timedwait_win32(cnd, mtx, (DWORD)timeout_ms); #else int ret; struct timeval tv; struct timespec ts; gettimeofday(&tv, NULL); ts.tv_sec = tv.tv_sec; ts.tv_nsec = tv.tv_usec * 1000; ts.tv_sec += timeout_ms / 1000; ts.tv_nsec += (timeout_ms % 1000) * 1000000; if (ts.tv_nsec > 1000000000) { ts.tv_sec++; ts.tv_nsec -= 1000000000; } ret = pthread_cond_timedwait(cnd, mtx, &ts); if (ret == ETIMEDOUT) { return thrd_timedout; } return ret == 0 ? thrd_success : thrd_error; #endif }
int cnd_wait(cnd_t *cond, mtx_t *mtx) { #if defined(_TTHREAD_WIN32_) return _cnd_timedwait_win32(cond, mtx, INFINITE); #else return pthread_cond_wait(cond, mtx) == 0 ? thrd_success : thrd_error; #endif }
int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts) { #if defined(_TTHREAD_WIN32_) struct timespec now; if (clock_gettime(TIME_UTC, &now) == 0) { DWORD delta = (DWORD) ((ts->tv_sec - now.tv_sec) * 1000 + (ts->tv_nsec - now.tv_nsec + 500000) / 1000000); return _cnd_timedwait_win32(cond, mtx, delta); } return thrd_error; #else int ret; ret = pthread_cond_timedwait(cond, mtx, ts); if (ret == ETIMEDOUT) return thrd_timeout; return ret == 0 ? thrd_success : thrd_error; #endif }
int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts) { #if defined(_TTHREAD_WIN32_) struct timespec now; if (timespec_get(&now, TIME_UTC) == TIME_UTC) { unsigned long long nowInMilliseconds = now.tv_sec * 1000 + now.tv_nsec / 1000000; unsigned long long tsInMilliseconds = ts->tv_sec * 1000 + ts->tv_nsec / 1000000; DWORD delta = (tsInMilliseconds > nowInMilliseconds) ? (DWORD)(tsInMilliseconds - nowInMilliseconds) : 0; return _cnd_timedwait_win32(cond, mtx, delta); } else return thrd_error; #else int ret; ret = pthread_cond_timedwait(cond, mtx, ts); if (ret == ETIMEDOUT) { return thrd_timedout; } return ret == 0 ? thrd_success : thrd_error; #endif }
int cnd_timedwait_ms(cnd_t *cnd, mtx_t *mtx, int timeout_ms) { if (timeout_ms == -1 /* INFINITE*/) return cnd_wait(cnd, mtx); #if defined(_TTHREAD_WIN32_) return _cnd_timedwait_win32(cnd, mtx, (DWORD)timeout_ms); #else struct timeval tv; struct timespec ts; gettimeofday(&tv, NULL); ts.tv_sec = tv.tv_sec; ts.tv_nsec = tv.tv_usec * 1000; ts.tv_sec += timeout_ms / 1000; ts.tv_nsec += (timeout_ms % 1000) * 1000000; if (ts.tv_nsec >= 1000000000) { ts.tv_sec++; ts.tv_nsec -= 1000000000; } return cnd_timedwait(cnd, mtx, &ts); #endif }