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#include <stdio.h>
7#include <stdlib.h>
8#include <pthread.h>
9#include <time.h>
10#include <errno.h>
11
12int main() {
13  typedef unsigned long long u64;
14  pthread_mutex_t m;
15  pthread_cond_t c;
16  pthread_condattr_t at;
17  struct timespec ts0, ts1, ts2;
18  int res;
19  u64 sleep;
20
21  pthread_mutex_init(&m, 0);
22  pthread_condattr_init(&at);
23  pthread_condattr_setclock(&at, CLOCK_MONOTONIC);
24  pthread_cond_init(&c, &at);
25
26  clock_gettime(CLOCK_MONOTONIC, &ts0);
27  ts1 = ts0;
28  ts1.tv_sec += 2;
29
30  pthread_mutex_lock(&m);
31  do {
32    res = pthread_cond_timedwait(&c, &m, &ts1);
33  } while (res == 0);
34  pthread_mutex_unlock(&m);
35
36  clock_gettime(CLOCK_MONOTONIC, &ts2);
37  sleep = (u64)ts2.tv_sec * 1000000000 + ts2.tv_nsec -
38      ((u64)ts0.tv_sec * 1000000000 + ts0.tv_nsec);
39  if (res != ETIMEDOUT)
40    exit(printf("bad return value %d, want %d\n", res, ETIMEDOUT));
41  if (sleep < 1000000000)
42    exit(printf("bad sleep duration %lluns, want %dns\n", sleep, 1000000000));
43  fprintf(stderr, "OK\n");
44}
45