2018-10-23 21:32:53 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-11-10 14:15:31 +00:00
|
|
|
#include "Assertions.h"
|
2018-10-23 21:32:53 +00:00
|
|
|
#include "Types.h"
|
2018-10-29 20:54:11 +00:00
|
|
|
#include "i386.h"
|
2019-02-06 14:05:47 +00:00
|
|
|
#include <Kernel/Scheduler.h>
|
2019-01-16 15:03:50 +00:00
|
|
|
|
|
|
|
class Process;
|
|
|
|
extern Process* current;
|
|
|
|
|
|
|
|
#ifndef KERNEL
|
|
|
|
#error This thing is kernel-only right now.
|
2018-10-29 20:54:11 +00:00
|
|
|
#endif
|
|
|
|
|
2018-10-23 21:32:53 +00:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
static inline dword CAS(volatile dword* mem, dword newval, dword oldval)
|
|
|
|
{
|
|
|
|
dword ret;
|
|
|
|
asm volatile(
|
|
|
|
"cmpxchgl %2, %1"
|
2018-10-29 20:54:11 +00:00
|
|
|
:"=a"(ret), "+m"(*mem)
|
|
|
|
:"r"(newval), "0"(oldval)
|
2019-01-14 13:14:30 +00:00
|
|
|
:"cc", "memory");
|
2018-10-23 21:32:53 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-17 15:25:02 +00:00
|
|
|
class Lock {
|
2018-10-23 21:32:53 +00:00
|
|
|
public:
|
2019-02-07 10:12:23 +00:00
|
|
|
Lock(const char* name = nullptr) : m_name(name) { }
|
2019-01-17 15:25:02 +00:00
|
|
|
~Lock() { }
|
2018-10-23 21:32:53 +00:00
|
|
|
|
2019-01-16 15:03:50 +00:00
|
|
|
void lock();
|
|
|
|
void unlock();
|
2018-10-23 21:32:53 +00:00
|
|
|
|
2019-02-07 10:12:23 +00:00
|
|
|
const char* name() const { return m_name; }
|
|
|
|
|
2018-10-23 21:32:53 +00:00
|
|
|
private:
|
|
|
|
volatile dword m_lock { 0 };
|
2019-01-16 15:03:50 +00:00
|
|
|
dword m_level { 0 };
|
|
|
|
Process* m_holder { nullptr };
|
2019-02-07 10:12:23 +00:00
|
|
|
const char* m_name { nullptr };
|
2018-10-23 21:32:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Locker {
|
|
|
|
public:
|
2019-02-20 12:09:59 +00:00
|
|
|
[[gnu::always_inline]] inline explicit Locker(Lock& l) : m_lock(l) { lock(); }
|
|
|
|
[[gnu::always_inline]] inline ~Locker() { unlock(); }
|
|
|
|
[[gnu::always_inline]] inline void unlock() { m_lock.unlock(); }
|
|
|
|
[[gnu::always_inline]] inline void lock() { m_lock.lock(); }
|
2018-10-23 21:32:53 +00:00
|
|
|
|
|
|
|
private:
|
2019-01-17 15:25:02 +00:00
|
|
|
Lock& m_lock;
|
2018-10-23 21:32:53 +00:00
|
|
|
};
|
|
|
|
|
2019-02-20 12:09:59 +00:00
|
|
|
[[gnu::always_inline]] inline void Lock::lock()
|
2019-01-16 15:03:50 +00:00
|
|
|
{
|
2019-02-20 12:09:59 +00:00
|
|
|
if (!are_interrupts_enabled()) {
|
|
|
|
kprintf("Interrupts disabled when trying to take Lock{%s}\n", m_name);
|
|
|
|
hang();
|
|
|
|
}
|
2019-02-06 14:05:47 +00:00
|
|
|
ASSERT(!Scheduler::is_active());
|
2019-01-16 15:03:50 +00:00
|
|
|
for (;;) {
|
|
|
|
if (CAS(&m_lock, 1, 0) == 0) {
|
|
|
|
if (!m_holder || m_holder == current) {
|
|
|
|
m_holder = current;
|
|
|
|
++m_level;
|
|
|
|
memory_barrier();
|
|
|
|
m_lock = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_lock = 0;
|
|
|
|
}
|
2019-02-07 10:12:23 +00:00
|
|
|
Scheduler::donate_to(m_holder, m_name);
|
2019-01-16 15:03:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 15:25:02 +00:00
|
|
|
inline void Lock::unlock()
|
2019-01-16 15:03:50 +00:00
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
if (CAS(&m_lock, 1, 0) == 0) {
|
|
|
|
ASSERT(m_holder == current);
|
|
|
|
ASSERT(m_level);
|
|
|
|
--m_level;
|
|
|
|
if (m_level) {
|
|
|
|
memory_barrier();
|
|
|
|
m_lock = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_holder = nullptr;
|
|
|
|
memory_barrier();
|
|
|
|
m_lock = 0;
|
|
|
|
return;
|
|
|
|
}
|
2019-02-07 10:12:23 +00:00
|
|
|
Scheduler::donate_to(m_holder, m_name);
|
2019-01-16 15:03:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LOCKER(lock) Locker locker(lock)
|
2018-10-29 20:54:11 +00:00
|
|
|
|
2019-02-08 08:46:13 +00:00
|
|
|
template<typename T>
|
|
|
|
class Lockable {
|
|
|
|
public:
|
|
|
|
Lockable() { }
|
|
|
|
Lockable(T&& resource) : m_resource(move(resource)) { }
|
|
|
|
Lock& lock() { return m_lock; }
|
|
|
|
T& resource() { return m_resource; }
|
|
|
|
|
2019-02-08 15:18:24 +00:00
|
|
|
T lock_and_copy()
|
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
|
|
|
return m_resource;
|
|
|
|
}
|
|
|
|
|
2019-02-08 08:46:13 +00:00
|
|
|
private:
|
|
|
|
T m_resource;
|
|
|
|
Lock m_lock;
|
|
|
|
};
|
|
|
|
|
2018-10-23 21:32:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 15:25:02 +00:00
|
|
|
using AK::Lock;
|
2018-10-23 21:32:53 +00:00
|
|
|
using AK::Locker;
|
2019-02-08 08:46:13 +00:00
|
|
|
using AK::Lockable;
|