xref: /llvm-project/compiler-rt/test/tsan/bench_threads.cpp (revision b332134921b42796c6b46453eaf2affdc09e3154)
1 // RUN: %clangxx_tsan %s -o %t
2 // RUN: %run %t 2>&1 | FileCheck %s
3 
4 // bench.h needs pthread barriers which are not available on OS X
5 // UNSUPPORTED: darwin
6 
7 #include "bench.h"
8 
nop_thread(void * arg)9 void *nop_thread(void *arg) {
10   pthread_setname_np(pthread_self(), "nop_thread");
11   return nullptr;
12 }
13 
thread(int tid)14 void thread(int tid) {
15   for (int i = 0; i < bench_niter; i++) {
16     pthread_t th;
17     pthread_create(&th, nullptr, nop_thread, nullptr);
18     pthread_join(th, nullptr);
19   }
20 }
21 
bench()22 void bench() {
23   // Benchmark thread creation/joining in presence of a large number
24   // of threads (both alive and already joined).
25   printf("starting transient threads...\n");
26   for (int i = 0; i < 200; i++) {
27     const int kBatch = 100;
28     pthread_t th[kBatch];
29     for (int j = 0; j < kBatch; j++)
30       pthread_create(&th[j], nullptr, nop_thread, nullptr);
31     for (int j = 0; j < kBatch; j++)
32       pthread_join(th[j], nullptr);
33   }
34   printf("starting persistent threads...\n");
35   const int kLiveThreads = 2000;
36   pthread_t th[kLiveThreads];
37   for (int j = 0; j < kLiveThreads; j++)
38     pthread_create(&th[j], nullptr, nop_thread, nullptr);
39   printf("starting benchmark threads...\n");
40   start_thread_group(bench_nthread, thread);
41   for (int j = 0; j < kLiveThreads; j++)
42     pthread_join(th[j], nullptr);
43 }
44 
45 // CHECK: DONE
46