On 25/11/20 18:39 +0000, Jonathan Wakely wrote:
On 25/11/20 10:35 +0000, Jonathan Wakely wrote:
I've pushed that as ad9cbcee543ecccd79fa49dafcd925532d2ce210 but there
are still other FAILs to be fixed.

I think the other FAILs are due to a race condition in the tests,
fixed by this patch. Tested x86_64-linux, powerpc64le-linux,
sparc-solaris2.11 and powerpc-aix. Committed to trunk.

I'll keep an eye on the testresults to see if this really fixes it or
not.

This fixes some more races of the same kind, in different tiles.

Tested as above. Committed to trunk.


commit 10522ed1089277e2aa6cd708205aa5c730179cf0
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Thu Nov 26 12:55:47 2020

    libstdc++: Fix some more deadlocks in tests [PR 97936]
    
    The missed notifications fixed in r11-5383 also happen in some other
    tests which have similar code.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/97936
            * testsuite/29_atomics/atomic/wait_notify/bool.cc: Fix missed
            notifications by making the new thread wait until the parent
            thread is waiting on the condition variable.
            * testsuite/29_atomics/atomic/wait_notify/pointers.cc: Likewise.
            * testsuite/29_atomics/atomic_flag/wait_notify/1.cc: Likewise.
            * testsuite/29_atomics/atomic_ref/wait_notify.cc: Likewise.

diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/bool.cc b/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/bool.cc
index c14a2391d68b..1fc014911737 100644
--- a/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/bool.cc
+++ b/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/bool.cc
@@ -36,11 +36,16 @@ main ()
 
   std::mutex m;
   std::condition_variable cv;
+  std::unique_lock<std::mutex> l(m);
 
   std::atomic<bool> a(false);
   std::atomic<bool> b(false);
   std::thread t([&]
 		{
+		  {
+		    // This ensures we block until cv.wait(l) starts.
+		    std::lock_guard<std::mutex> ll(m);
+		  }
 		  cv.notify_one();
 		  a.wait(false);
 		  if (a.load())
@@ -48,7 +53,6 @@ main ()
 		      b.store(true);
 		    }
 		});
-  std::unique_lock<std::mutex> l(m);
   cv.wait(l);
   std::this_thread::sleep_for(100ms);
   a.store(true);
diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/pointers.cc b/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/pointers.cc
index 87830236e0ee..3b699e9133b2 100644
--- a/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/pointers.cc
+++ b/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/pointers.cc
@@ -36,6 +36,7 @@ main ()
 
   std::mutex m;
   std::condition_variable cv;
+  std::unique_lock<std::mutex> l(m);
 
   long aa;
   long bb;
@@ -43,12 +44,15 @@ main ()
   std::atomic<long*> a(nullptr);
   std::thread t([&]
 		{
+		  {
+		    // This ensures we block until cv.wait(l) starts.
+		    std::lock_guard<std::mutex> ll(m);
+		  }
 		  cv.notify_one();
 		  a.wait(nullptr);
 		  if (a.load() == &aa)
 		    a.store(&bb);
 		});
-  std::unique_lock<std::mutex> l(m);
   cv.wait(l);
   std::this_thread::sleep_for(100ms);
   a.store(&aa);
diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_flag/wait_notify/1.cc b/libstdc++-v3/testsuite/29_atomics/atomic_flag/wait_notify/1.cc
index 991713fbcdee..5d5e06dde31c 100644
--- a/libstdc++-v3/testsuite/29_atomics/atomic_flag/wait_notify/1.cc
+++ b/libstdc++-v3/testsuite/29_atomics/atomic_flag/wait_notify/1.cc
@@ -36,18 +36,22 @@ main()
 
   std::mutex m;
   std::condition_variable cv;
+  std::unique_lock<std::mutex> l(m);
 
   std::atomic_flag a;
   std::atomic_flag b;
   std::thread t([&]
 		{
+		  {
+		    // This ensures we block until cv.wait(l) starts.
+		    std::lock_guard<std::mutex> ll(m);
+		  }
 		  cv.notify_one();
 		  a.wait(false);
 		  b.test_and_set();
 		  b.notify_one();
 		});
 
-  std::unique_lock<std::mutex> l(m);
   cv.wait(l);
   std::this_thread::sleep_for(100ms);
   a.test_and_set();
diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_ref/wait_notify.cc b/libstdc++-v3/testsuite/29_atomics/atomic_ref/wait_notify.cc
index b38fc206d468..bc5a7d0d8bf9 100644
--- a/libstdc++-v3/testsuite/29_atomics/atomic_ref/wait_notify.cc
+++ b/libstdc++-v3/testsuite/29_atomics/atomic_ref/wait_notify.cc
@@ -37,17 +37,21 @@ Tp check_wait_notify(Tp val1, Tp val2)
 
   std::mutex m;
   std::condition_variable cv;
+  std::unique_lock<std::mutex> l(m);
 
   Tp aa = val1;
   std::atomic_ref<Tp> a(aa);
   std::thread t([&]
 		{
+		  {
+		    // This ensures we block until cv.wait(l) starts.
+		    std::lock_guard<std::mutex> ll(m);
+		  }
 		  cv.notify_one();
 		  a.wait(val1);
 		  if (a.load() != val2)
 		    a = val1;
 		});
-  std::unique_lock<std::mutex> l(m);
   cv.wait(l);
   std::this_thread::sleep_for(100ms);
   a.store(val2);

Reply via email to