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

<condition_variable>: Fixes bad timeout in wait_until with unsigned rep #3761

Merged
merged 3 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion stl/inc/condition_variable
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ public:
#if _HAS_CXX20
static_assert(chrono::is_clock_v<_Clock>, "Clock type required");
#endif // _HAS_CXX20
return wait_for(_Lck, _Abs_time - _Clock::now());
using _CommonDuration = common_type_t<_Duration, typename _Clock::duration>;
const auto _Now = _Clock::now();
const _CommonDuration _Rel_time = (_Abs_time <= _Now) ? _CommonDuration::zero() : _Abs_time - _Now;
MattStephanson marked this conversation as resolved.
Show resolved Hide resolved
return wait_for(_Lck, _Rel_time);
}

template <class _Lock, class _Clock, class _Duration, class _Predicate>
Expand Down
35 changes: 31 additions & 4 deletions tests/std/tests/GH_000685_condition_variable_any/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <type_traits>

using namespace std;
using namespace std::chrono;

namespace {
class my_mutex { // user-defined mutex type
Expand All @@ -31,8 +33,8 @@ namespace {
int num_lock = 0;
};

const chrono::nanoseconds interval{20};
const chrono::nanoseconds zero_dur{0};
const nanoseconds interval{20};
const nanoseconds zero_dur{0};

void test_condition_variable_any() { // test wait functions of condition variables
condition_variable_any cnd;
Expand All @@ -46,15 +48,40 @@ namespace {
cnd.wait_for(mtx, interval);
assert(mtx.num_locks() == 3);

cnd.wait_until(mtx, chrono::steady_clock::now());
cnd.wait_until(mtx, steady_clock::now());
assert(mtx.num_locks() == 4);

cnd.wait_until(mtx, chrono::steady_clock::now() + interval);
cnd.wait_until(mtx, steady_clock::now() + interval);
assert(mtx.num_locks() == 5);
}
}

void test_condition_variable_any_already_timed_out() {
using unsigned_duration = duration<make_unsigned_t<system_clock::rep>, system_clock::period>;
const auto right_now = time_point_cast<unsigned_duration>(system_clock::now());
const auto yesterday = right_now - hours{24};
assert(yesterday - right_now > system_clock::duration::zero()); // unsigned overflow

my_mutex m;
condition_variable_any cond;
unique_lock<my_mutex> guard(m);

const auto status = cond.wait_until(m, yesterday);
assert(status == cv_status::timeout);
assert(m.num_locks() == 2);

assert(cond.wait_until(m, yesterday, []() { return false; }) == false);
assert(m.num_locks() == 3);

#if _HAS_CXX20
stop_token token;
assert(cond.wait_until(m, token, yesterday, []() { return false; }) == false);
assert(m.num_locks() == 4);
#endif // _HAS_CXX20
}
} // unnamed namespace

int main() {
test_condition_variable_any();
test_condition_variable_any_already_timed_out();
}