1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include <pthread.h>
3#include <stdio.h>
4#include <unistd.h>
5
6int Global;
7
8void __attribute__((noinline)) foo1() {
9  Global = 42;
10}
11
12void __attribute__((noinline)) bar1() {
13  volatile int tmp = 42;
14  int tmp2 = tmp;
15  (void)tmp2;
16  foo1();
17}
18
19void __attribute__((noinline)) foo2() {
20  volatile int tmp = Global;
21  int tmp2 = tmp;
22  (void)tmp2;
23}
24
25void __attribute__((noinline)) bar2() {
26  volatile int tmp = 42;
27  int tmp2 = tmp;
28  (void)tmp2;
29  foo2();
30}
31
32void *Thread1(void *x) {
33  sleep(1);
34  bar1();
35  return NULL;
36}
37
38int main() {
39  pthread_t t;
40  pthread_create(&t, NULL, Thread1, NULL);
41  bar2();
42  pthread_join(t, NULL);
43}
44
45// CHECK:      WARNING: ThreadSanitizer: data race
46// CHECK-NEXT:   Write of size 4 at {{.*}} by thread T1:
47// CHECK-NEXT:     #0 foo1{{.*}} {{.*}}simple_stack2.cc:9{{(:3)?}} ({{.*}})
48// CHECK-NEXT:     #1 bar1{{.*}} {{.*}}simple_stack2.cc:16{{(:3)?}} ({{.*}})
49// CHECK-NEXT:     #2 Thread1{{.*}} {{.*}}simple_stack2.cc:34{{(:3)?}} ({{.*}})
50// CHECK:        Previous read of size 4 at {{.*}} by main thread:
51// CHECK-NEXT:     #0 foo2{{.*}} {{.*}}simple_stack2.cc:20{{(:28)?}} ({{.*}})
52// CHECK-NEXT:     #1 bar2{{.*}} {{.*}}simple_stack2.cc:29{{(:3)?}} ({{.*}})
53// CHECK-NEXT:     #2 main{{.*}} {{.*}}simple_stack2.cc:41{{(:3)?}} ({{.*}})
54