xref: /llvm-project/compiler-rt/test/sanitizer_common/TestCases/Posix/create_thread_loop2.cpp (revision 0245dc94ebee82a14118cdb1b39c9f4470e99e1d)
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 10
4 
5 // Crashes on Android.
6 // UNSUPPORTED: android
7 
8 #include <cstdint>
9 #include <pthread.h>
10 #include <stdlib.h>
11 
__asan_default_options()12 extern "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 
null_func(void * args)17 static void *null_func(void *args) { return nullptr; }
18 
loop(void * args)19 static 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 
main(int argc,char ** argv)29 int 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