2016-06-14 23:32:35 -04:00
|
|
|
#pragma once
|
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
#include <atomic>
|
2016-12-22 23:18:58 -05:00
|
|
|
#include <map>
|
2016-06-14 23:32:35 -04:00
|
|
|
#include <mutex>
|
2016-11-20 17:04:31 -05:00
|
|
|
#include <thread>
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "utils/mixins.hpp"
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
namespace this_thread = std::this_thread;
|
|
|
|
|
2016-12-22 23:18:58 -05:00
|
|
|
using std::atomic;
|
|
|
|
using std::map;
|
|
|
|
using std::mutex;
|
2016-11-20 17:04:31 -05:00
|
|
|
using std::thread;
|
|
|
|
|
|
|
|
namespace concurrency_util {
|
2016-06-14 23:32:35 -04:00
|
|
|
namespace locking_strategy {
|
|
|
|
struct no_backoff {
|
|
|
|
bool operator()() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct yield_backoff {
|
|
|
|
bool operator()() {
|
|
|
|
this_thread::yield();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class spin_lock : public non_copyable_mixin<spin_lock> {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct spin_lock
|
|
|
|
*/
|
|
|
|
spin_lock() = default;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lock using custom strategy
|
|
|
|
*/
|
|
|
|
template <typename Backoff>
|
|
|
|
void lock(Backoff backoff) noexcept {
|
|
|
|
while (m_locked.test_and_set(std::memory_order_acquire)) {
|
|
|
|
backoff();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lock using default strategy
|
|
|
|
*/
|
|
|
|
void lock() noexcept {
|
|
|
|
lock(locking_strategy::no_backoff{});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlock
|
|
|
|
*/
|
|
|
|
void unlock() noexcept {
|
|
|
|
m_locked.clear(std::memory_order_release);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::atomic_flag m_locked{false};
|
|
|
|
};
|
2016-12-22 23:18:58 -05:00
|
|
|
|
|
|
|
size_t thread_id(const thread::id id);
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|