From patchwork Fri Jun 4 22:29:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Rodgers X-Patchwork-Id: 1488158 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=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@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=D985FNzK; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (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 4FxctW0Pcwz9sRN for ; Sat, 5 Jun 2021 08:31:14 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4C4183857C68 for ; Fri, 4 Jun 2021 22:31:11 +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 7DA4A3857C68; Fri, 4 Jun 2021 22:30:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7DA4A3857C68 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=appliantology.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=appliantology.com Received: from localhost (unknown [127.0.0.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTP id 34615A6C; Sat, 5 Jun 2021 00:30:44 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:message-id:date:date :subject:subject:from:from:received:received:received; s= dkim20160331; t=1622845843; x=1624660244; bh=oxKi0OSnZceGhbKKYjy LIZZZCtmvYy0ZhEpIXNRMW6g=; b=D985FNzKwy31A60ycF4CoQjsYApTNlWtWs4 Gpr/+7tQGLmCQd1OYe4MbZN+89fk7kpZDxku1C0b4UUKwDZBbUzJTUiatazOKH8R EVOiagn5hmfhPSqADkzrEqXXyeFZQlmIR5JCRYJUO88F1IGd64B+K0t64FUKcKr6 uscqE7DMixbFx2IfTAtacXTw6gltpILuS5jSuijbXtjvTEeGnBecKDCiSyirkOY5 QD4Fz0xO3SKaHBANZo8glPk7wWpLOVO3Pmzznz+3B0KhBFr2TYgoYNpc7gjsAaHU lxTC+Sg7SmXGSxF8NFs4DGGNIf4fcVC41czmGRd0lpfH+V/BT3UoSk0/Q1tcuEHR JFumfhubqvkbNT81kY509QFfhJxxZcK2EgVd0QNk4iP1L/9O4kIQ19Ni/vtBQYLD n2VW8zjyeFcJS0G8eT6hv+SQblRO+cyt3/YNLCKQENQ2GDLWURs7uv5t/9RMuwsJ hW19MM8vLsDjl5scp8uyGZK9hlYDfQY5z/uxcRIu1ZAfh6kioUpoByxaoHwZobYW yRr2EPE7/315LYxPgmY+EiOLeBa5DxT1TFHnFTar9L8iZ4d8isRrcmHg3/tqpox8 dBAw5Zf+FT72Mxt6fThZxvup21CioctIU7VP9oB4PKdlosjLWzA8JRiAS9/4xme/ +cVOdLSg= X-Virus-Scanned: amavisd-new at mykolab.com X-Spam-Score: -1.9 X-Spam-Level: X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP 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 3VnCY4lfDQWI; Sat, 5 Jun 2021 00:30:43 +0200 (CEST) Received: from int-mx002.mykolab.com (unknown [10.9.13.2]) by ext-mx-out001.mykolab.com (Postfix) with ESMTPS id 2D219A25; Sat, 5 Jun 2021 00:30:42 +0200 (CEST) Received: from ext-subm001.mykolab.com (unknown [10.9.6.1]) by int-mx002.mykolab.com (Postfix) with ESMTPS id 62C104684; Sat, 5 Jun 2021 00:30:40 +0200 (CEST) From: Thomas Rodgers To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH] [libstdc++] Cleanup atomic timed wait implementation Date: Fri, 4 Jun 2021 15:29:13 -0700 Message-Id: <20210604222912.815996-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 Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This cleans up the implementation of atomic_timed_wait.h and fixes the accidental pessimization of spinning after waiting in __timed_waiter_pool::_M_do_wait_until. libstdc++-v3/ChangeLog: * include/bits/atomic_timed_wait.h (__wait_clock_t): Define conditionally. (__cond_wait_until_impl): Define conditionally. (__cond_wait_until): Define conditionally. Simplify clock type detection/conversion. (__timed_waiter_pool::_M_do_wait_until): Move the spin above the wait. --- libstdc++-v3/include/bits/atomic_timed_wait.h | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/libstdc++-v3/include/bits/atomic_timed_wait.h b/libstdc++-v3/include/bits/atomic_timed_wait.h index ec7ff51cdbc..19386e5806a 100644 --- a/libstdc++-v3/include/bits/atomic_timed_wait.h +++ b/libstdc++-v3/include/bits/atomic_timed_wait.h @@ -51,7 +51,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION namespace __detail { +#ifdef _GLIBCXX_HAVE_LINUX_FUTEX || _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT using __wait_clock_t = chrono::steady_clock; +#else + using __wait_clock_t = chrono::system_clock; +#endif template __wait_clock_t::time_point @@ -133,11 +137,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return false; } } -#else +// #elsif // define _GLIBCXX_HAVE_PLATFORM_TIMED_WAIT and implement __platform_wait_until() // if there is a more efficient primitive supported by the platform // (e.g. __ulock_wait())which is better than pthread_cond_clockwait -#endif // ! PLATFORM_TIMED_WAIT +#else +// Use wait on condition variable // Returns true if wait ended before timeout. // _Clock must be either steady_clock or system_clock. @@ -173,12 +178,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __cond_wait_until(__condvar& __cv, mutex& __mx, const chrono::time_point<_Clock, _Dur>& __atime) { -#ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT - if constexpr (is_same_v<_Clock, chrono::steady_clock>) - return __detail::__cond_wait_until_impl(__cv, __mx, __atime); - else -#endif - if constexpr (is_same_v<_Clock, chrono::system_clock>) + if constexpr (is_same_v<__wait_clock_t, _Clock>) return __detail::__cond_wait_until_impl(__cv, __mx, __atime); else { @@ -194,6 +194,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return false; } } +#endif // ! PLATFORM_TIMED_WAIT struct __timed_waiter_pool : __waiter_pool_base { @@ -300,17 +301,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const chrono::time_point<_Clock, _Dur>& __atime) noexcept { + for (auto __now = _Clock::now(); __now < __atime; __now = _Clock::now()) { + if (__base_type::_M_do_spin(__pred, __val, + __timed_backoff_spin_policy(__atime, __now))) + return true; + if (__base_type::_M_w._M_do_wait_until( __base_type::_M_addr, __val, __atime) && __pred()) return true; - - if (__base_type::_M_do_spin(__pred, __val, - __timed_backoff_spin_policy(__atime, __now))) - return true; } return false; }