Merge pull request #1500 from RobertMueller2/clock-thread-sleep

Replace sleep_until with sleep_for to prevent clock from getting stuck with system time adjustment
This commit is contained in:
Alex 2022-04-20 05:11:49 +02:00 committed by GitHub
commit 23369aa14c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 6 deletions

View File

@ -75,9 +75,10 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
thread_ = [this] { thread_ = [this] {
dp.emit(); dp.emit();
auto now = std::chrono::system_clock::now(); auto now = std::chrono::system_clock::now();
auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_); /* difference with projected wakeup time */
auto diff = std::chrono::seconds(timeout.time_since_epoch().count() % interval_.count()); auto diff = now.time_since_epoch() % interval_;
thread_.sleep_until(timeout - diff); /* sleep until the next projected time */
thread_.sleep_for(interval_ - diff);
}; };
} }

View File

@ -7,9 +7,10 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
thread_ = [this] { thread_ = [this] {
dp.emit(); dp.emit();
auto now = std::chrono::system_clock::now(); auto now = std::chrono::system_clock::now();
auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_); /* difference with projected wakeup time */
auto diff = std::chrono::seconds(timeout.time_since_epoch().count() % interval_.count()); auto diff = now.time_since_epoch() % interval_;
thread_.sleep_until(timeout - diff); /* sleep until the next projected time */
thread_.sleep_for(interval_ - diff);
}; };
} }