From patchwork Fri May 14 01:54:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Rodgers X-Patchwork-Id: 1478288 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (4096-bit key; secure) header.d=kolabnow.com header.i=@kolabnow.com header.a=rsa-sha256 header.s=dkim20160331 header.b=XUo+6CmP; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FhBRp2jr8z9sWQ for ; Fri, 14 May 2021 11:55:02 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3D372388A837; Fri, 14 May 2021 01:54:55 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx.kolabnow.com (mx.kolabnow.com [95.128.36.42]) by sourceware.org (Postfix) with ESMTPS id 565CC3886C4B; Fri, 14 May 2021 01:54:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 565CC3886C4B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=appliantology.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=rodgert@appliantology.com Received: from localhost (unknown [127.0.0.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTP id E0B9CB08; Fri, 14 May 2021 03:54:50 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1620957290; x=1622771691; bh=ctjYh/ucmZpJYDGaaQUKHnBs+72LFZepgLfqKNtrJnQ=; b=XUo+6CmPewVq HIzviNsIOaM2LGlxXdaMypfc+EdHjKHJ1bDL1eB1dj4Xfuf1KY8mT4Tujq3lBe7G q5b+NJ+Es7Hnr4ldxOKRZEsUWw9hinJrmD+L4cEOcbb9xZyyyPoEnJeBIje0MUGY IT++T3VRZPhKnBplkzcV4+h6VpAxR1tZYvo/kAl+TjKYmJ4gZ4z9ObapSWcq/0np uXIwyuSTlcK46YCAZDf8P4ocWaL06G1IsPWD1eBqnquyCeLdFV22A4n7mrOS0dtJ HAyL9c/eJp5CEo2f/ocfR30kYurkFXcz/w7S7DFZYEuXYi1/u0KoEnvAHv8eBy57 Btf4DN/HXzGTGA3khWE1iijk4YD29KEcdc9+rFrVJ423/iDsfjNjSZWue0/g6eOC Y0+rYlXVabppPBW/vCLzkp33da8vm0FDbKzRCPWcR/YYTVFBeSBwLHV5hh9jA7ee 85qtZYVPA9lc6CiAnZSLSIEDw0zNc/Znq3g9dQMrQ+uZ/UyIpM0sFuMUWp+XT5Et fV83oJtp1z5NDHkmV15rsMT9GOFyKlJehEGaYmVNUESgOJdm03HndTGcpZxE+ksD 8JF+qaz2QoBwf4K//muEKv8kaxxhlfZJihk+ZPOFsVtxZWJBl3TJzelF4/3TtqLq +inag6HCjMtRwpj47b5zA8nsN+/lApg= X-Virus-Scanned: amavisd-new at mykolab.com X-Spam-Score: -0.777 X-Spam-Level: X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=ham autolearn_force=no version=3.4.2 Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out001.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id NG0MSBxIuM7p; Fri, 14 May 2021 03:54:50 +0200 (CEST) Received: from int-mx002.mykolab.com (unknown [10.9.13.2]) by ext-mx-out001.mykolab.com (Postfix) with ESMTPS id BDAA14EE; Fri, 14 May 2021 03:54:48 +0200 (CEST) Received: from ext-subm001.mykolab.com (unknown [10.9.6.1]) by int-mx002.mykolab.com (Postfix) with ESMTPS id B596A11F7E; Fri, 14 May 2021 03:54:46 +0200 (CEST) From: Thomas Rodgers To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH] libstdc++: Fix wrong thread waking on notify [PR100334] Date: Thu, 13 May 2021 18:54:36 -0700 Message-Id: <20210514015436.111757-1-rodgert@appliantology.com> In-Reply-To: <20210513233535.110937-1-rodgert@appliantology.com> References: <20210513233535.110937-1-rodgert@appliantology.com> MIME-Version: 1.0 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: trodgers@redhat.com, Thomas Rodgers Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" From: Thomas Rodgers Please ignore the previous patch. This one removes the need to carry any extra state in the case of a 'laundered' atomic wait. libstdc++/ChangeLog: * include/bits/atomic_wait.h (__waiter::_M_do_wait_v): loop until value change observed. (__waiter_base::_M_laundered): New member function. (__watier_base::_M_notify): Check _M_laundered() to determine whether to wake one or all. (__detail::__atomic_compare): Return true if call to __builtin_memcmp() == 0. (__waiter_base::_S_do_spin_v): Adjust predicate. * testsuite/29_atomics/atomic/wait_notify/100334.cc: New test. --- libstdc++-v3/include/bits/atomic_wait.h | 28 ++++-- .../29_atomics/atomic/wait_notify/100334.cc | 94 +++++++++++++++++++ 2 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/100334.cc diff --git a/libstdc++-v3/include/bits/atomic_wait.h b/libstdc++-v3/include/bits/atomic_wait.h index 984ed70f16c..07bb744d822 100644 --- a/libstdc++-v3/include/bits/atomic_wait.h +++ b/libstdc++-v3/include/bits/atomic_wait.h @@ -181,11 +181,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return false; } + // return true if equal template bool __atomic_compare(const _Tp& __a, const _Tp& __b) { // TODO make this do the correct padding bit ignoring comparison - return __builtin_memcmp(&__a, &__b, sizeof(_Tp)) != 0; + return __builtin_memcmp(&__a, &__b, sizeof(_Tp)) == 0; } struct __waiter_pool_base @@ -300,14 +301,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION explicit __waiter_base(const _Up* __addr) noexcept : _M_w(_S_for(__addr)) , _M_addr(_S_wait_addr(__addr, &_M_w._M_ver)) - { - } + { } + + bool + _M_laundered() const + { return _M_addr == &_M_w._M_ver; } void _M_notify(bool __all, bool __bare = false) { - if (_M_addr == &_M_w._M_ver) - __atomic_fetch_add(_M_addr, 1, __ATOMIC_ACQ_REL); + if (_M_laundered()) + { + __atomic_fetch_add(_M_addr, 1, __ATOMIC_ACQ_REL); + __all = true; + } _M_w._M_notify(_M_addr, __all, __bare); } @@ -320,7 +327,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Spin __spin = _Spin{ }) { auto const __pred = [=] - { return __detail::__atomic_compare(__old, __vfn()); }; + { return !__detail::__atomic_compare(__old, __vfn()); }; if constexpr (__platform_wait_uses_type<_Up>) { @@ -387,7 +394,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __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); + + do + { + __base_type::_M_w._M_do_wait(__base_type::_M_addr, __val); + } + while (__detail::__atomic_compare(__old, __vfn())); } template @@ -452,7 +464,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __atomic_notify_address(const _Tp* __addr, bool __all) noexcept { __detail::__bare_wait __w(__addr); - __w._M_notify(__all, true); + __w._M_notify(__all); } // This call is to be used by atomic types which track contention externally diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/100334.cc b/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/100334.cc new file mode 100644 index 00000000000..3e63eca42fa --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/100334.cc @@ -0,0 +1,94 @@ +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } +// { dg-require-gthreads "" } +// { dg-additional-options "-pthread" { target pthread } } +// { dg-add-options libatomic } + +// Copyright (C) 2021 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +#include + +template +struct atomics_sharing_same_waiter +{ + std::atomic tmp[49 * 4] = {}; + std::atomic* a[4] = { + { &tmp[0] }, + { &tmp[16 * 4] }, + { &tmp[32 * 4] }, + { &tmp[48 * 4] } + }; +}; + +constexpr unsigned key(void * a) +{ + constexpr uintptr_t ct = 16; + return (uintptr_t(a) >> 2) % ct; +} + +int +main() +{ + // all atomic share the same waiter +// atomics_sharing_same_waiter atomics; + atomics_sharing_same_waiter atomics; + for (auto& atom : atomics.a) + { + atom->store(0); + } + + auto a = &std::__detail::__waiter_pool_base::_S_for(reinterpret_cast(atomics.a[0])); + auto b = &std::__detail::__waiter_pool_base::_S_for(reinterpret_cast(atomics.a[1])); + VERIFY( a == b ); + + auto fut0 = std::async(std::launch::async, [&] { atomics.a[0]->wait(0); }); + auto fut1 = std::async(std::launch::async, [&] { atomics.a[1]->wait(0); }); + auto fut2 = std::async(std::launch::async, [&] { atomics.a[2]->wait(0); }); + auto fut3 = std::async(std::launch::async, [&] { atomics.a[3]->wait(0); }); + + // make sure the all threads already await + std::this_thread::sleep_for(std::chrono::milliseconds{100}); + + atomics.a[2]->store(1); + atomics.a[2]->notify_one(); + + VERIFY(std::future_status::timeout == fut0.wait_for(std::chrono::milliseconds{100})); + VERIFY(atomics.a[0]->load() == 0); + + VERIFY(std::future_status::timeout == fut1.wait_for(std::chrono::milliseconds{100})); + VERIFY(atomics.a[1]->load() == 0); + + VERIFY(std::future_status::ready == fut2.wait_for(std::chrono::milliseconds{100})); + VERIFY(atomics.a[2]->load() == 1); + + VERIFY(std::future_status::timeout == fut3.wait_for(std::chrono::milliseconds{100})); + VERIFY(atomics.a[3]->load() == 0); + + atomics.a[0]->store(1); + atomics.a[0]->notify_one(); + atomics.a[1]->store(1); + atomics.a[1]->notify_one(); + atomics.a[3]->store(1); + atomics.a[3]->notify_one(); + + return 0; +} +