ThreadPool unittests: do not hold mutex when calling condition_variable:notify()
authorMehdi Amini <mehdi.amini@apple.com>
Sat, 19 Dec 2015 22:56:24 +0000 (22:56 +0000)
committerMehdi Amini <mehdi.amini@apple.com>
Sat, 19 Dec 2015 22:56:24 +0000 (22:56 +0000)
From: Mehdi Amini <mehdi.amini@apple.com>

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256111 91177308-0d34-0410-b5e6-96231b3b80d8

unittests/Support/ThreadPool.cpp

index b0307d1f16a4eb145e6697d99b26cb2a7dfc01ce..0f36c383d494cf4dc2db25706a834d55f678dcbe 100644 (file)
@@ -62,12 +62,16 @@ protected:
   }
 
   /// Set the readiness of the main thread.
   }
 
   /// Set the readiness of the main thread.
-  void setMainThreadReadyState(bool Ready) {
-    std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
-    MainThreadReady = Ready;
+  void setMainThreadReady() {
+    {
+      std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
+      MainThreadReady = true;
+    }
     WaitMainThread.notify_all();
   }
 
     WaitMainThread.notify_all();
   }
 
+  void SetUp() override { MainThreadReady = false; }
+
   std::condition_variable WaitMainThread;
   std::mutex WaitMainThreadMutex;
   bool MainThreadReady;
   std::condition_variable WaitMainThread;
   std::mutex WaitMainThreadMutex;
   bool MainThreadReady;
@@ -86,7 +90,6 @@ TEST_F(ThreadPoolTest, AsyncBarrier) {
 
   std::atomic_int checked_in{0};
 
 
   std::atomic_int checked_in{0};
 
-  setMainThreadReadyState(false);
   ThreadPool Pool;
   for (size_t i = 0; i < 5; ++i) {
     Pool.async([this, &checked_in, i] {
   ThreadPool Pool;
   for (size_t i = 0; i < 5; ++i) {
     Pool.async([this, &checked_in, i] {
@@ -95,7 +98,7 @@ TEST_F(ThreadPoolTest, AsyncBarrier) {
     });
   }
   ASSERT_EQ(0, checked_in);
     });
   }
   ASSERT_EQ(0, checked_in);
-  setMainThreadReadyState(true);
+  setMainThreadReady();
   Pool.wait();
   ASSERT_EQ(5, checked_in);
 }
   Pool.wait();
   ASSERT_EQ(5, checked_in);
 }
@@ -119,14 +122,13 @@ TEST_F(ThreadPoolTest, Async) {
   CHECK_UNSUPPORTED();
   ThreadPool Pool;
   std::atomic_int i{0};
   CHECK_UNSUPPORTED();
   ThreadPool Pool;
   std::atomic_int i{0};
-  setMainThreadReadyState(false);
   Pool.async([this, &i] {
     waitForMainThread();
     ++i;
   });
   Pool.async([&i] { ++i; });
   ASSERT_NE(2, i.load());
   Pool.async([this, &i] {
     waitForMainThread();
     ++i;
   });
   Pool.async([&i] { ++i; });
   ASSERT_NE(2, i.load());
-  setMainThreadReadyState(true);
+  setMainThreadReady();
   Pool.wait();
   ASSERT_EQ(2, i.load());
 }
   Pool.wait();
   ASSERT_EQ(2, i.load());
 }
@@ -135,7 +137,6 @@ TEST_F(ThreadPoolTest, GetFuture) {
   CHECK_UNSUPPORTED();
   ThreadPool Pool;
   std::atomic_int i{0};
   CHECK_UNSUPPORTED();
   ThreadPool Pool;
   std::atomic_int i{0};
-  setMainThreadReadyState(false);
   Pool.async([this, &i] {
     waitForMainThread();
     ++i;
   Pool.async([this, &i] {
     waitForMainThread();
     ++i;
@@ -143,7 +144,7 @@ TEST_F(ThreadPoolTest, GetFuture) {
   // Force the future using get()
   Pool.async([&i] { ++i; }).get();
   ASSERT_NE(2, i.load());
   // Force the future using get()
   Pool.async([&i] { ++i; }).get();
   ASSERT_NE(2, i.load());
-  setMainThreadReadyState(true);
+  setMainThreadReady();
   Pool.wait();
   ASSERT_EQ(2, i.load());
 }
   Pool.wait();
   ASSERT_EQ(2, i.load());
 }
@@ -153,7 +154,6 @@ TEST_F(ThreadPoolTest, PoolDestruction) {
   // Test that we are waiting on destruction
   std::atomic_int checked_in{0};
   {
   // Test that we are waiting on destruction
   std::atomic_int checked_in{0};
   {
-    setMainThreadReadyState(false);
     ThreadPool Pool;
     for (size_t i = 0; i < 5; ++i) {
       Pool.async([this, &checked_in, i] {
     ThreadPool Pool;
     for (size_t i = 0; i < 5; ++i) {
       Pool.async([this, &checked_in, i] {
@@ -162,7 +162,7 @@ TEST_F(ThreadPoolTest, PoolDestruction) {
       });
     }
     ASSERT_EQ(0, checked_in);
       });
     }
     ASSERT_EQ(0, checked_in);
-    setMainThreadReadyState(true);
+    setMainThreadReady();
   }
   ASSERT_EQ(5, checked_in);
 }
   }
   ASSERT_EQ(5, checked_in);
 }