1 // RUN: %clang_tsan -O1 %s -o %t -lrt && %run %t 2>&1 | FileCheck %s
2 // Test that pthread_cond is properly intercepted,
3 // previously there were issues with versioned symbols.
4 // CHECK: OK
5 
6 // OS X doesn't have pthread_condattr_setclock.
7 // UNSUPPORTED: darwin
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <pthread.h>
12 #include <time.h>
13 #include <errno.h>
14 
main()15 int main() {
16   typedef unsigned long long u64;
17   pthread_mutex_t m;
18   pthread_cond_t c;
19   pthread_condattr_t at;
20   struct timespec ts0, ts1, ts2;
21   int res;
22   u64 sleep;
23 
24   pthread_mutex_init(&m, 0);
25   pthread_condattr_init(&at);
26   pthread_condattr_setclock(&at, CLOCK_MONOTONIC);
27   pthread_cond_init(&c, &at);
28 
29   clock_gettime(CLOCK_MONOTONIC, &ts0);
30   ts1 = ts0;
31   ts1.tv_sec += 2;
32 
33   pthread_mutex_lock(&m);
34   do {
35     res = pthread_cond_timedwait(&c, &m, &ts1);
36   } while (res == 0);
37   pthread_mutex_unlock(&m);
38 
39   clock_gettime(CLOCK_MONOTONIC, &ts2);
40   sleep = (u64)ts2.tv_sec * 1000000000 + ts2.tv_nsec -
41       ((u64)ts0.tv_sec * 1000000000 + ts0.tv_nsec);
42   if (res != ETIMEDOUT)
43     exit(printf("bad return value %d, want %d\n", res, ETIMEDOUT));
44   if (sleep < 1000000000)
45     exit(printf("bad sleep duration %lluns, want %dns\n", sleep, 1000000000));
46   fprintf(stderr, "OK\n");
47 }
48