1// RUN: %clangxx_tsan -O1 %s -o %t && %env_tsan_opts=atexit_sleep_ms=50 %run %t 2>&1 | FileCheck %s
2#include <pthread.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <unistd.h>
6#include <sys/types.h>
7#include <sys/wait.h>
8
9void foo() {
10 fprintf(stderr, format: "CHILD ATEXIT\n");
11}
12
13void *worker(void *unused) {
14 return 0;
15}
16
17int main() {
18 pthread_t t;
19 pthread_create(newthread: &t, NULL, start_routine: worker, NULL);
20 int pid = fork();
21 if (pid == 0) {
22 // child
23 atexit(func: foo);
24 fprintf(stderr, format: "CHILD DONE\n");
25 } else {
26 pthread_join(th: t, thread_return: 0);
27 if (waitpid(pid: pid, stat_loc: 0, options: 0) == -1) {
28 perror(s: "waitpid");
29 exit(status: 1);
30 }
31 fprintf(stderr, format: "PARENT DONE\n");
32 }
33}
34
35// CHECK: CHILD DONE
36// CHECK: CHILD ATEXIT
37// CHECK: PARENT DONE
38

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