2015-12-15 08:59:19 +08:00
|
|
|
//========- unittests/Support/ThreadPools.cpp - ThreadPools.h tests --========//
|
|
|
|
//
|
2019-01-19 16:50:56 +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
|
2015-12-15 08:59:19 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/ThreadPool.h"
|
|
|
|
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-12-15 17:10:25 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
#include "llvm/Support/Host.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2015-12-15 08:59:19 +08:00
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-12-15 17:10:25 +08:00
|
|
|
// Fixture for the unittests, allowing to *temporarily* disable the unittests
|
|
|
|
// on a particular platform
|
|
|
|
class ThreadPoolTest : public testing::Test {
|
|
|
|
Triple Host;
|
|
|
|
SmallVector<Triple::ArchType, 4> UnsupportedArchs;
|
|
|
|
SmallVector<Triple::OSType, 4> UnsupportedOSs;
|
|
|
|
SmallVector<Triple::EnvironmentType, 1> UnsupportedEnvironments;
|
|
|
|
protected:
|
|
|
|
// This is intended for platform as a temporary "XFAIL"
|
|
|
|
bool isUnsupportedOSOrEnvironment() {
|
|
|
|
Triple Host(Triple::normalize(sys::getProcessTriple()));
|
|
|
|
|
2016-08-12 06:21:41 +08:00
|
|
|
if (find(UnsupportedEnvironments, Host.getEnvironment()) !=
|
|
|
|
UnsupportedEnvironments.end())
|
2015-12-15 17:10:25 +08:00
|
|
|
return true;
|
|
|
|
|
2016-08-12 06:21:41 +08:00
|
|
|
if (is_contained(UnsupportedOSs, Host.getOS()))
|
2015-12-15 17:10:25 +08:00
|
|
|
return true;
|
|
|
|
|
2016-08-12 06:21:41 +08:00
|
|
|
if (is_contained(UnsupportedArchs, Host.getArch()))
|
2015-12-15 17:10:25 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadPoolTest() {
|
|
|
|
// Add unsupported configuration here, example:
|
|
|
|
// UnsupportedArchs.push_back(Triple::x86_64);
|
2015-12-15 17:10:28 +08:00
|
|
|
|
|
|
|
// See https://llvm.org/bugs/show_bug.cgi?id=25829
|
|
|
|
UnsupportedArchs.push_back(Triple::ppc64le);
|
|
|
|
UnsupportedArchs.push_back(Triple::ppc64);
|
2015-12-15 17:10:25 +08:00
|
|
|
}
|
2015-12-19 13:12:07 +08:00
|
|
|
|
|
|
|
/// Make sure this thread not progress faster than the main thread.
|
|
|
|
void waitForMainThread() {
|
2015-12-19 17:54:27 +08:00
|
|
|
std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
|
|
|
|
WaitMainThread.wait(LockGuard, [&] { return MainThreadReady; });
|
2015-12-19 13:12:07 +08:00
|
|
|
}
|
2015-12-19 17:49:09 +08:00
|
|
|
|
|
|
|
/// Set the readiness of the main thread.
|
2015-12-20 06:56:24 +08:00
|
|
|
void setMainThreadReady() {
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
|
|
|
|
MainThreadReady = true;
|
|
|
|
}
|
2015-12-19 17:49:09 +08:00
|
|
|
WaitMainThread.notify_all();
|
|
|
|
}
|
|
|
|
|
2015-12-20 06:56:24 +08:00
|
|
|
void SetUp() override { MainThreadReady = false; }
|
|
|
|
|
2015-12-19 13:12:07 +08:00
|
|
|
std::condition_variable WaitMainThread;
|
|
|
|
std::mutex WaitMainThreadMutex;
|
2019-11-18 20:05:40 +08:00
|
|
|
bool MainThreadReady = false;
|
2015-12-15 17:10:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define CHECK_UNSUPPORTED() \
|
|
|
|
do { \
|
|
|
|
if (isUnsupportedOSOrEnvironment()) \
|
|
|
|
return; \
|
|
|
|
} while (0); \
|
|
|
|
|
|
|
|
TEST_F(ThreadPoolTest, AsyncBarrier) {
|
|
|
|
CHECK_UNSUPPORTED();
|
2015-12-15 08:59:19 +08:00
|
|
|
// test that async & barrier work together properly.
|
|
|
|
|
|
|
|
std::atomic_int checked_in{0};
|
|
|
|
|
|
|
|
ThreadPool Pool;
|
|
|
|
for (size_t i = 0; i < 5; ++i) {
|
2017-01-14 01:12:16 +08:00
|
|
|
Pool.async([this, &checked_in] {
|
2015-12-19 13:12:07 +08:00
|
|
|
waitForMainThread();
|
2015-12-15 08:59:19 +08:00
|
|
|
++checked_in;
|
|
|
|
});
|
|
|
|
}
|
2015-12-19 13:12:07 +08:00
|
|
|
ASSERT_EQ(0, checked_in);
|
2015-12-20 06:56:24 +08:00
|
|
|
setMainThreadReady();
|
2015-12-15 08:59:19 +08:00
|
|
|
Pool.wait();
|
|
|
|
ASSERT_EQ(5, checked_in);
|
|
|
|
}
|
|
|
|
|
2015-12-15 12:44:02 +08:00
|
|
|
static void TestFunc(std::atomic_int &checked_in, int i) { checked_in += i; }
|
|
|
|
|
2015-12-15 17:10:25 +08:00
|
|
|
TEST_F(ThreadPoolTest, AsyncBarrierArgs) {
|
|
|
|
CHECK_UNSUPPORTED();
|
2015-12-15 12:44:02 +08:00
|
|
|
// Test that async works with a function requiring multiple parameters.
|
|
|
|
std::atomic_int checked_in{0};
|
|
|
|
|
|
|
|
ThreadPool Pool;
|
|
|
|
for (size_t i = 0; i < 5; ++i) {
|
|
|
|
Pool.async(TestFunc, std::ref(checked_in), i);
|
|
|
|
}
|
|
|
|
Pool.wait();
|
|
|
|
ASSERT_EQ(10, checked_in);
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:10:25 +08:00
|
|
|
TEST_F(ThreadPoolTest, Async) {
|
|
|
|
CHECK_UNSUPPORTED();
|
2015-12-15 08:59:19 +08:00
|
|
|
ThreadPool Pool;
|
|
|
|
std::atomic_int i{0};
|
2015-12-19 13:12:07 +08:00
|
|
|
Pool.async([this, &i] {
|
|
|
|
waitForMainThread();
|
2015-12-15 08:59:19 +08:00
|
|
|
++i;
|
|
|
|
});
|
|
|
|
Pool.async([&i] { ++i; });
|
2015-12-19 13:12:07 +08:00
|
|
|
ASSERT_NE(2, i.load());
|
2015-12-20 06:56:24 +08:00
|
|
|
setMainThreadReady();
|
2015-12-15 08:59:19 +08:00
|
|
|
Pool.wait();
|
|
|
|
ASSERT_EQ(2, i.load());
|
|
|
|
}
|
|
|
|
|
2016-11-28 17:17:12 +08:00
|
|
|
TEST_F(ThreadPoolTest, GetFuture) {
|
|
|
|
CHECK_UNSUPPORTED();
|
|
|
|
ThreadPool Pool{2};
|
|
|
|
std::atomic_int i{0};
|
|
|
|
Pool.async([this, &i] {
|
|
|
|
waitForMainThread();
|
|
|
|
++i;
|
|
|
|
});
|
|
|
|
// Force the future using get()
|
|
|
|
Pool.async([&i] { ++i; }).get();
|
|
|
|
ASSERT_NE(2, i.load());
|
|
|
|
setMainThreadReady();
|
|
|
|
Pool.wait();
|
|
|
|
ASSERT_EQ(2, i.load());
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:10:25 +08:00
|
|
|
TEST_F(ThreadPoolTest, PoolDestruction) {
|
|
|
|
CHECK_UNSUPPORTED();
|
2015-12-15 08:59:19 +08:00
|
|
|
// Test that we are waiting on destruction
|
|
|
|
std::atomic_int checked_in{0};
|
|
|
|
{
|
|
|
|
ThreadPool Pool;
|
|
|
|
for (size_t i = 0; i < 5; ++i) {
|
2017-01-14 01:12:16 +08:00
|
|
|
Pool.async([this, &checked_in] {
|
2015-12-19 13:12:07 +08:00
|
|
|
waitForMainThread();
|
2015-12-15 08:59:19 +08:00
|
|
|
++checked_in;
|
|
|
|
});
|
|
|
|
}
|
2015-12-19 13:12:07 +08:00
|
|
|
ASSERT_EQ(0, checked_in);
|
2015-12-20 06:56:24 +08:00
|
|
|
setMainThreadReady();
|
2015-12-15 08:59:19 +08:00
|
|
|
}
|
|
|
|
ASSERT_EQ(5, checked_in);
|
|
|
|
}
|