2022-12-17 10:52:40 +01:00
|
|
|
#include "thread/Spinlock.h"
|
|
|
|
#include "Log.h"
|
|
|
|
#include "arch/CPU.h"
|
|
|
|
|
|
|
|
void Spinlock::lock()
|
|
|
|
{
|
2022-12-18 20:36:15 +01:00
|
|
|
int expected = 0;
|
|
|
|
while (!m_lock.compare_exchange_strong(expected, 1))
|
2022-12-17 10:52:40 +01:00
|
|
|
{
|
2022-12-18 20:36:15 +01:00
|
|
|
expected = 0;
|
2022-12-17 10:52:40 +01:00
|
|
|
CPU::pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-18 20:37:26 +01:00
|
|
|
bool Spinlock::try_lock()
|
|
|
|
{
|
|
|
|
int expected = 0;
|
|
|
|
return m_lock.compare_exchange_strong(expected, 1);
|
|
|
|
}
|
|
|
|
|
2022-12-17 10:52:40 +01:00
|
|
|
void Spinlock::unlock()
|
|
|
|
{
|
2022-12-18 20:36:15 +01:00
|
|
|
int expected = 1;
|
|
|
|
if (!m_lock.compare_exchange_strong(expected, 0))
|
2022-12-17 10:52:40 +01:00
|
|
|
{
|
|
|
|
kwarnln("Spinlock::unlock() called on an unlocked lock with value %d", expected);
|
|
|
|
}
|
|
|
|
}
|