mirror of
https://github.com/EnderIce2/Fennix.git
synced 2025-07-08 13:59:17 +00:00
Added timeout lock
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
|
||||
#include <debug.h>
|
||||
#include <smp.hpp>
|
||||
#include <atomic.hpp>
|
||||
|
||||
#include "../kernel.h"
|
||||
|
||||
@ -22,9 +23,7 @@ void LockClass::DeadLock(SpinLockData Lock)
|
||||
|
||||
if (Config.UnlockDeadLock && this->DeadLocks > 10)
|
||||
{
|
||||
warn("Unlocking lock '%s' held by '%s'! %ld locks in queue. Core %ld is being held by %ld.",
|
||||
Lock.AttemptingToGet, Lock.CurrentHolder,
|
||||
Lock.Count, CCore, Lock.Core);
|
||||
warn("Unlocking lock '%s' to prevent deadlock. (this is enabled in the kernel config)", Lock.AttemptingToGet);
|
||||
this->DeadLocks = 0;
|
||||
this->Unlock();
|
||||
}
|
||||
@ -85,3 +84,52 @@ int LockClass::Unlock()
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void LockClass::TimeoutDeadLock(SpinLockData Lock, uint64_t Timeout)
|
||||
{
|
||||
CPUData *CoreData = GetCurrentCPU();
|
||||
long CCore = 0xdead;
|
||||
if (CoreData != nullptr)
|
||||
CCore = CoreData->ID;
|
||||
warn("Potential deadlock in lock '%s' held by '%s'! %ld locks in queue. Core %ld is being held by %ld. Timeout in %ld",
|
||||
Lock.AttemptingToGet, Lock.CurrentHolder,
|
||||
Lock.Count, CCore, Lock.Core,
|
||||
Timeout);
|
||||
|
||||
// TODO: Print on screen too.
|
||||
|
||||
uint64_t Counter = TimeManager->GetCounter();
|
||||
if (Timeout < Counter)
|
||||
{
|
||||
warn("Unlocking lock '%s' because of timeout. (%ld < %ld)", Lock.AttemptingToGet, Timeout, Counter);
|
||||
this->Unlock();
|
||||
}
|
||||
|
||||
if (TaskManager)
|
||||
TaskManager->Schedule();
|
||||
}
|
||||
|
||||
int LockClass::TimeoutLock(const char *FunctionName, uint64_t Timeout)
|
||||
{
|
||||
LockData.AttemptingToGet = FunctionName;
|
||||
Atomic<uint64_t> Target = 0;
|
||||
Retry:
|
||||
unsigned int i = 0;
|
||||
while (__atomic_exchange_n(&IsLocked, true, __ATOMIC_ACQUIRE) && ++i < 0x10000000)
|
||||
CPU::Pause();
|
||||
if (i >= 0x10000000)
|
||||
{
|
||||
if (Target.Load() == 0)
|
||||
Target = TimeManager->CalculateTarget(Timeout);
|
||||
TimeoutDeadLock(LockData, Target.Load());
|
||||
goto Retry;
|
||||
}
|
||||
LockData.Count++;
|
||||
LockData.CurrentHolder = FunctionName;
|
||||
CPUData *CoreData = GetCurrentCPU();
|
||||
if (CoreData != nullptr)
|
||||
LockData.Core = CoreData->ID;
|
||||
__sync_synchronize();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user