concurrency_ytil: Thread id

This commit is contained in:
Michael Carlberg 2016-12-23 05:18:58 +01:00
parent 48438b2556
commit debb3534c7
13 changed files with 66 additions and 32 deletions

View File

@ -3,6 +3,7 @@
#include <cstdio>
#include <map>
#include <string>
#include <thread>
#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

View File

@ -33,7 +33,7 @@ namespace modules {
string m_date;
string m_time;
stateflag m_toggled{false};
std::atomic<bool> m_toggled{false};
};
}

View File

@ -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;

View File

@ -47,7 +47,7 @@ namespace modules {
template <typename Impl>
void module<Impl>::stop() {
if (!running()) {
if (!static_cast<bool>(m_enabled)) {
return;
}
@ -117,10 +117,6 @@ namespace modules {
template <typename Impl>
string module<Impl>::get_output() {
if (!running()) {
m_log.trace("%s: Module is disabled", name());
return "";
}
std::lock_guard<std::mutex> guard(m_buildlock);
auto format_name = CONST_MOD(Impl).get_format();

View File

@ -40,8 +40,8 @@ namespace modules {
animation_t m_animation_packetloss;
map<connection_state, label_t> m_label;
stateflag m_connected{false};
stateflag m_packetloss{false};
atomic<bool> m_connected{false};
atomic<bool> m_packetloss{false};
int m_signal{0};
int m_quality{0};

View File

@ -58,9 +58,9 @@ namespace modules {
map<control, control_t> m_ctrl;
int m_headphoneid{0};
bool m_mapped{false};
stateflag m_muted{false};
stateflag m_headphones{false};
std::atomic<int> m_volume{0};
atomic<bool> m_muted{false};
atomic<bool> m_headphones{false};
atomic<int> m_volume{0};
};
}

View File

@ -1,6 +1,7 @@
#pragma once
#include <atomic>
#include <map>
#include <mutex>
#include <thread>
@ -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<bool>;
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

View File

@ -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);

View File

@ -148,14 +148,14 @@ class tray_manager : public xpp::event::sink<evt::expose, evt::visibility_notify
xcb_window_t m_tray{0};
xcb_window_t m_othermanager{0};
stateflag m_activated{false};
stateflag m_mapped{false};
stateflag m_hidden{false};
stateflag m_acquired_selection{false};
atomic<bool> m_activated{false};
atomic<bool> m_mapped{false};
atomic<bool> m_hidden{false};
atomic<bool> m_acquired_selection{false};
thread m_delaythread;
std::mutex m_mtx{};
mutex m_mtx{};
chrono::time_point<chrono::system_clock, chrono::milliseconds> m_drawtime;

View File

@ -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
*/

17
src/utils/concurrency.cpp Normal file
View File

@ -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<thread::id, size_t> ids;
std::lock_guard<mutex> lock(mtx);
if (ids.find(id) == ids.end())
ids[id] = idx++;
return ids[id];
}
}
POLYBAR_NS_END

View File

@ -297,7 +297,7 @@ void tray_manager::reconfigure() {
return;
}
std::unique_lock<std::mutex> guard(m_mtx, std::adopt_lock);
std::unique_lock<mutex> guard(m_mtx, std::adopt_lock);
try {
reconfigure_clients();
@ -481,7 +481,7 @@ void tray_manager::refresh_window() {
return;
}
std::lock_guard<std::mutex> lock(m_mtx, std::adopt_lock);
std::lock_guard<mutex> lock(m_mtx, std::adopt_lock);
m_log.trace("tray: Refreshing window");

View File

@ -1,7 +1,7 @@
#include <X11/X.h>
#include "x11/xlib.hpp"
#include "utils/factory.hpp"
#include "x11/xlib.hpp"
POLYBAR_NS