xref: /aosp_15_r20/external/compiler-rt/test/tsan/cond_race.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2*7c3d14c8STreehugger Robot // CHECK-NOT: unlock of unlocked mutex
3*7c3d14c8STreehugger Robot // CHECK: ThreadSanitizer: data race
4*7c3d14c8STreehugger Robot // CHECK: pthread_cond_signal
5*7c3d14c8STreehugger Robot 
6*7c3d14c8STreehugger Robot #include "test.h"
7*7c3d14c8STreehugger Robot 
8*7c3d14c8STreehugger Robot struct Ctx {
9*7c3d14c8STreehugger Robot   pthread_mutex_t m;
10*7c3d14c8STreehugger Robot   pthread_cond_t c;
11*7c3d14c8STreehugger Robot   bool done;
12*7c3d14c8STreehugger Robot };
13*7c3d14c8STreehugger Robot 
thr(void * p)14*7c3d14c8STreehugger Robot void *thr(void *p) {
15*7c3d14c8STreehugger Robot   Ctx *c = (Ctx*)p;
16*7c3d14c8STreehugger Robot   pthread_mutex_lock(&c->m);
17*7c3d14c8STreehugger Robot   c->done = true;
18*7c3d14c8STreehugger Robot   pthread_mutex_unlock(&c->m);
19*7c3d14c8STreehugger Robot   pthread_cond_signal(&c->c);
20*7c3d14c8STreehugger Robot   barrier_wait(&barrier);
21*7c3d14c8STreehugger Robot   return 0;
22*7c3d14c8STreehugger Robot }
23*7c3d14c8STreehugger Robot 
main()24*7c3d14c8STreehugger Robot int main() {
25*7c3d14c8STreehugger Robot   barrier_init(&barrier, 2);
26*7c3d14c8STreehugger Robot   Ctx *c = new Ctx();
27*7c3d14c8STreehugger Robot   pthread_mutex_init(&c->m, 0);
28*7c3d14c8STreehugger Robot   pthread_cond_init(&c->c, 0);
29*7c3d14c8STreehugger Robot   pthread_t th;
30*7c3d14c8STreehugger Robot   pthread_create(&th, 0, thr, c);
31*7c3d14c8STreehugger Robot   pthread_mutex_lock(&c->m);
32*7c3d14c8STreehugger Robot   while (!c->done)
33*7c3d14c8STreehugger Robot     pthread_cond_wait(&c->c, &c->m);
34*7c3d14c8STreehugger Robot   pthread_mutex_unlock(&c->m);
35*7c3d14c8STreehugger Robot   // otherwise it can be reported as use-after-free
36*7c3d14c8STreehugger Robot   barrier_wait(&barrier);
37*7c3d14c8STreehugger Robot   delete c;
38*7c3d14c8STreehugger Robot   pthread_join(th, 0);
39*7c3d14c8STreehugger Robot }
40