/* * Copyright (C) 2018 James E. King III * * This test exposes a problem in cygwin's condition variable clock * handling when setclock(CLOCK_MONOTONIC) is used on the attribute. * * Although ETIMEDOUT is being returned by pthread_cond_timedwait, * a subsequent fetch of the clock shows that the time point was not * reached. */ #include #include #include #include #include int main() { int ok; pthread_cond_t cv; pthread_condattr_t attr; pthread_mutex_t m; assert(!pthread_condattr_init(&attr)); pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); assert(!pthread_mutex_init(&m, NULL)); assert(!pthread_cond_init(&cv, &attr)); pthread_condattr_destroy(&attr); assert(!pthread_mutex_lock(&m)); do { struct timespec ts_begin; struct timespec ts_target; struct timespec ts_end; assert(!clock_gettime(CLOCK_MONOTONIC, &ts_begin)); ts_target = ts_begin; ts_target.tv_nsec += 50000000; // add 50ms if (ts_target.tv_nsec >= 1000000000) { ++ts_target.tv_sec; ts_target.tv_nsec -= 1000000000; } assert(ETIMEDOUT == pthread_cond_timedwait(&cv, &m, &ts_target)); assert(!clock_gettime(CLOCK_MONOTONIC, &ts_end)); ok = ts_end.tv_sec > ts_target.tv_sec || (ts_end.tv_sec == ts_target.tv_sec && ts_end.tv_nsec >= ts_target.tv_nsec); printf(" begin: %10us %10un\n", ts_begin.tv_sec, ts_begin.tv_nsec); printf("target: %10us %10un\n", ts_target.tv_sec, ts_target.tv_nsec); printf(" end: %10us %10un\n", ts_end.tv_sec, ts_end.tv_nsec); printf(" ok: %s\n", ok ? "true" : "false"); } while (ok); pthread_mutex_unlock(&m); pthread_mutex_destroy(&m); pthread_cond_destroy(&cv); return !ok; }