47 lines · cpp
1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s2 3// The test tries to provoke internal allocator to be locked during fork4// and then force the child process to use the internal allocator.5 6#include "../test.h"7#include <errno.h>8#include <sys/types.h>9#include <sys/wait.h>10 11static 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);19 }20 if (pid == 0) {21 __atomic_fetch_add(&static_cast<int *>(p)[1], 1, __ATOMIC_SEQ_CST);22 exit(0);23 }24 int status = 0;25 while (waitpid(pid, &status, 0) != pid) {26 }27 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {28 fprintf(stderr, "subprocess failed (%d)\n", status);29 exit(1);30 }31 free(p);32 return 0;33}34 35int 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);42 }43 fprintf(stderr, "DONE\n");44}45 46// CHECK: DONE47