1// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4void *thread(void *x) {
5 barrier_wait(barrier: &barrier);
6 *static_cast<int *>(x) = 2;
7 return nullptr;
8}
9
10static void race() {
11 int data = 0;
12 pthread_t t;
13 pthread_create(newthread: &t, attr: nullptr, start_routine: thread, arg: &data);
14 data = 1;
15 barrier_wait(barrier: &barrier);
16 pthread_join(th: t, thread_return: nullptr);
17}
18
19struct X {
20 __attribute__((noinline))
21 X() { atexit(func: race); }
22} x;
23
24int main() {
25 barrier_init(barrier: &barrier, count: 2);
26 fprintf(stderr, format: "DONE\n");
27}
28
29// CHECK: DONE
30// CHECK: WARNING: ThreadSanitizer: data race
31// CHECK: Write of size 4
32// CHECK: #0 thread
33// CHECK: Previous write of size 4
34// CHECK: #0 race
35// CHECK: #1 at_exit_callback_installed_at
36// CHECK: #2 X
37

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