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
8void *Thread(void *stop) {
9 while (!__atomic_load_n((int *)stop, __ATOMIC_RELAXED))
10 close(fd: open(file: "/dev/null", O_RDONLY));
11 return 0;
12}
13
14int 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(newthread: &th[i], attr: 0, start_routine: Thread, arg: &stop);
20 sleep(seconds: 5);
21 __atomic_store_n(&stop, 1, __ATOMIC_RELAXED);
22 for (int i = 0; i < kThreads; i++)
23 pthread_join(th: th[i], thread_return: 0);
24 fprintf(stderr, format: "DONE\n");
25}
26
27// CHECK-NOT: WARNING: ThreadSanitizer: data race
28// CHECK: DONE
29

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