1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:37:35 +00:00

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.
This commit is contained in:
Andreas Kling 2018-10-23 23:32:53 +02:00
parent e4bfcd2346
commit 018da1be11
11 changed files with 88 additions and 203 deletions

54
AK/Lock.h Normal file
View file

@ -0,0 +1,54 @@
#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;