Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 1 | //========- 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 Amini | 942e52c | 2015-12-15 09:10:25 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallVector.h" |
| 14 | #include "llvm/ADT/Triple.h" |
| 15 | #include "llvm/Support/Host.h" |
| 16 | #include "llvm/Support/TargetSelect.h" |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 17 | |
| 18 | #include "gtest/gtest.h" |
| 19 | |
| 20 | using namespace llvm; |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 21 | |
Mehdi Amini | 942e52c | 2015-12-15 09:10:25 +0000 | [diff] [blame] | 22 | // Fixture for the unittests, allowing to *temporarily* disable the unittests |
| 23 | // on a particular platform |
| 24 | class 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; |
| 29 | protected: |
| 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 Amini | 4b8d75b | 2015-12-15 09:10:28 +0000 | [diff] [blame] | 52 | |
| 53 | // See https://llvm.org/bugs/show_bug.cgi?id=25829 |
| 54 | UnsupportedArchs.push_back(Triple::ppc64le); |
| 55 | UnsupportedArchs.push_back(Triple::ppc64); |
Mehdi Amini | 942e52c | 2015-12-15 09:10:25 +0000 | [diff] [blame] | 56 | } |
Mehdi Amini | 0129fca | 2015-12-19 05:12:07 +0000 | [diff] [blame] | 57 | |
| 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; }); |
| 63 | } |
| 64 | } |
Vedant Kumar | 2cf7533 | 2015-12-19 09:49:09 +0000 | [diff] [blame^] | 65 | |
| 66 | /// Set the readiness of the main thread. |
| 67 | void setMainThreadReadyState(bool Ready) { |
| 68 | std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex); |
| 69 | MainThreadReady = Ready; |
| 70 | WaitMainThread.notify_all(); |
| 71 | } |
| 72 | |
Mehdi Amini | 0129fca | 2015-12-19 05:12:07 +0000 | [diff] [blame] | 73 | std::condition_variable WaitMainThread; |
| 74 | std::mutex WaitMainThreadMutex; |
| 75 | bool MainThreadReady; |
| 76 | |
Mehdi Amini | 942e52c | 2015-12-15 09:10:25 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | #define CHECK_UNSUPPORTED() \ |
| 80 | do { \ |
| 81 | if (isUnsupportedOSOrEnvironment()) \ |
| 82 | return; \ |
| 83 | } while (0); \ |
| 84 | |
| 85 | TEST_F(ThreadPoolTest, AsyncBarrier) { |
| 86 | CHECK_UNSUPPORTED(); |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 87 | // test that async & barrier work together properly. |
| 88 | |
| 89 | std::atomic_int checked_in{0}; |
| 90 | |
Vedant Kumar | 2cf7533 | 2015-12-19 09:49:09 +0000 | [diff] [blame^] | 91 | setMainThreadReadyState(false); |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 92 | ThreadPool Pool; |
| 93 | for (size_t i = 0; i < 5; ++i) { |
Mehdi Amini | 0129fca | 2015-12-19 05:12:07 +0000 | [diff] [blame] | 94 | Pool.async([this, &checked_in, i] { |
| 95 | waitForMainThread(); |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 96 | ++checked_in; |
| 97 | }); |
| 98 | } |
Mehdi Amini | 0129fca | 2015-12-19 05:12:07 +0000 | [diff] [blame] | 99 | ASSERT_EQ(0, checked_in); |
Vedant Kumar | 2cf7533 | 2015-12-19 09:49:09 +0000 | [diff] [blame^] | 100 | setMainThreadReadyState(true); |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 101 | Pool.wait(); |
| 102 | ASSERT_EQ(5, checked_in); |
| 103 | } |
| 104 | |
Teresa Johnson | f064d62 | 2015-12-15 04:44:02 +0000 | [diff] [blame] | 105 | static void TestFunc(std::atomic_int &checked_in, int i) { checked_in += i; } |
| 106 | |
Mehdi Amini | 942e52c | 2015-12-15 09:10:25 +0000 | [diff] [blame] | 107 | TEST_F(ThreadPoolTest, AsyncBarrierArgs) { |
| 108 | CHECK_UNSUPPORTED(); |
Teresa Johnson | f064d62 | 2015-12-15 04:44:02 +0000 | [diff] [blame] | 109 | // Test that async works with a function requiring multiple parameters. |
| 110 | std::atomic_int checked_in{0}; |
| 111 | |
| 112 | ThreadPool Pool; |
| 113 | for (size_t i = 0; i < 5; ++i) { |
| 114 | Pool.async(TestFunc, std::ref(checked_in), i); |
| 115 | } |
| 116 | Pool.wait(); |
| 117 | ASSERT_EQ(10, checked_in); |
| 118 | } |
| 119 | |
Mehdi Amini | 942e52c | 2015-12-15 09:10:25 +0000 | [diff] [blame] | 120 | TEST_F(ThreadPoolTest, Async) { |
| 121 | CHECK_UNSUPPORTED(); |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 122 | ThreadPool Pool; |
| 123 | std::atomic_int i{0}; |
Vedant Kumar | 2cf7533 | 2015-12-19 09:49:09 +0000 | [diff] [blame^] | 124 | setMainThreadReadyState(false); |
Mehdi Amini | 0129fca | 2015-12-19 05:12:07 +0000 | [diff] [blame] | 125 | Pool.async([this, &i] { |
| 126 | waitForMainThread(); |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 127 | ++i; |
| 128 | }); |
| 129 | Pool.async([&i] { ++i; }); |
Mehdi Amini | 0129fca | 2015-12-19 05:12:07 +0000 | [diff] [blame] | 130 | ASSERT_NE(2, i.load()); |
Vedant Kumar | 2cf7533 | 2015-12-19 09:49:09 +0000 | [diff] [blame^] | 131 | setMainThreadReadyState(true); |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 132 | Pool.wait(); |
| 133 | ASSERT_EQ(2, i.load()); |
| 134 | } |
| 135 | |
Mehdi Amini | 942e52c | 2015-12-15 09:10:25 +0000 | [diff] [blame] | 136 | TEST_F(ThreadPoolTest, GetFuture) { |
| 137 | CHECK_UNSUPPORTED(); |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 138 | ThreadPool Pool; |
| 139 | std::atomic_int i{0}; |
Vedant Kumar | 2cf7533 | 2015-12-19 09:49:09 +0000 | [diff] [blame^] | 140 | setMainThreadReadyState(false); |
Mehdi Amini | 0129fca | 2015-12-19 05:12:07 +0000 | [diff] [blame] | 141 | Pool.async([this, &i] { |
| 142 | waitForMainThread(); |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 143 | ++i; |
| 144 | }); |
| 145 | // Force the future using get() |
| 146 | Pool.async([&i] { ++i; }).get(); |
Mehdi Amini | 0129fca | 2015-12-19 05:12:07 +0000 | [diff] [blame] | 147 | ASSERT_NE(2, i.load()); |
Vedant Kumar | 2cf7533 | 2015-12-19 09:49:09 +0000 | [diff] [blame^] | 148 | setMainThreadReadyState(true); |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 149 | Pool.wait(); |
| 150 | ASSERT_EQ(2, i.load()); |
| 151 | } |
| 152 | |
Mehdi Amini | 942e52c | 2015-12-15 09:10:25 +0000 | [diff] [blame] | 153 | TEST_F(ThreadPoolTest, PoolDestruction) { |
| 154 | CHECK_UNSUPPORTED(); |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 155 | // Test that we are waiting on destruction |
| 156 | std::atomic_int checked_in{0}; |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 157 | { |
Vedant Kumar | 2cf7533 | 2015-12-19 09:49:09 +0000 | [diff] [blame^] | 158 | setMainThreadReadyState(false); |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 159 | ThreadPool Pool; |
| 160 | for (size_t i = 0; i < 5; ++i) { |
Mehdi Amini | 0129fca | 2015-12-19 05:12:07 +0000 | [diff] [blame] | 161 | Pool.async([this, &checked_in, i] { |
| 162 | waitForMainThread(); |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 163 | ++checked_in; |
| 164 | }); |
| 165 | } |
Mehdi Amini | 0129fca | 2015-12-19 05:12:07 +0000 | [diff] [blame] | 166 | ASSERT_EQ(0, checked_in); |
Vedant Kumar | 2cf7533 | 2015-12-19 09:49:09 +0000 | [diff] [blame^] | 167 | setMainThreadReadyState(true); |
Mehdi Amini | 33a7ea4 | 2015-12-15 00:59:19 +0000 | [diff] [blame] | 168 | } |
| 169 | ASSERT_EQ(5, checked_in); |
| 170 | } |