1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4int Global;
5pthread_mutex_t mtx1;
6pthread_mutex_t mtx2;
7
8void *Thread1(void *x) {
9 barrier_wait(barrier: &barrier);
10 pthread_mutex_lock(mutex: &mtx1);
11 Global++;
12 pthread_mutex_unlock(mutex: &mtx1);
13 return NULL;
14}
15
16void *Thread2(void *x) {
17 pthread_mutex_lock(mutex: &mtx2);
18 Global--;
19 pthread_mutex_unlock(mutex: &mtx2);
20 barrier_wait(barrier: &barrier);
21 return NULL;
22}
23
24int main() {
25 barrier_init(barrier: &barrier, count: 2);
26 // CHECK: WARNING: ThreadSanitizer: data race
27 // CHECK: Write of size 4 at {{.*}} by thread T1
28 // CHECK: (mutexes: write [[M1:M[0-9]+]]):
29 // CHECK: Previous write of size 4 at {{.*}} by thread T2
30 // CHECK: (mutexes: write [[M2:M[0-9]+]]):
31 // CHECK: Mutex [[M1]] (0x{{.*}}) created at:
32 // CHECK: #0 pthread_mutex_init
33 // CHECK: #1 main {{.*}}mutexset5.cpp:[[@LINE+4]]
34 // CHECK: Mutex [[M2]] (0x{{.*}}) created at:
35 // CHECK: #0 pthread_mutex_init
36 // CHECK: #1 main {{.*}}mutexset5.cpp:[[@LINE+2]]
37 pthread_mutex_init(mutex: &mtx1, mutexattr: 0);
38 pthread_mutex_init(mutex: &mtx2, mutexattr: 0);
39 pthread_t t[2];
40 pthread_create(newthread: &t[0], NULL, start_routine: Thread1, NULL);
41 pthread_create(newthread: &t[1], NULL, start_routine: Thread2, NULL);
42 pthread_join(th: t[0], NULL);
43 pthread_join(th: t[1], NULL);
44 pthread_mutex_destroy(mutex: &mtx1);
45 pthread_mutex_destroy(mutex: &mtx2);
46}
47

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