Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 1 | //===----------------------------- test_guard.cpp -------------------------===// |
2 | // | ||||
3 | // The LLVM Compiler Infrastructure | ||||
4 | // | ||||
5 | // This file is dual licensed under the MIT and the University of Illinois Open | ||||
6 | // Source Licenses. See LICENSE.TXT for details. | ||||
7 | // | ||||
8 | //===----------------------------------------------------------------------===// | ||||
9 | |||||
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 10 | #include "cxxabi.h" |
11 | |||||
12 | #include <cassert> | ||||
Eric Fiselier | 559f867 | 2014-11-24 22:42:03 | [diff] [blame] | 13 | |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 | [diff] [blame] | 14 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 15 | #include <thread> |
Eric Fiselier | 559f867 | 2014-11-24 22:42:03 | [diff] [blame] | 16 | #endif |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 17 | |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 18 | // Ensure that we initialize each variable once and only once. |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 19 | namespace test1 { |
20 | static int run_count = 0; | ||||
21 | int increment() { | ||||
22 | ++run_count; | ||||
23 | return 0; | ||||
24 | } | ||||
25 | void helper() { | ||||
26 | static int a = increment(); | ||||
Eric Fiselier | a140cba | 2016-12-24 00:37:13 | [diff] [blame] | 27 | ((void)a); |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 28 | } |
29 | void test() { | ||||
Eric Fiselier | a140cba | 2016-12-24 00:37:13 | [diff] [blame] | 30 | static int a = increment(); ((void)a); |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 31 | assert(run_count == 1); |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 | [diff] [blame] | 32 | static int b = increment(); ((void)b); |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 33 | assert(run_count == 2); |
34 | helper(); | ||||
35 | assert(run_count == 3); | ||||
36 | helper(); | ||||
37 | assert(run_count == 3); | ||||
38 | } | ||||
39 | } | ||||
40 | |||||
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 41 | // When initialization fails, ensure that we try to initialize it again next |
42 | // time. | ||||
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 43 | namespace test2 { |
Asiri Rathnayake | 57e446d | 2016-05-31 12:01:32 | [diff] [blame] | 44 | #ifndef LIBCXXABI_HAS_NO_EXCEPTIONS |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 45 | static int run_count = 0; |
46 | int increment() { | ||||
47 | ++run_count; | ||||
48 | throw 0; | ||||
49 | } | ||||
50 | void helper() { | ||||
51 | try { | ||||
52 | static int a = increment(); | ||||
Eric Fiselier | a140cba | 2016-12-24 00:37:13 | [diff] [blame] | 53 | assert(false); |
54 | ((void)a); | ||||
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 55 | } catch (...) {} |
56 | } | ||||
57 | void test() { | ||||
58 | helper(); | ||||
59 | assert(run_count == 1); | ||||
60 | helper(); | ||||
61 | assert(run_count == 2); | ||||
62 | } | ||||
Asiri Rathnayake | 57e446d | 2016-05-31 12:01:32 | [diff] [blame] | 63 | #else |
64 | void test() {} | ||||
65 | #endif | ||||
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 66 | } |
67 | |||||
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 68 | // Check that we can initialize a second value while initializing a first. |
69 | namespace test3 { | ||||
70 | int zero() { | ||||
71 | return 0; | ||||
72 | } | ||||
73 | |||||
74 | int one() { | ||||
Eric Fiselier | a140cba | 2016-12-24 00:37:13 | [diff] [blame] | 75 | static int b = zero(); ((void)b); |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 76 | return 0; |
77 | } | ||||
78 | |||||
79 | void test() { | ||||
Eric Fiselier | a140cba | 2016-12-24 00:37:13 | [diff] [blame] | 80 | static int a = one(); ((void)a); |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 81 | } |
82 | } | ||||
83 | |||||
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 | [diff] [blame] | 84 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 85 | // A simple thread test of two threads racing to initialize a variable. This |
86 | // isn't guaranteed to catch any particular threading problems. | ||||
87 | namespace test4 { | ||||
88 | static int run_count = 0; | ||||
89 | int increment() { | ||||
90 | ++run_count; | ||||
Howard Hinnant | 575498b | 2011-06-07 19:56:49 | [diff] [blame] | 91 | return 0; |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 92 | } |
93 | |||||
94 | void helper() { | ||||
Eric Fiselier | a140cba | 2016-12-24 00:37:13 | [diff] [blame] | 95 | static int a = increment(); ((void)a); |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 96 | } |
97 | |||||
98 | void test() { | ||||
99 | std::thread t1(helper), t2(helper); | ||||
100 | t1.join(); | ||||
101 | t2.join(); | ||||
Howard Hinnant | 575498b | 2011-06-07 19:56:49 | [diff] [blame] | 102 | assert(run_count == 1); |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 103 | } |
104 | } | ||||
105 | |||||
106 | // Check that we don't re-initialize a static variable even when it's | ||||
107 | // encountered from two different threads. | ||||
108 | namespace test5 { | ||||
109 | static int run_count = 0; | ||||
110 | int zero() { | ||||
111 | ++run_count; | ||||
112 | return 0; | ||||
113 | } | ||||
114 | |||||
115 | int one() { | ||||
Eric Fiselier | a140cba | 2016-12-24 00:37:13 | [diff] [blame] | 116 | static int b = zero(); ((void)b); |
Howard Hinnant | 575498b | 2011-06-07 19:56:49 | [diff] [blame] | 117 | return 0; |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 118 | } |
119 | |||||
120 | void another_helper() { | ||||
Eric Fiselier | a140cba | 2016-12-24 00:37:13 | [diff] [blame] | 121 | static int a = one(); ((void)a); |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 122 | } |
123 | |||||
124 | void helper() { | ||||
Eric Fiselier | a140cba | 2016-12-24 00:37:13 | [diff] [blame] | 125 | static int a = one(); ((void)a); |
Howard Hinnant | 575498b | 2011-06-07 19:56:49 | [diff] [blame] | 126 | std::thread t(another_helper); |
127 | t.join(); | ||||
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 128 | } |
129 | |||||
130 | void test() { | ||||
131 | std::thread t(helper); | ||||
132 | t.join(); | ||||
Howard Hinnant | 575498b | 2011-06-07 19:56:49 | [diff] [blame] | 133 | assert(run_count == 1); |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 134 | } |
135 | } | ||||
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 | [diff] [blame] | 136 | #endif /* _LIBCXXABI_HAS_NO_THREADS */ |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 137 | |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 138 | int main() |
139 | { | ||||
140 | test1::test(); | ||||
141 | test2::test(); | ||||
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 142 | test3::test(); |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 | [diff] [blame] | 143 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 144 | test4::test(); |
145 | test5::test(); | ||||
Eric Fiselier | 559f867 | 2014-11-24 22:42:03 | [diff] [blame] | 146 | #endif |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 147 | } |