[llvm] r256096 - [unittests] ThreadPool: Guard updates to MainThreadReady

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 19 01:49:10 PST 2015


Author: vedantk
Date: Sat Dec 19 03:49:09 2015
New Revision: 256096

URL: http://llvm.org/viewvc/llvm-project?rev=256096&view=rev
Log:
[unittests] ThreadPool: Guard updates to MainThreadReady

Modified:
    llvm/trunk/unittests/Support/ThreadPool.cpp

Modified: llvm/trunk/unittests/Support/ThreadPool.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ThreadPool.cpp?rev=256096&r1=256095&r2=256096&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ThreadPool.cpp (original)
+++ llvm/trunk/unittests/Support/ThreadPool.cpp Sat Dec 19 03:49:09 2015
@@ -62,6 +62,14 @@ protected:
       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 +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] {
@@ -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);
 }
@@ -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());
 }
@@ -131,7 +137,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 +145,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 +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] {
@@ -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);
 }




More information about the llvm-commits mailing list