From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2062) id 7585C3858433; Wed, 9 Feb 2022 20:32:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7585C3858433 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Thomas Rodgers To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-9547] libstdc++: Fix deadlock in atomic wait [PR104442] X-Act-Checkin: gcc X-Git-Author: Thomas Rodgers X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: ec01f11091ad54aeeea343647847c85b090c60d7 X-Git-Newrev: 5669a60e2fcc90afaa16fe20391cdcf18ab398d7 Message-Id: <20220209203229.7585C3858433@sourceware.org> Date: Wed, 9 Feb 2022 20:32:29 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Feb 2022 20:32:29 -0000 https://gcc.gnu.org/g:5669a60e2fcc90afaa16fe20391cdcf18ab398d7 commit r11-9547-g5669a60e2fcc90afaa16fe20391cdcf18ab398d7 Author: Thomas Rodgers Date: Wed Feb 9 12:29:19 2022 -0800 libstdc++: Fix deadlock in atomic wait [PR104442] This issue was observed as a deadlock in 29_atomics/atomic/wait_notify/100334.cc on vxworks. When a wait is "laundered" (e.g. type T* does not suffice as a waitable address for the platform's native waiting primitive), the address waited is that of the _M_ver member of __waiter_pool_base, so several threads may wait on the same address for unrelated atomic objects. As noted in the PR, the implementation correctly exits the wait for the thread whose data changed, but not for any other threads waiting on the same address. As noted in the PR the __waiter::_M_do_wait_v member was correctly exiting but the other waiters were not reloading the value of _M_ver before re-entering the wait. Moving the spin call inside the loop accomplishes this, and is consistent with the predicate accepting version of __waiter::_M_do_wait. libstdc++-v3/ChangeLog: PR libstdc++/104442 * include/bits/atomic_wait.h (__waiter::_M_do_wait_v): Move spin loop inside do loop so that threads failing the wait, reload _M_ver. (cherry picked from commit 4cf3c339815cdfa636b25a512f91b63d7c313fd6) Diff: --- libstdc++-v3/include/bits/atomic_wait.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/include/bits/atomic_wait.h b/libstdc++-v3/include/bits/atomic_wait.h index f6a1c0d8c32..d7ded906360 100644 --- a/libstdc++-v3/include/bits/atomic_wait.h +++ b/libstdc++-v3/include/bits/atomic_wait.h @@ -390,12 +390,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void _M_do_wait_v(_Tp __old, _ValFn __vfn) { - __platform_wait_t __val; - if (__base_type::_M_do_spin_v(__old, __vfn, __val)) - return; - do { + __platform_wait_t __val; + if (__base_type::_M_do_spin_v(__old, __vfn, __val)) + return; __base_type::_M_w._M_do_wait(__base_type::_M_addr, __val); } while (__detail::__atomic_compare(__old, __vfn()));