1// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4// We want to establish the following sequence of accesses to X:
5// - main thread writes X
6// - thread2 reads X, this read happens-before the write in main thread
7// - thread1 reads X, this read is concurrent with the write in main thread
8// Write in main thread and read in thread1 should be detected as a race.
9// Previously tsan replaced write by main thread with read by thread1,
10// as the result the race was not detected.
11
12volatile long X, Y, Z;
13
14void *Thread1(void *x) {
15 barrier_wait(barrier: &barrier);
16 barrier_wait(barrier: &barrier);
17 Y = X;
18 return NULL;
19}
20
21void *Thread2(void *x) {
22 Z = X;
23 barrier_wait(barrier: &barrier);
24 return NULL;
25}
26
27int main() {
28 barrier_init(barrier: &barrier, count: 2);
29 pthread_t t[2];
30 pthread_create(newthread: &t[0], attr: 0, start_routine: Thread1, arg: 0);
31 X = 42;
32 barrier_wait(barrier: &barrier);
33 pthread_create(newthread: &t[1], attr: 0, start_routine: Thread2, arg: 0);
34 pthread_join(th: t[0], thread_return: 0);
35 pthread_join(th: t[1], thread_return: 0);
36 return 0;
37}
38
39// CHECK: WARNING: ThreadSanitizer: data race
40
41

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