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); + } +}