1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2#include <pthread.h>
3#include <stdio.h>
4#include <unistd.h>
5
6int pipes[2];
7
8void *Thread(void *x) {
9 // wait for shutown signal
10 while (read(fd: pipes[0], buf: &x, nbytes: 1) != 1) {
11 }
12 close(fd: pipes[0]);
13 close(fd: pipes[1]);
14 return 0;
15}
16
17int main() {
18 if (pipe(pipedes: pipes))
19 return 1;
20 pthread_t t;
21 pthread_create(newthread: &t, attr: 0, start_routine: Thread, arg: 0);
22 // send shutdown signal
23 while (write(fd: pipes[1], buf: &t, n: 1) != 1) {
24 }
25 pthread_join(th: t, thread_return: 0);
26 fprintf(stderr, format: "OK\n");
27}
28
29// CHECK-NOT: WARNING: ThreadSanitizer: data race
30// CHECK: OK
31

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