blob: 3e9e3f52e41c383621d91590b8ab4617ac1a83f7 [file] [log] [blame]
Nick Lewyckyfe856112011-11-14 20:50:16 +00001//===- llvm/unittest/Support/ManagedStatic.cpp - ManagedStatic tests ------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Nick Lewyckyfe856112011-11-14 20:50:16 +00006//
7//===----------------------------------------------------------------------===//
Serge Pavlov76d8cce2018-02-20 05:41:26 +00008
9#include "llvm/Support/Allocator.h"
Nick Lewyckyfe856112011-11-14 20:50:16 +000010#include "llvm/Support/ManagedStatic.h"
Nick Lewyckye5dc7552011-11-14 22:10:23 +000011#include "llvm/Config/config.h"
12#ifdef HAVE_PTHREAD_H
Nick Lewyckyfe856112011-11-14 20:50:16 +000013#include <pthread.h>
Nick Lewyckye5dc7552011-11-14 22:10:23 +000014#endif
Nick Lewyckyfe856112011-11-14 20:50:16 +000015
16#include "gtest/gtest.h"
17
18using namespace llvm;
19
20namespace {
21
Duncan Sandsb33790d2013-05-14 13:29:16 +000022#if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H) && \
23 !__has_feature(memory_sanitizer)
Nick Lewyckyfe856112011-11-14 20:50:16 +000024namespace test1 {
25 llvm::ManagedStatic<int> ms;
26 void *helper(void*) {
27 *ms;
Craig Topper66f09ad2014-06-08 22:29:17 +000028 return nullptr;
Nick Lewyckyfe856112011-11-14 20:50:16 +000029 }
NAKAMURA Takumi2998dcb2013-01-24 14:44:02 +000030
31 // Valgrind's leak checker complains glibc's stack allocation.
32 // To appease valgrind, we provide our own stack for each thread.
33 void *allocate_stack(pthread_attr_t &a, size_t n = 65536) {
Serge Pavlov76d8cce2018-02-20 05:41:26 +000034 void *stack = safe_malloc(n);
NAKAMURA Takumi2998dcb2013-01-24 14:44:02 +000035 pthread_attr_init(&a);
NAKAMURA Takumicbc86bc2013-01-24 15:29:27 +000036#if defined(__linux__)
NAKAMURA Takumi2998dcb2013-01-24 14:44:02 +000037 pthread_attr_setstack(&a, stack, n);
NAKAMURA Takumicbc86bc2013-01-24 15:29:27 +000038#endif
NAKAMURA Takumi2998dcb2013-01-24 14:44:02 +000039 return stack;
40 }
Nick Lewyckyfe856112011-11-14 20:50:16 +000041}
42
43TEST(Initialize, MultipleThreads) {
44 // Run this test under tsan: http://code.google.com/p/data-race-test/
45
NAKAMURA Takumi2998dcb2013-01-24 14:44:02 +000046 pthread_attr_t a1, a2;
47 void *p1 = test1::allocate_stack(a1);
48 void *p2 = test1::allocate_stack(a2);
49
Nick Lewyckyfe856112011-11-14 20:50:16 +000050 pthread_t t1, t2;
Craig Topper66f09ad2014-06-08 22:29:17 +000051 pthread_create(&t1, &a1, test1::helper, nullptr);
52 pthread_create(&t2, &a2, test1::helper, nullptr);
53 pthread_join(t1, nullptr);
54 pthread_join(t2, nullptr);
NAKAMURA Takumi2998dcb2013-01-24 14:44:02 +000055 free(p1);
56 free(p2);
Nick Lewyckyfe856112011-11-14 20:50:16 +000057}
Nick Lewyckye5dc7552011-11-14 22:10:23 +000058#endif
Nick Lewyckyfe856112011-11-14 20:50:16 +000059
Benjamin Kramer1533eda2017-05-29 14:05:26 +000060namespace NestedStatics {
61static ManagedStatic<int> Ms1;
62struct Nest {
63 Nest() {
64 ++(*Ms1);
65 }
66
67 ~Nest() {
68 assert(Ms1.isConstructed());
69 ++(*Ms1);
70 }
71};
72static ManagedStatic<Nest> Ms2;
73
74TEST(ManagedStaticTest, NestedStatics) {
75 EXPECT_FALSE(Ms1.isConstructed());
76 EXPECT_FALSE(Ms2.isConstructed());
77
78 *Ms2;
79 EXPECT_TRUE(Ms1.isConstructed());
80 EXPECT_TRUE(Ms2.isConstructed());
Benjamin Kramer1533eda2017-05-29 14:05:26 +000081}
82} // namespace NestedStatics
83
84namespace CustomCreatorDeletor {
Benjamin Kramer74de0802017-05-29 20:56:27 +000085struct CustomCreate {
86 static void *call() {
Serge Pavlov76d8cce2018-02-20 05:41:26 +000087 void *Mem = safe_malloc(sizeof(int));
Benjamin Kramer74de0802017-05-29 20:56:27 +000088 *((int *)Mem) = 42;
89 return Mem;
90 }
91};
92struct CustomDelete {
93 static void call(void *P) { std::free(P); }
94};
95static ManagedStatic<int, CustomCreate, CustomDelete> Custom;
Benjamin Kramer1533eda2017-05-29 14:05:26 +000096TEST(ManagedStaticTest, CustomCreatorDeletor) {
97 EXPECT_EQ(42, *Custom);
Benjamin Kramer1533eda2017-05-29 14:05:26 +000098}
99} // namespace CustomCreatorDeletor
100
Nick Lewyckyfe856112011-11-14 20:50:16 +0000101} // anonymous namespace