From fcf2d18a011e7ec1339c9d952ee9d007e91f2c13 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 29 May 2019 17:53:22 +0200 Subject: [PATCH] refactor: destroy threads first --- include/modules/backlight.hpp | 2 +- include/modules/battery.hpp | 13 +++++++------ include/modules/clock.hpp | 2 +- include/modules/cpu.hpp | 7 ++++--- include/modules/custom.hpp | 23 ++++++++++++----------- include/modules/memory.hpp | 8 +++++--- include/modules/network.hpp | 23 ++++++++++++----------- include/modules/sway/mode.hpp | 9 +++++---- include/modules/sway/window.hpp | 17 +++++++++-------- include/modules/sway/workspaces.hpp | 5 +++-- include/modules/temperature.hpp | 6 +++--- src/modules/custom.cpp | 4 ++-- 12 files changed, 64 insertions(+), 55 deletions(-) diff --git a/include/modules/backlight.hpp b/include/modules/backlight.hpp index e41e9213..c225aa04 100644 --- a/include/modules/backlight.hpp +++ b/include/modules/backlight.hpp @@ -56,6 +56,6 @@ class Backlight : public ALabel { std::mutex udev_thread_mutex_; std::vector devices_; // thread must destruct before shared data - waybar::util::SleeperThread udev_thread_; + util::SleeperThread udev_thread_; }; } // namespace waybar::modules diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 1174e847..d4d20d1e 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -31,19 +31,20 @@ class Battery : public ALabel { private: static inline const fs::path data_dir_ = "/sys/class/power_supply/"; - void getBatteries(); - void worker(); - const std::string getAdapterStatus(uint8_t capacity) const; + void getBatteries(); + void worker(); + const std::string getAdapterStatus(uint8_t capacity) const; const std::tuple getInfos() const; - const std::string formatTimeRemaining(float hoursRemaining); + const std::string formatTimeRemaining(float hoursRemaining); - util::SleeperThread thread_; - util::SleeperThread thread_timer_; std::vector batteries_; fs::path adapter_; int fd_; std::vector wds_; std::string old_status_; + + util::SleeperThread thread_; + util::SleeperThread thread_timer_; }; } // namespace waybar::modules diff --git a/include/modules/clock.hpp b/include/modules/clock.hpp index a42b882f..b812b702 100644 --- a/include/modules/clock.hpp +++ b/include/modules/clock.hpp @@ -14,7 +14,7 @@ class Clock : public ALabel { auto update() -> void; private: - waybar::util::SleeperThread thread_; + util::SleeperThread thread_; }; } // namespace waybar::modules diff --git a/include/modules/cpu.hpp b/include/modules/cpu.hpp index ee44952e..32883952 100644 --- a/include/modules/cpu.hpp +++ b/include/modules/cpu.hpp @@ -2,12 +2,12 @@ #include #include -#include #include +#include #include -#include #include #include +#include #include "ALabel.hpp" #include "util/sleeper_thread.hpp" @@ -26,7 +26,8 @@ class Cpu : public ALabel { std::vector> parseCpuinfo(); std::vector> prev_times_; - waybar::util::SleeperThread thread_; + + util::SleeperThread thread_; }; } // namespace waybar::modules diff --git a/include/modules/custom.hpp b/include/modules/custom.hpp index 40320cfc..b8dad9dd 100644 --- a/include/modules/custom.hpp +++ b/include/modules/custom.hpp @@ -25,17 +25,18 @@ class Custom : public ALabel { bool handleScroll(GdkEventScroll* e); bool handleToggle(GdkEventButton* const& e); - const std::string name_; - std::string text_; - std::string alt_; - std::string tooltip_; - std::vector class_; - int percentage_; - waybar::util::SleeperThread thread_; - waybar::util::command::res output_; - waybar::util::JsonParser parser_; - FILE* fp_; - int pid_; + const std::string name_; + std::string text_; + std::string alt_; + std::string tooltip_; + std::vector class_; + int percentage_; + FILE* fp_; + int pid_; + util::command::res output_; + util::JsonParser parser_; + + util::SleeperThread thread_; }; } // namespace waybar::modules diff --git a/include/modules/memory.hpp b/include/modules/memory.hpp index c2d157d7..be66611e 100644 --- a/include/modules/memory.hpp +++ b/include/modules/memory.hpp @@ -15,10 +15,12 @@ class Memory : public ALabel { private: static inline const std::string data_dir_ = "/proc/meminfo"; - unsigned long memtotal_; - unsigned long memfree_; void parseMeminfo(); - waybar::util::SleeperThread thread_; + + unsigned long memtotal_; + unsigned long memfree_; + + util::SleeperThread thread_; }; } // namespace waybar::modules diff --git a/include/modules/network.hpp b/include/modules/network.hpp index 9853ce92..4fa1342c 100644 --- a/include/modules/network.hpp +++ b/include/modules/network.hpp @@ -46,17 +46,15 @@ class Network : public ALabel { void clearIface(); bool wildcardMatch(const std::string& pattern, const std::string& text) const; - waybar::util::SleeperThread thread_; - waybar::util::SleeperThread thread_timer_; - int ifid_; - sa_family_t family_; - struct sockaddr_nl nladdr_ = {0}; - struct nl_sock* sock_ = nullptr; - struct nl_sock* ev_sock_ = nullptr; - int efd_; - int ev_fd_; - int nl80211_id_; - std::mutex mutex_; + int ifid_; + sa_family_t family_; + struct sockaddr_nl nladdr_ = {0}; + struct nl_sock* sock_ = nullptr; + struct nl_sock* ev_sock_ = nullptr; + int efd_; + int ev_fd_; + int nl80211_id_; + std::mutex mutex_; unsigned long long bandwidth_down_total_; unsigned long long bandwidth_up_total_; @@ -70,6 +68,9 @@ class Network : public ALabel { int32_t signal_strength_dbm_; uint8_t signal_strength_; uint32_t frequency_; + + util::SleeperThread thread_; + util::SleeperThread thread_timer_; }; } // namespace waybar::modules diff --git a/include/modules/sway/mode.hpp b/include/modules/sway/mode.hpp index ab7ca125..62509a28 100644 --- a/include/modules/sway/mode.hpp +++ b/include/modules/sway/mode.hpp @@ -5,8 +5,8 @@ #include "bar.hpp" #include "client.hpp" #include "modules/sway/ipc/client.hpp" -#include "util/sleeper_thread.hpp" #include "util/json.hpp" +#include "util/sleeper_thread.hpp" namespace waybar::modules::sway { @@ -20,10 +20,11 @@ class Mode : public ALabel, public sigc::trackable { void onEvent(const struct Ipc::ipc_response&); void worker(); - waybar::util::SleeperThread thread_; - Ipc ipc_; - std::string mode_; + std::string mode_; util::JsonParser parser_; + + util::SleeperThread thread_; + Ipc ipc_; }; } // namespace waybar::modules::sway diff --git a/include/modules/sway/window.hpp b/include/modules/sway/window.hpp index 3715eb2f..735a3e8f 100644 --- a/include/modules/sway/window.hpp +++ b/include/modules/sway/window.hpp @@ -24,14 +24,15 @@ class Window : public ALabel, public sigc::trackable { std::tuple getFocusedNode(const Json::Value& nodes); void getTree(); - const Bar& bar_; - waybar::util::SleeperThread thread_; - Ipc ipc_; - std::mutex mutex_; - std::string window_; - int windowId_; - std::string app_id_; - util::JsonParser parser_; + const Bar& bar_; + std::mutex mutex_; + std::string window_; + int windowId_; + std::string app_id_; + util::JsonParser parser_; + + util::SleeperThread thread_; + Ipc ipc_; }; } // namespace waybar::modules::sway diff --git a/include/modules/sway/workspaces.hpp b/include/modules/sway/workspaces.hpp index 8ea365e9..b7632de1 100644 --- a/include/modules/sway/workspaces.hpp +++ b/include/modules/sway/workspaces.hpp @@ -36,13 +36,14 @@ class Workspaces : public IModule, public sigc::trackable { const Json::Value& config_; std::vector workspaces_; std::vector workspaces_order_; - waybar::util::SleeperThread thread_; - Ipc ipc_; std::mutex mutex_; Gtk::Box box_; util::JsonParser parser_; bool scrolling_; std::unordered_map buttons_; + + util::SleeperThread thread_; + Ipc ipc_; }; } // namespace waybar::modules::sway diff --git a/include/modules/temperature.hpp b/include/modules/temperature.hpp index 22bc21c2..9a7ecc32 100644 --- a/include/modules/temperature.hpp +++ b/include/modules/temperature.hpp @@ -17,8 +17,8 @@ class Temperature : public ALabel { std::tuple getTemperature(); bool isCritical(uint16_t); - std::string file_path_; - waybar::util::SleeperThread thread_; + std::string file_path_; + util::SleeperThread thread_; }; -} // namespace waybar::modules \ No newline at end of file +} // namespace waybar::modules diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index cdf13d7a..d6afce53 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -25,14 +25,14 @@ void waybar::modules::Custom::delayWorker() { thread_ = [this] { bool can_update = true; if (config_["exec-if"].isString()) { - auto res = waybar::util::command::exec(config_["exec-if"].asString()); + auto res = util::command::exec(config_["exec-if"].asString()); if (res.exit_code != 0) { can_update = false; event_box_.hide(); } } if (can_update) { - output_ = waybar::util::command::exec(config_["exec"].asString()); + output_ = util::command::exec(config_["exec"].asString()); dp.emit(); } thread_.sleep_for(interval_);