All checks were successful
continuous-integration/drone/push Build is passing
For when you want to lock a resource if possible but not get blocked if it is locked by another thread.
28 lines
545 B
C++
28 lines
545 B
C++
#include "thread/Spinlock.h"
|
|
#include "Log.h"
|
|
#include "arch/CPU.h"
|
|
|
|
void Spinlock::lock()
|
|
{
|
|
int expected = 0;
|
|
while (!m_lock.compare_exchange_strong(expected, 1))
|
|
{
|
|
expected = 0;
|
|
CPU::pause();
|
|
}
|
|
}
|
|
|
|
bool Spinlock::try_lock()
|
|
{
|
|
int expected = 0;
|
|
return m_lock.compare_exchange_strong(expected, 1);
|
|
}
|
|
|
|
void Spinlock::unlock()
|
|
{
|
|
int expected = 1;
|
|
if (!m_lock.compare_exchange_strong(expected, 0))
|
|
{
|
|
kwarnln("Spinlock::unlock() called on an unlocked lock with value %d", expected);
|
|
}
|
|
} |