[unittests] ThreadPool: Remove redundant loop, NFC
[oota-llvm.git] / unittests / Support / ThreadPool.cpp
index ca55237d49102bcda5e61188262b5439b9564a7f..b0307d1f16a4eb145e6697d99b26cb2a7dfc01ce 100644 (file)
@@ -57,11 +57,17 @@ protected:
 
   /// Make sure this thread not progress faster than the main thread.
   void waitForMainThread() {
-    while (!MainThreadReady) {
-      std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
-      WaitMainThread.wait(LockGuard, [&] { return MainThreadReady; });
-    }
+    std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
+    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;
@@ -80,7 +86,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] {
@@ -89,8 +95,7 @@ TEST_F(ThreadPoolTest, AsyncBarrier) {
     });
   }
   ASSERT_EQ(0, checked_in);
-  MainThreadReady = true;
-  WaitMainThread.notify_all();
+  setMainThreadReadyState(true);
   Pool.wait();
   ASSERT_EQ(5, checked_in);
 }
@@ -114,15 +119,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());
 }
@@ -131,7 +135,7 @@ TEST_F(ThreadPoolTest, GetFuture) {
   CHECK_UNSUPPORTED();
   ThreadPool Pool;
   std::atomic_int i{0};
-  MainThreadReady = false;
+  setMainThreadReadyState(false);
   Pool.async([this, &i] {
     waitForMainThread();
     ++i;
@@ -139,8 +143,7 @@ TEST_F(ThreadPoolTest, GetFuture) {
   // 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());
 }
@@ -150,7 +153,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] {
@@ -159,8 +162,7 @@ TEST_F(ThreadPoolTest, PoolDestruction) {
       });
     }
     ASSERT_EQ(0, checked_in);
-    MainThreadReady = true;
-    WaitMainThread.notify_all();
+    setMainThreadReadyState(true);
   }
   ASSERT_EQ(5, checked_in);
 }