ladybird/AK/Lock.h
2019-01-14 14:14:30 +01:00

99 lines
1.8 KiB
C++

#pragma once
#include "Assertions.h"
#include "Types.h"
#ifdef SERENITY
#include "i386.h"
int sched_yield();
#else
#include <sched.h>
typedef int InterruptDisabler;
#endif
//#define DEBUG_LOCKS
void log_try_lock(const char*);
void log_locked(const char*);
void log_unlocked(const char*);
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), "0"(oldval)
:"cc", "memory");
return ret;
}
// FIXME: Rename to YieldingLock?
class SpinLock {
public:
SpinLock() { }
~SpinLock() { }
ALWAYS_INLINE void lock(const char* func = nullptr)
{
(void)func;
#ifdef DEBUG_LOCKS
{
InterruptDisabler dis;
log_try_lock(func);
}
#endif
for (;;) {
if (CAS(&m_lock, 1, 0) == 0) {
#ifdef DEBUG_LOCKS
InterruptDisabler dis;
log_locked(func);
#endif
return;
}
sched_yield();
}
}
ALWAYS_INLINE void unlock(const char* func = nullptr)
{
(void)func;
// barrier();
ASSERT(m_lock);
m_lock = 0;
#ifdef DEBUG_LOCKS
InterruptDisabler dis;
log_unlocked(func);
#endif
}
void init()
{
m_lock = 0;
}
private:
volatile dword m_lock { 0 };
};
class Locker {
public:
ALWAYS_INLINE explicit Locker(SpinLock& l, const char* func) : m_lock(l), m_func(func) { lock(); }
ALWAYS_INLINE ~Locker() { unlock(); }
ALWAYS_INLINE void unlock() { m_lock.unlock(m_func); }
ALWAYS_INLINE void lock() { m_lock.lock(m_func); }
private:
SpinLock& m_lock;
const char* m_func { nullptr };
};
#define LOCKER(lock) Locker locker(lock, __FUNCTION__)
}
using AK::SpinLock;
using AK::Locker;