[PATCH] libstdc++: Tolerate float rounding on coarse-tick clocks in 68519.cc test [PR127523]
Gokbey Gazi Keskin
gokbeykeskin@gmail.com
Wed Nov 18 06:34:07 GMT 2026
test_wait_until() in 68519.cc verifies that condition_variable::wait_until
with a user-defined clock returns cv_status::timeout. The clock has a float
duration and a recent epoch, and its now() converts a steady_clock reading
to duration<float>.
On a system where clock_gettime advances in coarse ticks (2 ms and 500 us
were observed) and pthread_cond_timedwait returns within the very tick
that equals the deadline, clock::now() after the wait reads exactly the
deadline tick. Converting that value to float rounds twice, once from the
nanosecond count and once in the division, and start + 1.0f rounds again,
so clock::now() can be one or two float ULPs (about 0.5 to 1 us) before
wait_time. The final _Clock::now() < __atime check in the generic
wait_until overload then correctly returns no_timeout, which the test
treats as a failure.
The library behaves as specified: the caller's clock has not reached the
deadline, so this is a spurious wakeup as seen by that clock. On Linux and
most other systems the wake-up latency of tens of microseconds hides the
rounding, so the test only fails where a timed wait wakes with no latency
beyond the clock tick. Emulating such a clock on Linux by rounding
steady_clock::now() down to a 2 ms tick makes about a quarter of the
iterations return no_timeout.
Accept a no_timeout result only when clock::now() is within a few float
ULPs of wait_time, then wait once more and require timeout. The bound is
computed from numeric_limits<float>::epsilon() and wait_time itself,
because a float ULP doubles each time the time since the epoch crosses a
power of two, so an absolute bound would only hold for short runs. A few
ULPs is still far below the rounding error the PR68519 fix guards against.
The same rounding bites a second time when wait_until does return
timeout. The test then compares elapsed = clock::now() - start with
seconds(1). On the deadline tick elapsed equals wait_time - start, and
start + 1.0f was itself rounded: a start in [2, 4) seconds has a float
ULP of 2^-22 while start + 1 lies in [4, 8) with an ULP of 2^-21, so half
of the possible sums are ties and round-to-even sends half of those
down. Then elapsed is 1 - 2^-22 and the check fails although the clock
did reach wait_time. Replaying the test's arithmetic over every possible
start offset on a 500 us tick, about 8% of the offsets fail this way, and
forcing those offsets on the target reproduces the failure every time.
Compare elapsed with wait_time - start, the deadline the test actually
computed, instead of with a fresh seconds(1).
PR libstdc++/127523
libstdc++-v3/ChangeLog:
* testsuite/30_threads/condition_variable/members/68519.cc
(test_wait_until): Wait again if wait_until returns no_timeout
within a few float ULPs of the deadline. Compare the elapsed time
with wait_time - start instead of seconds(1).
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: Gokbey Gazi Keskin <gokbeykeskin@gmail.com>
---
Tested on x86_64-pc-linux-gnu (the modified test against a trunk build),
where the test passed before and after the change. The failure itself was
seen on aarch64 with a custom POSIX OS whose clock_gettime advances in
500 us ticks and whose timed waits wake exactly on the deadline tick. On
that target the first hunk was confirmed in the testsuite; for the
second, forcing the predicted start offsets reproduced the elapsed-time
failure with the original assertion every time, and replaying the test's
arithmetic over every possible tick offset shows no failure with the
patched assertions.
An alternative, changing the library to push the converted deadline past
the next representable float of the caller's clock, was rejected: the
library already returns the status the caller's clock justifies, and
only the test's own float comparisons are an ULP off.
To reproduce on Linux, round steady_clock::now() down to a coarse tick in
the test's clock, as described in the commit message.
I don't have commit access; please push for me if approved.
.../condition_variable/members/68519.cc | 25 ++++++++++++++++---
1 file changed, 22 insertions(+), 3 deletions(-)
diff --git a/libstdc++-v3/testsuite/30_threads/condition_variable/members/68519.cc b/libstdc++-v3/testsuite/30_threads/condition_variable/members/68519.cc
index 21adf2c0ebf..6a62c0b5596 100644
--- a/libstdc++-v3/testsuite/30_threads/condition_variable/members/68519.cc
+++ b/libstdc++-v3/testsuite/30_threads/condition_variable/members/68519.cc
@@ -21,6 +21,7 @@
// { dg-require-gthreads "" }
#include <condition_variable>
+#include <limits>
#include <testsuite_hooks.h>
// PR libstdc++/68519
@@ -80,12 +81,30 @@ test_wait_until()
const auto start = clock::now();
const auto wait_time = start + std::chrono::duration<float>{1.0};
- // In theory we could get a spurious wakeup, but in practice we won't.
- const auto result = cv.wait_until(l, wait_time);
+ // A spurious wakeup is possible. It happens on a system where clock_gettime
+ // advances in coarse ticks and the timed wait returns within the tick that
+ // equals the deadline: clock::now() then reads exactly the deadline tick,
+ // and rounding in the conversion of that steady_clock time to
+ // duration<float> can place it one or two ULPs before wait_time, so
+ // wait_until correctly returns no_timeout. Accept that only if we really
+ // are at the deadline, within a few ULPs of wait_time, then wait again.
+ auto result = cv.wait_until(l, wait_time);
+ if (result == std::cv_status::no_timeout)
+ {
+ const auto early = wait_time - clock::now();
+ const float ulp = std::numeric_limits<float>::epsilon()
+ * wait_time.time_since_epoch().count();
+ VERIFY( early.count() <= 4 * ulp );
+ result = cv.wait_until(l, wait_time);
+ }
VERIFY( result == std::cv_status::timeout );
const auto elapsed = clock::now() - start;
- VERIFY( elapsed >= std::chrono::seconds(1) );
+ // Compare against the deadline the test actually computed: start + 1.0f
+ // is already rounded to the clock's float precision, so when the wait
+ // ends exactly at the deadline, elapsed can be an ULP short of 1 s even
+ // though the clock did reach wait_time.
+ VERIFY( elapsed >= wait_time - start );
}
}
--
2.43.0
More information about the Libstdc++
mailing list