blob: b0307d1f16a4eb145e6697d99b26cb2a7dfc01ce [file] [log] [blame]
Mehdi Amini33a7ea42015-12-15 00:59:19 +00001//========- unittests/Support/ThreadPools.cpp - ThreadPools.h tests --========//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Support/ThreadPool.h"
11
12#include "llvm/ADT/STLExtras.h"
Mehdi Amini942e52c2015-12-15 09:10:25 +000013#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/Triple.h"
15#include "llvm/Support/Host.h"
16#include "llvm/Support/TargetSelect.h"
Mehdi Amini33a7ea42015-12-15 00:59:19 +000017
18#include "gtest/gtest.h"
19
20using namespace llvm;
Mehdi Amini33a7ea42015-12-15 00:59:19 +000021
Mehdi Amini942e52c2015-12-15 09:10:25 +000022// Fixture for the unittests, allowing to *temporarily* disable the unittests
23// on a particular platform
24class ThreadPoolTest : public testing::Test {
25 Triple Host;
26 SmallVector<Triple::ArchType, 4> UnsupportedArchs;
27 SmallVector<Triple::OSType, 4> UnsupportedOSs;
28 SmallVector<Triple::EnvironmentType, 1> UnsupportedEnvironments;
29protected:
30 // This is intended for platform as a temporary "XFAIL"
31 bool isUnsupportedOSOrEnvironment() {
32 Triple Host(Triple::normalize(sys::getProcessTriple()));
33
34 if (std::find(UnsupportedEnvironments.begin(), UnsupportedEnvironments.end(),
35 Host.getEnvironment()) != UnsupportedEnvironments.end())
36 return true;
37
38 if (std::find(UnsupportedOSs.begin(), UnsupportedOSs.end(), Host.getOS())
39 != UnsupportedOSs.end())
40 return true;
41
42 if (std::find(UnsupportedArchs.begin(), UnsupportedArchs.end(), Host.getArch())
43 != UnsupportedArchs.end())
44 return true;
45
46 return false;
47 }
48
49 ThreadPoolTest() {
50 // Add unsupported configuration here, example:
51 // UnsupportedArchs.push_back(Triple::x86_64);
Mehdi Amini4b8d75b2015-12-15 09:10:28 +000052
53 // See https://llvm.org/bugs/show_bug.cgi?id=25829
54 UnsupportedArchs.push_back(Triple::ppc64le);
55 UnsupportedArchs.push_back(Triple::ppc64);
Mehdi Amini942e52c2015-12-15 09:10:25 +000056 }
Mehdi Amini0129fca2015-12-19 05:12:07 +000057
58 /// Make sure this thread not progress faster than the main thread.
59 void waitForMainThread() {
Vedant Kumar3791e3d2015-12-19 09:54:27 +000060 std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
61 WaitMainThread.wait(LockGuard, [&] { return MainThreadReady; });
Mehdi Amini0129fca2015-12-19 05:12:07 +000062 }
Vedant Kumar2cf75332015-12-19 09:49:09 +000063
64 /// Set the readiness of the main thread.
65 void setMainThreadReadyState(bool Ready) {
66 std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
67 MainThreadReady = Ready;
68 WaitMainThread.notify_all();
69 }
70
Mehdi Amini0129fca2015-12-19 05:12:07 +000071 std::condition_variable WaitMainThread;
72 std::mutex WaitMainThreadMutex;
73 bool MainThreadReady;
74
Mehdi Amini942e52c2015-12-15 09:10:25 +000075};
76
77#define CHECK_UNSUPPORTED() \
78 do { \
79 if (isUnsupportedOSOrEnvironment()) \
80 return; \
81 } while (0); \
82
83TEST_F(ThreadPoolTest, AsyncBarrier) {
84 CHECK_UNSUPPORTED();
Mehdi Amini33a7ea42015-12-15 00:59:19 +000085 // test that async & barrier work together properly.
86
87 std::atomic_int checked_in{0};
88
Vedant Kumar2cf75332015-12-19 09:49:09 +000089 setMainThreadReadyState(false);
Mehdi Amini33a7ea42015-12-15 00:59:19 +000090 ThreadPool Pool;
91 for (size_t i = 0; i < 5; ++i) {
Mehdi Amini0129fca2015-12-19 05:12:07 +000092 Pool.async([this, &checked_in, i] {
93 waitForMainThread();
Mehdi Amini33a7ea42015-12-15 00:59:19 +000094 ++checked_in;
95 });
96 }
Mehdi Amini0129fca2015-12-19 05:12:07 +000097 ASSERT_EQ(0, checked_in);
Vedant Kumar2cf75332015-12-19 09:49:09 +000098 setMainThreadReadyState(true);
Mehdi Amini33a7ea42015-12-15 00:59:19 +000099 Pool.wait();
100 ASSERT_EQ(5, checked_in);
101}
102
Teresa Johnsonf064d622015-12-15 04:44:02 +0000103static void TestFunc(std::atomic_int &checked_in, int i) { checked_in += i; }
104
Mehdi Amini942e52c2015-12-15 09:10:25 +0000105TEST_F(ThreadPoolTest, AsyncBarrierArgs) {
106 CHECK_UNSUPPORTED();
Teresa Johnsonf064d622015-12-15 04:44:02 +0000107 // Test that async works with a function requiring multiple parameters.
108 std::atomic_int checked_in{0};
109
110 ThreadPool Pool;
111 for (size_t i = 0; i < 5; ++i) {
112 Pool.async(TestFunc, std::ref(checked_in), i);
113 }
114 Pool.wait();
115 ASSERT_EQ(10, checked_in);
116}
117
Mehdi Amini942e52c2015-12-15 09:10:25 +0000118TEST_F(ThreadPoolTest, Async) {
119 CHECK_UNSUPPORTED();
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000120 ThreadPool Pool;
121 std::atomic_int i{0};
Vedant Kumar2cf75332015-12-19 09:49:09 +0000122 setMainThreadReadyState(false);
Mehdi Amini0129fca2015-12-19 05:12:07 +0000123 Pool.async([this, &i] {
124 waitForMainThread();
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000125 ++i;
126 });
127 Pool.async([&i] { ++i; });
Mehdi Amini0129fca2015-12-19 05:12:07 +0000128 ASSERT_NE(2, i.load());
Vedant Kumar2cf75332015-12-19 09:49:09 +0000129 setMainThreadReadyState(true);
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000130 Pool.wait();
131 ASSERT_EQ(2, i.load());
132}
133
Mehdi Amini942e52c2015-12-15 09:10:25 +0000134TEST_F(ThreadPoolTest, GetFuture) {
135 CHECK_UNSUPPORTED();
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000136 ThreadPool Pool;
137 std::atomic_int i{0};
Vedant Kumar2cf75332015-12-19 09:49:09 +0000138 setMainThreadReadyState(false);
Mehdi Amini0129fca2015-12-19 05:12:07 +0000139 Pool.async([this, &i] {
140 waitForMainThread();
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000141 ++i;
142 });
143 // Force the future using get()
144 Pool.async([&i] { ++i; }).get();
Mehdi Amini0129fca2015-12-19 05:12:07 +0000145 ASSERT_NE(2, i.load());
Vedant Kumar2cf75332015-12-19 09:49:09 +0000146 setMainThreadReadyState(true);
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000147 Pool.wait();
148 ASSERT_EQ(2, i.load());
149}
150
Mehdi Amini942e52c2015-12-15 09:10:25 +0000151TEST_F(ThreadPoolTest, PoolDestruction) {
152 CHECK_UNSUPPORTED();
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000153 // Test that we are waiting on destruction
154 std::atomic_int checked_in{0};
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000155 {
Vedant Kumar2cf75332015-12-19 09:49:09 +0000156 setMainThreadReadyState(false);
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000157 ThreadPool Pool;
158 for (size_t i = 0; i < 5; ++i) {
Mehdi Amini0129fca2015-12-19 05:12:07 +0000159 Pool.async([this, &checked_in, i] {
160 waitForMainThread();
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000161 ++checked_in;
162 });
163 }
Mehdi Amini0129fca2015-12-19 05:12:07 +0000164 ASSERT_EQ(0, checked_in);
Vedant Kumar2cf75332015-12-19 09:49:09 +0000165 setMainThreadReadyState(true);
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000166 }
167 ASSERT_EQ(5, checked_in);
168}