1 //========- unittests/Support/ThreadPools.cpp - ThreadPools.h tests --========//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/ThreadPool.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/Support/Host.h"
16 #include "llvm/Support/TargetSelect.h"
18 #include "gtest/gtest.h"
22 // Fixture for the unittests, allowing to *temporarily* disable the unittests
23 // on a particular platform
24 class ThreadPoolTest : public testing::Test {
26 SmallVector<Triple::ArchType, 4> UnsupportedArchs;
27 SmallVector<Triple::OSType, 4> UnsupportedOSs;
28 SmallVector<Triple::EnvironmentType, 1> UnsupportedEnvironments;
30 // This is intended for platform as a temporary "XFAIL"
31 bool isUnsupportedOSOrEnvironment() {
32 Triple Host(Triple::normalize(sys::getProcessTriple()));
34 if (find(UnsupportedEnvironments, Host.getEnvironment()) !=
35 UnsupportedEnvironments.end())
38 if (is_contained(UnsupportedOSs, Host.getOS()))
41 if (is_contained(UnsupportedArchs, Host.getArch()))
48 // Add unsupported configuration here, example:
49 // UnsupportedArchs.push_back(Triple::x86_64);
51 // See https://llvm.org/bugs/show_bug.cgi?id=25829
52 UnsupportedArchs.push_back(Triple::ppc64le);
53 UnsupportedArchs.push_back(Triple::ppc64);
56 /// Make sure this thread not progress faster than the main thread.
57 void waitForMainThread() {
58 std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
59 WaitMainThread.wait(LockGuard, [&] { return MainThreadReady; });
62 /// Set the readiness of the main thread.
63 void setMainThreadReady() {
65 std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
66 MainThreadReady = true;
68 WaitMainThread.notify_all();
71 void SetUp() override { MainThreadReady = false; }
73 std::condition_variable WaitMainThread;
74 std::mutex WaitMainThreadMutex;
79 #define CHECK_UNSUPPORTED() \
81 if (isUnsupportedOSOrEnvironment()) \
85 TEST_F(ThreadPoolTest, AsyncBarrier) {
87 // test that async & barrier work together properly.
89 std::atomic_int checked_in{0};
92 for (size_t i = 0; i < 5; ++i) {
93 Pool.async([this, &checked_in] {
98 ASSERT_EQ(0, checked_in);
101 ASSERT_EQ(5, checked_in);
104 static void TestFunc(std::atomic_int &checked_in, int i) { checked_in += i; }
106 TEST_F(ThreadPoolTest, AsyncBarrierArgs) {
108 // Test that async works with a function requiring multiple parameters.
109 std::atomic_int checked_in{0};
112 for (size_t i = 0; i < 5; ++i) {
113 Pool.async(TestFunc, std::ref(checked_in), i);
116 ASSERT_EQ(10, checked_in);
119 TEST_F(ThreadPoolTest, Async) {
122 std::atomic_int i{0};
123 Pool.async([this, &i] {
127 Pool.async([&i] { ++i; });
128 ASSERT_NE(2, i.load());
129 setMainThreadReady();
131 ASSERT_EQ(2, i.load());
134 TEST_F(ThreadPoolTest, GetFuture) {
137 std::atomic_int i{0};
138 Pool.async([this, &i] {
142 // Force the future using get()
143 Pool.async([&i] { ++i; }).get();
144 ASSERT_NE(2, i.load());
145 setMainThreadReady();
147 ASSERT_EQ(2, i.load());
150 TEST_F(ThreadPoolTest, PoolDestruction) {
152 // Test that we are waiting on destruction
153 std::atomic_int checked_in{0};
156 for (size_t i = 0; i < 5; ++i) {
157 Pool.async([this, &checked_in] {
162 ASSERT_EQ(0, checked_in);
163 setMainThreadReady();
165 ASSERT_EQ(5, checked_in);