1 // RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s 2 // CHECK-NOT: unlock of unlocked mutex 3 // CHECK: ThreadSanitizer: data race 4 // CHECK: pthread_cond_signal 5 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <pthread.h> 9 #include <unistd.h> 10 11 struct Ctx { 12 pthread_mutex_t m; 13 pthread_cond_t c; 14 bool done; 15 }; 16 17 void *thr(void *p) { 18 Ctx *c = (Ctx*)p; 19 pthread_mutex_lock(&c->m); 20 c->done = true; 21 pthread_mutex_unlock(&c->m); 22 pthread_cond_signal(&c->c); 23 return 0; 24 } 25 26 int main() { 27 Ctx *c = new Ctx(); 28 pthread_mutex_init(&c->m, 0); 29 pthread_cond_init(&c->c, 0); 30 pthread_t th; 31 pthread_create(&th, 0, thr, c); 32 pthread_mutex_lock(&c->m); 33 while (!c->done) 34 pthread_cond_wait(&c->c, &c->m); 35 pthread_mutex_unlock(&c->m); 36 // w/o this sleep, it can be reported as use-after-free 37 sleep(1); 38 delete c; 39 pthread_join(th, 0); 40 } 41