diff --git a/include/components/logger.hpp b/include/components/logger.hpp index d787f6fe..d9c37f49 100644 --- a/include/components/logger.hpp +++ b/include/components/logger.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "common.hpp" @@ -79,11 +80,14 @@ class logger { } /** - * Convert string to const char* + * Convert string */ - const char* convert(string arg) const { - return arg.c_str(); - } + const char* convert(string arg) const; + + /** + * Convert thread id + */ + size_t convert(const std::thread::id arg) const; /** * Write the log message to the output channel diff --git a/include/modules/date.hpp b/include/modules/date.hpp index 3dc6583d..1dcf62a0 100644 --- a/include/modules/date.hpp +++ b/include/modules/date.hpp @@ -33,7 +33,7 @@ namespace modules { string m_date; string m_time; - stateflag m_toggled{false}; + std::atomic m_toggled{false}; }; } diff --git a/include/modules/meta/base.hpp b/include/modules/meta/base.hpp index aa708f1f..7dffb169 100644 --- a/include/modules/meta/base.hpp +++ b/include/modules/meta/base.hpp @@ -120,9 +120,6 @@ namespace modules { module(const bar_settings bar, string name); ~module() noexcept; - void set_update_cb(callback<>&& cb); - void set_stop_cb(callback<>&& cb); - string name() const; bool running() const; void stop(); @@ -144,9 +141,9 @@ namespace modules { const logger& m_log; const config& m_conf; - std::mutex m_buildlock; - std::mutex m_updatelock; - std::mutex m_sleeplock; + mutex m_buildlock; + mutex m_updatelock; + mutex m_sleeplock; std::condition_variable m_sleephandler; string m_name; diff --git a/include/modules/meta/base.inl b/include/modules/meta/base.inl index e291026a..217c93cf 100644 --- a/include/modules/meta/base.inl +++ b/include/modules/meta/base.inl @@ -47,7 +47,7 @@ namespace modules { template void module::stop() { - if (!running()) { + if (!static_cast(m_enabled)) { return; } @@ -117,10 +117,6 @@ namespace modules { template string module::get_output() { - if (!running()) { - m_log.trace("%s: Module is disabled", name()); - return ""; - } std::lock_guard guard(m_buildlock); auto format_name = CONST_MOD(Impl).get_format(); diff --git a/include/modules/network.hpp b/include/modules/network.hpp index 00cbf20b..4165a2bf 100644 --- a/include/modules/network.hpp +++ b/include/modules/network.hpp @@ -40,8 +40,8 @@ namespace modules { animation_t m_animation_packetloss; map m_label; - stateflag m_connected{false}; - stateflag m_packetloss{false}; + atomic m_connected{false}; + atomic m_packetloss{false}; int m_signal{0}; int m_quality{0}; diff --git a/include/modules/volume.hpp b/include/modules/volume.hpp index 557f1ab3..fb4196f7 100644 --- a/include/modules/volume.hpp +++ b/include/modules/volume.hpp @@ -58,9 +58,9 @@ namespace modules { map m_ctrl; int m_headphoneid{0}; bool m_mapped{false}; - stateflag m_muted{false}; - stateflag m_headphones{false}; - std::atomic m_volume{0}; + atomic m_muted{false}; + atomic m_headphones{false}; + atomic m_volume{0}; }; } diff --git a/include/utils/concurrency.hpp b/include/utils/concurrency.hpp index 3d46c8e6..69186925 100644 --- a/include/utils/concurrency.hpp +++ b/include/utils/concurrency.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -11,8 +12,10 @@ POLYBAR_NS namespace this_thread = std::this_thread; +using std::atomic; +using std::map; +using std::mutex; using std::thread; -using stateflag = std::atomic; namespace concurrency_util { namespace locking_strategy { @@ -64,6 +67,8 @@ namespace concurrency_util { protected: std::atomic_flag m_locked{false}; }; + + size_t thread_id(const thread::id id); } POLYBAR_NS_END diff --git a/include/utils/string.hpp b/include/utils/string.hpp index 060f0fa5..7af4e16b 100644 --- a/include/utils/string.hpp +++ b/include/utils/string.hpp @@ -16,9 +16,9 @@ namespace string_util { string upper(const string& s); string lower(const string& s); bool compare(const string& s1, const string& s2); - string replace(const string& haystack, const string& needle, const string& reply, size_t start = 0, + string replace(const string& haystack, const string& needle, const string& replacement, size_t start = 0, size_t end = string::npos); - string replace_all(const string& haystack, const string& needle, const string& reply, size_t start = 0, + string replace_all(const string& haystack, const string& needle, const string& replacement, size_t start = 0, size_t end = string::npos); string squeeze(const string& haystack, char needle); string strip(const string& haystack, char needle); diff --git a/include/x11/tray_manager.hpp b/include/x11/tray_manager.hpp index e44768b0..d8b2c62e 100644 --- a/include/x11/tray_manager.hpp +++ b/include/x11/tray_manager.hpp @@ -148,14 +148,14 @@ class tray_manager : public xpp::event::sink m_activated{false}; + atomic m_mapped{false}; + atomic m_hidden{false}; + atomic m_acquired_selection{false}; thread m_delaythread; - std::mutex m_mtx{}; + mutex m_mtx{}; chrono::time_point m_drawtime; diff --git a/src/components/logger.cpp b/src/components/logger.cpp index cf31144f..04ee0b63 100644 --- a/src/components/logger.cpp +++ b/src/components/logger.cpp @@ -2,11 +2,26 @@ #include "components/logger.hpp" #include "errors.hpp" +#include "utils/concurrency.hpp" #include "utils/factory.hpp" #include "utils/string.hpp" POLYBAR_NS +/** + * Convert string + */ +const char* logger::convert(string arg) const { + return arg.c_str(); +} + +/** + * Convert thread id + */ +size_t logger::convert(const std::thread::id arg) const { + return concurrency_util::thread_id(arg); +} + /** * Create instance */ diff --git a/src/utils/concurrency.cpp b/src/utils/concurrency.cpp new file mode 100644 index 00000000..dad1612a --- /dev/null +++ b/src/utils/concurrency.cpp @@ -0,0 +1,17 @@ +#include "utils/concurrency.hpp" + +POLYBAR_NS + +namespace concurrency_util { + size_t thread_id(const thread::id id) { + static size_t idx{1_z}; + static mutex mtx; + static map ids; + std::lock_guard lock(mtx); + if (ids.find(id) == ids.end()) + ids[id] = idx++; + return ids[id]; + } +} + +POLYBAR_NS_END diff --git a/src/x11/tray_manager.cpp b/src/x11/tray_manager.cpp index d87022e8..47c5d23d 100644 --- a/src/x11/tray_manager.cpp +++ b/src/x11/tray_manager.cpp @@ -297,7 +297,7 @@ void tray_manager::reconfigure() { return; } - std::unique_lock guard(m_mtx, std::adopt_lock); + std::unique_lock guard(m_mtx, std::adopt_lock); try { reconfigure_clients(); @@ -481,7 +481,7 @@ void tray_manager::refresh_window() { return; } - std::lock_guard lock(m_mtx, std::adopt_lock); + std::lock_guard lock(m_mtx, std::adopt_lock); m_log.trace("tray: Refreshing window"); diff --git a/src/x11/xlib.cpp b/src/x11/xlib.cpp index 9740df7c..b2b196f8 100644 --- a/src/x11/xlib.cpp +++ b/src/x11/xlib.cpp @@ -1,7 +1,7 @@ #include -#include "x11/xlib.hpp" #include "utils/factory.hpp" +#include "x11/xlib.hpp" POLYBAR_NS