xref: /aosp_15_r20/external/llvm/unittests/Support/ManagedStatic.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- llvm/unittest/Support/ManagedStatic.cpp - ManagedStatic tests ------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ManagedStatic.h"
10*9880d681SAndroid Build Coastguard Worker #include "llvm/Config/config.h"
11*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Threading.h"
12*9880d681SAndroid Build Coastguard Worker #ifdef HAVE_PTHREAD_H
13*9880d681SAndroid Build Coastguard Worker #include <pthread.h>
14*9880d681SAndroid Build Coastguard Worker #endif
15*9880d681SAndroid Build Coastguard Worker 
16*9880d681SAndroid Build Coastguard Worker #include "gtest/gtest.h"
17*9880d681SAndroid Build Coastguard Worker 
18*9880d681SAndroid Build Coastguard Worker using namespace llvm;
19*9880d681SAndroid Build Coastguard Worker 
20*9880d681SAndroid Build Coastguard Worker namespace {
21*9880d681SAndroid Build Coastguard Worker 
22*9880d681SAndroid Build Coastguard Worker #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H) && \
23*9880d681SAndroid Build Coastguard Worker   !__has_feature(memory_sanitizer)
24*9880d681SAndroid Build Coastguard Worker namespace test1 {
25*9880d681SAndroid Build Coastguard Worker   llvm::ManagedStatic<int> ms;
helper(void *)26*9880d681SAndroid Build Coastguard Worker   void *helper(void*) {
27*9880d681SAndroid Build Coastguard Worker     *ms;
28*9880d681SAndroid Build Coastguard Worker     return nullptr;
29*9880d681SAndroid Build Coastguard Worker   }
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker   // Valgrind's leak checker complains glibc's stack allocation.
32*9880d681SAndroid Build Coastguard Worker   // To appease valgrind, we provide our own stack for each thread.
allocate_stack(pthread_attr_t & a,size_t n=65536)33*9880d681SAndroid Build Coastguard Worker   void *allocate_stack(pthread_attr_t &a, size_t n = 65536) {
34*9880d681SAndroid Build Coastguard Worker     void *stack = malloc(n);
35*9880d681SAndroid Build Coastguard Worker     pthread_attr_init(&a);
36*9880d681SAndroid Build Coastguard Worker #if defined(__linux__)
37*9880d681SAndroid Build Coastguard Worker     pthread_attr_setstack(&a, stack, n);
38*9880d681SAndroid Build Coastguard Worker #endif
39*9880d681SAndroid Build Coastguard Worker     return stack;
40*9880d681SAndroid Build Coastguard Worker   }
41*9880d681SAndroid Build Coastguard Worker }
42*9880d681SAndroid Build Coastguard Worker 
TEST(Initialize,MultipleThreads)43*9880d681SAndroid Build Coastguard Worker TEST(Initialize, MultipleThreads) {
44*9880d681SAndroid Build Coastguard Worker   // Run this test under tsan: http://code.google.com/p/data-race-test/
45*9880d681SAndroid Build Coastguard Worker 
46*9880d681SAndroid Build Coastguard Worker   pthread_attr_t a1, a2;
47*9880d681SAndroid Build Coastguard Worker   void *p1 = test1::allocate_stack(a1);
48*9880d681SAndroid Build Coastguard Worker   void *p2 = test1::allocate_stack(a2);
49*9880d681SAndroid Build Coastguard Worker 
50*9880d681SAndroid Build Coastguard Worker   pthread_t t1, t2;
51*9880d681SAndroid Build Coastguard Worker   pthread_create(&t1, &a1, test1::helper, nullptr);
52*9880d681SAndroid Build Coastguard Worker   pthread_create(&t2, &a2, test1::helper, nullptr);
53*9880d681SAndroid Build Coastguard Worker   pthread_join(t1, nullptr);
54*9880d681SAndroid Build Coastguard Worker   pthread_join(t2, nullptr);
55*9880d681SAndroid Build Coastguard Worker   free(p1);
56*9880d681SAndroid Build Coastguard Worker   free(p2);
57*9880d681SAndroid Build Coastguard Worker }
58*9880d681SAndroid Build Coastguard Worker #endif
59*9880d681SAndroid Build Coastguard Worker 
60*9880d681SAndroid Build Coastguard Worker } // anonymous namespace
61