Remove possibility of failures to due race in ThreadPool unittest
[oota-llvm.git] / unittests / Support / ThreadPool.cpp
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"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/Support/Host.h"
16 #include "llvm/Support/TargetSelect.h"
17
18 #include "gtest/gtest.h"
19
20 using namespace llvm;
21
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);
52
53     // See https://llvm.org/bugs/show_bug.cgi?id=25829
54     UnsupportedArchs.push_back(Triple::ppc64le);
55     UnsupportedArchs.push_back(Triple::ppc64);
56   }
57 };
58
59 #define CHECK_UNSUPPORTED() \
60   do { \
61     if (isUnsupportedOSOrEnvironment()) \
62       return; \
63   } while (0); \
64
65 TEST_F(ThreadPoolTest, AsyncBarrier) {
66   CHECK_UNSUPPORTED();
67   // test that async & barrier work together properly.
68
69   std::atomic_int checked_in{0};
70
71   ThreadPool Pool;
72   for (size_t i = 0; i < 5; ++i) {
73     Pool.async([&checked_in, i] {
74       ++checked_in;
75     });
76   }
77   Pool.wait();
78   ASSERT_EQ(5, checked_in);
79 }
80
81 static void TestFunc(std::atomic_int &checked_in, int i) { checked_in += i; }
82
83 TEST_F(ThreadPoolTest, AsyncBarrierArgs) {
84   CHECK_UNSUPPORTED();
85   // Test that async works with a function requiring multiple parameters.
86   std::atomic_int checked_in{0};
87
88   ThreadPool Pool;
89   for (size_t i = 0; i < 5; ++i) {
90     Pool.async(TestFunc, std::ref(checked_in), i);
91   }
92   Pool.wait();
93   ASSERT_EQ(10, checked_in);
94 }
95
96 TEST_F(ThreadPoolTest, Async) {
97   CHECK_UNSUPPORTED();
98   ThreadPool Pool;
99   std::atomic_int i{0};
100   Pool.async([&i] {
101     ++i;
102   });
103   Pool.async([&i] { ++i; });
104   Pool.wait();
105   ASSERT_EQ(2, i.load());
106 }
107
108 TEST_F(ThreadPoolTest, GetFuture) {
109   CHECK_UNSUPPORTED();
110   ThreadPool Pool;
111   std::atomic_int i{0};
112   Pool.async([&i] {
113     ++i;
114   });
115   // Force the future using get()
116   Pool.async([&i] { ++i; }).get();
117   Pool.wait();
118   ASSERT_EQ(2, i.load());
119 }
120
121 TEST_F(ThreadPoolTest, PoolDestruction) {
122   CHECK_UNSUPPORTED();
123   // Test that we are waiting on destruction
124   std::atomic_int checked_in{0};
125
126   {
127     ThreadPool Pool;
128     for (size_t i = 0; i < 5; ++i) {
129       Pool.async([&checked_in, i] {
130         ++checked_in;
131       });
132     }
133   }
134   ASSERT_EQ(5, checked_in);
135 }