From: Mike Crowe Date: Sun, 14 Sep 2025 20:21:31 +0000 (+0100) Subject: libstdc++: Add std::future tests for negative timeouts [PR116586] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bb96099a8dc8cf69bb0d2e424c3b38e6365f90f6;p=thirdparty%2Fgcc.git libstdc++: Add std::future tests for negative timeouts [PR116586] Add tests to show that std::future::wait_until and std::future::wait_for correctly handle negative timeouts. libstdc++-v3/ChangeLog: PR libstdc++/116586 * testsuite/30_threads/future/members/116586.cc: New test. Signed-off-by: Mike Crowe --- 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 000000000000..b7cd12c6009c --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/future/members/116586.cc @@ -0,0 +1,55 @@ +// { dg-do run { target c++11 } } + +#include +#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); + } +}