xref: /aosp_15_r20/external/compiler-rt/test/tsan/lots_of_threads.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2*7c3d14c8STreehugger Robot #include "test.h"
3*7c3d14c8STreehugger Robot 
thr(void * arg)4*7c3d14c8STreehugger Robot void *thr(void *arg) {
5*7c3d14c8STreehugger Robot   // Create a sync object on stack, so there is something to free on thread end.
6*7c3d14c8STreehugger Robot   volatile int x;
7*7c3d14c8STreehugger Robot   __atomic_fetch_add(&x, 1, __ATOMIC_SEQ_CST);
8*7c3d14c8STreehugger Robot   barrier_wait(&barrier);
9*7c3d14c8STreehugger Robot   return 0;
10*7c3d14c8STreehugger Robot }
11*7c3d14c8STreehugger Robot 
main()12*7c3d14c8STreehugger Robot int main() {
13*7c3d14c8STreehugger Robot   const int kThreads = 10;
14*7c3d14c8STreehugger Robot   barrier_init(&barrier, kThreads + 1);
15*7c3d14c8STreehugger Robot   pthread_t t[kThreads];
16*7c3d14c8STreehugger Robot   pthread_attr_t attr;
17*7c3d14c8STreehugger Robot   pthread_attr_init(&attr);
18*7c3d14c8STreehugger Robot   pthread_attr_setstacksize(&attr, 16 << 20);
19*7c3d14c8STreehugger Robot   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
20*7c3d14c8STreehugger Robot   for (int i = 0; i < kThreads; i++)
21*7c3d14c8STreehugger Robot     pthread_create(&t[i], &attr, thr, 0);
22*7c3d14c8STreehugger Robot   pthread_attr_destroy(&attr);
23*7c3d14c8STreehugger Robot   barrier_wait(&barrier);
24*7c3d14c8STreehugger Robot   sleep(1);
25*7c3d14c8STreehugger Robot   fprintf(stderr, "DONE\n");
26*7c3d14c8STreehugger Robot   return 0;
27*7c3d14c8STreehugger Robot }
28*7c3d14c8STreehugger Robot 
29*7c3d14c8STreehugger Robot // CHECK: DONE
30*7c3d14c8STreehugger Robot 
31