From: Jonathan Wakely Date: Thu, 26 Nov 2020 12:55:47 +0000 (+0000) Subject: libstdc++: Fix some more deadlocks in tests [PR 97936] X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=10522ed1089277e2aa6cd708205aa5c730179cf0;p=gcc.git 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 c14a2391d68..1fc01491173 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 l(m); std::atomic a(false); std::atomic b(false); std::thread t([&] { + { + // This ensures we block until cv.wait(l) starts. + std::lock_guard ll(m); + } cv.notify_one(); a.wait(false); if (a.load()) @@ -48,7 +53,6 @@ main () b.store(true); } }); - std::unique_lock 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 87830236e0e..3b699e9133b 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 l(m); long aa; long bb; @@ -43,12 +44,15 @@ main () std::atomic a(nullptr); std::thread t([&] { + { + // This ensures we block until cv.wait(l) starts. + std::lock_guard ll(m); + } cv.notify_one(); a.wait(nullptr); if (a.load() == &aa) a.store(&bb); }); - std::unique_lock 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 991713fbcde..5d5e06dde31 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 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 ll(m); + } cv.notify_one(); a.wait(false); b.test_and_set(); b.notify_one(); }); - std::unique_lock 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 b38fc206d46..bc5a7d0d8bf 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 l(m); Tp aa = val1; std::atomic_ref a(aa); std::thread t([&] { + { + // This ensures we block until cv.wait(l) starts. + std::lock_guard ll(m); + } cv.notify_one(); a.wait(val1); if (a.load() != val2) a = val1; }); - std::unique_lock l(m); cv.wait(l); std::this_thread::sleep_for(100ms); a.store(val2);