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 (std::find(UnsupportedEnvironments.begin(), UnsupportedEnvironments.end(),
35 Host.getEnvironment()) != UnsupportedEnvironments.end())
38 if (std::find(UnsupportedOSs.begin(), UnsupportedOSs.end(), Host.getOS())
39 != UnsupportedOSs.end())
42 if (std::find(UnsupportedArchs.begin(), UnsupportedArchs.end(), Host.getArch())
43 != UnsupportedArchs.end())
50 // Add unsupported configuration here, example:
51 // UnsupportedArchs.push_back(Triple::x86_64);
53 // See https://llvm.org/bugs/show_bug.cgi?id=25829
54 UnsupportedArchs.push_back(Triple::ppc64le);
55 UnsupportedArchs.push_back(Triple::ppc64);
58 /// Make sure this thread not progress faster than the main thread.
59 void waitForMainThread() {
60 while (!MainThreadReady) {
61 std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
62 WaitMainThread.wait(LockGuard, [&] { return MainThreadReady; });
65 std::condition_variable WaitMainThread;
66 std::mutex WaitMainThreadMutex;
71 #define CHECK_UNSUPPORTED() \
73 if (isUnsupportedOSOrEnvironment()) \
77 TEST_F(ThreadPoolTest, AsyncBarrier) {
79 // test that async & barrier work together properly.
81 std::atomic_int checked_in{0};
83 MainThreadReady = false;
85 for (size_t i = 0; i < 5; ++i) {
86 Pool.async([this, &checked_in, i] {
91 ASSERT_EQ(0, checked_in);
92 MainThreadReady = true;
93 WaitMainThread.notify_all();
95 ASSERT_EQ(5, checked_in);
98 static void TestFunc(std::atomic_int &checked_in, int i) { checked_in += i; }
100 TEST_F(ThreadPoolTest, AsyncBarrierArgs) {
102 // Test that async works with a function requiring multiple parameters.
103 std::atomic_int checked_in{0};
106 for (size_t i = 0; i < 5; ++i) {
107 Pool.async(TestFunc, std::ref(checked_in), i);
110 ASSERT_EQ(10, checked_in);
113 TEST_F(ThreadPoolTest, Async) {
116 std::atomic_int i{0};
117 MainThreadReady = false;
118 Pool.async([this, &i] {
122 Pool.async([&i] { ++i; });
123 ASSERT_NE(2, i.load());
124 MainThreadReady = true;
125 WaitMainThread.notify_all();
127 ASSERT_EQ(2, i.load());
130 TEST_F(ThreadPoolTest, GetFuture) {
133 std::atomic_int i{0};
134 MainThreadReady = false;
135 Pool.async([this, &i] {
139 // Force the future using get()
140 Pool.async([&i] { ++i; }).get();
141 ASSERT_NE(2, i.load());
142 MainThreadReady = true;
143 WaitMainThread.notify_all();
145 ASSERT_EQ(2, i.load());
148 TEST_F(ThreadPoolTest, PoolDestruction) {
150 // Test that we are waiting on destruction
151 std::atomic_int checked_in{0};
153 MainThreadReady = false;
155 for (size_t i = 0; i < 5; ++i) {
156 Pool.async([this, &checked_in, i] {
161 ASSERT_EQ(0, checked_in);
162 MainThreadReady = true;
163 WaitMainThread.notify_all();
165 ASSERT_EQ(5, checked_in);