From c968b6f2d57caef9381230785d26beee99adc923 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 10 Feb 2023 05:25:08 +0200 Subject: [PATCH] Updated lock to use atomic operations --- Core/Lock.cpp | 13 ++++++------- include/lock.hpp | 16 +++++++++------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Core/Lock.cpp b/Core/Lock.cpp index 88c7784..5155f6a 100644 --- a/Core/Lock.cpp +++ b/Core/Lock.cpp @@ -2,7 +2,6 @@ #include #include -#include #include "../kernel.h" @@ -21,7 +20,7 @@ void LockClass::DeadLock(SpinLockData Lock) this->DeadLocks++; - if (Config.UnlockDeadLock && this->DeadLocks > 10) + if (Config.UnlockDeadLock && this->DeadLocks.Load() > 10) { warn("Unlocking lock '%s' to prevent deadlock. (this is enabled in the kernel config)", Lock.AttemptingToGet); this->DeadLocks = 0; @@ -50,7 +49,7 @@ int LockClass::Lock(const char *FunctionName) LockData.AttemptingToGet = FunctionName; Retry: unsigned int i = 0; - while (__atomic_exchange_n(&IsLocked, true, __ATOMIC_ACQUIRE) && ++i < 0x10000000) + while (IsLocked.Exchange(true, MemoryBorder::Acquire) && ++i < 0x10000000) CPU::Pause(); if (i >= 0x10000000) { @@ -78,7 +77,7 @@ int LockClass::Unlock() // IsLocked = false; __sync_synchronize(); - __atomic_store_n(&IsLocked, false, __ATOMIC_RELEASE); + IsLocked.Store(false, MemoryBorder::Release); LockData.Count--; IsLocked = false; @@ -115,13 +114,13 @@ int LockClass::TimeoutLock(const char *FunctionName, uint64_t Timeout) Atomic Target = 0; Retry: unsigned int i = 0; - while (__atomic_exchange_n(&IsLocked, true, __ATOMIC_ACQUIRE) && ++i < 0x10000000) + while (IsLocked.Exchange(true, MemoryBorder::Acquire) && ++i < 0x10000000) CPU::Pause(); if (i >= 0x10000000) { - if (Target.Load() == 0) + if (Target == 0) Target = TimeManager->CalculateTarget(Timeout); - TimeoutDeadLock(LockData, Target.Load()); + TimeoutDeadLock(LockData, Target); goto Retry; } LockData.Count++; diff --git a/include/lock.hpp b/include/lock.hpp index 6a36297..bfe233a 100644 --- a/include/lock.hpp +++ b/include/lock.hpp @@ -2,6 +2,8 @@ #define __FENNIX_KERNEL_LOCK_H__ #include + +#include #include #ifdef __cplusplus @@ -11,17 +13,17 @@ class LockClass { struct SpinLockData { - uint64_t LockData = 0x0; - const char *CurrentHolder = "(nul)"; - const char *AttemptingToGet = "(nul)"; - size_t Count = 0; - long Core = 0; + Atomic LockData = 0x0; + Atomic CurrentHolder = "(nul)"; + Atomic AttemptingToGet = "(nul)"; + Atomic Count = 0; + Atomic Core = 0; }; private: SpinLockData LockData; - bool IsLocked = false; - unsigned long DeadLocks = 0; + Atomic IsLocked = false; + Atomic DeadLocks = 0; void DeadLock(SpinLockData Lock); void TimeoutDeadLock(SpinLockData Lock, uint64_t Timeout);