[NTOS] Introduce KiQueuedSpinLockGuard, similar to std::lock_guard for Queued Spin lock

And use it in Mm as MiPfnLockGuard
This commit is contained in:
Jérôme Gardou 2021-05-11 16:14:03 +02:00 committed by Jérôme Gardou
parent ffa7cfc1ff
commit aeffd16b38
2 changed files with 40 additions and 0 deletions

View file

@ -1065,6 +1065,40 @@ KeBugCheckUnicodeToAnsi(
#ifdef __cplusplus
} // extern "C"
namespace ntoskrnl
{
/* Like std::lock_guard, but for a Queued Spinlock */
template <KSPIN_LOCK_QUEUE_NUMBER n>
class KiQueuedSpinLockGuard
{
private:
KIRQL m_OldIrql;
public:
_Requires_lock_not_held_(n)
_Acquires_lock_(n)
_IRQL_raises_(DISPATCH_LEVEL)
explicit KiQueuedSpinLockGuard()
{
m_OldIrql = KeAcquireQueuedSpinLock(n);
}
_Requires_lock_held_(n)
_Releases_lock_(n)
~KiQueuedSpinLockGuard()
{
KeReleaseQueuedSpinLock(n, m_OldIrql);
}
private:
KiQueuedSpinLockGuard(KiQueuedSpinLockGuard const&) = delete;
KiQueuedSpinLockGuard& operator=(KiQueuedSpinLockGuard const&) = delete;
};
}
#endif
#include "ke_x.h"

View file

@ -1676,4 +1676,10 @@ MiInitializeWorkingSetList(_Inout_ PMMSUPPORT WorkingSet);
#ifdef __cplusplus
} // extern "C"
namespace ntoskrnl
{
using MiPfnLockGuard = const KiQueuedSpinLockGuard<LockQueuePfnLock>;
} // namespace ntoskrnl
#endif