Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions include/hx/thread/ConditionVariable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace hx
bool tryAcquire();
void release();
void wait();
bool timedWait(double seconds);
void signal();
void broadcast();

Expand Down
23 changes: 23 additions & 0 deletions src/hx/thread/ConditionVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ void hx::thread::ConditionVariable_obj::wait()
impl->condition.wait(impl->mutex);
}

bool hx::thread::ConditionVariable_obj::timedWait(double seconds)
{
// Upper bound for the timeout, about 31 years. Larger values would overflow
// the integer nanosecond count of the clock once added to the current time.
constexpr double maxSeconds = 1e9;

// `!(seconds > 0)` also catches NaN.
if (!(seconds > 0))
{
seconds = 0;
}
else if (seconds > maxSeconds)
{
seconds = maxSeconds;
}

auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::duration<double>(seconds));

hx::AutoGCFreeZone zone;

return impl->condition.wait_for(impl->mutex, duration) == std::cv_status::no_timeout;
}

void hx::thread::ConditionVariable_obj::signal()
{
impl->condition.notify_one();
Expand Down
Loading