From patchwork Sun Sep 14 20:21:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Crowe X-Patchwork-Id: 120226 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5143C3857C6E for ; Sun, 14 Sep 2025 20:28:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5143C3857C6E Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=mcrowe.com header.i=@mcrowe.com header.a=rsa-sha256 header.s=20191005 header.b=F4Jro5oo X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smarthost01c.sbp.mail.zen.net.uk (smarthost01c.sbp.mail.zen.net.uk [212.23.1.5]) by sourceware.org (Postfix) with ESMTPS id 53A4F3858C51; Sun, 14 Sep 2025 20:22:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 53A4F3858C51 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=mcrowe.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mcrowe.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 53A4F3858C51 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.23.1.5 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757881333; cv=none; b=f3gJ5YlsPjuS3JLpwR0WNYFchAOZRqtCSQArKILmZwCPKnk2i8cwk/8nVF+UeU4s21dMc4T8bTzoeV4Ky8peNwEhprBKvN64GMtiFoc6/tbrh3QhfQNbhA8lsmsaKGY/TMTv8YD212lrSHtmARo3CsYqTxLEeP13YPvN5omDrzg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757881333; c=relaxed/simple; bh=EJxbuDxDIfSne2vS0NHvOtl0yd/+dah1R8bpmWMsp3s=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=U7rPMU1Gui4wi9hVA/94Yq4/3X6/9nt7+suXnrt7C+GBr5+tleC6dAKCb+sElPrh3/0IYHkGbaXI9s3iUlr5qSPSRlCQPrsAhk+LK7SveTd/yO7cZMCfRkYp7ITiDVlJvMBDsu5o3LCHjtmoel1bTJWn13uVO7jOHsCEHe8Wprw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 53A4F3858C51 Received: from [88.97.37.36] (helo=deneb.mcrowe.com) by smarthost01c.sbp.mail.zen.net.uk with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1uxtEi-008WVP-BI; Sun, 14 Sep 2025 20:22:12 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mcrowe.com; s=20191005; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description; bh=gmlFviXAwrh++uYUwngpeSUbFn1goqkkDcT/X21af6Y=; b=F4Jro 5ooq/yey90DIvNR8o4o9ceQ9wjhBHU0Ggz0wTHMKHkryf/yQqyiduTFlIL+/y59GV58GqAv6ZUxDg NHJ5aVe98G3RDpUuWD5o8DZyhC3VGc5wVyNpfuYJN6kKwWUpPMYTzbr0XYzFumO2pMqHLHv0Y86UM FMN9dYZ1RcsoHSt/EKod2L+X8QQ30Esiv6WUH3hwbrIYd/3LHtII4z1bDYCRRXTMwV8nimbIa7qN0 5UbFsKu5hyfswbtLMPztPvEBCclWFOQ6JHGybQPEHxAzPQ/LzkLjAjsvfcI87Iq4tgFti/271NeST JjR2XGfEkFRp+f6FmMmfgQOa0RU5A==; Received: from mac by deneb.mcrowe.com with local (Exim 4.96) (envelope-from ) id 1uxtEg-003hKQ-2x; Sun, 14 Sep 2025 21:22:10 +0100 From: Mike Crowe To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Mike Crowe Subject: [PATCH 1/6] libstdc++: Fix __platform_wait_until for negative timeouts [PR116586] Date: Sun, 14 Sep 2025 21:21:28 +0100 Message-Id: <20250914202133.1197710-2-mac@mcrowe.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250914202133.1197710-1-mac@mcrowe.com> References: <20250914202133.1197710-1-mac@mcrowe.com> MIME-Version: 1.0 X-Originating-smarthost01c-IP: [88.97.37.36] Feedback-ID: 88.97.37.36 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_PASS, 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Passing a timeout from before the epoch to __platform_wait_until() currently results in an exception being thrown because futex(2) doesn't support negative timeouts. Let's just immediately return indicating a timeout has happened. Add test cases to prove that this bug is fixed for std::binary_semaphore (which is just an alias for std::counting_semaphore<1>). The tests exercise cases that aren't problematic with the current code since system_clock is converted to steady_clock before calling __platform_wait_until() is called but they will protect against changes in the implementation reintroducing this bug. PR libstdc++/116586 libstdc++-v3/ChangeLog: * src/c++20/atomic.cc (__platform_wait_until): Return false on timeout before epoch. * testsuite/30_threads/semaphore/try_acquire_for.cc: Add tests. * testsuite/30_threads/semaphore/try_acquire_until.cc: Add tests. Signed-off-by: Mike Crowe --- libstdc++-v3/src/c++20/atomic.cc | 4 ++++ .../30_threads/semaphore/try_acquire_for.cc | 20 +++++++++++++++++ .../30_threads/semaphore/try_acquire_until.cc | 22 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/libstdc++-v3/src/c++20/atomic.cc b/libstdc++-v3/src/c++20/atomic.cc index 4120e1a0817..1e987213d80 100644 --- a/libstdc++-v3/src/c++20/atomic.cc +++ b/libstdc++-v3/src/c++20/atomic.cc @@ -359,6 +359,10 @@ __platform_wait_until(const __platform_wait_t* __addr, static_cast(__ns.count()) }; + // SYS_futex returns EINVAL if timeout is negative, act as if we timed out immediately + if (__rt.tv_sec < 0 || __rt.tv_nsec < 0) + return false; + if (syscall (SYS_futex, __addr, static_cast(__futex_wait_flags::__wait_bitset_private), __old, &__rt, nullptr, diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc index 39681c7ee56..fe0d704baf8 100644 --- a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc +++ b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc @@ -90,9 +90,29 @@ test03() s.try_acquire_for(timeout); } +// Prove semaphore doesn't suffer from PR116586 +template +void +test_relative(std::chrono::nanoseconds offset) +{ + std::binary_semaphore sem(1); + VERIFY(sem.try_acquire_for(offset)); + VERIFY(!sem.try_acquire_for(offset)); +} + int main() { test01(); test02(); test03(); + for (const std::chrono::nanoseconds offset : { + std::chrono::nanoseconds{0}, + std::chrono::duration_cast(std::chrono::milliseconds{-10}), + std::chrono::duration_cast(std::chrono::seconds{-10}) + }) { + test_relative(offset); + test_relative(offset - std::chrono::system_clock::now().time_since_epoch()); + test_relative(offset); + test_relative(offset - std::chrono::steady_clock::now().time_since_epoch()); + } } diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc index de0068d670a..a00f124b147 100644 --- a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc +++ b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc @@ -87,8 +87,30 @@ void test02() b.wait(1); } +// Prove semaphore doesn't suffer from PR116586 +template +void +test_absolute(std::chrono::nanoseconds offset) +{ + std::binary_semaphore sem(1); + std::chrono::time_point tp(offset); + VERIFY(sem.try_acquire_until(tp)); + VERIFY(!sem.try_acquire_until(tp)); +} + int main() { test01(); test02(); + for (const std::chrono::nanoseconds offset : { + // tv_sec == 0, tv_nsec == 0 + std::chrono::nanoseconds{0}, + // tv_sec == 0, tv_nsec < 0 + std::chrono::duration_cast(std::chrono::milliseconds{-10}), + // tv_sec < 0 + std::chrono::duration_cast(std::chrono::seconds{-10}) + }) { + test_absolute(offset); + test_absolute(offset); + } } From patchwork Sun Sep 14 20:21:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Crowe X-Patchwork-Id: 120227 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6B0293858C24 for ; Sun, 14 Sep 2025 20:33:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6B0293858C24 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=mcrowe.com header.i=@mcrowe.com header.a=rsa-sha256 header.s=20191005 header.b=YEBBF6MZ X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smarthost01c.sbp.mail.zen.net.uk (smarthost01c.sbp.mail.zen.net.uk [212.23.1.5]) by sourceware.org (Postfix) with ESMTPS id 5D55E3857BAF; Sun, 14 Sep 2025 20:22:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5D55E3857BAF Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=mcrowe.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mcrowe.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5D55E3857BAF Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.23.1.5 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757881335; cv=none; b=ItqR+hgyKSPxkfFJNbFnBCh4J4iD/9syqJekgqFJXIqxW/mO/S1MjnGq6WuvYH08h9nqVvybJRBPY1/flk6mzWGddrjAZW2u7gTlT9JeavvvND8YVfkHCnbHCtmrF1A7DOkBHWmAEheaVdSihh2eZzVoNM6Gz167U9Td2OtJNYI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757881335; c=relaxed/simple; bh=tgF2qUtlbtkt/REpeim94PQBzbij0cYOuQPQsz01DN8=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=jlS7OYosIdscsUNSvTMZfZhYg/PvUxU17YbuspKEQ/dD1l+BrSEio1qmj+DfM07JREZmKzHXx9exWxsBu3lgYC49P9tZTLsUZ0am9oO6NfbeCe0HXY/wLvBnnbmD4NTal18BX1zMfzLTROYwEEvxHd8GDWkowTg3g809TLwHiuo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5D55E3857BAF Received: from [88.97.37.36] (helo=deneb.mcrowe.com) by smarthost01c.sbp.mail.zen.net.uk with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1uxtEk-008WVd-BC; Sun, 14 Sep 2025 20:22:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mcrowe.com; s=20191005; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description; bh=mlCCOSOJbEMt0tGGge/QxsLFrlRtzEB5JH3WXS0ZsZU=; b=YEBBF 6MZfzmDVLWeTXhVIOUMTVgZHljmmZhbFAouRXgVhRYIG2byOKm8dDDeHBTbbpSp11+Hz5fGzvzFrH wvNjaQVij3C/AI/YezZLkbtNDnFqljMzuSigBKQ9R1J/YF9Iaw7y+eF93XL/bXYzIgJumzyukM84u hbytTIb1Di802J6Lq2OQgrmnC8aHPDKtra9HxjGJ4QCdjiZxJZlZjWrMuJg1086be/NtKC4xlq+Ez 7HFy/QGb2Vi2bZanLGrr32uuaMDBd4EDwaVUIES3SPzC3NIW5BIbb8Q2jxCS8yiKghkmIsNUdEbdU WtY03f3hv29FGkKIQN3HSZY9AXNkA==; Received: from mac by deneb.mcrowe.com with local (Exim 4.96) (envelope-from ) id 1uxtEi-003hLY-39; Sun, 14 Sep 2025 21:22:12 +0100 From: Mike Crowe To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Mike Crowe Subject: [PATCH 2/6] libstdc++: Fix shared_timed_mutex for negative timeouts [PR116586] Date: Sun, 14 Sep 2025 21:21:29 +0100 Message-Id: <20250914202133.1197710-3-mac@mcrowe.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250914202133.1197710-1-mac@mcrowe.com> References: <20250914202133.1197710-1-mac@mcrowe.com> MIME-Version: 1.0 X-Originating-smarthost01c-IP: [88.97.37.36] Feedback-ID: 88.97.37.36 X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_PASS, TXREP, URI_HEX 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Passing a timeout from before the epoch to std::shared_timed_mutex::try_lock_until or std::shared_timed_mutex::try_lock_shared_until causes the POSIX rwlock functions to be passed an invalid negative timeout which results in them returning EINVAL. thread.timedmutex.requirements.general in the C++ standard says: > If abs_time has already passed, the function attempts to obtain > ownership without blocking (as if by calling try_lock()). and a time before the epoch has clearly already passed (see description of libstdc++/PR116586). Let's treat such negative times as being at the epoch so that these methods work correctly. Add test cases to prove that this, and potential similar problems are no longer present. PR libstdc++-v3/116586 libstdc++-v3/ChangeLog: * include/std/shared_mutex: (shared_timed_mutex::try_lock_until, shared_timed_mutex::try_lock_shared_until): Ensure that negative timeout is not passed to POSIX rwlock functions. * testsuite/30_threads/shared_timed_mutex/try_lock_until/116586.cc: New test. Signed-off-by: Mike Crowe --- libstdc++-v3/include/std/shared_mutex | 20 ++++ .../try_lock_until/116586.cc | 94 +++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 libstdc++-v3/testsuite/30_threads/shared_timed_mutex/try_lock_until/116586.cc diff --git a/libstdc++-v3/include/std/shared_mutex b/libstdc++-v3/include/std/shared_mutex index 94c8532399d..ef0f1df5a2b 100644 --- a/libstdc++-v3/include/std/shared_mutex +++ b/libstdc++-v3/include/std/shared_mutex @@ -529,6 +529,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_cast(__ns.count()) }; + if (__ts.tv_sec < 0 || __ts.tv_nsec < 0) { + __ts.tv_sec = 0; + __ts.tv_nsec = 0; + } + int __ret = __glibcxx_rwlock_timedwrlock(&_M_rwlock, &__ts); // On self-deadlock, we just fail to acquire the lock. Technically, // the program violated the precondition. @@ -555,6 +560,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_cast(__ns.count()) }; + if (__ts.tv_sec < 0 || __ts.tv_nsec < 0) { + __ts.tv_sec = 0; + __ts.tv_nsec = 0; + } + int __ret = pthread_rwlock_clockwrlock(&_M_rwlock, CLOCK_MONOTONIC, &__ts); // On self-deadlock, we just fail to acquire the lock. Technically, @@ -605,6 +615,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_cast(__ns.count()) }; + if (__ts.tv_sec < 0 || __ts.tv_nsec < 0) { + __ts.tv_sec = 0; + __ts.tv_nsec = 0; + } + int __ret; // Unlike for lock(), we are not allowed to throw an exception so if // the maximum number of read locks has been exceeded, or we would @@ -645,6 +660,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_cast(__ns.count()) }; + if (__ts.tv_sec < 0 || __ts.tv_nsec < 0) { + __ts.tv_sec = 0; + __ts.tv_nsec = 0; + } + int __ret = pthread_rwlock_clockrdlock(&_M_rwlock, CLOCK_MONOTONIC, &__ts); // On self-deadlock, we just fail to acquire the lock. Technically, diff --git a/libstdc++-v3/testsuite/30_threads/shared_timed_mutex/try_lock_until/116586.cc b/libstdc++-v3/testsuite/30_threads/shared_timed_mutex/try_lock_until/116586.cc new file mode 100644 index 00000000000..4ed650a78cf --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/shared_timed_mutex/try_lock_until/116586.cc @@ -0,0 +1,94 @@ +// { dg-do run { target c++14 } } + +#include +#include +#include + +namespace chrono = std::chrono; + +// thread.timedmutex.requirements.general: +// If abs_time has already passed, the function attempts to obtain +// ownership without blocking (as if by calling try_lock()). + +template +void +test_exclusive_absolute(chrono::nanoseconds offset) +{ + std::shared_timed_mutex stm; + chrono::time_point tp(offset); + VERIFY(stm.try_lock_until(tp)); + VERIFY(!stm.try_lock_until(tp)); +} + +template +void +test_shared_absolute(chrono::nanoseconds offset) +{ + std::shared_timed_mutex stm; + chrono::time_point tp(offset); + VERIFY(stm.try_lock_shared_until(tp)); + stm.unlock_shared(); + + VERIFY(stm.try_lock_for(chrono::seconds{10})); + + { + // NPTL will give us EDEADLK if pthread_rwlock_timedrdlock() is called on + // the same thread that already holds the exclusive (write) lock, so let's + // arrange for a different thread to try to acquire the shared lock. + auto t = std::async(std::launch::async, [&stm, tp]() { + VERIFY(!stm.try_lock_shared_until(tp)); + }); + } +} + +// The type of clock used for the actual wait depends on whether +// _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK is defined. We might as well just test +// both steady_clock and system_clock. +template +void +test_exclusive_relative(chrono::nanoseconds offset) +{ + std::shared_timed_mutex stm; + const auto d = -Clock::now().time_since_epoch() + offset; + VERIFY(stm.try_lock_for(d)); + VERIFY(!stm.try_lock_for(d)); +} + +template +void +test_shared_relative(chrono::nanoseconds offset) +{ + std::shared_timed_mutex stm; + const auto d = -Clock::now().time_since_epoch() + offset; + VERIFY(stm.try_lock_shared_for(d)); + stm.unlock_shared(); + // Should complete immediately + VERIFY(stm.try_lock_for(chrono::seconds{10})); + VERIFY(!stm.try_lock_shared_for(d)); +} + +int main() +{ + // Try once with an offset that ought to result in tv_sec == 0, tv_nsec < 0 + // and one with an offset that ought to result in tv_sec < 0, tv_nsec == 0 + // for the absolute calls at least. It's not really possible to arrange for + // the relative calls to have tv_nsec == 0 due to time advancing. + for (const chrono::nanoseconds offset : { + // tv_sec == 0, tv_nsec == 0 + chrono::nanoseconds{0}, + // tv_sec == 0, tv_nsec < 0 + chrono::duration_cast(chrono::milliseconds{-10}), + // tv_sec < 0 + chrono::duration_cast(chrono::seconds{-10}) + }) { + test_exclusive_absolute(offset); + test_shared_absolute(offset); + test_exclusive_relative(offset); + test_shared_relative(offset); + + test_exclusive_absolute(offset); + test_shared_absolute(offset); + test_exclusive_relative(offset); + test_shared_relative(offset); + } +} From patchwork Sun Sep 14 20:21:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Crowe X-Patchwork-Id: 120229 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A029C3858CD1 for ; Sun, 14 Sep 2025 20:39:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A029C3858CD1 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=mcrowe.com header.i=@mcrowe.com header.a=rsa-sha256 header.s=20191005 header.b=AwMl/znO X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smarthost01a.ixn.mail.zen.net.uk (smarthost01a.ixn.mail.zen.net.uk [212.23.1.20]) by sourceware.org (Postfix) with ESMTPS id 7F98B3858C2F; Sun, 14 Sep 2025 20:22:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7F98B3858C2F Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=mcrowe.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mcrowe.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7F98B3858C2F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.23.1.20 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757881344; cv=none; b=vCI0dLfDwORDfuXrHa+/uWkkwDyAU0OTJEXYM/uc328I9kQb/8jUnfBCwNKOpjw4h33X0kgnZhUeecB8Vqp2qYzl2A803a8SVi5swLtk/umMrHEIBxRnd2kmj8quj4k6XrSGT7Jl1SORu+KaJlxPjxf97gl4ekPiR+vAKkXa/8k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757881344; c=relaxed/simple; bh=/gwzZcLQ/TG3JI99x1EeGgjgviTud1eyHn8tdwMr7NM=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=rFT/F8nC5NRgvWpKiFHU3p9c09hnUeLyUUl8ofBLcrdYvIuNGaonI8QfLx3EcyhXCh4ZPHxBK54DtXytRQ/uek64D6J96sLjNacBc8Gpsot/zyug1MP8KqucIwH6A8m+bjdOnkyjit+1ocUdI5QzapcStEOUS/vNF8hKczRWF/Y= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7F98B3858C2F Received: from [88.97.37.36] (helo=deneb.mcrowe.com) by smarthost01a.ixn.mail.zen.net.uk with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1uxtEt-0062oS-Eq; Sun, 14 Sep 2025 20:22:23 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mcrowe.com; s=20191005; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description; bh=RVomBYTbQXG4S7tl+mUhVyINWOyzOJoNJcBsse14BrE=; b=AwMl/ znOT7RHgxoriAr8K1n78TXQ7DMqBxxNNwHOvD7yzAopLv5OWIgMTyS/DPCK00G5lY5Rx7VspE1bk9 2E0N3F2ebWz1cgzkBq0aDUFS8qPwuKbxTyaJ8OcDZ50Cd6l4k4G3Oc4w9lfC5F6//lFYAZnx6ub1z QNhjI59wkPYnDeSxFdHJr4Lw0mKUhcl3iOiBcTNPkF2mc0qZBx4GOIJZJuEIoTmBsabhCiZmLv0w5 H22fb40FYj2Lr1On8s9pkRJ7t8b/oLQQEbMUmy6Z8jFx1SQUUDs8XSEnJoUWV+ahO7Z0l3Er2Ccxd rxLNCTGHEPEOJd3XI6t8dLmQ+KZuA==; Received: from mac by deneb.mcrowe.com with local (Exim 4.96) (envelope-from ) id 1uxtEk-003hMX-0E; Sun, 14 Sep 2025 21:22:14 +0100 From: Mike Crowe To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Mike Crowe Subject: [PATCH 3/6] libstdc++: Add condition_variable tests for negative timeouts Date: Sun, 14 Sep 2025 21:21:30 +0100 Message-Id: <20250914202133.1197710-4-mac@mcrowe.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250914202133.1197710-1-mac@mcrowe.com> References: <20250914202133.1197710-1-mac@mcrowe.com> MIME-Version: 1.0 X-Originating-smarthost01a-IP: [88.97.37.36] Feedback-ID: 88.97.37.36 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_PASS, TXREP, URI_HEX 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Add tests to show that std::condition_variable::wait_until and std::condition_variable::wait_for don't suffer from libstdc++/PR116586. libstdc++-v3/ChangeLog: * testsuite/30_threads/condition_variable/members/116586.c: New test. Signed-off-by: Mike Crowe --- .../condition_variable/members/116586.cc | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 libstdc++-v3/testsuite/30_threads/condition_variable/members/116586.cc diff --git a/libstdc++-v3/testsuite/30_threads/condition_variable/members/116586.cc b/libstdc++-v3/testsuite/30_threads/condition_variable/members/116586.cc new file mode 100644 index 00000000000..d10f8036bb6 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/condition_variable/members/116586.cc @@ -0,0 +1,58 @@ +// { dg-do run { target c++11 } } + +#include +#include +#include + +namespace chrono = std::chrono; + +// thread.timedmutex.requirements.general: +// If abs_time has already passed, the function attempts to obtain +// ownership without blocking (as if by calling try_lock()). + +template +void +test_absolute(chrono::nanoseconds offset) +{ + std::mutex mtx; + std::condition_variable cv; + chrono::time_point tp(offset); + std::unique_lock lock(mtx); + // Doesn't cope with spurious wakeup + VERIFY(cv.wait_until(lock, tp) == std::cv_status::timeout); +} + +// The type of clock used for the actual wait depends on whether +// _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK is defined. We might as well just test +// both steady_clock and system_clock. +template +void +test_relative(chrono::nanoseconds offset) +{ + std::mutex mtx; + std::condition_variable cv; + const auto d = -Clock::now().time_since_epoch() + offset; + std::unique_lock lock(mtx); + // Doesn't cope with spurious wakeup + VERIFY(cv.wait_for(lock, d) == std::cv_status::timeout); +} + +int main() +{ + // It's not really possible to arrange for the relative calls to have + // tv_nsec == 0 due to time advancing. + for (const chrono::nanoseconds offset : { + // tv_sec == 0, tv_nsec == 0 + chrono::nanoseconds{0}, + // tv_sec == 0, tv_nsec < 0 + chrono::duration_cast(chrono::milliseconds{-10}), + // tv_sec < 0 + chrono::duration_cast(chrono::seconds{-10}) + }) { + test_absolute(offset); + test_relative(offset); + + test_absolute(offset); + test_relative(offset); + } +} From patchwork Sun Sep 14 20:21:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Crowe X-Patchwork-Id: 120228 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1D6443857BA5 for ; Sun, 14 Sep 2025 20:37:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1D6443857BA5 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=mcrowe.com header.i=@mcrowe.com header.a=rsa-sha256 header.s=20191005 header.b=lAA73/mK X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smarthost01b.sbp.mail.zen.net.uk (smarthost01b.sbp.mail.zen.net.uk [212.23.1.3]) by sourceware.org (Postfix) with ESMTPS id A7EB83858C40; Sun, 14 Sep 2025 20:22:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A7EB83858C40 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=mcrowe.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mcrowe.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A7EB83858C40 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.23.1.3 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757881337; cv=none; b=FZcDAY7nHoSlcG3gkSFFJLzY2kDKCslwGd0rUJfkL9Ctx11SE6tsli4mpZkTjYnkv4/xLgN/DiMhtitpZ+/5cc/nXyX/U2wF8Kxwr9aZVK7jtxWS7cL/fDaDOgo3PLawnkqhxNGiBmL+Q+E0/4xRz4Uwwiw8x6SF8Y1wgSF3MVI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757881337; c=relaxed/simple; bh=G+kdS8Wm8+7PnsJzilm6xHCg6sXuhoLO172Lf/CAZdg=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=IXpsqOfFpwPIZBn+9MPVLATi7lYv8QJ3T8lL/aA9rw4t3RycohHASV+X/aEahN730b/KxNFeRdbfUu3S3EmklAioKxWyOet4tnNeiwjdpCfrDLadP4qZtX2H0WskX7lZRPCdJUQP62rm3EnAbMWzCDQCm0DONDjwtzeJX2OQeio= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A7EB83858C40 Received: from [88.97.37.36] (helo=deneb.mcrowe.com) by smarthost01b.sbp.mail.zen.net.uk with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1uxtEm-00CHqT-6P; Sun, 14 Sep 2025 20:22:16 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mcrowe.com; s=20191005; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description; bh=cXyWNy9/WqW1lW23FvlQIQFV2E1t/5GXq33Vwk/bWtg=; b=lAA73 /mKzr209kig13kVFfIfcmKh+0HdWs8BAxgO7KGu+23Zf1Y2C6GsUR9IGaT5c1Ehv0LuybWYJh5wvR SJUa4gq6XQiYY5uDsM2Q6AU2vRooGxRB9JHCVwfOItHiphqn+UuXDsjvwfKnKLKiGfkTweuAEjVG7 e/FUML9NvON6WEOo5SZ0ymByotnNCNO0pfJ+DB0cGTj2jFeCqDz4YUI0uXs5eCJMsafclkct/fIul ItqKQ1sHj0gZmtONBk3FEdpZnYhjdJSIlsdXQEwn473MLBGXm+706611GNqwYJse0Ub/08tWQRZiG b7wySe5VUUqnyKsMhfp2EF5gVw92A==; Received: from mac by deneb.mcrowe.com with local (Exim 4.96) (envelope-from ) id 1uxtEl-003hNc-0D; Sun, 14 Sep 2025 21:22:15 +0100 From: Mike Crowe To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Mike Crowe Subject: [PATCH 4/6] libstdc++: Add test future tests for negative timeouts Date: Sun, 14 Sep 2025 21:21:31 +0100 Message-Id: <20250914202133.1197710-5-mac@mcrowe.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250914202133.1197710-1-mac@mcrowe.com> References: <20250914202133.1197710-1-mac@mcrowe.com> MIME-Version: 1.0 X-Originating-smarthost01b-IP: [88.97.37.36] Feedback-ID: 88.97.37.36 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_PASS, TXREP, URI_HEX 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Add tests to show that std::future::wait_until and std::future::wait_for don't suffer from libstdc++/PR116586. libstdc++-v3/ChangeLog: * testsuite/30_threads/future/members/116586.cc: New test. Signed-off-by: Mike Crowe --- .../30_threads/future/members/116586.cc | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 libstdc++-v3/testsuite/30_threads/future/members/116586.cc diff --git a/libstdc++-v3/testsuite/30_threads/future/members/116586.cc b/libstdc++-v3/testsuite/30_threads/future/members/116586.cc new file mode 100644 index 00000000000..6d753e189a0 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/future/members/116586.cc @@ -0,0 +1,54 @@ +// { dg-do run { target c++11 } } + +#include +#include +#include + +namespace chrono = std::chrono; + +// thread.timedmutex.requirements.general: +// If abs_time has already passed, the function attempts to obtain +// ownership without blocking (as if by calling try_lock()). + +template +void +test_absolute(chrono::nanoseconds offset) +{ + std::promise p; + std::future f = p.get_future(); + const chrono::time_point tp(offset); + VERIFY(f.wait_until(tp) == std::future_status::timeout); +} + +// The type of clock used for the actual wait depends on whether +// _GLIBCXX_HAVE_LINUX_FUTEX is defined. We might as well just test both +// steady_clock and system_clock. +template +void +test_relative(chrono::nanoseconds offset) +{ + std::promise p; + std::future f = p.get_future(); + const auto d = -Clock::now().time_since_epoch() + offset; + VERIFY(f.wait_for(d) == std::future_status::timeout); +} + +int main() +{ + // It's not really possible to arrange for the relative calls to have tv_nsec + // == 0 due to time advancing. + for (const chrono::nanoseconds offset : { + // tv_sec == 0, tv_nsec == 0 + chrono::nanoseconds{0}, + // tv_sec == 0, tv_nsec < 0 + chrono::duration_cast(chrono::milliseconds{-10}), + // tv_sec < 0 + chrono::duration_cast(chrono::seconds{-10}) + }) { + test_absolute(offset); + test_relative(offset); + + test_absolute(offset); + test_relative(offset); + } +} From patchwork Sun Sep 14 20:21:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Crowe X-Patchwork-Id: 120224 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 701833858D39 for ; Sun, 14 Sep 2025 20:26:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 701833858D39 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=mcrowe.com header.i=@mcrowe.com header.a=rsa-sha256 header.s=20191005 header.b=V/hyut/y X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smarthost01c.sbp.mail.zen.net.uk (smarthost01c.sbp.mail.zen.net.uk [212.23.1.5]) by sourceware.org (Postfix) with ESMTPS id 6D2C13857B90; Sun, 14 Sep 2025 20:22:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6D2C13857B90 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=mcrowe.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mcrowe.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6D2C13857B90 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.23.1.5 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757881346; cv=none; b=OvpTvoOBdrr6J3vfOnEHaXrTamC7ZNXgihtwvN3dK23H3QWqnicG5m9W1+lnue+aqFl7QiYapyYEf9Zhb++6beDJRDtJ82COXWNlN259tQnxMVJJnZGSePzFJlVVUGQVJt8TapxQIP5PFDAv2sjGSLKXvXlyA2PkOlj5TX7tXrw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757881346; c=relaxed/simple; bh=cpPbCAMv8QDzCmlqUZbWybGo/ZVdqah11rKR4cJP4KI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=GnpH/PmKoQowo0w5yzanUFD15pEipCFZpU3XRETraLCfvyhBDRLOoVpMrG04CLsZeIMfT6+1N9hvtr3S7IOfPew/D2TudDKVFreDB43BhESImH/vNje9RbzEQVdmZrN6Vm/VB5kRijGtTqHMoYDzyByM+WqWcRHiTE8f6jVfbdY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6D2C13857B90 Received: from [88.97.37.36] (helo=deneb.mcrowe.com) by smarthost01c.sbp.mail.zen.net.uk with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1uxtEv-008WX1-F8; Sun, 14 Sep 2025 20:22:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mcrowe.com; s=20191005; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description; bh=47LCam+xDiRLib3gmT/l0gZTdlHeJDHbqT16LBbTAPs=; b=V/hyu t/yOG4NID/ILdP7TwmrIfVOauRfZ9oqNcYzS9bw3d/lccDkoCix2WuwqPWOTTTMrLjbzQ4J8oB6D6 DTFMTZLFI8U00bBDYZ4BaTA2MNqdevar1KzR3ND8zen22EYW4Q8DRHEtYY+HAC82wOdWU9dOBrYLE tWgkA0pT/vRfR0XdHISD1t4jLGXfmPMaVWdgapCyOVqaHa004M4hPE2IJEBraFYVmZjEm0VTDCbsI KnQoqQYQUGHI+1DgN5xH0dvgd4AzgZppxhffWSMwmCf/SwUa/x4xUKnSutaGJ73R5waWpDoaXMT8d bq7Jj/SPKmbOG80RspQAytu3pIGMQ==; Received: from mac by deneb.mcrowe.com with local (Exim 4.96) (envelope-from ) id 1uxtEm-003hPH-0W; Sun, 14 Sep 2025 21:22:16 +0100 From: Mike Crowe To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Mike Crowe Subject: [PATCH 5/6] libstdc++: Add recursive_timed_mutex tests for negative timeouts Date: Sun, 14 Sep 2025 21:21:32 +0100 Message-Id: <20250914202133.1197710-6-mac@mcrowe.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250914202133.1197710-1-mac@mcrowe.com> References: <20250914202133.1197710-1-mac@mcrowe.com> MIME-Version: 1.0 X-Originating-smarthost01c-IP: [88.97.37.36] Feedback-ID: 88.97.37.36 X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_PASS, TXREP, URI_HEX 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Add tests to show that std::recursive_timed_mutex::try_lock_until and std::recursive_timed_mutex::try_lock_for don't suffer from libstdc++/PR116586. libstdc++-v3/ChangeLog: * testsuite/30_threads/recursive_timed_mutex/try_lock_until/116586.cc: New test. Signed-off-by: Mike Crowe --- .../try_lock_until/116586.cc | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/try_lock_until/116586.cc diff --git a/libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/try_lock_until/116586.cc b/libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/try_lock_until/116586.cc new file mode 100644 index 00000000000..af7a4bef5f6 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/try_lock_until/116586.cc @@ -0,0 +1,70 @@ +// { dg-do run { target c++11 } } + +#include +#include +#include + +namespace chrono = std::chrono; + +// thread.timedmutex.requirements.general: +// If abs_time has already passed, the function attempts to obtain +// ownership without blocking (as if by calling try_lock()). + +template +void +test_absolute(chrono::nanoseconds offset) +{ + std::recursive_timed_mutex mtx; + chrono::time_point tp(offset); + VERIFY(mtx.try_lock_until(tp)); + + { + // To test failing to lock a recursive mutex we need to try to lock on a + // different thread. + auto t = std::async(std::launch::async, [&mtx, tp]() { + VERIFY(!mtx.try_lock_until(tp)); + }); + } +} + +// The type of clock used for the actual wait depends on whether +// _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK is defined. We might as well just test +// both steady_clock and system_clock. +template +void +test_relative(chrono::nanoseconds offset) +{ + std::recursive_timed_mutex mtx; + const auto d = -Clock::now().time_since_epoch() + offset; + VERIFY(mtx.try_lock_for(d)); + + { + // To test failing to lock a recursive mutex we need to try to lock on a + // different thread. + auto t = std::async(std::launch::async, [&mtx, d]() { + VERIFY(!mtx.try_lock_for(d)); + }); + } +} + +int main() +{ + // Try once with an offset that ought to result in tv_sec == 0, tv_nsec < 0 + // and one with an offset that ought to result in tv_sec < 0, tv_nsec == 0 + // for the absolute calls at least. It's not really possible to arrange for + // the relative calls to have tv_nsec == 0 due to time advancing. + for (const chrono::nanoseconds offset : { + // tv_sec == 0, tv_nsec == 0 + chrono::nanoseconds{0}, + // tv_sec == 0, tv_nsec < 0 + chrono::duration_cast(chrono::milliseconds{-10}), + // tv_sec < 0 + chrono::duration_cast(chrono::seconds{-10}) + }) { + test_absolute(offset); + test_relative(offset); + + test_absolute(offset); + test_relative(offset); + } +} From patchwork Sun Sep 14 20:21:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Crowe X-Patchwork-Id: 120225 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0A7483857BA5 for ; Sun, 14 Sep 2025 20:26:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0A7483857BA5 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=mcrowe.com header.i=@mcrowe.com header.a=rsa-sha256 header.s=20191005 header.b=ES89QP4p X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smarthost01c.ixn.mail.zen.net.uk (smarthost01c.ixn.mail.zen.net.uk [212.23.1.22]) by sourceware.org (Postfix) with ESMTPS id 9D98B3858CD1; Sun, 14 Sep 2025 20:22:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9D98B3858CD1 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=mcrowe.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mcrowe.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9D98B3858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.23.1.22 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757881339; cv=none; b=FoPgoC8hu48L/WSLZFMsAlwLroPScBX1yg7za1FxlAV0JXK5MSYTvwddNx7t5SevEDgOy3BhBhFJcbA5Pj91Q39PGOulwMwA65KZPK56huQNSh87O6nUW+hEydLWYtdQ1YqliTL8Ib9cLU/8AVSdodEIxp3gwO4YrRfBczGFxvc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757881339; c=relaxed/simple; bh=CHXShGfmDM0M7yMl/V4/gOL4cDvKaoVX24+fwatdjZU=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=ok5/KPO1WHTdKEksdeycdVmkjCu8AH7I91+Umt/1JA4bTILp0+wTXmon2azXPmjssYVjnYeaRiwoyrwnIvM0FhZO4Rxgzc9DrMzpR1BDL4U+oHHKy30nAoC90Gy+dPrCkVX5nAPorqj0S4d5B96wOHJo5ObbnwsrGoSoEr8N6LY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9D98B3858CD1 Received: from [88.97.37.36] (helo=deneb.mcrowe.com) by smarthost01c.ixn.mail.zen.net.uk with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1uxtEo-00CzjA-LY; Sun, 14 Sep 2025 20:22:18 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mcrowe.com; s=20191005; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description; bh=MMvoRUAOF4Engha2zEpaMXCJk58lJslUlHRbi3+CkWc=; b=ES89Q P4p9uEeKKqVrPrYXK/GmExoEtF4icT3FJnzH8vGgyFoUtrK1GqiULGx6M1gPOrrAEan2zpxIBA9m2 MdvnRDA3/VSu88AVadA1lyu2+76vQZXCnVvE9+zkXyA9FzykkCdJ/0dSB9+P03WiftQeULL+1224L 5qRpnCnKAqCS7c90aWu61ypN9CtCFFPtDBljwgVQdvHyHD9LJ008FXWJdPu9Ps3SOOcK+4VXKbQC1 fJuS3KPCVdzaTIVkB8bNe5bLuVcHw4nbBJG3AX/yPeKYeKLdUurBo1GrhzS7C14l14f3lGRpvfrhi 48s+nZcIObsKV6cKwqf1PrgGiVLyw==; Received: from mac by deneb.mcrowe.com with local (Exim 4.96) (envelope-from ) id 1uxtEn-003hQS-0v; Sun, 14 Sep 2025 21:22:17 +0100 From: Mike Crowe To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Mike Crowe Subject: [PATCH 6/6] libstdc++: Add timed_mutex tests for negative timeouts Date: Sun, 14 Sep 2025 21:21:33 +0100 Message-Id: <20250914202133.1197710-7-mac@mcrowe.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250914202133.1197710-1-mac@mcrowe.com> References: <20250914202133.1197710-1-mac@mcrowe.com> MIME-Version: 1.0 X-Originating-smarthost01c-IP: [88.97.37.36] Feedback-ID: 88.97.37.36 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_PASS, TXREP, URI_HEX 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Add tests to show that std::timed_mutex::try_lock_until and std::timed_mutex::try_lock_for don't suffer from libstdc++/PR116586. libstdc++-v3/ChangeLog: * testsuite/30_threads/timed_mutex/try_lock_until/116586.cc: New test. Signed-off-by: Mike Crowe --- .../timed_mutex/try_lock_until/116586.cc | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/116586.cc diff --git a/libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/116586.cc b/libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/116586.cc new file mode 100644 index 00000000000..13918430a31 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/116586.cc @@ -0,0 +1,56 @@ +// { dg-do run { target c++11 } } + +#include +#include +#include + +namespace chrono = std::chrono; + +// thread.timedmutex.requirements.general: +// If abs_time has already passed, the function attempts to obtain +// ownership without blocking (as if by calling try_lock()). + +template +void +test_absolute(chrono::nanoseconds offset) +{ + std::timed_mutex mtx; + chrono::time_point tp(offset); + VERIFY(mtx.try_lock_until(tp)); + VERIFY(!mtx.try_lock_until(tp)); +} + +// The type of clock used for the actual wait depends on whether +// _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK is defined. We might as well just test +// both steady_clock and system_clock. +template +void +test_relative(chrono::nanoseconds offset) +{ + std::timed_mutex mtx; + const auto d = -Clock::now().time_since_epoch() + offset; + VERIFY(mtx.try_lock_for(d)); + VERIFY(!mtx.try_lock_for(d)); +} + +int main() +{ + // Try once with an offset that ought to result in tv_sec == 0, tv_nsec < 0 + // and one with an offset that ought to result in tv_sec < 0, tv_nsec == 0 + // for the absolute calls at least. It's not really possible to arrange for + // the relative calls to have tv_nsec == 0 due to time advancing. + for (const chrono::nanoseconds offset : { + // tv_sec == 0, tv_nsec == 0 + chrono::nanoseconds{0}, + // tv_sec == 0, tv_nsec < 0 + chrono::duration_cast(chrono::milliseconds{-10}), + // tv_sec < 0 + chrono::duration_cast(chrono::seconds{-10}) + }) { + test_absolute(offset); + test_relative(offset); + + test_absolute(offset); + test_relative(offset); + } +}