1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4int fds[2];
5
6void *ThreadCreatePipe(void *x) {
7  pipe(fds);
8  return NULL;
9}
10
11void *ThreadDummy(void *x) {
12  return NULL;
13}
14
15void *ThreadWrite(void *x) {
16  write(fds[1], "a", 1);
17  barrier_wait(&barrier);
18  return NULL;
19}
20
21void *ThreadClose(void *x) {
22  barrier_wait(&barrier);
23  close(fds[0]);
24  close(fds[1]);
25  return NULL;
26}
27
28int main() {
29  barrier_init(&barrier, 2);
30  pthread_t t_create;
31  pthread_create(&t_create, NULL, ThreadCreatePipe, NULL);
32  pthread_join(t_create, NULL);
33
34  for (int i = 0; i < 100; i++) {
35    pthread_t t_dummy;
36    pthread_create(&t_dummy, NULL, ThreadDummy, NULL);
37    pthread_join(t_dummy, NULL);
38  }
39
40  pthread_t t[2];
41  pthread_create(&t[0], NULL, ThreadWrite, NULL);
42  pthread_create(&t[1], NULL, ThreadClose, NULL);
43  pthread_join(t[0], NULL);
44  pthread_join(t[1], NULL);
45}
46
47// CHECK-NOT: CHECK failed
48// CHECK: WARNING: ThreadSanitizer: data race
49// CHECK:   Write of size 8
50// CHECK:     #0 close
51// CHECK:     #1 ThreadClose
52// CHECK:   Previous read of size 8
53// CHECK:     #0 write
54// CHECK:     #1 ThreadWrite
55