From f76cad692a62d44ed32d010200bad74f36c73092 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 25 Nov 2020 14:39:54 +0000 Subject: [PATCH] libstdc++: Fix testsuite helper functions [PR 97936] This fixes a race condition in the util/atomic/wait_notify_util.h header used by several tests, which should make the tests work properly. libstdc++-v3/ChangeLog: PR libstdc++/97936 * testsuite/29_atomics/atomic/wait_notify/bool.cc: Re-eneable test. * testsuite/29_atomics/atomic/wait_notify/generic.cc: Likewise. * testsuite/29_atomics/atomic/wait_notify/pointers.cc: Likewise. * testsuite/29_atomics/atomic_flag/wait_notify/1.cc: Likewise. * testsuite/29_atomics/atomic_float/wait_notify.cc: Likewise. * testsuite/29_atomics/atomic_integral/wait_notify.cc: Likewise. * testsuite/util/atomic/wait_notify_util.h: Fix missed notifications by making the new thread wait until the parent thread is waiting on the condition variable. --- .../29_atomics/atomic/wait_notify/bool.cc | 1 - .../29_atomics/atomic/wait_notify/generic.cc | 1 - .../29_atomics/atomic/wait_notify/pointers.cc | 1 - .../29_atomics/atomic_flag/wait_notify/1.cc | 1 - .../29_atomics/atomic_float/wait_notify.cc | 1 - .../29_atomics/atomic_integral/wait_notify.cc | 1 - .../testsuite/util/atomic/wait_notify_util.h | 24 +++++++++++++++---- 7 files changed, 20 insertions(+), 10 deletions(-) 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 29781c6e135..c14a2391d68 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/bool.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/bool.cc @@ -2,7 +2,6 @@ // { dg-do run { target c++2a } } // { dg-require-gthreads "" } // { dg-additional-options "-pthread" { target pthread } } -// { dg-skip-if "broken" { ! *-*-*linux } } // Copyright (C) 2020 Free Software Foundation, Inc. // diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/generic.cc b/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/generic.cc index 629556a9d2d..988fe7b334f 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/generic.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/generic.cc @@ -2,7 +2,6 @@ // { dg-do run { target c++2a } } // { dg-require-gthreads "" } // { dg-additional-options "-pthread" { target pthread } } -// { dg-skip-if "broken" { ! *-*-*linux } } // Copyright (C) 2020 Free Software Foundation, Inc. // 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 f54961f893d..87830236e0e 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/pointers.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/pointers.cc @@ -2,7 +2,6 @@ // { dg-do run { target c++2a } } // { dg-additional-options "-pthread" { target pthread } } // { dg-require-gthreads "" } -// { dg-skip-if "broken" { ! *-*-*linux } } // Copyright (C) 2020 Free Software Foundation, Inc. // 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 763d3e77159..991713fbcde 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 @@ -2,7 +2,6 @@ // { dg-do run { target c++2a } } // { dg-require-gthreads "" } // { dg-additional-options "-pthread" { target pthread } } -// { dg-skip-if "broken" { ! *-*-*linux } } // Copyright (C) 2020 Free Software Foundation, Inc. // diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_float/wait_notify.cc b/libstdc++-v3/testsuite/29_atomics/atomic_float/wait_notify.cc index 8f9e4a39a21..134eff39e1b 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic_float/wait_notify.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic_float/wait_notify.cc @@ -3,7 +3,6 @@ // { dg-require-gthreads "" } // { dg-additional-options "-pthread" { target pthread } } // { dg-add-options libatomic } -// { dg-skip-if "broken" { ! *-*-*linux } } // Copyright (C) 2020 Free Software Foundation, Inc. // diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_integral/wait_notify.cc b/libstdc++-v3/testsuite/29_atomics/atomic_integral/wait_notify.cc index 762583cf8c7..c65379cba61 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic_integral/wait_notify.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic_integral/wait_notify.cc @@ -3,7 +3,6 @@ // { dg-require-gthreads "" } // { dg-add-options libatomic } // { dg-additional-options "-pthread" { target pthread } } -// { dg-skip-if "broken" { *-*-* } } // Copyright (C) 2020 Free Software Foundation, Inc. // diff --git a/libstdc++-v3/testsuite/util/atomic/wait_notify_util.h b/libstdc++-v3/testsuite/util/atomic/wait_notify_util.h index a319e8b60a6..f5fff4af4e4 100644 --- a/libstdc++-v3/testsuite/util/atomic/wait_notify_util.h +++ b/libstdc++-v3/testsuite/util/atomic/wait_notify_util.h @@ -34,16 +34,20 @@ Tp check_wait_notify(Tp val1, Tp val2) std::mutex m; std::condition_variable cv; + std::unique_lock l(m); std::atomic a(val1); 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); @@ -59,10 +63,15 @@ Tp check_wait_notify(Tp val1, Tp val2) std::mutex m; std::condition_variable cv; + std::unique_lock l(m); std::atomic a(val1); std::thread t([&] { + { + // This ensures we block until cv.wait(l) starts. + std::lock_guard ll(m); + } cv.notify_one(); a.wait(val1); auto v = a.load(); @@ -70,7 +79,6 @@ Tp check_wait_notify(Tp val1, Tp val2) if (__builtin_memcmp(&v, &val2, sizeof(Tp)) != 0) a = val1; }); - std::unique_lock l(m); cv.wait(l); std::this_thread::sleep_for(100ms); a.store(val2); @@ -87,16 +95,20 @@ Tp check_atomic_wait_notify(Tp val1, Tp val2) std::mutex m; std::condition_variable cv; + std::unique_lock l(m); std::atomic a(val1); std::thread t([&] { + { + // This ensures we block until cv.wait(l) starts. + std::lock_guard ll(m); + } cv.notify_one(); std::atomic_wait(&a, val1); if (a.load() != val2) a = val1; }); - std::unique_lock l(m); cv.wait(l); std::this_thread::sleep_for(100ms); a.store(val2); @@ -112,10 +124,15 @@ Tp check_atomic_wait_notify(Tp val1, Tp val2) std::mutex m; std::condition_variable cv; + std::unique_lock l(m); std::atomic a(val1); std::thread t([&] { + { + // This ensures we block until cv.wait(l) starts. + std::lock_guard ll(m); + } cv.notify_one(); std::atomic_wait(&a, val1); auto v = a.load(); @@ -123,7 +140,6 @@ Tp check_atomic_wait_notify(Tp val1, Tp val2) if (__builtin_memcmp(&v, &val2, sizeof(Tp)) != 0) a = val1; }); - std::unique_lock l(m); cv.wait(l); std::this_thread::sleep_for(100ms); a.store(val2); -- 2.30.2