From: Mike Crowe Date: Sun, 14 Sep 2025 20:21:28 +0000 (+0100) Subject: libstdc++: Add std::binary_semaphore tests for negative timeouts [PR116586] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e0525a6d2bba07d623424ab6f10a023de90e65a2;p=thirdparty%2Fgcc.git libstdc++: Add std::binary_semaphore tests for negative timeouts [PR116586] Add test cases to prove that negative timeouts are correctly handled by 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. libstdc++-v3/ChangeLog: PR libstdc++/116586 * 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 --- 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..94acb253091 100644 --- a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc +++ b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc @@ -24,6 +24,7 @@ #include #include #include +#include #include void test01() @@ -90,9 +91,30 @@ 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(); + using namespace std::chrono; + for (const nanoseconds offset : { + nanoseconds{0}, + nanoseconds{-10ms}, + nanoseconds{-10s} + }) { + 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..ed6bd118fee 100644 --- a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc +++ b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc @@ -24,6 +24,7 @@ #include #include #include +#include #include void test01() @@ -87,8 +88,31 @@ 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(); + using namespace std::chrono; + for (const nanoseconds offset : { + // tv_sec == 0, tv_nsec == 0 + nanoseconds{0}, + // tv_sec == 0, tv_nsec < 0 + nanoseconds{-10ms}, + // tv_sec < 0 + nanoseconds{-10s} + }) { + test_absolute(offset); + test_absolute(offset); + } }