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
10Mutex mu(false, __tsan_mutex_write_reentrant);
11long data;
12
13void *thr1(void *arg) {
14 mu.Lock();
15 mu.~Mutex();
16 new(&mu) Mutex(true, __tsan_mutex_write_reentrant);
17 return 0;
18}
19
20void *thr2(void *arg) {
21 barrier_wait(barrier: &barrier);
22 mu.Lock();
23 data++;
24 mu.Unlock();
25 return 0;
26}
27
28int main() {
29 barrier_init(barrier: &barrier, count: 2);
30 pthread_t th;
31 pthread_create(newthread: &th, attr: 0, start_routine: thr1, arg: 0);
32 pthread_join(th: th, thread_return: 0);
33
34 barrier_init(barrier: &barrier, count: 2);
35 pthread_create(newthread: &th, attr: 0, start_routine: thr2, arg: 0);
36 mu.Lock();
37 data++;
38 mu.Unlock();
39 barrier_wait(barrier: &barrier);
40 pthread_join(th: th, thread_return: 0);
41 fprintf(stderr, format: "DONE\n");
42 return 0;
43}
44
45// CHECK-NOT: WARNING: ThreadSanitizer: data race
46// CHECK: DONE
47

source code of compiler-rt/test/tsan/custom_mutex3.cpp