Added timeout lock

This commit is contained in:
Alex
2023-02-06 19:32:20 +02:00
parent 98b797a95c
commit 88008ac470
2 changed files with 80 additions and 4 deletions

View File

@ -5,6 +5,7 @@
#include <cpu.hpp>
#ifdef __cplusplus
/** @brief Please use this macro to create a new lock. */
class LockClass
{
@ -16,18 +17,23 @@ class LockClass
size_t Count = 0;
long Core = 0;
};
void DeadLock(SpinLockData Lock);
private:
SpinLockData LockData;
bool IsLocked = false;
unsigned long DeadLocks = 0;
void DeadLock(SpinLockData Lock);
void TimeoutDeadLock(SpinLockData Lock, uint64_t Timeout);
public:
SpinLockData *GetLockData() { return &LockData; }
int Lock(const char *FunctionName);
int Unlock();
int TimeoutLock(const char *FunctionName, uint64_t Timeout);
};
/** @brief Please use this macro to create a new smart lock. */
class SmartLockClass
{
@ -42,6 +48,21 @@ public:
}
~SmartLockClass() { this->LockPointer->Unlock(); }
};
class SmartTimeoutLockClass
{
private:
LockClass *LockPointer = nullptr;
public:
SmartTimeoutLockClass(LockClass &Lock, const char *FunctionName, uint64_t Timeout)
{
this->LockPointer = &Lock;
this->LockPointer->TimeoutLock(FunctionName, Timeout);
}
~SmartTimeoutLockClass() { this->LockPointer->Unlock(); }
};
/** @brief Please use this macro to create a new smart critical section lock. */
class SmartLockCriticalSectionClass
{
@ -65,6 +86,7 @@ public:
CPU::Interrupts(CPU::Enable);
}
};
/** @brief Please use this macro to create a new critical section. */
class SmartCriticalSectionClass
{
@ -89,10 +111,16 @@ public:
/** @brief Create a new lock (can be used with SmartCriticalSection). */
#define NewLock(Name) LockClass Name
/** @brief Simple lock that is automatically released when the scope ends. */
#define SmartLock(LockClassName) SmartLockClass CONCAT(lock##_, __COUNTER__)(LockClassName, __FUNCTION__)
/** @brief Simple lock with timeout that is automatically released when the scope ends. */
#define SmartTimeoutLock(LockClassName, Timeout) SmartTimeoutLockClass CONCAT(lock##_, __COUNTER__)(LockClassName, __FUNCTION__, Timeout)
/** @brief Simple critical section that is automatically released when the scope ends and interrupts are restored if they were enabled. */
#define SmartCriticalSection(LockClassName) SmartLockCriticalSectionClass CONCAT(lock##_, __COUNTER__)(LockClassName, __FUNCTION__)
/** @brief Automatically disable interrupts and restore them when the scope ends. */
#define CriticalSection SmartCriticalSectionClass