polybar/src/utils/concurrency.cpp

32 lines
671 B
C++
Raw Normal View History

2016-12-23 04:18:58 +00:00
#include "utils/concurrency.hpp"
POLYBAR_NS
2016-12-26 09:33:23 +00:00
bool spin_lock::no_backoff_strategy::operator()() {
return true;
}
bool spin_lock::yield_backoff_strategy::operator()() {
this_thread::yield();
return false;
}
void spin_lock::lock() noexcept {
lock(no_backoff_strategy{});
}
void spin_lock::unlock() noexcept {
m_locked.clear(std::memory_order_release);
}
2016-12-23 04:18:58 +00:00
namespace concurrency_util {
size_t thread_id(const thread::id id) {
static size_t idx{1_z};
2016-12-26 09:33:23 +00:00
static mutex_wrapper<map<thread::id, size_t>> ids;
std::lock_guard<decltype(ids)> lock(ids);
2016-12-23 14:54:06 +00:00
if (ids.find(id) == ids.end()) {
2016-12-23 04:18:58 +00:00
ids[id] = idx++;
2016-12-23 14:54:06 +00:00
}
2016-12-23 04:18:58 +00:00
return ids[id];
}
}
POLYBAR_NS_END