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