Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanups for condition_variable's waiting functions #3974

Merged
2 changes: 1 addition & 1 deletion stl/inc/latch
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ _STD_BEGIN
_EXPORT_STD class latch {
public:
_NODISCARD static constexpr ptrdiff_t(max)() noexcept {
return (1ULL << (sizeof(ptrdiff_t) * CHAR_BIT - 1)) - 1;
return PTRDIFF_MAX;
}

constexpr explicit latch(const ptrdiff_t _Expected) noexcept /* strengthened */ : _Counter{_Expected} {
Expand Down
44 changes: 9 additions & 35 deletions stl/inc/mutex
Original file line number Diff line number Diff line change
Expand Up @@ -573,26 +573,13 @@ public:
template <class _Rep, class _Period>
cv_status wait_for(unique_lock<mutex>& _Lck, const chrono::duration<_Rep, _Period>& _Rel_time) {
// wait for duration
if (_Rel_time <= chrono::duration<_Rep, _Period>::zero()) {
return cv_status::timeout;
}

// TRANSITION, ABI: The standard says that we should use a steady clock,
// but unfortunately our ABI relies on the system clock.
_timespec64 _Tgt;
const bool _Clamped = _To_timespec64_sys_10_day_clamped(_Tgt, _Rel_time);
const cv_status _Result = _Wait_until_sys_time(_Lck, &_Tgt);
if (_Clamped) {
return cv_status::no_timeout;
}

return _Result;
return wait_until(_Lck, _To_absolute_time(_Rel_time));
}
achabense marked this conversation as resolved.
Show resolved Hide resolved

template <class _Rep, class _Period, class _Predicate>
bool wait_for(unique_lock<mutex>& _Lck, const chrono::duration<_Rep, _Period>& _Rel_time, _Predicate _Pred) {
// wait for signal with timeout and check predicate
return _Wait_until1(_Lck, _To_absolute_time(_Rel_time), _Pred);
return wait_until(_Lck, _To_absolute_time(_Rel_time), _Pass_fn(_Pred));
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
}

template <class _Clock, class _Duration>
Expand Down Expand Up @@ -623,7 +610,13 @@ public:
#if _HAS_CXX20
static_assert(chrono::is_clock_v<_Clock>, "Clock type required");
#endif // _HAS_CXX20
return _Wait_until1(_Lck, _Abs_time, _Pred);
while (!_Pred()) {
if (wait_until(_Lck, _Abs_time) == cv_status::timeout) {
return _Pred();
}
}

return true;
}

// native_handle_type and native_handle() have intentionally been removed. See GH-3820.
Expand Down Expand Up @@ -658,25 +651,6 @@ private:
return cv_status::timeout;
}
}

template <class _Clock, class _Duration, class _Predicate>
bool _Wait_until1(
unique_lock<mutex>& _Lck, const chrono::time_point<_Clock, _Duration>& _Abs_time, _Predicate& _Pred) {
while (!_Pred()) {
const auto _Now = _Clock::now();
if (_Abs_time <= _Now) {
return false;
}

_timespec64 _Tgt;
const bool _Clamped = _To_timespec64_sys_10_day_clamped(_Tgt, _Abs_time - _Now);
if (_Wait_until_sys_time(_Lck, &_Tgt) == cv_status::timeout && !_Clamped) {
return _Pred();
}
}

return true;
}
};

struct _UInt_is_zero {
Expand Down
2 changes: 1 addition & 1 deletion stl/inc/semaphore
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ _NODISCARD unsigned long _Semaphore_remaining_timeout(const chrono::time_point<_
return static_cast<unsigned long>(_Rel_time.count());
}

inline constexpr ptrdiff_t _Semaphore_max = (1ULL << (sizeof(ptrdiff_t) * CHAR_BIT - 1)) - 1;
inline constexpr ptrdiff_t _Semaphore_max = PTRDIFF_MAX;

_EXPORT_STD template <ptrdiff_t _Least_max_value = _Semaphore_max>
class counting_semaphore {
Expand Down