1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4int fds[2];
5
6void *Thread1(void *x) {
7 write(fd: fds[1], buf: "a", n: 1);
8 barrier_wait(barrier: &barrier);
9 return NULL;
10}
11
12void *Thread2(void *x) {
13 barrier_wait(barrier: &barrier);
14 close(fd: fds[0]);
15 close(fd: fds[1]);
16 return NULL;
17}
18
19int main() {
20 barrier_init(barrier: &barrier, count: 2);
21 pipe(pipedes: fds);
22 pthread_t t[2];
23 pthread_create(newthread: &t[0], NULL, start_routine: Thread1, NULL);
24 pthread_create(newthread: &t[1], NULL, start_routine: Thread2, NULL);
25 pthread_join(th: t[0], NULL);
26 pthread_join(th: t[1], NULL);
27}
28
29// CHECK: WARNING: ThreadSanitizer: data race
30// CHECK: Write of size 8
31// CHECK: #0 close
32// CHECK: #1 Thread2
33// CHECK: Previous read of size 8
34// CHECK: #0 write
35// CHECK: #1 Thread1
36
37
38

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