1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-10-27 05:23:39 -04:00
polybar/include/utils/threading.hpp

63 lines
1 KiB
C++
Raw Normal View History

2016-06-14 23:32:35 -04:00
#pragma once
#include <mutex>
#include "common.hpp"
#include "utils/mixins.hpp"
LEMONBUDDY_NS
namespace threading_util {
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};
};
}
LEMONBUDDY_NS_END