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

source code of compiler-rt/test/tsan/race_on_mutex.c