annotate unittests/Support/ManagedStatic.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 54457678186b
children 3a76565eade5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- llvm/unittest/Support/ManagedStatic.cpp - ManagedStatic tests ------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #include "llvm/Support/ManagedStatic.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #include "llvm/Config/config.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 #include "llvm/Support/Threading.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 #ifdef HAVE_PTHREAD_H
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 #include <pthread.h>
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 #endif
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 #include "gtest/gtest.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 using namespace llvm;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 namespace {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H) && \
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 !__has_feature(memory_sanitizer)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 namespace test1 {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 llvm::ManagedStatic<int> ms;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 void *helper(void*) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 *ms;
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
28 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 // Valgrind's leak checker complains glibc's stack allocation.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 // To appease valgrind, we provide our own stack for each thread.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 void *allocate_stack(pthread_attr_t &a, size_t n = 65536) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 void *stack = malloc(n);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 pthread_attr_init(&a);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 #if defined(__linux__)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 pthread_attr_setstack(&a, stack, n);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 #endif
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 return stack;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 TEST(Initialize, MultipleThreads) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 // Run this test under tsan: http://code.google.com/p/data-race-test/
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 pthread_attr_t a1, a2;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 void *p1 = test1::allocate_stack(a1);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 void *p2 = test1::allocate_stack(a2);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 pthread_t t1, t2;
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
51 pthread_create(&t1, &a1, test1::helper, nullptr);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
52 pthread_create(&t2, &a2, test1::helper, nullptr);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
53 pthread_join(t1, nullptr);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
54 pthread_join(t2, nullptr);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 free(p1);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 free(p2);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 #endif
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59
121
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
60 namespace NestedStatics {
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
61 static ManagedStatic<int> Ms1;
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
62 struct Nest {
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
63 Nest() {
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
64 ++(*Ms1);
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
65 }
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
66
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
67 ~Nest() {
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
68 assert(Ms1.isConstructed());
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
69 ++(*Ms1);
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
70 }
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
71 };
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
72 static ManagedStatic<Nest> Ms2;
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
73
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
74 TEST(ManagedStaticTest, NestedStatics) {
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
75 EXPECT_FALSE(Ms1.isConstructed());
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
76 EXPECT_FALSE(Ms2.isConstructed());
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
77
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
78 *Ms2;
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
79 EXPECT_TRUE(Ms1.isConstructed());
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
80 EXPECT_TRUE(Ms2.isConstructed());
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
81 }
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
82 } // namespace NestedStatics
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
83
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
84 namespace CustomCreatorDeletor {
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
85 struct CustomCreate {
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
86 static void *call() {
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
87 void *Mem = std::malloc(sizeof(int));
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
88 *((int *)Mem) = 42;
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
89 return Mem;
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
90 }
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
91 };
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
92 struct CustomDelete {
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
93 static void call(void *P) { std::free(P); }
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
94 };
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
95 static ManagedStatic<int, CustomCreate, CustomDelete> Custom;
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
96 TEST(ManagedStaticTest, CustomCreatorDeletor) {
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
97 EXPECT_EQ(42, *Custom);
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
98 }
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
99 } // namespace CustomCreatorDeletor
803732b1fca8 LLVM 5.0
kono
parents: 77
diff changeset
100
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 } // anonymous namespace