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