From aeffd16b382435998b100007395660f85cd1abea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Gardou?= Date: Tue, 11 May 2021 16:14:03 +0200 Subject: [PATCH] [NTOS] Introduce KiQueuedSpinLockGuard, similar to std::lock_guard for Queued Spin lock And use it in Mm as MiPfnLockGuard --- ntoskrnl/include/internal/ke.h | 34 ++++++++++++++++++++++++++++++++++ ntoskrnl/include/internal/mm.h | 6 ++++++ 2 files changed, 40 insertions(+) diff --git a/ntoskrnl/include/internal/ke.h b/ntoskrnl/include/internal/ke.h index e5551a5527b..92558517f0f 100644 --- a/ntoskrnl/include/internal/ke.h +++ b/ntoskrnl/include/internal/ke.h @@ -1065,6 +1065,40 @@ KeBugCheckUnicodeToAnsi( #ifdef __cplusplus } // extern "C" + +namespace ntoskrnl +{ + +/* Like std::lock_guard, but for a Queued Spinlock */ +template +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" diff --git a/ntoskrnl/include/internal/mm.h b/ntoskrnl/include/internal/mm.h index 7b284a01335..c3391191a17 100644 --- a/ntoskrnl/include/internal/mm.h +++ b/ntoskrnl/include/internal/mm.h @@ -1676,4 +1676,10 @@ MiInitializeWorkingSetList(_Inout_ PMMSUPPORT WorkingSet); #ifdef __cplusplus } // extern "C" + +namespace ntoskrnl +{ +using MiPfnLockGuard = const KiQueuedSpinLockGuard; +} // namespace ntoskrnl + #endif