1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 ModuleName:
6 
7     MxPagedLockUm.h
8 
9 Abstract:
10 
11     User mode implementation of paged lock defined in
12     MxPagedLock.h
13 
14 Author:
15 
16 
17 
18 Revision History:
19 
20 
21 
22 --*/
23 
24 #pragma once
25 
26 typedef struct {
27     CRITICAL_SECTION Lock;
28     bool Initialized;
29     DWORD OwnerThreadId;
30 } MdPagedLock;
31 
32 #include "MxPagedLock.h"
33 
34 __inline
35 MxPagedLock::MxPagedLock(
36     )
37 {
38     m_Lock.Initialized = false;
39     m_Lock.OwnerThreadId = 0;
40 }
41 
42 _Must_inspect_result_
43 __inline
44 NTSTATUS
45 MxPagedLockNoDynam::Initialize(
46     )
47 {
48     if (InitializeCriticalSectionAndSpinCount(&m_Lock.Lock, 0)) {
49         m_Lock.Initialized = true;
50 
51         return S_OK;
52     }
53     else {
54         DWORD err = GetLastError();
55         return WinErrorToNtStatus(err);
56     }
57 }
58 
59 __inline
60 VOID
61 #pragma prefast(suppress:__WARNING_UNMATCHED_DEFN, "Can't apply kernel mode annotations.");
62 MxPagedLockNoDynam::Acquire(
63     )
64 {
65     EnterCriticalSection(&m_Lock.Lock);
66 
67     DWORD threadId = GetCurrentThreadId();
68 
69     if (threadId == m_Lock.OwnerThreadId) {
70         Mx::MxAssertMsg("Recursive acquision of the lock is not allowed", FALSE);
71     }
72 
73     m_Lock.OwnerThreadId = GetCurrentThreadId();
74 }
75 
76 __inline
77 VOID
78 MxPagedLockNoDynam::AcquireUnsafe(
79     )
80 {
81     MxPagedLockNoDynam::Acquire();
82 }
83 
84 __inline
85 BOOLEAN
86 #pragma prefast(suppress:__WARNING_UNMATCHED_DEFN, "Can't apply kernel mode annotations.");
87 MxPagedLockNoDynam::TryToAcquire(
88     )
89 {
90     return TryEnterCriticalSection(&m_Lock.Lock) == TRUE ? TRUE : FALSE;
91 }
92 
93 
94 __inline
95 VOID
96 #pragma prefast(suppress:__WARNING_UNMATCHED_DEFN, "Can't apply kernel mode annotations.");
97 MxPagedLockNoDynam::Release(
98     )
99 {
100     m_Lock.OwnerThreadId = 0;
101 
102     LeaveCriticalSection(&m_Lock.Lock);
103 }
104 
105 __inline
106 VOID
107 MxPagedLockNoDynam::ReleaseUnsafe(
108     )
109 {
110     MxPagedLockNoDynam::Release();
111 }
112 
113 __inline
114 VOID
115 MxPagedLockNoDynam::Uninitialize(
116     )
117 {
118     DeleteCriticalSection(&m_Lock.Lock);
119     m_Lock.Initialized = false;
120 }
121 
122 __inline
123 MxPagedLock::~MxPagedLock(
124     )
125 {
126     if (m_Lock.Initialized) {
127         this->Uninitialize();
128     }
129 }
130