Added CriticalSection

This commit is contained in:
Alex 2022-11-04 05:30:39 +02:00
parent 0c428efc93
commit 0775a2662b
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD

View File

@ -72,14 +72,14 @@ public:
~SmartLockClass() { this->LockPointer->Unlock(); } ~SmartLockClass() { this->LockPointer->Unlock(); }
}; };
/** @brief Please use this macro to create a new smart critical section lock. */ /** @brief Please use this macro to create a new smart critical section lock. */
class SmartCriticalSectionClass class SmartLockCriticalSectionClass
{ {
private: private:
LockClass *LockPointer = nullptr; LockClass *LockPointer = nullptr;
bool InterruptsEnabled = false; bool InterruptsEnabled = false;
public: public:
SmartCriticalSectionClass(LockClass &Lock, const char *FunctionName) SmartLockCriticalSectionClass(LockClass &Lock, const char *FunctionName)
{ {
if (CPU::Interrupts(CPU::Check)) if (CPU::Interrupts(CPU::Check))
InterruptsEnabled = true; InterruptsEnabled = true;
@ -87,20 +87,43 @@ public:
this->LockPointer = &Lock; this->LockPointer = &Lock;
this->LockPointer->Lock(FunctionName); this->LockPointer->Lock(FunctionName);
} }
~SmartCriticalSectionClass() ~SmartLockCriticalSectionClass()
{ {
if (InterruptsEnabled) if (InterruptsEnabled)
CPU::Interrupts(CPU::Enable); CPU::Interrupts(CPU::Enable);
this->LockPointer->Unlock(); this->LockPointer->Unlock();
} }
}; };
/** @brief Please use this macro to create a new critical section. */
class SmartCriticalSectionClass
{
private:
bool InterruptsEnabled = false;
public:
bool IsInterruptsEnabled() { return InterruptsEnabled; }
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). */ /** @brief Create a new lock (can be used with SmartCriticalSection). */
#define NewLock(Name) LockClass Name #define NewLock(Name) LockClass Name
/** @brief Simple lock that is automatically released when the scope ends. */ /** @brief Simple lock that is automatically released when the scope ends. */
#define SmartLock(LockClassName) SmartLockClass CONCAT(lock##_, __COUNTER__)(LockClassName, __FUNCTION__) #define SmartLock(LockClassName) SmartLockClass CONCAT(lock##_, __COUNTER__)(LockClassName, __FUNCTION__)
/** @brief Simple critical section that is automatically released when the scope ends and interrupts are restored if they were enabled. */ /** @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, __FUNCTION__) #define SmartCriticalSection(LockClassName) SmartLockCriticalSectionClass CONCAT(lock##_, __COUNTER__)(LockClassName, __FUNCTION__)
/** @brief Automatically disable interrupts and restore them when the scope ends. */
#define CriticalSection SmartCriticalSectionClass
#endif // __cplusplus #endif // __cplusplus
#endif // !__FENNIX_KERNEL_LOCK_H__ #endif // !__FENNIX_KERNEL_LOCK_H__