[libcxx-commits] [libcxx] [libc++][chrono][threading] Implement LWG 3504: `condition_variable::wait_for` is overspecified (PR #222443)
Vishank Singh via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Sep 10 05:53:26 PDT 2026
================
@@ -141,29 +147,7 @@ class _LIBCPP_EXPORTED_FROM_ABI condition_variable {
template <class _Rep, class _Period>
_LIBCPP_HIDE_FROM_ABI cv_status wait_for(unique_lock<mutex>& __lk, const chrono::duration<_Rep, _Period>& __d) {
- using namespace chrono;
- if (__d <= __d.zero())
- return cv_status::timeout;
- using __ns_rep = nanoseconds::rep;
- steady_clock::time_point __c_now = steady_clock::now();
-
-# if _LIBCPP_HAS_COND_CLOCKWAIT
- using __clock_tp_ns = time_point<steady_clock, nanoseconds>;
- __ns_rep __now_count_ns = std::__safe_nanosecond_cast(__c_now.time_since_epoch()).count();
-# else
- using __clock_tp_ns = time_point<system_clock, nanoseconds>;
- __ns_rep __now_count_ns = std::__safe_nanosecond_cast(system_clock::now().time_since_epoch()).count();
-# endif
-
- __ns_rep __d_ns_count = std::__safe_nanosecond_cast(__d).count();
-
- if (__now_count_ns > numeric_limits<__ns_rep>::max() - __d_ns_count) {
- __do_timed_wait(__lk, __clock_tp_ns::max());
- } else {
- __do_timed_wait(__lk, __clock_tp_ns(nanoseconds(__now_count_ns + __d_ns_count)));
- }
-
- return steady_clock::now() - __c_now < __d ? cv_status::no_timeout : cv_status::timeout;
+ return wait_until(__lk, std::__rel_to_abs(__d));
----------------
VishankSingh wrote:
No, this is only LWG 3504. The previous non-predicate `condition_variable::wait_for` had a custom implementation that manually converted to nanoseconds and measured elapsed time.
We can replace that entire path with the standard-mandated form `return wait_until(lock, *rel-to-abs*(rel_time));`
The other overloads were already simple one-liners (`now() + rel_time`), so they only needed a small change.
https://github.com/llvm/llvm-project/pull/222443
More information about the libcxx-commits
mailing list