1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 20:51:52 +01:00

[unittests] ThreadPool: Guard updates to MainThreadReady

llvm-svn: 256096
This commit is contained in:
Vedant Kumar 2015-12-19 09:49:09 +00:00
parent 358f3ea995
commit 1060fa635e

View File

@ -62,6 +62,14 @@ protected:
WaitMainThread.wait(LockGuard, [&] { return MainThreadReady; }); 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::condition_variable WaitMainThread;
std::mutex WaitMainThreadMutex; std::mutex WaitMainThreadMutex;
bool MainThreadReady; bool MainThreadReady;
@ -80,7 +88,7 @@ TEST_F(ThreadPoolTest, AsyncBarrier) {
std::atomic_int checked_in{0}; std::atomic_int checked_in{0};
MainThreadReady = false; setMainThreadReadyState(false);
ThreadPool Pool; ThreadPool Pool;
for (size_t i = 0; i < 5; ++i) { for (size_t i = 0; i < 5; ++i) {
Pool.async([this, &checked_in, i] { Pool.async([this, &checked_in, i] {
@ -89,8 +97,7 @@ TEST_F(ThreadPoolTest, AsyncBarrier) {
}); });
} }
ASSERT_EQ(0, checked_in); ASSERT_EQ(0, checked_in);
MainThreadReady = true; setMainThreadReadyState(true);
WaitMainThread.notify_all();
Pool.wait(); Pool.wait();
ASSERT_EQ(5, checked_in); ASSERT_EQ(5, checked_in);
} }
@ -114,15 +121,14 @@ TEST_F(ThreadPoolTest, Async) {
CHECK_UNSUPPORTED(); CHECK_UNSUPPORTED();
ThreadPool Pool; ThreadPool Pool;
std::atomic_int i{0}; std::atomic_int i{0};
MainThreadReady = false; setMainThreadReadyState(false);
Pool.async([this, &i] { Pool.async([this, &i] {
waitForMainThread(); waitForMainThread();
++i; ++i;
}); });
Pool.async([&i] { ++i; }); Pool.async([&i] { ++i; });
ASSERT_NE(2, i.load()); ASSERT_NE(2, i.load());
MainThreadReady = true; setMainThreadReadyState(true);
WaitMainThread.notify_all();
Pool.wait(); Pool.wait();
ASSERT_EQ(2, i.load()); ASSERT_EQ(2, i.load());
} }
@ -131,7 +137,7 @@ TEST_F(ThreadPoolTest, GetFuture) {
CHECK_UNSUPPORTED(); CHECK_UNSUPPORTED();
ThreadPool Pool; ThreadPool Pool;
std::atomic_int i{0}; std::atomic_int i{0};
MainThreadReady = false; setMainThreadReadyState(false);
Pool.async([this, &i] { Pool.async([this, &i] {
waitForMainThread(); waitForMainThread();
++i; ++i;
@ -139,8 +145,7 @@ TEST_F(ThreadPoolTest, GetFuture) {
// Force the future using get() // Force the future using get()
Pool.async([&i] { ++i; }).get(); Pool.async([&i] { ++i; }).get();
ASSERT_NE(2, i.load()); ASSERT_NE(2, i.load());
MainThreadReady = true; setMainThreadReadyState(true);
WaitMainThread.notify_all();
Pool.wait(); Pool.wait();
ASSERT_EQ(2, i.load()); ASSERT_EQ(2, i.load());
} }
@ -150,7 +155,7 @@ TEST_F(ThreadPoolTest, PoolDestruction) {
// Test that we are waiting on destruction // Test that we are waiting on destruction
std::atomic_int checked_in{0}; std::atomic_int checked_in{0};
{ {
MainThreadReady = false; setMainThreadReadyState(false);
ThreadPool Pool; ThreadPool Pool;
for (size_t i = 0; i < 5; ++i) { for (size_t i = 0; i < 5; ++i) {
Pool.async([this, &checked_in, i] { Pool.async([this, &checked_in, i] {
@ -159,8 +164,7 @@ TEST_F(ThreadPoolTest, PoolDestruction) {
}); });
} }
ASSERT_EQ(0, checked_in); ASSERT_EQ(0, checked_in);
MainThreadReady = true; setMainThreadReadyState(true);
WaitMainThread.notify_all();
} }
ASSERT_EQ(5, checked_in); ASSERT_EQ(5, checked_in);
} }