brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · 51e6378 Raw
64 lines · cpp
1// Stress test for https://reviews.llvm.org/D1018812// RUN: %clangxx_hwasan -DREUSE=0 %s -pthread -O2 -o %t && %run %t 2>&13// RUN: %clangxx_hwasan -DREUSE=1 %s -pthread -O2 -o %t_reuse && %run %t_reuse 2>&14 5#include <thread>6#include <vector>7 8#include <sys/types.h>9#include <sys/wait.h>10#include <unistd.h>11 12#include <stdio.h>13 14constexpr int kTopThreads = 20;15constexpr int kChildThreads = 30;16constexpr int kChildIterations = REUSE ? 8 : 1;17 18constexpr int kProcessIterations = 20;19 20void Thread() {21  for (int i = 0; i < kChildIterations; ++i) {22    std::vector<std::thread> threads;23    threads.reserve(kChildThreads);24    for (int i = 0; i < kChildThreads; ++i)25      threads.emplace_back([]() {});26    for (auto &t : threads)27      t.join();28  }29}30 31void run() {32  std::vector<std::thread> threads;33  threads.reserve(kTopThreads);34  for (int i = 0; i < kTopThreads; ++i)35    threads.emplace_back(Thread);36  for (auto &t : threads)37    t.join();38}39 40int main() {41#if REUSE42  // Test thread reuse with multiple iterations of thread create / join in a single process.43  run();44#else45  // Test new, non-reused thread creation by running a single iteration of create / join in a freshly started process.46  for (int i = 0; i < kProcessIterations; ++i) {47    int pid = fork();48    if (pid) {49      int wstatus;50      do {51        waitpid(pid, &wstatus, 0);52      } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));53      if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus)) {54        fprintf(stderr, "failed at iteration %d / %d\n", i, kProcessIterations);55        return 1;56      }57    } else {58      run();59      return 0;60    }61  }62#endif63}64