brintos

brintos / llvm-project-archived public Read only

0
0
Text · 980 B · fdbc571 Raw
40 lines · cpp
1// Simple stress test for of pthread_create. Increase arg to use as benchmark.2 3// RUN: %clangxx -O3 -pthread %s -o %t && %run %t 104 5// Crashes on Android.6// UNSUPPORTED: android7 8#include <cstdint>9#include <pthread.h>10#include <stdlib.h>11 12extern "C" const char *__asan_default_options() {13  // 32bit asan can allocate just a few FakeStacks.14  return sizeof(void *) < 8 ? "detect_stack_use_after_return=0" : "";15}16 17static void *null_func(void *args) { return nullptr; }18 19static void *loop(void *args) {20  uintptr_t n = (uintptr_t)args;21  for (int i = 0; i < n; ++i) {22    pthread_t thread;23    if (pthread_create(&thread, 0, null_func, nullptr) == 0)24      pthread_detach(thread);25  }26  return nullptr;27}28 29int main(int argc, char **argv) {30  uintptr_t n = atoi(argv[1]);31  pthread_t threads[64];32  for (auto &thread : threads)33    while (pthread_create(&thread, 0, loop, (void *)n) != 0) {34    }35 36  for (auto &thread : threads)37    pthread_join(thread, nullptr);38  return 0;39}40