Make controller button code thread-safe (#405)

* Refactor spinlock to meet Lockable requirements
* Input: Refactor button code and make it thread-safe
This commit is contained in:
Exzap 2022-10-23 15:47:42 +02:00 committed by GitHub
parent c40466f3a8
commit 028b3f7992
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 311 additions and 220 deletions

View file

@ -7,32 +7,33 @@
class FSpinlock
{
public:
void acquire()
bool is_locked() const
{
while( true )
return m_lockBool.load(std::memory_order_relaxed);
}
// implement BasicLockable and Lockable
void lock() const
{
while (true)
{
if (!m_lockBool.exchange(true, std::memory_order_acquire))
if (!m_lockBool.exchange(true, std::memory_order_acquire))
break;
while (m_lockBool.load(std::memory_order_relaxed)) _mm_pause();
}
}
bool tryAcquire()
bool try_lock() const
{
return !m_lockBool.exchange(true, std::memory_order_acquire);
}
void release()
void unlock() const
{
m_lockBool.store(false, std::memory_order_release);
}
bool isHolding() const
{
return m_lockBool.load(std::memory_order_relaxed);
}
private:
std::atomic<bool> m_lockBool = false;
mutable std::atomic<bool> m_lockBool = false;
};