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