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;
7pthread_rwlock_t mtx3;
8
9void *Thread1(void *x) {
10 barrier_wait(barrier: &barrier);
11 pthread_mutex_lock(mutex: &mtx1);
12 Global++;
13 pthread_mutex_unlock(mutex: &mtx1);
14 return NULL;
15}
16
17void *Thread2(void *x) {
18 pthread_mutex_lock(mutex: &mtx1);
19 pthread_mutex_unlock(mutex: &mtx1);
20 pthread_mutex_lock(mutex: &mtx2);
21 pthread_rwlock_rdlock(rwlock: &mtx3);
22 Global--;
23 pthread_mutex_unlock(mutex: &mtx2);
24 pthread_rwlock_unlock(rwlock: &mtx3);
25 barrier_wait(barrier: &barrier);
26 return NULL;
27}
28
29int main() {
30 barrier_init(barrier: &barrier, count: 2);
31 // CHECK: WARNING: ThreadSanitizer: data race
32 // CHECK: Write of size 4 at {{.*}} by thread T1
33 // CHECK: (mutexes: write [[M1:M[0-9]+]]):
34 // CHECK: Previous write of size 4 at {{.*}} by thread T2
35 // CHECK: (mutexes: write [[M2:M[0-9]+]], read [[M3:M[0-9]+]]):
36 // CHECK: Mutex [[M1]] (0x{{.*}}) created at:
37 // CHECK: #1 main {{.*}}mutexset6.cpp:[[@LINE+5]]
38 // CHECK: Mutex [[M2]] (0x{{.*}}) created at:
39 // CHECK: #1 main {{.*}}mutexset6.cpp:[[@LINE+4]]
40 // CHECK: Mutex [[M3]] (0x{{.*}}) created at:
41 // CHECK: #1 main {{.*}}mutexset6.cpp:[[@LINE+3]]
42 pthread_mutex_init(mutex: &mtx1, mutexattr: 0);
43 pthread_mutex_init(mutex: &mtx2, mutexattr: 0);
44 pthread_rwlock_init(rwlock: &mtx3, attr: 0);
45 pthread_t t[2];
46 pthread_create(newthread: &t[0], NULL, start_routine: Thread1, NULL);
47 pthread_create(newthread: &t[1], NULL, start_routine: Thread2, NULL);
48 pthread_join(th: t[0], NULL);
49 pthread_join(th: t[1], NULL);
50 pthread_mutex_destroy(mutex: &mtx1);
51 pthread_mutex_destroy(mutex: &mtx2);
52 pthread_rwlock_destroy(rwlock: &mtx3);
53}
54

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