-
-
Notifications
You must be signed in to change notification settings - Fork 710
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
Fix sleep_for for max duration #2601
Fix sleep_for for max duration #2601
Conversation
74b27fb
to
c5b1703
Compare
This PR fixes the issue by using sleep_until to emulate sleep_for since sleep_for is defined as identical to adding the current time and then using sleep_until by the standard.
That is not entirely true. `std::condition_variable::wait_for` at
least in GCC libstdc++ on GNU libc defaults to
`pthread_cond_clockwait` with monotonic clock
(`std::chrono::clock_steady`), protecting it from system clock
adjustments. The code here passes `std::chrono::system_clock` to
`wait_until` and thus the sleep duration will be affected by the
system clock changes.
|
Good point, thanks for the correction. Tomorrow, I'll also split this PR up into the testing/build system changes (since it's not really related to the change anyway) and the fix + re-applied PR. |
The standard library has the implicit requirement that for std::condition_variable::sleep_for() the duration must not cause an overflow if added to the current time. This commit will reduce the duration accordingly to fit into the duration type.
In the previous fix for a passed max duration, the assumption was made that at maximum one second will pass between the duration assignment and the std::condition_variable::sleep_for() call. This implementation makes the behavior more predictable by using sleep_until() instead to emulate the sleep_for() behavior.
This reverts commit 2d33c20 and reapplies various patches for memory leaks. The reason for the revert was a bug for a maximum duration interval which caused sleep_for() to cause unpredictable behavior.
Using pascal case for the class name keeps it more consistent with the majority of the other class names.
c5b1703
to
68dfd6a
Compare
Thanks :) |
As mentioned in the issue, the bug is caused by the standard library expecting for
std::condition_variable::sleep_for
a duration which does not cause an overflow when added to the current time.This PR fixes the issue by using
sleep_until
to emulatesleep_for
sincesleep_for
is defined as identical to adding the current time and then usingsleep_until
by the standard.Additionally, this PR re-applies #2586 which was reverted in #2599 because of this bug.
I also tried to write a test to catch this kind of error for
SleeperThread
, but I wasn't able to check for this kind of failure.The standard does not specify the behavior if a too big duration is passed.
I ended up writing a few basic other tests for the class and restructured the build system to allow for easier addition to the tests by providing all sources of
waybar
as a static library which is now simply linked againstmain.cpp
for the executable.Resolves #2598