1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4#include <fcntl.h>
5
6void *Thread(void *x) {
7 int fd = (long)x;
8 char buf;
9 read(fd: fd, buf: &buf, nbytes: 1);
10 barrier_wait(barrier: &barrier);
11 close(fd: fd);
12 return NULL;
13}
14
15int main() {
16 barrier_init(barrier: &barrier, count: 2);
17 int fd = open(file: "/dev/random", O_RDONLY);
18 pthread_t t[2];
19 pthread_create(newthread: &t[0], NULL, start_routine: Thread, arg: (void *)(long)fd);
20 pthread_create(newthread: &t[1], NULL, start_routine: Thread, arg: (void *)(long)fd);
21
22 pthread_join(th: t[0], NULL);
23 pthread_join(th: t[1], NULL);
24}
25
26// CHECK: WARNING: ThreadSanitizer: data race
27// CHECK: Location is file descriptor {{[0-9]+}} {{(destroyed by thread|created by main)}}
28// CHECK: #0 {{close|open}}
29// CHECK: #1 {{Thread|main}}
30

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