1// RUN: %clangxx_tsan -O1 --std=c++11 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s
2#include "custom_mutex.h"
3
4// Test that failed TryLock does not induce parasitic synchronization.
5
6Mutex mu(true, 0);
7long data;
8
9void *thr(void *arg) {
10 mu.Lock();
11 data++;
12 mu.Unlock();
13 mu.Lock();
14 barrier_wait(barrier: &barrier);
15 barrier_wait(barrier: &barrier);
16 mu.Unlock();
17 return 0;
18}
19
20int main() {
21 barrier_init(barrier: &barrier, count: 2);
22 pthread_t th;
23 pthread_create(newthread: &th, attr: 0, start_routine: thr, arg: 0);
24 barrier_wait(barrier: &barrier);
25 if (mu.TryLock()) {
26 fprintf(stderr, format: "TryLock succeeded, should not\n");
27 exit(status: 0);
28 }
29 data++;
30 barrier_wait(barrier: &barrier);
31 pthread_join(th: th, thread_return: 0);
32 fprintf(stderr, format: "DONE\n");
33 return 0;
34}
35
36// CHECK: ThreadSanitizer: data race
37// CHECK-NEXT: Write of size 8 at {{.*}} by main thread:
38// CHECK-NEXT: #0 main {{.*}}custom_mutex1.cpp:29
39// CHECK: DONE
40

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