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#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8
9int fds[2];
10
11void *Thread1(void *x) {
12 char buf;
13 read(fd: fds[0], buf: &buf, nbytes: 1);
14 close(fd: fds[0]);
15 return 0;
16}
17
18void *Thread2(void *x) {
19 close(fd: fds[1]);
20 return 0;
21}
22
23int main() {
24 fds[0] = open(file: "/dev/random", O_RDONLY);
25 fds[1] = dup2(fd: fds[0], fd2: 100);
26 pthread_t t[2];
27 pthread_create(newthread: &t[0], NULL, start_routine: Thread1, NULL);
28 pthread_create(newthread: &t[1], NULL, start_routine: Thread2, NULL);
29 pthread_join(th: t[0], NULL);
30 pthread_join(th: t[1], NULL);
31 fprintf(stderr, format: "OK\n");
32}
33
34// CHECK-NOT: WARNING: ThreadSanitizer: data race
35

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