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