mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-15 20:33:10 +00:00
018da1be11
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.
55 lines
830 B
C++
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;
|
|
|