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

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