mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-07-11 07:19:20 +00:00
Implemented SmartCriticalSection
This commit is contained in:
@ -1,11 +1,15 @@
|
||||
#ifndef __FENNIX_KERNEL_LOCK_H__
|
||||
#define __FENNIX_KERNEL_LOCK_H__
|
||||
|
||||
/*
|
||||
TODO: Add deadlock detection.
|
||||
*/
|
||||
|
||||
#include <types.h>
|
||||
#include <cpu.hpp>
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
/** @brief Please use this macro to create a new lock. */
|
||||
class LockClass
|
||||
{
|
||||
private:
|
||||
@ -28,26 +32,50 @@ public:
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
#define NEWLOCK(Name) LockClass Name
|
||||
|
||||
class SmartLock
|
||||
/** @brief Please use this macro to create a new smart lock. */
|
||||
class SmartLockClass
|
||||
{
|
||||
private:
|
||||
LockClass *LockPointer = nullptr;
|
||||
|
||||
public:
|
||||
SmartLock(LockClass &Lock)
|
||||
SmartLockClass(LockClass &Lock)
|
||||
{
|
||||
this->LockPointer = &Lock;
|
||||
this->LockPointer->Lock();
|
||||
}
|
||||
~SmartLock() { this->LockPointer->Unlock(); }
|
||||
~SmartLockClass() { this->LockPointer->Unlock(); }
|
||||
};
|
||||
/** @brief Please use this macro to create a new smart critical section lock. */
|
||||
class SmartCriticalSectionClass
|
||||
{
|
||||
private:
|
||||
LockClass *LockPointer = nullptr;
|
||||
bool InterruptsEnabled = false;
|
||||
|
||||
public:
|
||||
SmartCriticalSectionClass(LockClass &Lock)
|
||||
{
|
||||
if (CPU::Interrupts(CPU::Check))
|
||||
InterruptsEnabled = true;
|
||||
CPU::Interrupts(CPU::Disable);
|
||||
this->LockPointer = &Lock;
|
||||
this->LockPointer->Lock();
|
||||
}
|
||||
~SmartCriticalSectionClass()
|
||||
{
|
||||
if (InterruptsEnabled)
|
||||
CPU::Interrupts(CPU::Enable);
|
||||
this->LockPointer->Unlock();
|
||||
}
|
||||
};
|
||||
|
||||
#define SL_CONCAT(x, y) x##y
|
||||
#define SMARTLOCK(LockClassName) SmartLock SL_CONCAT(lock##_, __COUNTER__)(LockClassName)
|
||||
|
||||
#endif
|
||||
/** @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)
|
||||
/** @brief Simple critical section that is automatically released when the scope ends and interrupts are restored if they were enabled. */
|
||||
#define SmartCriticalSection(LockClassName) SmartCriticalSectionClass CONCAT(lock##_, __COUNTER__)(LockClassName)
|
||||
|
||||
#endif // __cplusplus
|
||||
#endif // !__FENNIX_KERNEL_LOCK_H__
|
||||
|
@ -232,7 +232,7 @@ namespace Memory
|
||||
class Physical
|
||||
{
|
||||
private:
|
||||
NEWLOCK(MemoryLock);
|
||||
NewLock(MemoryLock);
|
||||
|
||||
uint64_t TotalMemory = 0;
|
||||
uint64_t FreeMemory = 0;
|
||||
@ -358,7 +358,7 @@ namespace Memory
|
||||
class Virtual
|
||||
{
|
||||
private:
|
||||
NEWLOCK(MemoryLock);
|
||||
NewLock(MemoryLock);
|
||||
PageTable *Table = nullptr;
|
||||
|
||||
class PageMapIndexer
|
||||
|
@ -33,6 +33,7 @@
|
||||
#endif
|
||||
|
||||
#define UNUSED(x) (void)(x)
|
||||
#define CONCAT(x, y) x##y
|
||||
|
||||
#ifndef __va_list__
|
||||
typedef __builtin_va_list va_list;
|
||||
|
Reference in New Issue
Block a user