1 // RUN: %clangxx_tsan -O1 --std=c++11 %s -o %t
2 // RUN: %env_tsan_opts=report_destroy_locked=0 %run %t 2>&1 | FileCheck %s
3 #include "custom_mutex.h"
4 
5 // Regression test for a bug.
6 // Thr1 destroys a locked mutex, previously such mutex was not removed from
7 // sync map and as the result subsequent uses of a mutex located at the same
8 // address caused false race reports.
9 
10 Mutex mu(false, __tsan_mutex_write_reentrant);
11 long data;
12 
thr1(void * arg)13 void *thr1(void *arg) {
14   mu.Lock();
15   mu.~Mutex();
16   new(&mu) Mutex(true, __tsan_mutex_write_reentrant);
17   return 0;
18 }
19 
thr2(void * arg)20 void *thr2(void *arg) {
21   barrier_wait(&barrier);
22   mu.Lock();
23   data++;
24   mu.Unlock();
25   return 0;
26 }
27 
main()28 int main() {
29   barrier_init(&barrier, 2);
30   pthread_t th;
31   pthread_create(&th, 0, thr1, 0);
32   pthread_join(th, 0);
33 
34   barrier_init(&barrier, 2);
35   pthread_create(&th, 0, thr2, 0);
36   mu.Lock();
37   data++;
38   mu.Unlock();
39   barrier_wait(&barrier);
40   pthread_join(th, 0);
41   fprintf(stderr, "DONE\n");
42   return 0;
43 }
44 
45 // CHECK-NOT: WARNING: ThreadSanitizer: data race
46 // CHECK: DONE
47