xref: /llvm-project/compiler-rt/test/tsan/fd_close_norace3.cpp (revision 66298e1c54fcf3f4643528c461f5cf376d27055b)
1 // RUN: %clangxx_tsan -O1 %s -o %t && %env_tsan_opts=flush_memory_ms=1 %run %t 2>&1 | FileCheck %s
2 #include "test.h"
3 #include <fcntl.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7 
Thread(void * stop)8 void *Thread(void *stop) {
9   while (!__atomic_load_n((int *)stop, __ATOMIC_RELAXED))
10     close(open("/dev/null", O_RDONLY));
11   return 0;
12 }
13 
main()14 int main() {
15   int stop = 0;
16   const int kThreads = 10;
17   pthread_t th[kThreads];
18   for (int i = 0; i < kThreads; i++)
19     pthread_create(&th[i], 0, Thread, &stop);
20   sleep(5);
21   __atomic_store_n(&stop, 1, __ATOMIC_RELAXED);
22   for (int i = 0; i < kThreads; i++)
23     pthread_join(th[i], 0);
24   fprintf(stderr, "DONE\n");
25 }
26 
27 // CHECK-NOT: WARNING: ThreadSanitizer: data race
28 // CHECK: DONE
29