1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 14:17:34 +00:00

Fix broken SpinLock.

The SpinLock was all backwards and didn't actually work. Fixing it exposed
how wrong most of the locking here is.

I need to come up with a better granularity here.
This commit is contained in:
Andreas Kling 2018-10-29 21:54:11 +01:00
parent bea106fdb2
commit e6284a8774
24 changed files with 195 additions and 77 deletions

View file

@ -2,6 +2,20 @@
#include "Types.h"
#ifdef SERENITY
#include "i386.h"
#else
typedef int InterruptDisabler;
#endif
//#define DEBUG_LOCKS
void log_try_lock(const char*);
void log_locked(const char*);
void log_unlocked(const char*);
void yield();
namespace AK {
static inline dword CAS(volatile dword* mem, dword newval, dword oldval)
@ -9,28 +23,46 @@ 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));
:"=a"(ret), "+m"(*mem)
:"r"(newval), "0"(oldval)
:"memory");
return ret;
}
class SpinLock {
public:
SpinLock() { }
~SpinLock() { unlock(); }
~SpinLock() { }
void lock()
void lock(const char* func = nullptr)
{
#ifdef DEBUG_LOCKS
{
InterruptDisabler dis;
log_try_lock(func);
}
#endif
for (;;) {
if (CAS(&m_lock, 1, 0) == 1)
if (CAS(&m_lock, 1, 0) == 0) {
#ifdef DEBUG_LOCKS
InterruptDisabler dis;
log_locked(func);
#endif
return;
}
yield();
}
}
void unlock()
void unlock(const char* func = nullptr)
{
// barrier();
ASSERT(m_lock);
m_lock = 0;
#ifdef DEBUG_LOCKS
InterruptDisabler dis;
log_unlocked(func);
#endif
}
void init()
@ -44,14 +76,18 @@ private:
class Locker {
public:
explicit Locker(SpinLock& l) : m_lock(l) { m_lock.lock(); }
explicit Locker(SpinLock& l, const char* func) : m_lock(l), m_func(func) { lock(); }
~Locker() { unlock(); }
void unlock() { m_lock.unlock(); }
void unlock() { m_lock.unlock(m_func); }
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;

View file

@ -18,7 +18,7 @@ String StringBuilder::build()
if (strings.isEmpty())
return String::empty();
size_t sizeNeeded = 1;
size_t sizeNeeded = 0;
for (auto& string : strings)
sizeNeeded += string.length();

View file

@ -29,7 +29,7 @@ struct Traits<T*> {
#ifdef SERENITY
return intHash((dword)p);
#else
return intHash((ptrdiff_t)p & 0xffffffff);
return intHash((unsigned long long)p & 0xffffffff);
#endif
}
static void dump(const T* p) { kprintf("%p", p); }

View file

@ -11,13 +11,22 @@
#include "WeakPtr.h"
#include "CircularQueue.h"
#include "FileSystemPath.h"
#include "Lock.h"
static void testWeakPtr();
void log_locked() { }
void log_unlocked() { }
int main(int c, char** v)
{
StringImpl::initializeGlobals();
{
SpinLock lock;
Locker locker(lock);
}
{
const char* testpath = "/proc/../proc/1/../../proc/1/vm";
if (c == 2)