serenity/AK/Lock.h
Andreas Kling 018da1be11 Generalize the SpinLock and move it to AK.
Add a separate lock to protect the VFS. I think this might be a good idea.
I'm not sure it's a good approach though. I'll fiddle with it as I go along.

It's really fun to figure out all these things on my own.
2018-10-23 23:34:05 +02:00

55 lines
830 B
C++

#pragma once
#include "Types.h"
namespace AK {
static inline dword CAS(volatile dword* mem, dword newval, dword oldval)
{
dword ret;
asm volatile(
"cmpxchgl %2, %1"
:"=a"(ret), "=m"(*mem)
:"r"(newval), "m"(*mem), "0"(oldval));
return ret;
}
class SpinLock {
public:
SpinLock() { }
~SpinLock() { unlock(); }
void lock()
{
for (;;) {
if (CAS(&m_lock, 1, 0) == 1)
return;
}
}
void unlock()
{
// barrier();
m_lock = 0;
}
private:
volatile dword m_lock { 0 };
};
class Locker {
public:
explicit Locker(SpinLock& l) : m_lock(l) { m_lock.lock(); }
~Locker() { unlock(); }
void unlock() { m_lock.unlock(); }
private:
SpinLock& m_lock;
};
}
using AK::SpinLock;
using AK::Locker;