mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-05-29 15:57:59 +00:00
Updated lock to use atomic operations
This commit is contained in:
parent
ee6d589bfd
commit
c968b6f2d5
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
#include <smp.hpp>
|
#include <smp.hpp>
|
||||||
#include <atomic.hpp>
|
|
||||||
|
|
||||||
#include "../kernel.h"
|
#include "../kernel.h"
|
||||||
|
|
||||||
@ -21,7 +20,7 @@ void LockClass::DeadLock(SpinLockData Lock)
|
|||||||
|
|
||||||
this->DeadLocks++;
|
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);
|
warn("Unlocking lock '%s' to prevent deadlock. (this is enabled in the kernel config)", Lock.AttemptingToGet);
|
||||||
this->DeadLocks = 0;
|
this->DeadLocks = 0;
|
||||||
@ -50,7 +49,7 @@ int LockClass::Lock(const char *FunctionName)
|
|||||||
LockData.AttemptingToGet = FunctionName;
|
LockData.AttemptingToGet = FunctionName;
|
||||||
Retry:
|
Retry:
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
while (__atomic_exchange_n(&IsLocked, true, __ATOMIC_ACQUIRE) && ++i < 0x10000000)
|
while (IsLocked.Exchange(true, MemoryBorder::Acquire) && ++i < 0x10000000)
|
||||||
CPU::Pause();
|
CPU::Pause();
|
||||||
if (i >= 0x10000000)
|
if (i >= 0x10000000)
|
||||||
{
|
{
|
||||||
@ -78,7 +77,7 @@ int LockClass::Unlock()
|
|||||||
// IsLocked = false;
|
// IsLocked = false;
|
||||||
|
|
||||||
__sync_synchronize();
|
__sync_synchronize();
|
||||||
__atomic_store_n(&IsLocked, false, __ATOMIC_RELEASE);
|
IsLocked.Store(false, MemoryBorder::Release);
|
||||||
LockData.Count--;
|
LockData.Count--;
|
||||||
IsLocked = false;
|
IsLocked = false;
|
||||||
|
|
||||||
@ -115,13 +114,13 @@ int LockClass::TimeoutLock(const char *FunctionName, uint64_t Timeout)
|
|||||||
Atomic<uint64_t> Target = 0;
|
Atomic<uint64_t> Target = 0;
|
||||||
Retry:
|
Retry:
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
while (__atomic_exchange_n(&IsLocked, true, __ATOMIC_ACQUIRE) && ++i < 0x10000000)
|
while (IsLocked.Exchange(true, MemoryBorder::Acquire) && ++i < 0x10000000)
|
||||||
CPU::Pause();
|
CPU::Pause();
|
||||||
if (i >= 0x10000000)
|
if (i >= 0x10000000)
|
||||||
{
|
{
|
||||||
if (Target.Load() == 0)
|
if (Target == 0)
|
||||||
Target = TimeManager->CalculateTarget(Timeout);
|
Target = TimeManager->CalculateTarget(Timeout);
|
||||||
TimeoutDeadLock(LockData, Target.Load());
|
TimeoutDeadLock(LockData, Target);
|
||||||
goto Retry;
|
goto Retry;
|
||||||
}
|
}
|
||||||
LockData.Count++;
|
LockData.Count++;
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
#define __FENNIX_KERNEL_LOCK_H__
|
#define __FENNIX_KERNEL_LOCK_H__
|
||||||
|
|
||||||
#include <types.h>
|
#include <types.h>
|
||||||
|
|
||||||
|
#include <atomic.hpp>
|
||||||
#include <cpu.hpp>
|
#include <cpu.hpp>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
@ -11,17 +13,17 @@ class LockClass
|
|||||||
{
|
{
|
||||||
struct SpinLockData
|
struct SpinLockData
|
||||||
{
|
{
|
||||||
uint64_t LockData = 0x0;
|
Atomic<uint64_t> LockData = 0x0;
|
||||||
const char *CurrentHolder = "(nul)";
|
Atomic<const char *> CurrentHolder = "(nul)";
|
||||||
const char *AttemptingToGet = "(nul)";
|
Atomic<const char *> AttemptingToGet = "(nul)";
|
||||||
size_t Count = 0;
|
Atomic<size_t> Count = 0;
|
||||||
long Core = 0;
|
Atomic<long> Core = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SpinLockData LockData;
|
SpinLockData LockData;
|
||||||
bool IsLocked = false;
|
Atomic<bool> IsLocked = false;
|
||||||
unsigned long DeadLocks = 0;
|
Atomic<unsigned long> DeadLocks = 0;
|
||||||
|
|
||||||
void DeadLock(SpinLockData Lock);
|
void DeadLock(SpinLockData Lock);
|
||||||
void TimeoutDeadLock(SpinLockData Lock, uint64_t Timeout);
|
void TimeoutDeadLock(SpinLockData Lock, uint64_t Timeout);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user