Skip to content

Commit

Permalink
[unittests] ThreadPool: Guard updates to MainThreadReady
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256096 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
vedantk committed Dec 19, 2015
1 parent ef112d4 commit f99fc12
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions unittests/Support/ThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ class ThreadPoolTest : public testing::Test {
WaitMainThread.wait(LockGuard, [&] { return MainThreadReady; });
}
}

/// Set the readiness of the main thread.
void setMainThreadReadyState(bool Ready) {
std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
MainThreadReady = Ready;
WaitMainThread.notify_all();
}

std::condition_variable WaitMainThread;
std::mutex WaitMainThreadMutex;
bool MainThreadReady;
Expand All @@ -80,7 +88,7 @@ TEST_F(ThreadPoolTest, AsyncBarrier) {

std::atomic_int checked_in{0};

MainThreadReady = false;
setMainThreadReadyState(false);
ThreadPool Pool;
for (size_t i = 0; i < 5; ++i) {
Pool.async([this, &checked_in, i] {
Expand All @@ -89,8 +97,7 @@ TEST_F(ThreadPoolTest, AsyncBarrier) {
});
}
ASSERT_EQ(0, checked_in);
MainThreadReady = true;
WaitMainThread.notify_all();
setMainThreadReadyState(true);
Pool.wait();
ASSERT_EQ(5, checked_in);
}
Expand All @@ -114,15 +121,14 @@ TEST_F(ThreadPoolTest, Async) {
CHECK_UNSUPPORTED();
ThreadPool Pool;
std::atomic_int i{0};
MainThreadReady = false;
setMainThreadReadyState(false);
Pool.async([this, &i] {
waitForMainThread();
++i;
});
Pool.async([&i] { ++i; });
ASSERT_NE(2, i.load());
MainThreadReady = true;
WaitMainThread.notify_all();
setMainThreadReadyState(true);
Pool.wait();
ASSERT_EQ(2, i.load());
}
Expand All @@ -131,16 +137,15 @@ TEST_F(ThreadPoolTest, GetFuture) {
CHECK_UNSUPPORTED();
ThreadPool Pool;
std::atomic_int i{0};
MainThreadReady = false;
setMainThreadReadyState(false);
Pool.async([this, &i] {
waitForMainThread();
++i;
});
// Force the future using get()
Pool.async([&i] { ++i; }).get();
ASSERT_NE(2, i.load());
MainThreadReady = true;
WaitMainThread.notify_all();
setMainThreadReadyState(true);
Pool.wait();
ASSERT_EQ(2, i.load());
}
Expand All @@ -150,7 +155,7 @@ TEST_F(ThreadPoolTest, PoolDestruction) {
// Test that we are waiting on destruction
std::atomic_int checked_in{0};
{
MainThreadReady = false;
setMainThreadReadyState(false);
ThreadPool Pool;
for (size_t i = 0; i < 5; ++i) {
Pool.async([this, &checked_in, i] {
Expand All @@ -159,8 +164,7 @@ TEST_F(ThreadPoolTest, PoolDestruction) {
});
}
ASSERT_EQ(0, checked_in);
MainThreadReady = true;
WaitMainThread.notify_all();
setMainThreadReadyState(true);
}
ASSERT_EQ(5, checked_in);
}

0 comments on commit f99fc12

Please sign in to comment.