mirror of
				https://github.com/rad4day/Waybar.git
				synced 2025-10-24 22:52:32 +02:00 
			
		
		
		
	add signalhandler for module update
This commit is contained in:
		| @@ -22,6 +22,7 @@ class Bar { | ||||
|     ~Bar() = default; | ||||
|  | ||||
|     auto toggle() -> void; | ||||
|     void handleSignal(int); | ||||
|  | ||||
|     const Client& client; | ||||
|     Gtk::Window window; | ||||
|   | ||||
| @@ -2,6 +2,7 @@ | ||||
|  | ||||
| #include <fmt/format.h> | ||||
| #include <iostream> | ||||
| #include <csignal> | ||||
| #include "util/sleeper_thread.hpp" | ||||
| #include "util/command.hpp" | ||||
| #include "util/json.hpp" | ||||
| @@ -14,6 +15,7 @@ class Custom : public ALabel { | ||||
|     Custom(const std::string&, const Json::Value&); | ||||
|     ~Custom(); | ||||
|     auto update() -> void; | ||||
|     void refresh(int /*signal*/); | ||||
|   private: | ||||
|     void delayWorker(); | ||||
|     void continuousWorker(); | ||||
|   | ||||
| @@ -21,7 +21,10 @@ public: | ||||
|   SleeperThread& operator=(std::function<void()> func) | ||||
|   { | ||||
|     thread_ = std::thread([this, func] { | ||||
|       while (do_run_) func(); | ||||
|       while (do_run_) { | ||||
|         signal_ = false; | ||||
|         func(); | ||||
|       } | ||||
|     }); | ||||
|     return *this; | ||||
|   } | ||||
| @@ -34,18 +37,19 @@ public: | ||||
|   auto sleep_for(std::chrono::system_clock::duration dur) | ||||
|   { | ||||
|     std::unique_lock lk(mutex_); | ||||
|     return condvar_.wait_for(lk, dur, [this] { return !do_run_; }); | ||||
|     return condvar_.wait_for(lk, dur, [this] { return signal_ || !do_run_; }); | ||||
|   } | ||||
|  | ||||
|   auto sleep_until(std::chrono::time_point<std::chrono::system_clock, | ||||
|     std::chrono::system_clock::duration> time_point) | ||||
|   { | ||||
|     std::unique_lock lk(mutex_); | ||||
|     return condvar_.wait_until(lk, time_point, [this] { return !do_run_; }); | ||||
|     return condvar_.wait_until(lk, time_point, [this] { return signal_ || !do_run_; }); | ||||
|   } | ||||
|  | ||||
|   auto wake_up() | ||||
|   { | ||||
|     signal_ = true; | ||||
|     condvar_.notify_all(); | ||||
|   } | ||||
|  | ||||
| @@ -53,6 +57,7 @@ public: | ||||
|   { | ||||
|     { | ||||
|       std::lock_guard<std::mutex> lck(mutex_); | ||||
|       signal_ = true; | ||||
|       do_run_ = false; | ||||
|     } | ||||
|     condvar_.notify_all(); | ||||
| @@ -71,6 +76,7 @@ private: | ||||
|   std::condition_variable condvar_; | ||||
|   std::mutex mutex_; | ||||
|   bool do_run_ = true; | ||||
|   bool signal_ = false; | ||||
| }; | ||||
|  | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 hoellen
					hoellen