Implement RAII guard for critical regions
Some checks failed
Builds / ExectOS (i686, release) (push) Failing after 30s
Builds / ExectOS (amd64, release) (push) Failing after 32s
Builds / ExectOS (i686, debug) (push) Failing after 40s
Builds / ExectOS (amd64, debug) (push) Failing after 43s

This commit is contained in:
2026-07-10 08:03:58 +02:00
parent a12e21f15c
commit e8fa736c7a
3 changed files with 66 additions and 33 deletions

View File

@@ -16,6 +16,40 @@
/* Kernel Library */
namespace KE
{
class CriticalRegionGuard
{
private:
BOOLEAN Owned;
PKTHREAD SystemThread;
public:
CriticalRegionGuard(IN PKTHREAD Thread = NULLPTR,
IN BOOLEAN Acquire = TRUE)
{
Owned = Acquire;
SystemThread = Thread;
if(Owned)
{
if(!SystemThread)
{
SystemThread = KE::Processor::GetCurrentThread();
}
KE::KThread::EnterCriticalRegion(SystemThread);
}
}
~CriticalRegionGuard()
{
if(Owned)
{
KE::KThread::LeaveCriticalRegion(SystemThread);
}
}
CriticalRegionGuard(const CriticalRegionGuard&) = delete;
CriticalRegionGuard& operator=(const CriticalRegionGuard&) = delete;
};
class QueuedSpinLockGuard
{
private:
@@ -47,36 +81,6 @@ namespace KE
QueuedSpinLockGuard& operator=(const QueuedSpinLockGuard&) = delete;
};
class SystemQueuedSpinLockGuard
{
private:
KSPIN_LOCK_QUEUE_LEVEL QueuedLockLevel;
BOOLEAN Owned;
public:
SystemQueuedSpinLockGuard(IN OUT KSPIN_LOCK_QUEUE_LEVEL LockLevel,
IN BOOLEAN Acquire = TRUE)
{
QueuedLockLevel = LockLevel;
Owned = Acquire;
if(Owned)
{
KE::SpinLock::AcquireQueuedSpinLock(QueuedLockLevel);
}
}
~SystemQueuedSpinLockGuard()
{
if(Owned)
{
KE::SpinLock::ReleaseQueuedSpinLock(QueuedLockLevel);
}
}
SystemQueuedSpinLockGuard(const SystemQueuedSpinLockGuard&) = delete;
SystemQueuedSpinLockGuard& operator=(const SystemQueuedSpinLockGuard&) = delete;
};
class SpinLockGuard
{
private:
@@ -106,6 +110,36 @@ namespace KE
SpinLockGuard(const SpinLockGuard&) = delete;
SpinLockGuard& operator=(const SpinLockGuard&) = delete;
};
class SystemQueuedSpinLockGuard
{
private:
KSPIN_LOCK_QUEUE_LEVEL QueuedLockLevel;
BOOLEAN Owned;
public:
SystemQueuedSpinLockGuard(IN OUT KSPIN_LOCK_QUEUE_LEVEL LockLevel,
IN BOOLEAN Acquire = TRUE)
{
QueuedLockLevel = LockLevel;
Owned = Acquire;
if(Owned)
{
KE::SpinLock::AcquireQueuedSpinLock(QueuedLockLevel);
}
}
~SystemQueuedSpinLockGuard()
{
if(Owned)
{
KE::SpinLock::ReleaseQueuedSpinLock(QueuedLockLevel);
}
}
SystemQueuedSpinLockGuard(const SystemQueuedSpinLockGuard&) = delete;
SystemQueuedSpinLockGuard& operator=(const SystemQueuedSpinLockGuard&) = delete;
};
}
#endif /* __XTOSKRNL_KE_GUARD_HH */

View File

@@ -840,7 +840,7 @@ OB::LifeCycle::DeleteObject(IN PVOID Object,
if(CreatorInfo && !RTL::LinkedList::ListEmpty(&CreatorInfo->TypeList))
{
/* Acquire exclusive access to the object type tracking list */
KE::KThread::EnterCriticalRegion();
KE::CriticalRegionGuard CriticalRegion;
KE::PushLock::AcquireExclusivePushLock(&ObjectType->TypeLock);
/* Unlink the object from the creator type list */
@@ -848,7 +848,6 @@ OB::LifeCycle::DeleteObject(IN PVOID Object,
/* Release the object type tracking list */
KE::PushLock::ReleaseExclusivePushLock(&ObjectType->TypeLock);
KE::KThread::LeaveCriticalRegion();
}
/* Check if the object has an allocated name buffer */

View File

@@ -57,7 +57,7 @@ PS::ProcessManager::InitializeProcessManager(VOID)
RTL::Memory::ZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
/* Initialize common attributes */
ObjectTypeInitializer.InvalidAttributes = OBJECT_PERMANENT | OBJECT_EXCLUSIVE | OBJECT_OPENIF;
ObjectTypeInitializer.InvalidAttributes = OBJECT_EXCLUSIVE | OBJECT_OPENIF | OBJECT_PERMANENT;
ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
ObjectTypeInitializer.PoolType = NonPagedPool;
ObjectTypeInitializer.SecurityRequired = TRUE;