blob: 41ff79432b5627f272de0d98e5b8c4713c26f76c [file] [log] [blame]
Howard Hinnant4a889712011-05-24 22:01:161//===----------------------------- 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 Fiselier559f8672014-11-24 22:42:0310#include "../src/config.h"
Howard Hinnant4a889712011-05-24 22:01:1611#include "cxxabi.h"
12
13#include <cassert>
Eric Fiselier559f8672014-11-24 22:42:0314
15#if !LIBCXXABI_HAS_NO_THREADS
Nick Lewycky6fde1502011-06-04 18:01:2416#include <thread>
Eric Fiselier559f8672014-11-24 22:42:0317#endif
Howard Hinnant4a889712011-05-24 22:01:1618
Nick Lewycky6fde1502011-06-04 18:01:2419// Ensure that we initialize each variable once and only once.
Howard Hinnant4a889712011-05-24 22:01:1620namespace 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 Lewycky6fde1502011-06-04 18:01:2441// When initialization fails, ensure that we try to initialize it again next
42// time.
Howard Hinnant4a889712011-05-24 22:01:1643namespace 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 Lewycky6fde1502011-06-04 18:01:2463// Check that we can initialize a second value while initializing a first.
64namespace 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 Fiselier559f8672014-11-24 22:42:0379#if !LIBCXXABI_HAS_NO_THREADS
Nick Lewycky6fde1502011-06-04 18:01:2480// A simple thread test of two threads racing to initialize a variable. This
81// isn't guaranteed to catch any particular threading problems.
82namespace test4 {
83 static int run_count = 0;
84 int increment() {
85 ++run_count;
Howard Hinnant575498b2011-06-07 19:56:4986 return 0;
Nick Lewycky6fde1502011-06-04 18:01:2487 }
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 Hinnant575498b2011-06-07 19:56:4997 assert(run_count == 1);
Nick Lewycky6fde1502011-06-04 18:01:2498 }
99}
100
101// Check that we don't re-initialize a static variable even when it's
102// encountered from two different threads.
103namespace 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 Hinnant575498b2011-06-07 19:56:49112 return 0;
Nick Lewycky6fde1502011-06-04 18:01:24113 }
114
115 void another_helper() {
116 static int a = one();
117 }
118
119 void helper() {
120 static int a = one();
Howard Hinnant575498b2011-06-07 19:56:49121 std::thread t(another_helper);
122 t.join();
Nick Lewycky6fde1502011-06-04 18:01:24123 }
124
125 void test() {
126 std::thread t(helper);
127 t.join();
Howard Hinnant575498b2011-06-07 19:56:49128 assert(run_count == 1);
Nick Lewycky6fde1502011-06-04 18:01:24129 }
130}
Eric Fiselier559f8672014-11-24 22:42:03131#endif /* LIBCXXABI_HAS_NO_THREADS */
Nick Lewycky6fde1502011-06-04 18:01:24132
Howard Hinnant4a889712011-05-24 22:01:16133int main()
134{
135 test1::test();
136 test2::test();
Nick Lewycky6fde1502011-06-04 18:01:24137 test3::test();
Eric Fiselier559f8672014-11-24 22:42:03138#if !LIBCXXABI_HAS_NO_THREADS
Nick Lewycky6fde1502011-06-04 18:01:24139 test4::test();
140 test5::test();
Eric Fiselier559f8672014-11-24 22:42:03141#endif
Howard Hinnant4a889712011-05-24 22:01:16142}