From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 46C9C3858407 for ; Thu, 4 Aug 2022 12:30:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 46C9C3858407 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-613-lvrBLXnLMgCoE_Y6BjIUuw-1; Thu, 04 Aug 2022 08:30:46 -0400 X-MC-Unique: lvrBLXnLMgCoE_Y6BjIUuw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D45D13802B84; Thu, 4 Aug 2022 12:30:45 +0000 (UTC) Received: from localhost (unknown [10.33.36.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 99A0F492C3B; Thu, 4 Aug 2022 12:30:45 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Thomas Rodgers Subject: [committed] libstdc++: Unblock atomic wait on non-futex platforms [PR106183] Date: Thu, 4 Aug 2022 13:30:45 +0100 Message-Id: <20220804123045.726521-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Aug 2022 12:30:49 -0000 Tested x86_64-linux, powerpc64le-linux and sparc-solaris2.11, pushed to trunk. We want this on the gcc-12 and gcc-11 branches too. -- >8 -- When using a mutex and condition variable, the notifying thread needs to increment _M_ver while holding the mutex lock, and the waiting thread needs to re-check after locking the mutex. This avoids a missed notification as described in the PR. By moving the increment of _M_ver to the base _M_notify we can make the use of the mutex local to the use of the condition variable, and simplify the code a little. We can use a relaxed store because the mutex already provides sequential consistency. Also we don't need to check whether __addr == &_M_ver because we know that's always true for platforms that use a condition variable, and so we also know that we always need to use notify_all() not notify_one(). Reviewed-by: Thomas Rodgers libstdc++-v3/ChangeLog: PR libstdc++/106183 * include/bits/atomic_wait.h (__waiter_pool_base::_M_notify): Move increment of _M_ver here. [!_GLIBCXX_HAVE_PLATFORM_WAIT]: Lock mutex around increment. Use relaxed memory order and always notify all waiters. (__waiter_base::_M_do_wait) [!_GLIBCXX_HAVE_PLATFORM_WAIT]: Check value again after locking mutex. (__waiter_base::_M_notify): Remove increment of _M_ver. --- libstdc++-v3/include/bits/atomic_wait.h | 42 ++++++++++++------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/libstdc++-v3/include/bits/atomic_wait.h b/libstdc++-v3/include/bits/atomic_wait.h index a6d55d3af8a..76ed7409937 100644 --- a/libstdc++-v3/include/bits/atomic_wait.h +++ b/libstdc++-v3/include/bits/atomic_wait.h @@ -221,18 +221,25 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } void - _M_notify(const __platform_wait_t* __addr, bool __all, bool __bare) noexcept + _M_notify(__platform_wait_t* __addr, [[maybe_unused]] bool __all, + bool __bare) noexcept { - if (!(__bare || _M_waiting())) - return; - #ifdef _GLIBCXX_HAVE_PLATFORM_WAIT - __platform_notify(__addr, __all); + if (__addr == &_M_ver) + { + __atomic_fetch_add(__addr, 1, __ATOMIC_SEQ_CST); + __all = true; + } + + if (__bare || _M_waiting()) + __platform_notify(__addr, __all); #else - if (__all) + { + lock_guard __l(_M_mtx); + __atomic_fetch_add(__addr, 1, __ATOMIC_RELAXED); + } + if (__bare || _M_waiting()) _M_cv.notify_all(); - else - _M_cv.notify_one(); #endif } @@ -259,7 +266,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__val == __old) { lock_guard __l(_M_mtx); - _M_cv.wait(_M_mtx); + __atomic_load(__addr, &__val, __ATOMIC_RELAXED); + if (__val == __old) + _M_cv.wait(_M_mtx); } #endif // __GLIBCXX_HAVE_PLATFORM_WAIT } @@ -297,20 +306,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION , _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_laundered()) - { - __atomic_fetch_add(_M_addr, 1, __ATOMIC_SEQ_CST); - __all = true; - } - _M_w._M_notify(_M_addr, __all, __bare); - } + _M_notify(bool __all, bool __bare = false) noexcept + { _M_w._M_notify(_M_addr, __all, __bare); } template -- 2.37.1