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 | |
Eric Fiselier | 559f867 | 2014-11-24 22:42:03 | [diff] [blame^] | 10 | #include "../src/config.h" |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 11 | #include "cxxabi.h" |
| 12 | |
| 13 | #include <cassert> |
Eric Fiselier | 559f867 | 2014-11-24 22:42:03 | [diff] [blame^] | 14 | |
| 15 | #if !LIBCXXABI_HAS_NO_THREADS |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 16 | #include <thread> |
Eric Fiselier | 559f867 | 2014-11-24 22:42:03 | [diff] [blame^] | 17 | #endif |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 18 | |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 19 | // Ensure that we initialize each variable once and only once. |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [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(); |
| 28 | } |
| 29 | void test() { |
| 30 | static int a = increment(); |
| 31 | assert(run_count == 1); |
| 32 | static int b = increment(); |
| 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 { |
| 44 | static int run_count = 0; |
| 45 | int increment() { |
| 46 | ++run_count; |
| 47 | throw 0; |
| 48 | } |
| 49 | void helper() { |
| 50 | try { |
| 51 | static int a = increment(); |
| 52 | assert(0); |
| 53 | } catch (...) {} |
| 54 | } |
| 55 | void test() { |
| 56 | helper(); |
| 57 | assert(run_count == 1); |
| 58 | helper(); |
| 59 | assert(run_count == 2); |
| 60 | } |
| 61 | } |
| 62 | |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 63 | // Check that we can initialize a second value while initializing a first. |
| 64 | namespace test3 { |
| 65 | int zero() { |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | int one() { |
| 70 | static int b = zero(); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | void test() { |
| 75 | static int a = one(); |
| 76 | } |
| 77 | } |
| 78 | |
Eric Fiselier | 559f867 | 2014-11-24 22:42:03 | [diff] [blame^] | 79 | #if !LIBCXXABI_HAS_NO_THREADS |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 80 | // A simple thread test of two threads racing to initialize a variable. This |
| 81 | // isn't guaranteed to catch any particular threading problems. |
| 82 | namespace test4 { |
| 83 | static int run_count = 0; |
| 84 | int increment() { |
| 85 | ++run_count; |
Howard Hinnant | 575498b | 2011-06-07 19:56:49 | [diff] [blame] | 86 | return 0; |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void helper() { |
| 90 | static int a = increment(); |
| 91 | } |
| 92 | |
| 93 | void test() { |
| 94 | std::thread t1(helper), t2(helper); |
| 95 | t1.join(); |
| 96 | t2.join(); |
Howard Hinnant | 575498b | 2011-06-07 19:56:49 | [diff] [blame] | 97 | assert(run_count == 1); |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | // Check that we don't re-initialize a static variable even when it's |
| 102 | // encountered from two different threads. |
| 103 | namespace test5 { |
| 104 | static int run_count = 0; |
| 105 | int zero() { |
| 106 | ++run_count; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int one() { |
| 111 | static int b = zero(); |
Howard Hinnant | 575498b | 2011-06-07 19:56:49 | [diff] [blame] | 112 | return 0; |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void another_helper() { |
| 116 | static int a = one(); |
| 117 | } |
| 118 | |
| 119 | void helper() { |
| 120 | static int a = one(); |
Howard Hinnant | 575498b | 2011-06-07 19:56:49 | [diff] [blame] | 121 | std::thread t(another_helper); |
| 122 | t.join(); |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void test() { |
| 126 | std::thread t(helper); |
| 127 | t.join(); |
Howard Hinnant | 575498b | 2011-06-07 19:56:49 | [diff] [blame] | 128 | assert(run_count == 1); |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 129 | } |
| 130 | } |
Eric Fiselier | 559f867 | 2014-11-24 22:42:03 | [diff] [blame^] | 131 | #endif /* LIBCXXABI_HAS_NO_THREADS */ |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 132 | |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 133 | int main() |
| 134 | { |
| 135 | test1::test(); |
| 136 | test2::test(); |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 137 | test3::test(); |
Eric Fiselier | 559f867 | 2014-11-24 22:42:03 | [diff] [blame^] | 138 | #if !LIBCXXABI_HAS_NO_THREADS |
Nick Lewycky | 6fde150 | 2011-06-04 18:01:24 | [diff] [blame] | 139 | test4::test(); |
| 140 | test5::test(); |
Eric Fiselier | 559f867 | 2014-11-24 22:42:03 | [diff] [blame^] | 141 | #endif |
Howard Hinnant | 4a88971 | 2011-05-24 22:01:16 | [diff] [blame] | 142 | } |