1 //===- llvm/unittest/Support/ManagedStatic.cpp - ManagedStatic tests ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/Allocator.h"
11 #include "llvm/Support/ManagedStatic.h"
12 #include "llvm/Config/config.h"
17 #include "gtest/gtest.h"
23 #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H) && \
24 !__has_feature(memory_sanitizer)
26 llvm::ManagedStatic<int> ms;
32 // Valgrind's leak checker complains glibc's stack allocation.
33 // To appease valgrind, we provide our own stack for each thread.
34 void *allocate_stack(pthread_attr_t &a, size_t n = 65536) {
35 void *stack = safe_malloc(n);
36 pthread_attr_init(&a);
37 #if defined(__linux__)
38 pthread_attr_setstack(&a, stack, n);
44 TEST(Initialize, MultipleThreads) {
45 // Run this test under tsan: http://code.google.com/p/data-race-test/
47 pthread_attr_t a1, a2;
48 void *p1 = test1::allocate_stack(a1);
49 void *p2 = test1::allocate_stack(a2);
52 pthread_create(&t1, &a1, test1::helper, nullptr);
53 pthread_create(&t2, &a2, test1::helper, nullptr);
54 pthread_join(t1, nullptr);
55 pthread_join(t2, nullptr);
61 namespace NestedStatics {
62 static ManagedStatic<int> Ms1;
69 assert(Ms1.isConstructed());
73 static ManagedStatic<Nest> Ms2;
75 TEST(ManagedStaticTest, NestedStatics) {
76 EXPECT_FALSE(Ms1.isConstructed());
77 EXPECT_FALSE(Ms2.isConstructed());
80 EXPECT_TRUE(Ms1.isConstructed());
81 EXPECT_TRUE(Ms2.isConstructed());
83 } // namespace NestedStatics
85 namespace CustomCreatorDeletor {
88 void *Mem = safe_malloc(sizeof(int));
94 static void call(void *P) { std::free(P); }
96 static ManagedStatic<int, CustomCreate, CustomDelete> Custom;
97 TEST(ManagedStaticTest, CustomCreatorDeletor) {
98 EXPECT_EQ(42, *Custom);
100 } // namespace CustomCreatorDeletor
102 } // anonymous namespace