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);
67 #define CHECK_UNSUPPORTED() \
69 if (isUnsupportedOSOrEnvironment()) \
73 TEST_F(ThreadPoolTest, AsyncBarrier) {
75 // test that async & barrier work together properly.
77 std::atomic_int checked_in{0};
80 for (size_t i = 0; i < 5; ++i) {
81 Pool.async([&checked_in, i] {
86 ASSERT_EQ(0, checked_in);
88 ASSERT_EQ(5, checked_in);
91 static void TestFunc(std::atomic_int &checked_in, int i) { checked_in += i; }
93 TEST_F(ThreadPoolTest, AsyncBarrierArgs) {
95 // Test that async works with a function requiring multiple parameters.
96 std::atomic_int checked_in{0};
99 for (size_t i = 0; i < 5; ++i) {
100 Pool.async(TestFunc, std::ref(checked_in), i);
103 ASSERT_EQ(10, checked_in);
106 TEST_F(ThreadPoolTest, Async) {
109 std::atomic_int i{0};
110 // sleep here just to ensure that the not-equal is correct.
115 Pool.async([&i] { ++i; });
116 ASSERT_NE(2, i.load());
118 ASSERT_EQ(2, i.load());
121 TEST_F(ThreadPoolTest, GetFuture) {
124 std::atomic_int i{0};
125 // sleep here just to ensure that the not-equal is correct.
130 // Force the future using get()
131 Pool.async([&i] { ++i; }).get();
132 ASSERT_NE(2, i.load());
134 ASSERT_EQ(2, i.load());
137 TEST_F(ThreadPoolTest, PoolDestruction) {
139 // Test that we are waiting on destruction
140 std::atomic_int checked_in{0};
144 for (size_t i = 0; i < 5; ++i) {
145 Pool.async([&checked_in, i] {
150 ASSERT_EQ(0, checked_in);
152 ASSERT_EQ(5, checked_in);