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 TEST(ThreadPoolTest, Async) {
50 // sleep here just to ensure that the not-equal is correct.
55 Pool.async([&i] { ++i; });
56 ASSERT_NE(2, i.load());
58 ASSERT_EQ(2, i.load());
61 TEST(ThreadPoolTest, GetFuture) {
64 // sleep here just to ensure that the not-equal is correct.
69 // Force the future using get()
70 Pool.async([&i] { ++i; }).get();
71 ASSERT_NE(2, i.load());
73 ASSERT_EQ(2, i.load());
76 TEST(ThreadPoolTest, PoolDestruction) {
77 // Test that we are waiting on destruction
78 std::atomic_int checked_in{0};
82 for (size_t i = 0; i < 5; ++i) {
83 Pool.async([&checked_in, i] {
88 ASSERT_EQ(0, checked_in);
90 ASSERT_EQ(5, checked_in);