ReactOS  0.4.15-dev-2144-g2282205
fxwaitlock.hpp
Go to the documentation of this file.
1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7  FxWaitLock.hpp
8 
9 Abstract:
10 
11 --*/
12 
13 #ifndef _FXWAITLOCK_HPP_
14 #define _FXWAITLOCK_HPP_
15 
16 struct FxCREvent {
19  )
20  {
21  //
22  // For kernel mode letting c'tor do the initialization to not churn the
23  // non-shared code
24  //
25 #if (FX_CORE_MODE==FX_CORE_KERNEL_MODE)
27 #else
29 #endif
30  }
31 
32 
33 
34 
35 
39  )
40  {
41 #if (FX_CORE_MODE==FX_CORE_KERNEL_MODE)
43 #else
46 #endif
47  }
48 
50  NTSTATUS
53  )
54  {
56  }
57 
59  NTSTATUS
63  )
64  {
66  }
67 
68  __drv_valueIs(==0;==258)
69  _Acquires_lock_(_Global_critical_region_)
70  NTSTATUS
71  EnterCRAndWait(
72  VOID
73  )
74  {
76 
77  Mx::MxEnterCriticalRegion();
78 
80  KernelMode,
81  FALSE,
82  NULL);
83  return status;
84  }
85 
86  NTSTATUS
88  VOID
89  )
90  {
92 
93  status = EnterCRAndWait();
94  LeaveCR();
95 
96  return status;
97  }
98 
100  __drv_when(Timeout != NULL, __drv_valueIs(==0;==258))
102  _Acquires_lock_(_Global_critical_region_)
103  NTSTATUS
104  EnterCRAndWait(
106  )
107  {
109 
110  Mx::MxEnterCriticalRegion();
111 
113  KernelMode,
114  FALSE,
116 
117  return status;
118  }
119 
121  NTSTATUS
124  )
125  {
127 
128  status = EnterCRAndWait(Timeout);
129  LeaveCR();
130 
131  return status;
132  }
133 
134  _Releases_lock_(_Global_critical_region_)
135  VOID
136  LeaveCR(
137  VOID
138  )
139  {
140  Mx::MxLeaveCriticalRegion();
141  }
142 
143  VOID
145  VOID
146  )
147  {
148  m_Event.Set();
149  }
150 
151  VOID
153  VOID
154  )
155  {
156  m_Event.Clear();
157  }
158 
159  LONG
161  VOID
162  )
163  {
164  return m_Event.ReadState();
165  }
166 
167  //
168  // Return the underlying event
169  // PKEVENT in kernel mode and event HANDLE in user-mode
170  //
171  PVOID
173  VOID
174  )
175  {
176  return m_Event.GetEvent();
177  }
178 
179  FxCREvent*
181  VOID
182  )
183  {
184  //
185  // Since operator& is hidden, we still need to be able to get a pointer
186  // to this object, so we must make it an explicit method.
187  //
188  return this;
189  }
190 private:
192  VOID
193  )
194  {
195  //
196  // By making the address of operator private, we make it harder (hopefully
197  // impossible) to accidentally use this object in an improper fashion, ie
198  // something like this is prevented:
199  //
200  // FxCREvent event;
201  // KeWaitForSingleObject(&event, ...);
202  //
203  ASSERT(FALSE);
204  return NULL;
205  }
206 
207 private:
209 };
210 
211 //
212 // Non referencable object, just pure implementation
213 //
215 
216 public:
217 
219  VOID
220  )
221  {
222  //
223  // For kernel mode letting c'tor do the initialization to not churn the
224  // non-shared code
225  //
226 #if (FX_CORE_MODE==FX_CORE_KERNEL_MODE)
227  m_Event.Initialize(SynchronizationEvent, TRUE);
228 #endif
229 
231  }
232 
234  NTSTATUS
236  )
237  {
238  return m_Event.Initialize(SynchronizationEvent, TRUE);
239  }
240 
241  //
242 
243  //
245  __drv_when(Timeout != NULL, __drv_valueIs(==0;==258))
247  _When_(return!=0x00000102L, _Acquires_lock_(_Global_critical_region_))
248  NTSTATUS
249  AcquireLock(
250  __in PFX_DRIVER_GLOBALS FxDriverGlobals,
252  )
253  {
256 
257  UNREFERENCED_PARAMETER(FxDriverGlobals);
258 
260 
261  if (Timeout != NULL) {
262  li.QuadPart = *Timeout;
263  }
264 
265  Mx::MxEnterCriticalRegion();
266  status = m_Event.WaitFor(Executive,
267  KernelMode,
268  FALSE,
269  Timeout == NULL ? NULL : &li);
270 
271  if (status == STATUS_TIMEOUT) {
272  Mx::MxLeaveCriticalRegion();
273  }
274  else {
276  }
277 
278  return status;
279  }
280 
281  static
282  BOOLEAN
283  IsLockAcquired(
285  )
286  {
287  //
288  // STATUS_TIMEOUT will return TRUE for NT_SUCCESS so check explicitly
289  //
290  return (NT_SUCCESS(Status) && Status != STATUS_TIMEOUT) ? TRUE : FALSE;
291  }
292 
293  _Releases_lock_(_Global_critical_region_)
294  VOID
295  ReleaseLock(
296  __in PFX_DRIVER_GLOBALS FxDriverGlobals
297  )
298  {
299  UNREFERENCED_PARAMETER(FxDriverGlobals);
300 
303 
304  m_Event.Set();
305  Mx::MxLeaveCriticalRegion();
306  }
307 
308 protected:
309  MxEvent m_Event;
310 
312 };
313 
314 
315 //
316 // Order is important here, FxObject *must* be the first class in the
317 // list so that &FxWaitWaitLock == &FxNonPagedObject.
318 //
319 class FxWaitLock : public FxObject, public FxWaitLockInternal {
320 
321 public:
322  // Factory function
324  static
325  NTSTATUS
326  _Create(
329  __in_opt FxObject* ParentObject,
330  __in BOOLEAN AssignDriverAsDefaultParent,
331  __out WDFWAITLOCK* LockHandle
332  );
333 
334 
336  NTSTATUS
338  )
339  {
340  return FxWaitLockInternal::Initialize(); // __super call
341  }
342 
344  __in PFX_DRIVER_GLOBALS FxDriverGlobals
345  ) :
346  FxObject(FX_TYPE_WAIT_LOCK, sizeof(FxWaitLock), FxDriverGlobals)
347  {
348  }
349 };
350 
351 #endif // _FXWAITLOCK_HPP_
FxWaitLock(__in PFX_DRIVER_GLOBALS FxDriverGlobals)
Definition: fxwaitlock.hpp:343
__drv_when(Timeout==NULL, __drv_valueIs(==0)) __drv_when(Timeout !
return
Definition: dirsup.c:529
#define _Must_inspect_result_
Definition: no_sal2.h:62
CHECK_RETURN_IF_USER_MODE NTSTATUS Initialize(__in BOOLEAN InitialState=FALSE)
Definition: fxwaitlock.hpp:51
_Releases_lock_(_Global_critical_region_) VOID LeaveCR(VOID)
Definition: fxwaitlock.hpp:134
ActualNumberDriverObjects * sizeof(PDRIVER_OBJECT)) PDRIVER_OBJECT *DriverObjectList
PVOID GetEvent(VOID)
Definition: fxwaitlock.hpp:172
#define __in_opt
Definition: dbghelp.h:38
#define TRUE
Definition: types.h:120
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
#define CHECK_RETURN_IF_USER_MODE
Definition: mxmacros.h:26
CHECK_RETURN_IF_USER_MODE NTSTATUS Initialize()
Definition: fxwaitlock.hpp:235
LONG NTSTATUS
Definition: precomp.h:26
__inline VOID Set()
Definition: mxeventkm.h:91
CHECK_RETURN_IF_USER_MODE NTSTATUS Initialize()
Definition: fxwaitlock.hpp:337
DriverGlobals
VOID Set(VOID)
Definition: fxwaitlock.hpp:144
_Must_inspect_result_ _When_(return!=0x00000102L, _Acquires_lock_(_Global_critical_region_)) NTSTATUS AcquireLock(__in PFX_DRIVER_GLOBALS FxDriverGlobals
__inline PVOID GetEvent()
Definition: mxeventkm.h:69
FxCREvent * GetSelfPointer(VOID)
Definition: fxwaitlock.hpp:180
FxCREvent(__in BOOLEAN InitialState=FALSE)
Definition: fxwaitlock.hpp:17
#define STATUS_TIMEOUT
Definition: ntstatus.h:81
__GNU_EXTENSION typedef __int64 * PLONGLONG
Definition: ntbasedef.h:382
CHECK_RETURN_IF_USER_MODE __inline NTSTATUS Initialize(__in EVENT_TYPE Type, __in BOOLEAN InitialState)
Definition: mxeventkm.h:55
#define FALSE
Definition: types.h:117
pPkgPnp m_DeviceInterfaceLock ReleaseLock(pFxDriverGlobals)
long LONG
Definition: pedump.c:60
#define _Releases_lock_(a)
Definition: btrfs_drv.h:208
#define __out
Definition: dbghelp.h:62
NTSTATUS EnterCRAndWaitAndLeave(VOID)
Definition: fxwaitlock.hpp:87
unsigned char BOOLEAN
__inline VOID Clear()
Definition: mxeventkm.h:102
enum _EVENT_TYPE EVENT_TYPE
Status
Definition: gdiplustypes.h:24
_Must_inspect_result_ __inline NTSTATUS WaitFor(__in KWAIT_REASON WaitReason, __in KPROCESSOR_MODE WaitMode, __in BOOLEAN Alertable, __in_opt PLARGE_INTEGER Timeout)
Definition: mxeventkm.h:115
#define ASSERT(a)
Definition: mode.c:45
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
VOID Clear(VOID)
Definition: fxwaitlock.hpp:152
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_ EVENT_TYPE _In_ BOOLEAN InitialState
Definition: exfuncs.h:165
Type
Definition: Type.h:6
MxEvent m_Event
Definition: fxwaitlock.hpp:208
__drv_when(Timeout==NULL, __drv_valueIs(==0)) __drv_when(Timeout !
pPkgPnp m_DeviceInterfaceLock AcquireLock(pFxDriverGlobals)
__inline LONG ReadState()
Definition: mxeventkm.h:135
MxThread m_OwningThread
Definition: fxwaitlock.hpp:311
static __inline MxThread MxGetCurrentThread()
Definition: mxgeneralkm.h:61
static ULONG Timeout
Definition: ping.c:61
_Must_inspect_result_ __in_opt PLONGLONG Timeout
Definition: fxwaitlock.hpp:251
FxCREvent(__in EVENT_TYPE Type, __in BOOLEAN InitialState)
Definition: fxwaitlock.hpp:36
__drv_valueIs(==0;==258)) __drv_when(Timeout !
static _Must_inspect_result_ NTSTATUS _Create(__in PFX_DRIVER_GLOBALS DriverGlobals, __in_opt PWDF_OBJECT_ATTRIBUTES Attributes, __in_opt FxObject *ParentObject, __in BOOLEAN AssignDriverAsDefaultParent, __out WDFWAITLOCK *LockHandle)
Definition: fxwaitlock.cpp:32
#define NULL
Definition: types.h:112
_Must_inspect_result_ NTSTATUS EnterCRAndWaitAndLeave(__in PLONGLONG Timeout)
Definition: fxwaitlock.hpp:122
CHECK_RETURN_IF_USER_MODE NTSTATUS Initialize(__in EVENT_TYPE Type, __in BOOLEAN InitialState)
Definition: fxwaitlock.hpp:60
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:729
#define _Acquires_lock_(lock)
__drv_valueIs(==0;==258) _Acquires_lock_(_Global_critical_region_) NTSTATUS EnterCRAndWait(VOID)
Definition: fxwaitlock.hpp:68
#define __in
Definition: dbghelp.h:35
static SERVICE_STATUS status
Definition: service.c:31
_Must_inspect_result_ _Acquires_lock_(_Global_critical_region_) NTSTATUS EnterCRAndWait(__in PLONGLONG Timeout)
Definition: fxwaitlock.hpp:102
LONG ReadState(VOID)
Definition: fxwaitlock.hpp:160
FxCREvent * operator &(VOID)
Definition: fxwaitlock.hpp:191
LARGE_INTEGER li
Definition: fxtimerapi.cpp:235
LONGLONG QuadPart
Definition: typedefs.h:114
_Must_inspect_result_ _In_ WDFDMAENABLER _In_ _In_opt_ PWDF_OBJECT_ATTRIBUTES Attributes
Definition: ps.c:97