mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 05:48:12 +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:
parent
e4bfcd2346
commit
018da1be11
11 changed files with 88 additions and 203 deletions
|
@ -2,6 +2,7 @@
|
|||
#include "Task.h"
|
||||
#include "Syscall.h"
|
||||
#include "Console.h"
|
||||
#include <AK/Lock.h>
|
||||
|
||||
extern "C" void syscall_entry();
|
||||
extern "C" void syscall_ISR();
|
||||
|
@ -38,72 +39,6 @@ asm(
|
|||
" iret\n"
|
||||
);
|
||||
|
||||
static inline dword CAS(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()
|
||||
{
|
||||
volatile dword count = 0;
|
||||
for (;;) {
|
||||
if (CAS(&m_lock, 1, 0) == 1)
|
||||
return;
|
||||
++count;
|
||||
|
||||
}
|
||||
if (count)
|
||||
kprintf("waited %u in %s\n",count, current->name().characters());
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
// barrier();
|
||||
m_lock = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
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;
|
||||
};
|
||||
|
||||
namespace Syscall {
|
||||
|
||||
static SpinLock* s_lock;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue