Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / tsan / Linux / fork_multithreaded4.cpp
blob927f5a6ff5c0035f3c45302ead64be93bd4039b8
1 // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
3 // The test tries to provoke internal allocator to be locked during fork
4 // and then force the child process to use the internal allocator.
6 #include "../test.h"
7 #include <errno.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
11 static void *forker(void *arg) {
12 void *p = calloc(1, 16);
13 static_cast<volatile int *>(p)[0]++;
14 __atomic_fetch_add(static_cast<int *>(p), 1, __ATOMIC_SEQ_CST);
15 int pid = fork();
16 if (pid < 0) {
17 fprintf(stderr, "failed to fork (%d)\n", errno);
18 exit(1);
20 if (pid == 0) {
21 __atomic_fetch_add(&static_cast<int *>(p)[1], 1, __ATOMIC_SEQ_CST);
22 exit(0);
24 int status = 0;
25 while (waitpid(pid, &status, 0) != pid) {
27 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
28 fprintf(stderr, "subprocess failed (%d)\n", status);
29 exit(1);
31 free(p);
32 return 0;
35 int main() {
36 for (int i = 0; i < 10; i++) {
37 pthread_t threads[100];
38 for (auto &th : threads)
39 pthread_create(&th, 0, forker, 0);
40 for (auto th : threads)
41 pthread_join(th, 0);
43 fprintf(stderr, "DONE\n");
46 // CHECK: DONE