https://git.reactos.org/?p=reactos.git;a=commitdiff;h=aeffd16b382435998b100…
commit aeffd16b382435998b100007395660f85cd1abea
Author:     Jérôme Gardou <jerome.gardou(a)reactos.org>
AuthorDate: Tue May 11 16:14:03 2021 +0200
Commit:     Jérôme Gardou <zefklop(a)users.noreply.github.com>
CommitDate: Tue May 18 23:42:19 2021 +0200
    [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 <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"
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<LockQueuePfnLock>;
+} // namespace ntoskrnl
+
 #endif