xref: /llvm-project/compiler-rt/test/tsan/fd_close_race.cpp (revision f831d6fc800ccf22c1c09888fce3e3c8ebc2c992)
1 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2 #include "test.h"
3 #include <fcntl.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 
Thread(void * arg)7 void *Thread(void *arg) {
8   char buf;
9   read((long)arg, &buf, 1);
10   barrier_wait(&barrier);
11   return NULL;
12 }
13 
main()14 int main() {
15   barrier_init(&barrier, 2);
16   int fd = open("/dev/random", O_RDONLY);
17   pthread_t t;
18   pthread_create(&t, NULL, Thread, (void *)(long)fd);
19   barrier_wait(&barrier);
20   close(fd);
21   pthread_join(t, NULL);
22   fprintf(stderr, "DONE\n");
23 }
24 
25 // CHECK: WARNING: ThreadSanitizer: data race
26 // CHECK: DONE
27