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