]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/tsan/cond_destruction.cc
Vendor import of compiler-rt trunk r351319 (just before the release_80
[FreeBSD/FreeBSD.git] / test / tsan / cond_destruction.cc
1 // RUN: %clangxx_tsan -O1 %s -o %t
2 // RUN: %run %t 2>&1 | FileCheck %s
3 // RUN: %run %t arg 2>&1 | FileCheck %s
4 // RUN: %run %t arg arg 2>&1 | FileCheck %s
5 #include "test.h"
6
7 // Test for destruction of pthread_cond_t.
8 // POSIX states that it is safe  to destroy a condition variable upon which no
9 // threads are currently blocked. That is, it is not necessary to wait untill
10 // other threads return from pthread_cond_wait, they just need to be unblocked.
11
12 pthread_mutex_t m;
13 pthread_cond_t c;
14 bool done1, done2;
15
16 void *thr(void *p) {
17   pthread_mutex_lock(&m);
18   done1 = true;
19   pthread_cond_signal(&c);
20   while (!done2)
21     pthread_cond_wait(&c, &m);
22   pthread_mutex_unlock(&m);
23   return 0;
24 }
25
26 int main(int argc, char **argv) {
27   pthread_t th;
28   pthread_mutex_init(&m, 0);
29   pthread_cond_init(&c, 0);
30   pthread_create(&th, 0, thr, 0);
31   pthread_mutex_lock(&m);
32   while (!done1)
33     pthread_cond_wait(&c, &m);
34   done2 = true;
35   // Any of these sequences is legal.
36   if (argc == 1) {
37     pthread_cond_signal(&c);
38     pthread_mutex_unlock(&m);
39     pthread_cond_destroy(&c);
40   } else if (argc == 2) {
41     pthread_mutex_unlock(&m);
42     pthread_cond_signal(&c);
43     pthread_cond_destroy(&c);
44   } else {
45     pthread_cond_signal(&c);
46     pthread_cond_destroy(&c);
47     pthread_mutex_unlock(&m);
48   }
49   pthread_join(th, 0);
50   fprintf(stderr, "DONE\n");
51 }
52
53 // CHECK-NOT: ThreadSanitizer: data race