[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
Sat Sep 26 12:23:40 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:
Sure, I'll update the commit message. Also, on platforms without `_LIBCPP_HAS_COND_CLOCKWAIT`, `wait_until` calls `__do_timed_wait`, which used to call back into `wait_for`. That created recursion after the change, so I inlined the old wait_for body into `__do_timed_wait`. The public interface now matches the standard.
https://github.com/llvm/llvm-project/pull/222443
More information about the libcxx-commits
mailing list