From e5d05baba3bc01003dd7ca98565cfbba882c6699 Mon Sep 17 00:00:00 2001 From: "Rene D. Obermueller" Date: Thu, 31 Mar 2022 08:35:14 +0200 Subject: [PATCH 1/2] Issue 1226/1497: Replace sleep_until with sleep_for to prevent clock from getting stuck with system time adjustment --- src/modules/clock.cpp | 5 +---- src/modules/simpleclock.cpp | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index bb4da216..e99017f9 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -74,10 +74,7 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) thread_ = [this] { dp.emit(); - auto now = std::chrono::system_clock::now(); - auto timeout = std::chrono::floor(now + interval_); - auto diff = std::chrono::seconds(timeout.time_since_epoch().count() % interval_.count()); - thread_.sleep_until(timeout - diff); + thread_.sleep_for(interval_); }; } diff --git a/src/modules/simpleclock.cpp b/src/modules/simpleclock.cpp index f4a4ec47..5abc1de9 100644 --- a/src/modules/simpleclock.cpp +++ b/src/modules/simpleclock.cpp @@ -6,10 +6,7 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) : ALabel(config, "clock", id, "{:%H:%M}", 60) { thread_ = [this] { dp.emit(); - auto now = std::chrono::system_clock::now(); - auto timeout = std::chrono::floor(now + interval_); - auto diff = std::chrono::seconds(timeout.time_since_epoch().count() % interval_.count()); - thread_.sleep_until(timeout - diff); + thread_.sleep_for(interval_); }; } From 4e3f91d23744bdd8d9cfa2ec6370703eac3b778f Mon Sep 17 00:00:00 2001 From: "Rene D. Obermueller" Date: Thu, 31 Mar 2022 17:14:29 +0200 Subject: [PATCH 2/2] Issue 1226/1497: align updates with times divisible by interval --- src/modules/clock.cpp | 6 +++++- src/modules/simpleclock.cpp | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index e99017f9..69fdebb1 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -74,7 +74,11 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) thread_ = [this] { dp.emit(); - thread_.sleep_for(interval_); + auto now = std::chrono::system_clock::now(); + /* difference with projected wakeup time */ + auto diff = now.time_since_epoch() % interval_; + /* sleep until the next projected time */ + thread_.sleep_for(interval_ - diff); }; } diff --git a/src/modules/simpleclock.cpp b/src/modules/simpleclock.cpp index 5abc1de9..27c7ac77 100644 --- a/src/modules/simpleclock.cpp +++ b/src/modules/simpleclock.cpp @@ -6,7 +6,11 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) : ALabel(config, "clock", id, "{:%H:%M}", 60) { thread_ = [this] { dp.emit(); - thread_.sleep_for(interval_); + auto now = std::chrono::system_clock::now(); + /* difference with projected wakeup time */ + auto diff = now.time_since_epoch() % interval_; + /* sleep until the next projected time */ + thread_.sleep_for(interval_ - diff); }; }