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"
21 using namespace std::chrono;
23 /// Try best to make this thread not progress faster than the main thread
25 #ifdef LLVM_ENABLE_THREADS
26 std::this_thread::yield();
28 std::this_thread::sleep_for(milliseconds(200));
29 #ifdef LLVM_ENABLE_THREADS
30 std::this_thread::yield();
34 // Fixture for the unittests, allowing to *temporarily* disable the unittests
35 // on a particular platform
36 class ThreadPoolTest : public testing::Test {
38 SmallVector<Triple::ArchType, 4> UnsupportedArchs;
39 SmallVector<Triple::OSType, 4> UnsupportedOSs;
40 SmallVector<Triple::EnvironmentType, 1> UnsupportedEnvironments;
42 // This is intended for platform as a temporary "XFAIL"
43 bool isUnsupportedOSOrEnvironment() {
44 Triple Host(Triple::normalize(sys::getProcessTriple()));
46 if (std::find(UnsupportedEnvironments.begin(), UnsupportedEnvironments.end(),
47 Host.getEnvironment()) != UnsupportedEnvironments.end())
50 if (std::find(UnsupportedOSs.begin(), UnsupportedOSs.end(), Host.getOS())
51 != UnsupportedOSs.end())
54 if (std::find(UnsupportedArchs.begin(), UnsupportedArchs.end(), Host.getArch())
55 != UnsupportedArchs.end())
62 // Add unsupported configuration here, example:
63 // UnsupportedArchs.push_back(Triple::x86_64);
65 // See https://llvm.org/bugs/show_bug.cgi?id=25829
66 UnsupportedArchs.push_back(Triple::ppc64le);
67 UnsupportedArchs.push_back(Triple::ppc64);
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};
84 for (size_t i = 0; i < 5; ++i) {
85 Pool.async([&checked_in, i] {
90 ASSERT_EQ(0, checked_in);
92 ASSERT_EQ(5, checked_in);
95 static void TestFunc(std::atomic_int &checked_in, int i) { checked_in += i; }
97 TEST_F(ThreadPoolTest, AsyncBarrierArgs) {
99 // Test that async works with a function requiring multiple parameters.
100 std::atomic_int checked_in{0};
103 for (size_t i = 0; i < 5; ++i) {
104 Pool.async(TestFunc, std::ref(checked_in), i);
107 ASSERT_EQ(10, checked_in);
110 TEST_F(ThreadPoolTest, Async) {
113 std::atomic_int i{0};
114 // sleep here just to ensure that the not-equal is correct.
119 Pool.async([&i] { ++i; });
120 ASSERT_NE(2, i.load());
122 ASSERT_EQ(2, i.load());
125 TEST_F(ThreadPoolTest, GetFuture) {
128 std::atomic_int i{0};
129 // sleep here just to ensure that the not-equal is correct.
134 // Force the future using get()
135 Pool.async([&i] { ++i; }).get();
136 ASSERT_NE(2, i.load());
138 ASSERT_EQ(2, i.load());
141 TEST_F(ThreadPoolTest, PoolDestruction) {
143 // Test that we are waiting on destruction
144 std::atomic_int checked_in{0};
148 for (size_t i = 0; i < 5; ++i) {
149 Pool.async([&checked_in, i] {
154 ASSERT_EQ(0, checked_in);
156 ASSERT_EQ(5, checked_in);