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"
14 #include "gtest/gtest.h"
17 using namespace std::chrono;
19 /// Try best to make this thread not progress faster than the main thread
21 #ifdef LLVM_ENABLE_THREADS
22 std::this_thread::yield();
24 std::this_thread::sleep_for(milliseconds(200));
25 #ifdef LLVM_ENABLE_THREADS
26 std::this_thread::yield();
30 TEST(ThreadPoolTest, AsyncBarrier) {
31 // test that async & barrier work together properly.
33 std::atomic_int checked_in{0};
36 for (size_t i = 0; i < 5; ++i) {
37 Pool.async([&checked_in, i] {
42 ASSERT_EQ(0, checked_in);
44 ASSERT_EQ(5, checked_in);
47 static void TestFunc(std::atomic_int &checked_in, int i) { checked_in += i; }
49 TEST(ThreadPoolTest, AsyncBarrierArgs) {
50 // Test that async works with a function requiring multiple parameters.
51 std::atomic_int checked_in{0};
54 for (size_t i = 0; i < 5; ++i) {
55 Pool.async(TestFunc, std::ref(checked_in), i);
58 ASSERT_EQ(10, checked_in);
61 TEST(ThreadPoolTest, Async) {
64 // sleep here just to ensure that the not-equal is correct.
69 Pool.async([&i] { ++i; });
70 ASSERT_NE(2, i.load());
72 ASSERT_EQ(2, i.load());
75 TEST(ThreadPoolTest, GetFuture) {
78 // sleep here just to ensure that the not-equal is correct.
83 // Force the future using get()
84 Pool.async([&i] { ++i; }).get();
85 ASSERT_NE(2, i.load());
87 ASSERT_EQ(2, i.load());
90 TEST(ThreadPoolTest, PoolDestruction) {
91 // Test that we are waiting on destruction
92 std::atomic_int checked_in{0};
96 for (size_t i = 0; i < 5; ++i) {
97 Pool.async([&checked_in, i] {
102 ASSERT_EQ(0, checked_in);
104 ASSERT_EQ(5, checked_in);