Implement RAII guard for critical regions
This commit is contained in:
@@ -16,6 +16,40 @@
|
|||||||
/* Kernel Library */
|
/* Kernel Library */
|
||||||
namespace KE
|
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
|
class QueuedSpinLockGuard
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
@@ -47,36 +81,6 @@ namespace KE
|
|||||||
QueuedSpinLockGuard& operator=(const QueuedSpinLockGuard&) = delete;
|
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
|
class SpinLockGuard
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
@@ -106,6 +110,36 @@ namespace KE
|
|||||||
SpinLockGuard(const SpinLockGuard&) = delete;
|
SpinLockGuard(const SpinLockGuard&) = delete;
|
||||||
SpinLockGuard& operator=(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 */
|
#endif /* __XTOSKRNL_KE_GUARD_HH */
|
||||||
|
|||||||
@@ -840,7 +840,7 @@ OB::LifeCycle::DeleteObject(IN PVOID Object,
|
|||||||
if(CreatorInfo && !RTL::LinkedList::ListEmpty(&CreatorInfo->TypeList))
|
if(CreatorInfo && !RTL::LinkedList::ListEmpty(&CreatorInfo->TypeList))
|
||||||
{
|
{
|
||||||
/* Acquire exclusive access to the object type tracking list */
|
/* Acquire exclusive access to the object type tracking list */
|
||||||
KE::KThread::EnterCriticalRegion();
|
KE::CriticalRegionGuard CriticalRegion;
|
||||||
KE::PushLock::AcquireExclusivePushLock(&ObjectType->TypeLock);
|
KE::PushLock::AcquireExclusivePushLock(&ObjectType->TypeLock);
|
||||||
|
|
||||||
/* Unlink the object from the creator type list */
|
/* Unlink the object from the creator type list */
|
||||||
@@ -848,7 +848,6 @@ OB::LifeCycle::DeleteObject(IN PVOID Object,
|
|||||||
|
|
||||||
/* Release the object type tracking list */
|
/* Release the object type tracking list */
|
||||||
KE::PushLock::ReleaseExclusivePushLock(&ObjectType->TypeLock);
|
KE::PushLock::ReleaseExclusivePushLock(&ObjectType->TypeLock);
|
||||||
KE::KThread::LeaveCriticalRegion();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if the object has an allocated name buffer */
|
/* Check if the object has an allocated name buffer */
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ PS::ProcessManager::InitializeProcessManager(VOID)
|
|||||||
RTL::Memory::ZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
|
RTL::Memory::ZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
|
||||||
|
|
||||||
/* Initialize common attributes */
|
/* Initialize common attributes */
|
||||||
ObjectTypeInitializer.InvalidAttributes = OBJECT_PERMANENT | OBJECT_EXCLUSIVE | OBJECT_OPENIF;
|
ObjectTypeInitializer.InvalidAttributes = OBJECT_EXCLUSIVE | OBJECT_OPENIF | OBJECT_PERMANENT;
|
||||||
ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
|
ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
|
||||||
ObjectTypeInitializer.PoolType = NonPagedPool;
|
ObjectTypeInitializer.PoolType = NonPagedPool;
|
||||||
ObjectTypeInitializer.SecurityRequired = TRUE;
|
ObjectTypeInitializer.SecurityRequired = TRUE;
|
||||||
|
|||||||
Reference in New Issue
Block a user