From f51c7a5ce598928b32019587ea18801c27da0615 Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Fri, 10 Jul 2026 08:27:09 +0200 Subject: [PATCH] Implement RAII guards for exclusive and shared push locks --- xtoskrnl/includes/ke/guard.hh | 61 +++++++++++++++++++++++++++++++++++ xtoskrnl/ob/lifecycl.cc | 5 +-- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/xtoskrnl/includes/ke/guard.hh b/xtoskrnl/includes/ke/guard.hh index 7c1a6e2..42b2dd7 100644 --- a/xtoskrnl/includes/ke/guard.hh +++ b/xtoskrnl/includes/ke/guard.hh @@ -12,6 +12,7 @@ #include #include #include +#include #include @@ -52,6 +53,66 @@ namespace KE CriticalRegionGuard& operator=(const CriticalRegionGuard&) = delete; }; + class PushLockExclusiveGuard + { + private: + PKPUSH_LOCK Lock; + BOOLEAN Owned; + + public: + PushLockExclusiveGuard(IN PKPUSH_LOCK PushLock, + IN BOOLEAN Acquire = TRUE) + { + Lock = PushLock; + Owned = Acquire; + if(Owned) + { + KE::PushLock::AcquireExclusivePushLock(Lock); + } + } + + ~PushLockExclusiveGuard() + { + if(Owned) + { + KE::PushLock::ReleaseExclusivePushLock(Lock); + } + } + + PushLockExclusiveGuard(const PushLockExclusiveGuard&) = delete; + PushLockExclusiveGuard& operator=(const PushLockExclusiveGuard&) = delete; + }; + + class PushLockSharedGuard + { + private: + PKPUSH_LOCK Lock; + BOOLEAN Owned; + + public: + PushLockSharedGuard(IN PKPUSH_LOCK PushLock, + IN BOOLEAN Acquire = TRUE) + { + Lock = PushLock; + Owned = Acquire; + if(Owned) + { + KE::PushLock::AcquireSharedPushLock(Lock); + } + } + + ~PushLockSharedGuard() + { + if(Owned) + { + KE::PushLock::ReleaseSharedPushLock(Lock); + } + } + + PushLockSharedGuard(const PushLockSharedGuard&) = delete; + PushLockSharedGuard& operator=(const PushLockSharedGuard&) = delete; + }; + class QueuedSpinLockGuard { private: diff --git a/xtoskrnl/ob/lifecycl.cc b/xtoskrnl/ob/lifecycl.cc index 4cccc31..9ae5b23 100644 --- a/xtoskrnl/ob/lifecycl.cc +++ b/xtoskrnl/ob/lifecycl.cc @@ -841,13 +841,10 @@ OB::LifeCycle::DeleteObject(IN PVOID Object, { /* Acquire exclusive access to the object type tracking list */ KE::CriticalRegionGuard CriticalRegion; - KE::PushLock::AcquireExclusivePushLock(&ObjectType->TypeLock); + KE::PushLockExclusiveGuard PushLock(&ObjectType->TypeLock); /* Unlink the object from the creator type list */ RTL::LinkedList::RemoveEntryList(&CreatorInfo->TypeList); - - /* Release the object type tracking list */ - KE::PushLock::ReleaseExclusivePushLock(&ObjectType->TypeLock); } /* Check if the object has an allocated name buffer */