int main(void) { tds_condition cond; tds_thread th; void *res; check(tds_cond_init(&cond), "failed initializing condition"); tds_mutex_lock(&mtx); check(tds_thread_create(&th, signal_proc, &cond) != 0, "error creating thread"); sleep(1); check(tds_cond_wait(&cond, &mtx), "failed waiting condition"); check(tds_thread_join(th, &res) != 0, "error waiting thread"); check(ptr2int(res) != 0, "error signaling condition"); tds_mutex_unlock(&mtx); check(tds_cond_destroy(&cond), "failed destroying condition"); return 0; }
int tds_cond_timedwait(tds_condition *cond, pthread_mutex_t *mtx, int timeout_sec) { struct timespec ts; #ifndef USE_CLOCK_IN_COND struct timeval tv; #endif if (timeout_sec < 0) return tds_cond_wait(cond, mtx); #ifdef USE_CLOCK_IN_COND # if defined(USE_MONOTONIC_CLOCK_IN_COND) clock_gettime(CLOCK_MONOTONIC, &ts); # else clock_gettime(CLOCK_REALTIME, &ts); # endif #elif defined(HAVE_GETTIMEOFDAY) gettimeofday(&tv, NULL); ts.tv_sec = tv.tv_sec; ts.tv_nsec = tv.tv_usec * 1000u; #else #error No way to get a proper time! #endif ts.tv_sec += timeout_sec; return pthread_cond_timedwait(cond, mtx, &ts); }
int main(void) { tds_condition cond; tds_thread th; void *res; check(tds_cond_init(&cond), "failed initializing condition"); tds_mutex_lock(&mtx); check(tds_thread_create(&th, signal_proc, &cond) != 0, "error creating thread"); tds_sleep_ms(100); check(tds_cond_wait(&cond, &mtx), "failed waiting condition"); res = &th; check(tds_thread_join(th, &res) != 0, "error waiting thread"); check(ptr2int(res) != 0, "error signaling condition"); /* under Windows mutex are recursive */ #ifndef _WIN32 check(tds_mutex_trylock(&mtx) == 0, "mutex should be locked"); #endif /* check timed version */ check(tds_cond_timedwait(&cond, &mtx, 1) == 0, "should not succeed to wait condition"); check(tds_thread_create(&th, signal_proc, &cond) != 0, "error creating thread"); check(tds_cond_timedwait(&cond, &mtx, 1), "error on timed waiting condition"); res = &th; check(tds_thread_join(th, &res) != 0, "error waiting thread"); check(ptr2int(res) != 0, "error signaling condition"); tds_mutex_unlock(&mtx); check(tds_cond_destroy(&cond), "failed destroying condition"); return 0; }