1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
12 #include "test_macros.h"
14 #ifndef TEST_HAS_NO_THREADS
16 # include "make_test_thread.h"
19 // Ensure that we initialize each variable once and only once.
21 static int run_count
= 0;
27 static int a
= increment();
31 static int a
= increment(); ((void)a
);
32 assert(run_count
== 1);
33 static int b
= increment(); ((void)b
);
34 assert(run_count
== 2);
36 assert(run_count
== 3);
38 assert(run_count
== 3);
42 // When initialization fails, ensure that we try to initialize it again next
45 #ifndef TEST_HAS_NO_EXCEPTIONS
46 static int run_count
= 0;
53 static int a
= increment();
60 assert(run_count
== 1);
62 assert(run_count
== 2);
69 // Check that we can initialize a second value while initializing a first.
76 static int b
= zero(); ((void)b
);
81 static int a
= one(); ((void)a
);
85 #ifndef TEST_HAS_NO_THREADS
86 // A simple thread test of two threads racing to initialize a variable. This
87 // isn't guaranteed to catch any particular threading problems.
89 static int run_count
= 0;
96 static int a
= increment(); ((void)a
);
100 std::thread t1
= support::make_test_thread(helper
);
101 std::thread t2
= support::make_test_thread(helper
);
104 assert(run_count
== 1);
108 // Check that we don't re-initialize a static variable even when it's
109 // encountered from two different threads.
111 static int run_count
= 0;
118 static int b
= zero(); ((void)b
);
122 void another_helper() {
123 static int a
= one(); ((void)a
);
127 static int a
= one(); ((void)a
);
128 std::thread t
= support::make_test_thread(another_helper
);
133 std::thread t
= support::make_test_thread(helper
);
135 assert(run_count
== 1);
138 #endif /* TEST_HAS_NO_THREADS */
140 int main(int, char**)
145 #ifndef TEST_HAS_NO_THREADS