1// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4int Global;
5
6void __attribute__((noinline)) foo1() {
7  Global = 42;
8}
9
10void __attribute__((noinline)) bar1() {
11  volatile int tmp = 42; (void)tmp;
12  foo1();
13}
14
15void __attribute__((noinline)) foo2() {
16  volatile int v = Global; (void)v;
17}
18
19void __attribute__((noinline)) bar2() {
20  volatile int tmp = 42; (void)tmp;
21  foo2();
22}
23
24void *Thread1(void *x) {
25  barrier_wait(&barrier);
26  bar1();
27  return NULL;
28}
29
30void *Thread2(void *x) {
31  bar2();
32  barrier_wait(&barrier);
33  return NULL;
34}
35
36void StartThread(pthread_t *t, void *(*f)(void*)) {
37  pthread_create(t, NULL, f, NULL);
38}
39
40int main() {
41  barrier_init(&barrier, 2);
42  pthread_t t[2];
43  StartThread(&t[0], Thread1);
44  StartThread(&t[1], Thread2);
45  pthread_join(t[0], NULL);
46  pthread_join(t[1], NULL);
47  return 0;
48}
49
50// CHECK:      WARNING: ThreadSanitizer: data race
51// CHECK-NEXT:   Write of size 4 at {{.*}} by thread T1:
52// CHECK-NEXT:     #0 foo1{{.*}} {{.*}}simple_stack.c:7{{(:10)?}} ({{.*}})
53// CHECK-NEXT:     #1 bar1{{.*}} {{.*}}simple_stack.c:12{{(:3)?}} ({{.*}})
54// CHECK-NEXT:     #2 Thread1{{.*}} {{.*}}simple_stack.c:26{{(:3)?}} ({{.*}})
55// CHECK:        Previous read of size 4 at {{.*}} by thread T2:
56// CHECK-NEXT:     #0 foo2{{.*}} {{.*}}simple_stack.c:16{{(:20)?}} ({{.*}})
57// CHECK-NEXT:     #1 bar2{{.*}} {{.*}}simple_stack.c:21{{(:3)?}} ({{.*}})
58// CHECK-NEXT:     #2 Thread2{{.*}} {{.*}}simple_stack.c:31{{(:3)?}} ({{.*}})
59// CHECK:        Thread T1 (tid={{.*}}, running) created by main thread at:
60// CHECK-NEXT:     #0 pthread_create {{.*}} ({{.*}})
61// CHECK-NEXT:     #1 StartThread{{.*}} {{.*}}simple_stack.c:37{{(:3)?}} ({{.*}})
62// CHECK-NEXT:     #2 main{{.*}} {{.*}}simple_stack.c:43{{(:3)?}} ({{.*}})
63// CHECK:        Thread T2 ({{.*}}) created by main thread at:
64// CHECK-NEXT:     #0 pthread_create {{.*}} ({{.*}})
65// CHECK-NEXT:     #1 StartThread{{.*}} {{.*}}simple_stack.c:37{{(:3)?}} ({{.*}})
66// CHECK-NEXT:     #2 main{{.*}} {{.*}}simple_stack.c:44{{(:3)?}} ({{.*}})
67