mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-07-11 07:19:20 +00:00
Update kernel
This commit is contained in:
181
include/lock.hpp
181
include/lock.hpp
@ -39,121 +39,158 @@ size_t GetLocksCount();
|
||||
class LockClass
|
||||
{
|
||||
public:
|
||||
struct SpinLockData
|
||||
{
|
||||
std::atomic_uint64_t LockData = 0x0;
|
||||
std::atomic<const char *> CurrentHolder = "(nul)";
|
||||
std::atomic<const char *> AttemptingToGet = "(nul)";
|
||||
std::atomic_uintptr_t StackPointerHolder = 0;
|
||||
std::atomic_uintptr_t StackPointerAttempt = 0;
|
||||
std::atomic_size_t Count = 0;
|
||||
std::atomic_long Core = 0;
|
||||
};
|
||||
struct SpinLockData
|
||||
{
|
||||
std::atomic_uint64_t LockData = 0x0;
|
||||
std::atomic<const char *> CurrentHolder = "(nul)";
|
||||
std::atomic<const char *> AttemptingToGet = "(nul)";
|
||||
std::atomic_uintptr_t StackPointerHolder = 0;
|
||||
std::atomic_uintptr_t StackPointerAttempt = 0;
|
||||
std::atomic_size_t Count = 0;
|
||||
std::atomic_long Core = 0;
|
||||
};
|
||||
|
||||
private:
|
||||
SpinLockData LockData;
|
||||
std::atomic_bool IsLocked = false;
|
||||
std::atomic_ulong DeadLocks = 0;
|
||||
SpinLockData LockData;
|
||||
std::atomic_bool IsLocked = false;
|
||||
std::atomic_ulong DeadLocks = 0;
|
||||
|
||||
void DeadLock(SpinLockData Lock);
|
||||
void TimeoutDeadLock(SpinLockData Lock, uint64_t Timeout);
|
||||
void DeadLock(SpinLockData &Lock);
|
||||
void TimeoutDeadLock(SpinLockData &Lock, uint64_t Timeout);
|
||||
void Yield();
|
||||
|
||||
public:
|
||||
SpinLockData *GetLockData() { return &LockData; }
|
||||
int Lock(const char *FunctionName);
|
||||
int Unlock();
|
||||
SpinLockData *GetLockData() { return &LockData; }
|
||||
int Lock(const char *FunctionName);
|
||||
int Unlock();
|
||||
|
||||
int TimeoutLock(const char *FunctionName, uint64_t Timeout);
|
||||
int TimeoutLock(const char *FunctionName, uint64_t Timeout);
|
||||
};
|
||||
|
||||
/** @brief Please use this macro to create a new smart lock. */
|
||||
class SmartLockClass
|
||||
{
|
||||
private:
|
||||
LockClass *LockPointer = nullptr;
|
||||
LockClass *LockPointer = nullptr;
|
||||
|
||||
public:
|
||||
SmartLockClass(LockClass &Lock, const char *FunctionName)
|
||||
{
|
||||
this->LockPointer = &Lock;
|
||||
this->LockPointer->Lock(FunctionName);
|
||||
}
|
||||
~SmartLockClass() { this->LockPointer->Unlock(); }
|
||||
SmartLockClass(LockClass &Lock, const char *FunctionName)
|
||||
{
|
||||
this->LockPointer = &Lock;
|
||||
this->LockPointer->Lock(FunctionName);
|
||||
}
|
||||
~SmartLockClass() { this->LockPointer->Unlock(); }
|
||||
};
|
||||
|
||||
class SmartTimeoutLockClass
|
||||
{
|
||||
private:
|
||||
LockClass *LockPointer = nullptr;
|
||||
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(); }
|
||||
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
|
||||
{
|
||||
private:
|
||||
LockClass *LockPointer = nullptr;
|
||||
bool InterruptsEnabled = false;
|
||||
LockClass *LockPointer = nullptr;
|
||||
bool InterruptsEnabled = false;
|
||||
|
||||
public:
|
||||
SmartLockCriticalSectionClass(LockClass &Lock, const char *FunctionName)
|
||||
{
|
||||
if (CPU::Interrupts(CPU::Check))
|
||||
InterruptsEnabled = true;
|
||||
CPU::Interrupts(CPU::Disable);
|
||||
this->LockPointer = &Lock;
|
||||
this->LockPointer->Lock(FunctionName);
|
||||
}
|
||||
~SmartLockCriticalSectionClass()
|
||||
{
|
||||
this->LockPointer->Unlock();
|
||||
if (InterruptsEnabled)
|
||||
CPU::Interrupts(CPU::Enable);
|
||||
}
|
||||
SmartLockCriticalSectionClass(LockClass &Lock,
|
||||
const char *FunctionName)
|
||||
{
|
||||
if (CPU::Interrupts(CPU::Check))
|
||||
InterruptsEnabled = true;
|
||||
CPU::Interrupts(CPU::Disable);
|
||||
this->LockPointer = &Lock;
|
||||
this->LockPointer->Lock(FunctionName);
|
||||
}
|
||||
~SmartLockCriticalSectionClass()
|
||||
{
|
||||
this->LockPointer->Unlock();
|
||||
if (InterruptsEnabled)
|
||||
CPU::Interrupts(CPU::Enable);
|
||||
}
|
||||
};
|
||||
|
||||
/** @brief Please use this macro to create a new critical section. */
|
||||
class SmartCriticalSectionClass
|
||||
{
|
||||
private:
|
||||
bool InterruptsEnabled = false;
|
||||
bool InterruptsEnabled = false;
|
||||
|
||||
public:
|
||||
bool IsInterruptsEnabled() { return InterruptsEnabled; }
|
||||
bool IsInterruptsEnabled()
|
||||
{
|
||||
return InterruptsEnabled;
|
||||
}
|
||||
|
||||
SmartCriticalSectionClass()
|
||||
{
|
||||
if (CPU::Interrupts(CPU::Check))
|
||||
InterruptsEnabled = true;
|
||||
CPU::Interrupts(CPU::Disable);
|
||||
}
|
||||
~SmartCriticalSectionClass()
|
||||
{
|
||||
if (InterruptsEnabled)
|
||||
CPU::Interrupts(CPU::Enable);
|
||||
}
|
||||
SmartCriticalSectionClass()
|
||||
{
|
||||
if (CPU::Interrupts(CPU::Check))
|
||||
InterruptsEnabled = true;
|
||||
CPU::Interrupts(CPU::Disable);
|
||||
}
|
||||
~SmartCriticalSectionClass()
|
||||
{
|
||||
if (InterruptsEnabled)
|
||||
CPU::Interrupts(CPU::Enable);
|
||||
}
|
||||
};
|
||||
|
||||
/** @brief Create a new lock (can be used with SmartCriticalSection). */
|
||||
/**
|
||||
* Create a new lock
|
||||
*
|
||||
* @note 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__)
|
||||
/**
|
||||
* 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)
|
||||
/**
|
||||
* 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__)
|
||||
/**
|
||||
* 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. */
|
||||
/**
|
||||
* Automatically disable interrupts and
|
||||
* restore them when the scope ends.
|
||||
*/
|
||||
#define CriticalSection SmartCriticalSectionClass
|
||||
|
||||
#endif // __cplusplus
|
||||
|
Reference in New Issue
Block a user