xref: /llvm-project/compiler-rt/test/tsan/lots_of_threads.c (revision 3a1eb1cf2ae581415cc5750803b082f3676d835c)
1 // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2 #include "test.h"
3 
thr(void * arg)4 void *thr(void *arg) {
5   // Create a sync object on stack, so there is something to free on thread end.
6   volatile int x;
7   __atomic_fetch_add(&x, 1, __ATOMIC_SEQ_CST);
8   barrier_wait(&barrier);
9   return 0;
10 }
11 
main()12 int main() {
13   const int kThreads = 300;
14   const int kIters = 10;
15   barrier_init(&barrier, kThreads + 1);
16   pthread_attr_t attr;
17   pthread_attr_init(&attr);
18   pthread_attr_setstacksize(&attr, 16 << 20);
19   for (int iter = 0; iter < kIters; iter++) {
20     pthread_t threads[kThreads];
21     for (int t = 0; t < kThreads; t++) {
22       int err = pthread_create(&threads[t], &attr, thr, 0);
23       if (err) {
24         fprintf(stderr, "Failed to create thread #%d\n", t);
25         return 1;
26       }
27     }
28     barrier_wait(&barrier);
29     for (int t = 0; t < kThreads; t++)
30       pthread_join(threads[t], 0);
31   }
32   pthread_attr_destroy(&attr);
33   fprintf(stderr, "DONE\n");
34   return 0;
35 }
36 
37 // CHECK: DONE
38 
39