#pragma once
|
|
|
|
#include <atomic>
|
|
|
|
#include "gp_config.hpp"
|
|
|
|
namespace gp {
|
|
/**
|
|
* @brief CADR lock_guard for generic locking mechanisms
|
|
*
|
|
* @tparam T
|
|
*/
|
|
template<typename T>
|
|
class lock_guard {
|
|
T& ref;
|
|
public:
|
|
lock_guard(T& _ref)
|
|
: ref(_ref)
|
|
{
|
|
ref.lock();
|
|
}
|
|
|
|
~lock_guard() {
|
|
ref.unlock();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @brief A fast mutual exclusion handler WITHOUT deadlock detection
|
|
*/
|
|
class fast_bottleneck {
|
|
std::atomic_bool flag;
|
|
public:
|
|
fast_bottleneck() = default;
|
|
fast_bottleneck(fast_bottleneck&) = delete;
|
|
fast_bottleneck(fast_bottleneck&&) = delete;
|
|
|
|
void lock() {
|
|
while(not try_lock());
|
|
}
|
|
|
|
[[nodiscard]] bool try_lock() {
|
|
bool f = false;
|
|
bool t = true;
|
|
return flag.compare_exchange_strong(f,t,std::memory_order::acquire);
|
|
}
|
|
|
|
void unlock() {
|
|
gp_config::assertion(try_unlock(), "Unlocking failed in fast_bottleneck: potential double unlocking issue");
|
|
}
|
|
|
|
[[nodiscard]] bool try_unlock() {
|
|
bool f = false;
|
|
bool t = true;
|
|
return flag.compare_exchange_strong(t,f,std::memory_order::release);
|
|
}
|
|
};
|
|
}
|