blob: d3cc80cf5e92c8eb5c7c997c9a90f75bacf01df2 [file] [log] [blame]
Nick Lewyckyfe856112011-11-14 20:50:16 +00001//===- llvm/unittest/Support/ManagedStatic.cpp - ManagedStatic tests ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Serge Pavlov76d8cce2018-02-20 05:41:26 +00009
10#include "llvm/Support/Allocator.h"
Nick Lewyckyfe856112011-11-14 20:50:16 +000011#include "llvm/Support/ManagedStatic.h"
Nick Lewyckye5dc7552011-11-14 22:10:23 +000012#include "llvm/Config/config.h"
13#ifdef HAVE_PTHREAD_H
Nick Lewyckyfe856112011-11-14 20:50:16 +000014#include <pthread.h>
Nick Lewyckye5dc7552011-11-14 22:10:23 +000015#endif
Nick Lewyckyfe856112011-11-14 20:50:16 +000016
17#include "gtest/gtest.h"
18
19using namespace llvm;
20
21namespace {
22
Duncan Sandsb33790d2013-05-14 13:29:16 +000023#if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H) && \
24 !__has_feature(memory_sanitizer)
Nick Lewyckyfe856112011-11-14 20:50:16 +000025namespace test1 {
26 llvm::ManagedStatic<int> ms;
27 void *helper(void*) {
28 *ms;
Craig Topper66f09ad2014-06-08 22:29:17 +000029 return nullptr;
Nick Lewyckyfe856112011-11-14 20:50:16 +000030 }
NAKAMURA Takumi2998dcb2013-01-24 14:44:02 +000031
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) {
Serge Pavlov76d8cce2018-02-20 05:41:26 +000035 void *stack = safe_malloc(n);
NAKAMURA Takumi2998dcb2013-01-24 14:44:02 +000036 pthread_attr_init(&a);
NAKAMURA Takumicbc86bc2013-01-24 15:29:27 +000037#if defined(__linux__)
NAKAMURA Takumi2998dcb2013-01-24 14:44:02 +000038 pthread_attr_setstack(&a, stack, n);
NAKAMURA Takumicbc86bc2013-01-24 15:29:27 +000039#endif
NAKAMURA Takumi2998dcb2013-01-24 14:44:02 +000040 return stack;
41 }
Nick Lewyckyfe856112011-11-14 20:50:16 +000042}
43
44TEST(Initialize, MultipleThreads) {
45 // Run this test under tsan: http://code.google.com/p/data-race-test/
46
NAKAMURA Takumi2998dcb2013-01-24 14:44:02 +000047 pthread_attr_t a1, a2;
48 void *p1 = test1::allocate_stack(a1);
49 void *p2 = test1::allocate_stack(a2);
50
Nick Lewyckyfe856112011-11-14 20:50:16 +000051 pthread_t t1, t2;
Craig Topper66f09ad2014-06-08 22:29:17 +000052 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);
NAKAMURA Takumi2998dcb2013-01-24 14:44:02 +000056 free(p1);
57 free(p2);
Nick Lewyckyfe856112011-11-14 20:50:16 +000058}
Nick Lewyckye5dc7552011-11-14 22:10:23 +000059#endif
Nick Lewyckyfe856112011-11-14 20:50:16 +000060
Benjamin Kramer1533eda2017-05-29 14:05:26 +000061namespace NestedStatics {
62static ManagedStatic<int> Ms1;
63struct Nest {
64 Nest() {
65 ++(*Ms1);
66 }
67
68 ~Nest() {
69 assert(Ms1.isConstructed());
70 ++(*Ms1);
71 }
72};
73static ManagedStatic<Nest> Ms2;
74
75TEST(ManagedStaticTest, NestedStatics) {
76 EXPECT_FALSE(Ms1.isConstructed());
77 EXPECT_FALSE(Ms2.isConstructed());
78
79 *Ms2;
80 EXPECT_TRUE(Ms1.isConstructed());
81 EXPECT_TRUE(Ms2.isConstructed());
Benjamin Kramer1533eda2017-05-29 14:05:26 +000082}
83} // namespace NestedStatics
84
85namespace CustomCreatorDeletor {
Benjamin Kramer74de0802017-05-29 20:56:27 +000086struct CustomCreate {
87 static void *call() {
Serge Pavlov76d8cce2018-02-20 05:41:26 +000088 void *Mem = safe_malloc(sizeof(int));
Benjamin Kramer74de0802017-05-29 20:56:27 +000089 *((int *)Mem) = 42;
90 return Mem;
91 }
92};
93struct CustomDelete {
94 static void call(void *P) { std::free(P); }
95};
96static ManagedStatic<int, CustomCreate, CustomDelete> Custom;
Benjamin Kramer1533eda2017-05-29 14:05:26 +000097TEST(ManagedStaticTest, CustomCreatorDeletor) {
98 EXPECT_EQ(42, *Custom);
Benjamin Kramer1533eda2017-05-29 14:05:26 +000099}
100} // namespace CustomCreatorDeletor
101
Nick Lewyckyfe856112011-11-14 20:50:16 +0000102} // anonymous namespace