xref: /aosp_15_r20/external/compiler-rt/test/tsan/pthread_key.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -O1 %s -DBUILD_SO -fPIC -shared -o %t-so.so
2*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
3*7c3d14c8STreehugger Robot 
4*7c3d14c8STreehugger Robot // Extracted from:
5*7c3d14c8STreehugger Robot // https://bugs.chromium.org/p/v8/issues/detail?id=4995
6*7c3d14c8STreehugger Robot 
7*7c3d14c8STreehugger Robot #include "test.h"
8*7c3d14c8STreehugger Robot 
thr(void * arg)9*7c3d14c8STreehugger Robot void* thr(void* arg) {
10*7c3d14c8STreehugger Robot   const int N = 32;
11*7c3d14c8STreehugger Robot   pthread_key_t keys_[N];
12*7c3d14c8STreehugger Robot   for (size_t i = 0; i < N; ++i) {
13*7c3d14c8STreehugger Robot     int err = pthread_key_create(&keys_[i], 0);
14*7c3d14c8STreehugger Robot     if (err) {
15*7c3d14c8STreehugger Robot       fprintf(stderr, "pthread_key_create failed with %d\n", err);
16*7c3d14c8STreehugger Robot       exit(1);
17*7c3d14c8STreehugger Robot     }
18*7c3d14c8STreehugger Robot   }
19*7c3d14c8STreehugger Robot   for (size_t i = 0; i < N; i++)
20*7c3d14c8STreehugger Robot     pthread_setspecific(keys_[i], (void*)(long)i);
21*7c3d14c8STreehugger Robot   for (size_t i = 0; i < N; i++)
22*7c3d14c8STreehugger Robot     pthread_key_delete(keys_[i]);
23*7c3d14c8STreehugger Robot   return 0;
24*7c3d14c8STreehugger Robot }
25*7c3d14c8STreehugger Robot 
main()26*7c3d14c8STreehugger Robot int main() {
27*7c3d14c8STreehugger Robot   for (int i = 0; i < 10; i++) {
28*7c3d14c8STreehugger Robot     pthread_t th;
29*7c3d14c8STreehugger Robot     pthread_create(&th, 0, thr, 0);
30*7c3d14c8STreehugger Robot     pthread_join(th, 0);
31*7c3d14c8STreehugger Robot   }
32*7c3d14c8STreehugger Robot   pthread_t th[2];
33*7c3d14c8STreehugger Robot   pthread_create(&th[0], 0, thr, 0);
34*7c3d14c8STreehugger Robot   pthread_create(&th[1], 0, thr, 0);
35*7c3d14c8STreehugger Robot   pthread_join(th[0], 0);
36*7c3d14c8STreehugger Robot   pthread_join(th[1], 0);
37*7c3d14c8STreehugger Robot   fprintf(stderr, "DONE\n");
38*7c3d14c8STreehugger Robot   // CHECK: DONE
39*7c3d14c8STreehugger Robot }
40