2021-11-18 05:25:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2011-05-25 06:01:16 +08:00
|
|
|
//
|
2019-01-19 18:56:40 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-05-25 06:01:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "cxxabi.h"
|
|
|
|
|
|
|
|
#include <cassert>
|
2014-11-25 06:42:03 +08:00
|
|
|
|
2016-09-21 17:09:32 +08:00
|
|
|
#ifndef _LIBCXXABI_HAS_NO_THREADS
|
2011-06-05 02:01:24 +08:00
|
|
|
#include <thread>
|
2020-11-24 04:52:03 +08:00
|
|
|
#include "make_test_thread.h"
|
2014-11-25 06:42:03 +08:00
|
|
|
#endif
|
2011-05-25 06:01:16 +08:00
|
|
|
|
2020-06-10 03:47:37 +08:00
|
|
|
#include "test_macros.h"
|
|
|
|
|
2011-06-05 02:01:24 +08:00
|
|
|
// Ensure that we initialize each variable once and only once.
|
2011-05-25 06:01:16 +08:00
|
|
|
namespace test1 {
|
|
|
|
static int run_count = 0;
|
|
|
|
int increment() {
|
|
|
|
++run_count;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void helper() {
|
|
|
|
static int a = increment();
|
2016-12-24 08:37:13 +08:00
|
|
|
((void)a);
|
2011-05-25 06:01:16 +08:00
|
|
|
}
|
|
|
|
void test() {
|
2016-12-24 08:37:13 +08:00
|
|
|
static int a = increment(); ((void)a);
|
2011-05-25 06:01:16 +08:00
|
|
|
assert(run_count == 1);
|
2016-12-24 08:37:13 +08:00
|
|
|
static int b = increment(); ((void)b);
|
2011-05-25 06:01:16 +08:00
|
|
|
assert(run_count == 2);
|
|
|
|
helper();
|
|
|
|
assert(run_count == 3);
|
|
|
|
helper();
|
|
|
|
assert(run_count == 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-05 02:01:24 +08:00
|
|
|
// When initialization fails, ensure that we try to initialize it again next
|
|
|
|
// time.
|
2011-05-25 06:01:16 +08:00
|
|
|
namespace test2 {
|
2020-06-10 03:47:37 +08:00
|
|
|
#ifndef TEST_HAS_NO_EXCEPTIONS
|
2011-05-25 06:01:16 +08:00
|
|
|
static int run_count = 0;
|
|
|
|
int increment() {
|
|
|
|
++run_count;
|
|
|
|
throw 0;
|
|
|
|
}
|
|
|
|
void helper() {
|
|
|
|
try {
|
|
|
|
static int a = increment();
|
2016-12-24 08:37:13 +08:00
|
|
|
assert(false);
|
|
|
|
((void)a);
|
2011-05-25 06:01:16 +08:00
|
|
|
} catch (...) {}
|
|
|
|
}
|
|
|
|
void test() {
|
|
|
|
helper();
|
|
|
|
assert(run_count == 1);
|
|
|
|
helper();
|
|
|
|
assert(run_count == 2);
|
|
|
|
}
|
2016-05-31 20:01:32 +08:00
|
|
|
#else
|
|
|
|
void test() {}
|
|
|
|
#endif
|
2011-05-25 06:01:16 +08:00
|
|
|
}
|
|
|
|
|
2011-06-05 02:01:24 +08:00
|
|
|
// Check that we can initialize a second value while initializing a first.
|
|
|
|
namespace test3 {
|
|
|
|
int zero() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int one() {
|
2016-12-24 08:37:13 +08:00
|
|
|
static int b = zero(); ((void)b);
|
2011-06-05 02:01:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test() {
|
2016-12-24 08:37:13 +08:00
|
|
|
static int a = one(); ((void)a);
|
2011-06-05 02:01:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:09:32 +08:00
|
|
|
#ifndef _LIBCXXABI_HAS_NO_THREADS
|
2011-06-05 02:01:24 +08:00
|
|
|
// A simple thread test of two threads racing to initialize a variable. This
|
|
|
|
// isn't guaranteed to catch any particular threading problems.
|
|
|
|
namespace test4 {
|
|
|
|
static int run_count = 0;
|
|
|
|
int increment() {
|
|
|
|
++run_count;
|
2011-06-08 03:56:49 +08:00
|
|
|
return 0;
|
2011-06-05 02:01:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void helper() {
|
2016-12-24 08:37:13 +08:00
|
|
|
static int a = increment(); ((void)a);
|
2011-06-05 02:01:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test() {
|
2020-11-24 04:52:03 +08:00
|
|
|
std::thread t1 = support::make_test_thread(helper);
|
|
|
|
std::thread t2 = support::make_test_thread(helper);
|
2011-06-05 02:01:24 +08:00
|
|
|
t1.join();
|
|
|
|
t2.join();
|
2011-06-08 03:56:49 +08:00
|
|
|
assert(run_count == 1);
|
2011-06-05 02:01:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we don't re-initialize a static variable even when it's
|
|
|
|
// encountered from two different threads.
|
|
|
|
namespace test5 {
|
|
|
|
static int run_count = 0;
|
|
|
|
int zero() {
|
|
|
|
++run_count;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int one() {
|
2016-12-24 08:37:13 +08:00
|
|
|
static int b = zero(); ((void)b);
|
2011-06-08 03:56:49 +08:00
|
|
|
return 0;
|
2011-06-05 02:01:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void another_helper() {
|
2016-12-24 08:37:13 +08:00
|
|
|
static int a = one(); ((void)a);
|
2011-06-05 02:01:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void helper() {
|
2016-12-24 08:37:13 +08:00
|
|
|
static int a = one(); ((void)a);
|
2020-11-24 04:52:03 +08:00
|
|
|
std::thread t = support::make_test_thread(another_helper);
|
2011-06-08 03:56:49 +08:00
|
|
|
t.join();
|
2011-06-05 02:01:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test() {
|
2020-11-24 04:52:03 +08:00
|
|
|
std::thread t = support::make_test_thread(helper);
|
2011-06-05 02:01:24 +08:00
|
|
|
t.join();
|
2011-06-08 03:56:49 +08:00
|
|
|
assert(run_count == 1);
|
2011-06-05 02:01:24 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-21 17:09:32 +08:00
|
|
|
#endif /* _LIBCXXABI_HAS_NO_THREADS */
|
2011-06-05 02:01:24 +08:00
|
|
|
|
2020-10-09 01:36:33 +08:00
|
|
|
int main(int, char**)
|
2011-05-25 06:01:16 +08:00
|
|
|
{
|
|
|
|
test1::test();
|
|
|
|
test2::test();
|
2011-06-05 02:01:24 +08:00
|
|
|
test3::test();
|
2016-09-21 17:09:32 +08:00
|
|
|
#ifndef _LIBCXXABI_HAS_NO_THREADS
|
2011-06-05 02:01:24 +08:00
|
|
|
test4::test();
|
|
|
|
test5::test();
|
2014-11-25 06:42:03 +08:00
|
|
|
#endif
|
2020-10-09 01:36:33 +08:00
|
|
|
|
|
|
|
return 0;
|
2011-05-25 06:01:16 +08:00
|
|
|
}
|