2019-04-24 09:47:30 +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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-06-01 22:38:23 +08:00
|
|
|
// UNSUPPORTED: c++03
|
2022-05-26 23:49:47 +08:00
|
|
|
// UNSUPPORTED: no-threads
|
2020-04-17 22:29:15 +08:00
|
|
|
// UNSUPPORTED: no-exceptions
|
2019-04-24 09:47:30 +08:00
|
|
|
|
|
|
|
#define TESTING_CXA_GUARD
|
|
|
|
#include "../src/cxa_guard_impl.h"
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <thread>
|
|
|
|
#include <atomic>
|
|
|
|
#include <array>
|
|
|
|
#include <cassert>
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-11-24 04:52:03 +08:00
|
|
|
#include "make_test_thread.h"
|
2020-06-10 03:47:37 +08:00
|
|
|
#include "test_macros.h"
|
|
|
|
|
2019-04-24 09:47:30 +08:00
|
|
|
|
|
|
|
using namespace __cxxabiv1;
|
|
|
|
|
2019-05-02 21:22:55 +08:00
|
|
|
// Misc test configuration. It's used to tune the flakyness of the test.
|
|
|
|
// ThreadsPerTest - The number of threads used
|
|
|
|
constexpr int ThreadsPerTest = 10;
|
|
|
|
// The number of instances of a test to run concurrently.
|
|
|
|
constexpr int ConcurrentRunsPerTest = 10;
|
|
|
|
// The number of times to rerun each test.
|
|
|
|
constexpr int TestSamples = 50;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void BusyWait() {
|
2020-07-17 00:42:26 +08:00
|
|
|
std::this_thread::yield();
|
2019-05-02 21:22:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void YieldAfterBarrier() {
|
|
|
|
std::this_thread::sleep_for(std::chrono::nanoseconds(10));
|
|
|
|
std::this_thread::yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Barrier {
|
|
|
|
explicit Barrier(int n) : m_threads(n), m_remaining(n) { }
|
|
|
|
Barrier(Barrier const&) = delete;
|
|
|
|
Barrier& operator=(Barrier const&) = delete;
|
|
|
|
|
|
|
|
void arrive_and_wait() const {
|
|
|
|
--m_remaining;
|
|
|
|
while (m_remaining.load()) {
|
|
|
|
BusyWait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void arrive_and_drop() const {
|
|
|
|
--m_remaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wait_for_threads(int n) const {
|
|
|
|
while ((m_threads - m_remaining.load()) < n) {
|
|
|
|
std::this_thread::yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const int m_threads;
|
|
|
|
mutable std::atomic<int> m_remaining;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-04-24 09:47:30 +08:00
|
|
|
enum class InitResult {
|
|
|
|
COMPLETE,
|
|
|
|
PERFORMED,
|
|
|
|
WAITED,
|
|
|
|
ABORTED
|
|
|
|
};
|
|
|
|
constexpr InitResult COMPLETE = InitResult::COMPLETE;
|
|
|
|
constexpr InitResult PERFORMED = InitResult::PERFORMED;
|
|
|
|
constexpr InitResult WAITED = InitResult::WAITED;
|
|
|
|
constexpr InitResult ABORTED = InitResult::ABORTED;
|
|
|
|
|
|
|
|
|
|
|
|
template <class Impl, class GuardType, class Init>
|
|
|
|
InitResult check_guard(GuardType *g, Init init) {
|
|
|
|
uint8_t *first_byte = reinterpret_cast<uint8_t*>(g);
|
|
|
|
if (std::__libcpp_atomic_load(first_byte, std::_AO_Acquire) == 0) {
|
|
|
|
Impl impl(g);
|
|
|
|
if (impl.cxa_guard_acquire() == INIT_IS_PENDING) {
|
2020-06-10 03:47:37 +08:00
|
|
|
#ifndef TEST_HAS_NO_EXCEPTIONS
|
2019-04-24 09:47:30 +08:00
|
|
|
try {
|
|
|
|
#endif
|
|
|
|
init();
|
|
|
|
impl.cxa_guard_release();
|
|
|
|
return PERFORMED;
|
2020-06-10 03:47:37 +08:00
|
|
|
#ifndef TEST_HAS_NO_EXCEPTIONS
|
2019-04-24 09:47:30 +08:00
|
|
|
} catch (...) {
|
|
|
|
impl.cxa_guard_abort();
|
|
|
|
return ABORTED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return WAITED;
|
|
|
|
}
|
|
|
|
return COMPLETE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class GuardType, class Impl>
|
|
|
|
struct FunctionLocalStatic {
|
2019-05-02 21:22:55 +08:00
|
|
|
FunctionLocalStatic() {}
|
2019-04-24 09:47:30 +08:00
|
|
|
FunctionLocalStatic(FunctionLocalStatic const&) = delete;
|
|
|
|
|
|
|
|
template <class InitFunc>
|
|
|
|
InitResult access(InitFunc&& init) {
|
|
|
|
auto res = check_guard<Impl>(&guard_object, init);
|
|
|
|
++result_counts[static_cast<int>(res)];
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-05-02 21:22:55 +08:00
|
|
|
template <class InitFn>
|
|
|
|
struct AccessCallback {
|
|
|
|
void operator()() const { this_obj->access(init); }
|
2019-04-24 09:47:30 +08:00
|
|
|
|
|
|
|
FunctionLocalStatic *this_obj;
|
2019-05-02 21:22:55 +08:00
|
|
|
InitFn init;
|
2019-04-24 09:47:30 +08:00
|
|
|
};
|
|
|
|
|
2019-05-02 21:22:55 +08:00
|
|
|
template <class InitFn, class Callback = AccessCallback< InitFn > >
|
|
|
|
Callback access_callback(InitFn init) {
|
|
|
|
return Callback{this, init};
|
2019-04-24 09:47:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int get_count(InitResult I) const {
|
|
|
|
return result_counts[static_cast<int>(I)].load();
|
|
|
|
}
|
2019-05-02 21:22:55 +08:00
|
|
|
|
2019-04-24 09:47:30 +08:00
|
|
|
int num_completed() const {
|
|
|
|
return get_count(COMPLETE) + get_count(PERFORMED) + get_count(WAITED);
|
|
|
|
}
|
2019-05-02 21:22:55 +08:00
|
|
|
|
2019-04-24 09:47:30 +08:00
|
|
|
int num_waiting() const {
|
|
|
|
return waiting_threads.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-05-02 21:22:55 +08:00
|
|
|
GuardType guard_object = {};
|
|
|
|
std::atomic<int> waiting_threads{0};
|
|
|
|
std::array<std::atomic<int>, 4> result_counts{};
|
2019-04-24 09:47:30 +08:00
|
|
|
static_assert(static_cast<int>(ABORTED) == 3, "only 4 result kinds expected");
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ThreadGroup {
|
|
|
|
ThreadGroup() = default;
|
|
|
|
ThreadGroup(ThreadGroup const&) = delete;
|
|
|
|
|
|
|
|
template <class ...Args>
|
|
|
|
void Create(Args&& ...args) {
|
|
|
|
threads.emplace_back(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2019-05-02 21:22:55 +08:00
|
|
|
template <class Callback>
|
|
|
|
void CreateThreadsWithBarrier(int N, Callback cb) {
|
|
|
|
auto start = std::make_shared<Barrier>(N + 1);
|
|
|
|
for (int I=0; I < N; ++I) {
|
|
|
|
Create([start, cb]() {
|
|
|
|
start->arrive_and_wait();
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
start->arrive_and_wait();
|
|
|
|
}
|
|
|
|
|
2019-04-24 09:47:30 +08:00
|
|
|
void JoinAll() {
|
|
|
|
for (auto& t : threads) {
|
|
|
|
t.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::thread> threads;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <class GuardType, class Impl>
|
2019-05-02 21:22:55 +08:00
|
|
|
void test_free_for_all(int num_waiters) {
|
2019-04-24 09:47:30 +08:00
|
|
|
FunctionLocalStatic<GuardType, Impl> test_obj;
|
|
|
|
|
|
|
|
ThreadGroup threads;
|
2019-05-02 21:22:55 +08:00
|
|
|
|
|
|
|
bool already_init = false;
|
|
|
|
threads.CreateThreadsWithBarrier(num_waiters,
|
|
|
|
test_obj.access_callback([&]() {
|
|
|
|
assert(!already_init);
|
|
|
|
already_init = true;
|
|
|
|
})
|
|
|
|
);
|
2019-04-24 09:47:30 +08:00
|
|
|
|
|
|
|
// wait for the other threads to finish initialization.
|
|
|
|
threads.JoinAll();
|
|
|
|
|
|
|
|
assert(test_obj.get_count(PERFORMED) == 1);
|
2019-05-02 21:22:55 +08:00
|
|
|
assert(test_obj.get_count(COMPLETE) + test_obj.get_count(WAITED) == num_waiters - 1);
|
2019-04-24 09:47:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class GuardType, class Impl>
|
2019-05-02 21:22:55 +08:00
|
|
|
void test_waiting_for_init(int num_waiters) {
|
2019-04-24 09:47:30 +08:00
|
|
|
FunctionLocalStatic<GuardType, Impl> test_obj;
|
|
|
|
|
|
|
|
ThreadGroup threads;
|
2019-05-02 21:22:55 +08:00
|
|
|
|
|
|
|
Barrier start_init(2);
|
|
|
|
threads.Create(test_obj.access_callback(
|
2019-04-24 09:47:30 +08:00
|
|
|
[&]() {
|
2019-05-02 21:22:55 +08:00
|
|
|
start_init.arrive_and_wait();
|
|
|
|
// Take our sweet time completing the initialization...
|
|
|
|
//
|
|
|
|
// There's a race condition between the other threads reaching the
|
|
|
|
// start_init barrier, and them actually hitting the cxa guard.
|
|
|
|
// But we're trying to test the waiting logic, we want as many
|
|
|
|
// threads to enter the waiting loop as possible.
|
|
|
|
YieldAfterBarrier();
|
2019-04-24 09:47:30 +08:00
|
|
|
}
|
2019-05-02 21:22:55 +08:00
|
|
|
));
|
|
|
|
start_init.wait_for_threads(1);
|
2019-04-24 09:47:30 +08:00
|
|
|
|
2019-05-02 21:22:55 +08:00
|
|
|
threads.CreateThreadsWithBarrier(num_waiters,
|
|
|
|
test_obj.access_callback([]() { assert(false); })
|
|
|
|
);
|
2019-04-24 09:47:30 +08:00
|
|
|
// unblock the initializing thread
|
2019-05-02 21:22:55 +08:00
|
|
|
start_init.arrive_and_drop();
|
2019-04-24 09:47:30 +08:00
|
|
|
|
|
|
|
// wait for the other threads to finish initialization.
|
|
|
|
threads.JoinAll();
|
|
|
|
|
|
|
|
assert(test_obj.get_count(PERFORMED) == 1);
|
2019-05-02 21:22:55 +08:00
|
|
|
assert(test_obj.get_count(ABORTED) == 0);
|
|
|
|
assert(test_obj.get_count(COMPLETE) + test_obj.get_count(WAITED) == num_waiters);
|
2019-04-24 09:47:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class GuardType, class Impl>
|
2019-05-02 21:22:55 +08:00
|
|
|
void test_aborted_init(int num_waiters) {
|
2019-04-24 09:47:30 +08:00
|
|
|
FunctionLocalStatic<GuardType, Impl> test_obj;
|
|
|
|
|
2019-05-02 21:22:55 +08:00
|
|
|
Barrier start_init(2);
|
2019-04-24 09:47:30 +08:00
|
|
|
ThreadGroup threads;
|
2019-05-02 21:22:55 +08:00
|
|
|
threads.Create(test_obj.access_callback(
|
|
|
|
[&]() {
|
|
|
|
start_init.arrive_and_wait();
|
|
|
|
YieldAfterBarrier();
|
|
|
|
throw 42;
|
|
|
|
})
|
2019-04-24 09:47:30 +08:00
|
|
|
);
|
2019-05-02 21:22:55 +08:00
|
|
|
start_init.wait_for_threads(1);
|
2019-04-24 09:47:30 +08:00
|
|
|
|
|
|
|
bool already_init = false;
|
2019-05-02 21:22:55 +08:00
|
|
|
threads.CreateThreadsWithBarrier(num_waiters,
|
|
|
|
test_obj.access_callback([&]() {
|
|
|
|
assert(!already_init);
|
|
|
|
already_init = true;
|
|
|
|
})
|
|
|
|
);
|
2019-04-24 09:47:30 +08:00
|
|
|
// unblock the initializing thread
|
2019-05-02 21:22:55 +08:00
|
|
|
start_init.arrive_and_drop();
|
2019-04-24 09:47:30 +08:00
|
|
|
|
|
|
|
// wait for the other threads to finish initialization.
|
|
|
|
threads.JoinAll();
|
|
|
|
|
|
|
|
assert(test_obj.get_count(ABORTED) == 1);
|
|
|
|
assert(test_obj.get_count(PERFORMED) == 1);
|
2019-05-02 21:22:55 +08:00
|
|
|
assert(test_obj.get_count(WAITED) + test_obj.get_count(COMPLETE) == num_waiters - 1);
|
2019-04-24 09:47:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class GuardType, class Impl>
|
2019-05-02 21:22:55 +08:00
|
|
|
void test_completed_init(int num_waiters) {
|
2019-04-24 09:47:30 +08:00
|
|
|
|
|
|
|
FunctionLocalStatic<GuardType, Impl> test_obj;
|
|
|
|
|
2019-05-02 21:22:55 +08:00
|
|
|
test_obj.access([]() {}); // initialize the object
|
2019-04-24 09:47:30 +08:00
|
|
|
assert(test_obj.num_waiting() == 0);
|
|
|
|
assert(test_obj.num_completed() == 1);
|
|
|
|
assert(test_obj.get_count(PERFORMED) == 1);
|
|
|
|
|
|
|
|
ThreadGroup threads;
|
2019-05-02 21:22:55 +08:00
|
|
|
threads.CreateThreadsWithBarrier(num_waiters,
|
|
|
|
test_obj.access_callback([]() { assert(false); })
|
|
|
|
);
|
2019-04-24 09:47:30 +08:00
|
|
|
// wait for the other threads to finish initialization.
|
|
|
|
threads.JoinAll();
|
|
|
|
|
|
|
|
assert(test_obj.get_count(ABORTED) == 0);
|
|
|
|
assert(test_obj.get_count(PERFORMED) == 1);
|
|
|
|
assert(test_obj.get_count(WAITED) == 0);
|
2019-05-02 21:22:55 +08:00
|
|
|
assert(test_obj.get_count(COMPLETE) == num_waiters);
|
2019-04-24 09:47:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Impl>
|
|
|
|
void test_impl() {
|
2019-05-02 21:22:55 +08:00
|
|
|
using TestFn = void(*)(int);
|
|
|
|
TestFn TestList[] = {
|
|
|
|
test_free_for_all<uint32_t, Impl>,
|
|
|
|
test_free_for_all<uint32_t, Impl>,
|
|
|
|
test_waiting_for_init<uint32_t, Impl>,
|
|
|
|
test_waiting_for_init<uint64_t, Impl>,
|
|
|
|
test_aborted_init<uint32_t, Impl>,
|
|
|
|
test_aborted_init<uint64_t, Impl>,
|
|
|
|
test_completed_init<uint32_t, Impl>,
|
|
|
|
test_completed_init<uint64_t, Impl>
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto test_func : TestList) {
|
|
|
|
ThreadGroup test_threads;
|
|
|
|
test_threads.CreateThreadsWithBarrier(ConcurrentRunsPerTest, [=]() {
|
|
|
|
for (int I = 0; I < TestSamples; ++I) {
|
|
|
|
test_func(ThreadsPerTest);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
test_threads.JoinAll();
|
|
|
|
}
|
2019-04-24 09:47:30 +08:00
|
|
|
}
|
|
|
|
|
2019-04-24 12:21:05 +08:00
|
|
|
void test_all_impls() {
|
2021-08-11 22:47:48 +08:00
|
|
|
using MutexImpl = SelectImplementation<Implementation::GlobalMutex>::type;
|
2019-04-24 09:47:30 +08:00
|
|
|
|
|
|
|
// Attempt to test the Futex based implementation if it's supported on the
|
|
|
|
// target platform.
|
|
|
|
using RealFutexImpl = SelectImplementation<Implementation::Futex>::type;
|
|
|
|
using FutexImpl = typename std::conditional<
|
2019-04-24 12:21:05 +08:00
|
|
|
PlatformSupportsFutex(),
|
2019-04-24 09:47:30 +08:00
|
|
|
RealFutexImpl,
|
|
|
|
MutexImpl
|
|
|
|
>::type;
|
|
|
|
|
2019-05-02 21:22:55 +08:00
|
|
|
test_impl<MutexImpl>();
|
|
|
|
if (PlatformSupportsFutex())
|
|
|
|
test_impl<FutexImpl>();
|
2019-04-24 09:47:30 +08:00
|
|
|
}
|
2019-04-24 12:21:05 +08:00
|
|
|
|
|
|
|
// A dummy
|
|
|
|
template <bool Dummy = true>
|
|
|
|
void test_futex_syscall() {
|
|
|
|
if (!PlatformSupportsFutex())
|
|
|
|
return;
|
|
|
|
int lock1 = 0;
|
|
|
|
int lock2 = 0;
|
|
|
|
int lock3 = 0;
|
2020-11-24 04:52:03 +08:00
|
|
|
std::thread waiter1 = support::make_test_thread([&]() {
|
2019-04-24 12:21:05 +08:00
|
|
|
int expect = 0;
|
|
|
|
PlatformFutexWait(&lock1, expect);
|
|
|
|
assert(lock1 == 1);
|
|
|
|
});
|
2020-11-24 04:52:03 +08:00
|
|
|
std::thread waiter2 = support::make_test_thread([&]() {
|
2019-04-24 12:21:05 +08:00
|
|
|
int expect = 0;
|
|
|
|
PlatformFutexWait(&lock2, expect);
|
|
|
|
assert(lock2 == 2);
|
|
|
|
});
|
2020-11-24 04:52:03 +08:00
|
|
|
std::thread waiter3 = support::make_test_thread([&]() {
|
2019-04-24 12:21:05 +08:00
|
|
|
int expect = 42; // not the value
|
|
|
|
PlatformFutexWait(&lock3, expect); // doesn't block
|
|
|
|
});
|
2020-11-24 04:52:03 +08:00
|
|
|
std::thread waker = support::make_test_thread([&]() {
|
2019-04-24 12:21:05 +08:00
|
|
|
lock1 = 1;
|
|
|
|
PlatformFutexWake(&lock1);
|
|
|
|
lock2 = 2;
|
|
|
|
PlatformFutexWake(&lock2);
|
|
|
|
});
|
|
|
|
waiter1.join();
|
|
|
|
waiter2.join();
|
|
|
|
waiter3.join();
|
|
|
|
waker.join();
|
|
|
|
}
|
|
|
|
|
2020-10-09 01:36:33 +08:00
|
|
|
int main(int, char**) {
|
2019-04-24 12:21:05 +08:00
|
|
|
// Test each multi-threaded implementation with real threads.
|
|
|
|
test_all_impls();
|
|
|
|
// Test the basic sanity of the futex syscall wrappers.
|
|
|
|
test_futex_syscall();
|
2020-10-09 01:36:33 +08:00
|
|
|
|
|
|
|
return 0;
|
2019-04-24 12:21:05 +08:00
|
|
|
}
|