1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s
2#include "test.h"
3
4long long Data;
5long long Sync;
6
7void *Thread1(void *x) {
8 Data++;
9 __atomic_store_n(&Sync, 1, __ATOMIC_RELEASE);
10 barrier_wait(barrier: &barrier);
11 barrier_wait(barrier: &barrier);
12 return NULL;
13}
14
15void *Thread2(void *x) {
16 barrier_wait(barrier: &barrier);
17 if (__atomic_load_n(&Sync, __ATOMIC_RELAXED) != 1)
18 exit(status: 0);
19 // This store must terminate release sequence of the store in Thread1,
20 // thus tsan must detect race between Thread1 and main on Data.
21 __atomic_store_n(&Sync, 2, __ATOMIC_RELEASE);
22 barrier_wait(barrier: &barrier);
23 return NULL;
24}
25
26int main() {
27 barrier_init(barrier: &barrier, count: 3);
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 barrier_wait(barrier: &barrier);
32 barrier_wait(barrier: &barrier);
33 if (__atomic_load_n(&Sync, __ATOMIC_ACQUIRE) != 2)
34 exit(status: 0);
35 if (Data != 1)
36 exit(status: 0);
37 pthread_join(th: t[0], NULL);
38 pthread_join(th: t[1], NULL);
39 fprintf(stderr, format: "DONE\n");
40 return 0;
41}
42
43// CHECK: WARNING: ThreadSanitizer: data race
44// CHECK: Read
45// CHECK: #0 main
46// CHECK: Previous write
47// CHECK: #0 Thread1
48// CHECK: Location is global 'Data'
49// CHECK: DONE
50

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