ReactOS  0.4.15-dev-3453-gff89651
evtpair.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS Kernel
4  * FILE: ntoskrnl/ex/evtpair.c
5  * PURPOSE: Support for event pairs
6  * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
7  * Thomas Weidenmueller
8  */
9 
10 /* INCLUDES *****************************************************************/
11 
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15 
16 /* GLOBALS *******************************************************************/
17 
19 
21 {
26 };
27 
28 /* FUNCTIONS *****************************************************************/
29 
30 CODE_SEG("INIT")
31 BOOLEAN
32 NTAPI
34 {
35  OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
38  DPRINT("Creating Event Pair Object Type\n");
39 
40  /* Create the Event Pair Object Type */
41  RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
42  RtlInitUnicodeString(&Name, L"EventPair");
43  ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
44  ObjectTypeInitializer.DefaultNonPagedPoolCharge = sizeof(KEVENT_PAIR);
45  ObjectTypeInitializer.GenericMapping = ExEventPairMapping;
46  ObjectTypeInitializer.PoolType = NonPagedPool;
47  ObjectTypeInitializer.ValidAccessMask = EVENT_PAIR_ALL_ACCESS;
48  ObjectTypeInitializer.UseDefaultObject = TRUE;
49  ObjectTypeInitializer.InvalidAttributes = OBJ_OPENLINK;
50  Status = ObCreateObjectType(&Name, &ObjectTypeInitializer, NULL, &ExEventPairObjectType);
51  if (!NT_SUCCESS(Status)) return FALSE;
52  return TRUE;
53 }
54 
56 NTAPI
57 NtCreateEventPair(OUT PHANDLE EventPairHandle,
60 {
61  PKEVENT_PAIR EventPair;
62  HANDLE hEventPair;
65  PAGED_CODE();
66  DPRINT("NtCreateEventPair: 0x%p\n", EventPairHandle);
67 
68  /* Check if we were called from user-mode */
69  if (PreviousMode != KernelMode)
70  {
71  /* Enter SEH Block */
72  _SEH2_TRY
73  {
74  /* Check handle pointer */
75  ProbeForWriteHandle(EventPairHandle);
76  }
78  {
79  /* Return the exception code */
81  }
82  _SEH2_END;
83  }
84 
85  /* Create the Object */
86  DPRINT("Creating EventPair\n");
91  NULL,
92  sizeof(KEVENT_PAIR),
93  0,
94  0,
95  (PVOID*)&EventPair);
96 
97  /* Check for Success */
98  if (NT_SUCCESS(Status))
99  {
100  /* Initialize the Event */
101  DPRINT("Initializing EventPair\n");
102  KeInitializeEventPair(EventPair);
103 
104  /* Insert it */
105  Status = ObInsertObject((PVOID)EventPair,
106  NULL,
108  0,
109  NULL,
110  &hEventPair);
111 
112  /* Check for success */
113  if (NT_SUCCESS(Status))
114  {
115  /* Enter SEH */
116  _SEH2_TRY
117  {
118  /* Return the handle */
119  *EventPairHandle = hEventPair;
120  }
122  {
123  /* Get the exception code */
125  }
126  _SEH2_END;
127  }
128  }
129 
130  /* Return Status */
131  return Status;
132 }
133 
134 NTSTATUS
135 NTAPI
136 NtOpenEventPair(OUT PHANDLE EventPairHandle,
139 {
140  HANDLE hEventPair;
143  PAGED_CODE();
144 
145  /* Check if we were called from user-mode */
146  if (PreviousMode != KernelMode)
147  {
148  /* Enter SEH Block */
149  _SEH2_TRY
150  {
151  /* Check handle pointer */
152  ProbeForWriteHandle(EventPairHandle);
153  }
155  {
156  /* Return the exception code */
158  }
159  _SEH2_END;
160  }
161 
162  /* Open the Object */
165  PreviousMode,
166  NULL,
168  NULL,
169  &hEventPair);
170 
171  /* Check for success */
172  if (NT_SUCCESS(Status))
173  {
174  /* Enter SEH */
175  _SEH2_TRY
176  {
177  /* Return the handle */
178  *EventPairHandle = hEventPair;
179  }
181  {
182  /* Get the exception code */
184  }
185  _SEH2_END;
186  }
187 
188  /* Return status */
189  return Status;
190 }
191 
192 NTSTATUS
193 NTAPI
194 NtSetHighEventPair(IN HANDLE EventPairHandle)
195 {
196  PKEVENT_PAIR EventPair;
199  PAGED_CODE();
200  DPRINT("NtSetHighEventPair(EventPairHandle 0x%p)\n", EventPairHandle);
201 
202  /* Open the Object */
203  Status = ObReferenceObjectByHandle(EventPairHandle,
204  SYNCHRONIZE,
206  PreviousMode,
207  (PVOID*)&EventPair,
208  NULL);
209 
210  /* Check for Success */
211  if(NT_SUCCESS(Status))
212  {
213  /* Set the Event */
214  KeSetEvent(&EventPair->HighEvent, EVENT_INCREMENT, FALSE);
215 
216  /* Dereference Object */
217  ObDereferenceObject(EventPair);
218  }
219 
220  /* Return status */
221  return Status;
222 }
223 
224 NTSTATUS
225 NTAPI
227 {
228  PKEVENT_PAIR EventPair;
231  PAGED_CODE();
232  DPRINT("NtSetHighWaitLowEventPair(Handle 0x%p)\n", EventPairHandle);
233 
234  /* Open the Object */
235  Status = ObReferenceObjectByHandle(EventPairHandle,
236  SYNCHRONIZE,
238  PreviousMode,
239  (PVOID*)&EventPair,
240  NULL);
241 
242  /* Check for Success */
243  if(NT_SUCCESS(Status))
244  {
245  /* Set the Event */
246  KeSetEvent(&EventPair->HighEvent, EVENT_INCREMENT, FALSE);
247 
248  /* Wait for the Other one */
249  KeWaitForSingleObject(&EventPair->LowEvent,
250  WrEventPair,
251  PreviousMode,
252  FALSE,
253  NULL);
254 
255  /* Dereference Object */
256  ObDereferenceObject(EventPair);
257  }
258 
259  /* Return status */
260  return Status;
261 }
262 
263 NTSTATUS
264 NTAPI
265 NtSetLowEventPair(IN HANDLE EventPairHandle)
266 {
267  PKEVENT_PAIR EventPair;
270  PAGED_CODE();
271  DPRINT1("NtSetHighEventPair(EventPairHandle 0x%p)\n", EventPairHandle);
272 
273  /* Open the Object */
274  Status = ObReferenceObjectByHandle(EventPairHandle,
275  SYNCHRONIZE,
277  PreviousMode,
278  (PVOID*)&EventPair,
279  NULL);
280 
281  /* Check for Success */
282  if(NT_SUCCESS(Status))
283  {
284  /* Set the Event */
285  KeSetEvent(&EventPair->LowEvent, EVENT_INCREMENT, FALSE);
286 
287  /* Dereference Object */
288  ObDereferenceObject(EventPair);
289  }
290 
291  /* Return status */
292  return Status;
293 }
294 
295 NTSTATUS
296 NTAPI
298 {
299  PKEVENT_PAIR EventPair;
302  PAGED_CODE();
303  DPRINT("NtSetHighWaitLowEventPair(Handle 0x%p)\n", EventPairHandle);
304 
305  /* Open the Object */
306  Status = ObReferenceObjectByHandle(EventPairHandle,
307  SYNCHRONIZE,
309  PreviousMode,
310  (PVOID*)&EventPair,
311  NULL);
312 
313  /* Check for Success */
314  if(NT_SUCCESS(Status))
315  {
316  /* Set the Event */
317  KeSetEvent(&EventPair->LowEvent, EVENT_INCREMENT, FALSE);
318 
319  /* Wait for the Other one */
320  KeWaitForSingleObject(&EventPair->HighEvent,
321  WrEventPair,
322  PreviousMode,
323  FALSE,
324  NULL);
325 
326  /* Dereference Object */
327  ObDereferenceObject(EventPair);
328  }
329 
330  /* Return status */
331  return Status;
332 }
333 
334 
335 NTSTATUS
336 NTAPI
337 NtWaitLowEventPair(IN HANDLE EventPairHandle)
338 {
339  PKEVENT_PAIR EventPair;
342  PAGED_CODE();
343  DPRINT("NtSetHighWaitLowEventPair(Handle 0x%p)\n", EventPairHandle);
344 
345  /* Open the Object */
346  Status = ObReferenceObjectByHandle(EventPairHandle,
347  SYNCHRONIZE,
349  PreviousMode,
350  (PVOID*)&EventPair,
351  NULL);
352 
353  /* Check for Success */
354  if(NT_SUCCESS(Status))
355  {
356  /* Wait for the Event */
357  KeWaitForSingleObject(&EventPair->LowEvent,
358  WrEventPair,
359  PreviousMode,
360  FALSE,
361  NULL);
362 
363  /* Dereference Object */
364  ObDereferenceObject(EventPair);
365  }
366 
367  /* Return status */
368  return Status;
369 }
370 
371 NTSTATUS
372 NTAPI
373 NtWaitHighEventPair(IN HANDLE EventPairHandle)
374 {
375  PKEVENT_PAIR EventPair;
378 
379  PAGED_CODE();
380  DPRINT("NtSetHighWaitLowEventPair(Handle 0x%p)\n", EventPairHandle);
381 
382  /* Open the Object */
383  Status = ObReferenceObjectByHandle(EventPairHandle,
384  SYNCHRONIZE,
386  PreviousMode,
387  (PVOID*)&EventPair,
388  NULL);
389 
390  /* Check for Success */
391  if(NT_SUCCESS(Status))
392  {
393  /* Wait for the Event */
394  KeWaitForSingleObject(&EventPair->HighEvent,
395  WrEventPair,
396  PreviousMode,
397  FALSE,
398  NULL);
399 
400  /* Dereference Object */
401  ObDereferenceObject(EventPair);
402  }
403 
404  /* Return status */
405  return Status;
406 }
407 
408 /* EOF */
#define OBJ_OPENLINK
Definition: winternl.h:230
NTSTATUS NTAPI ObOpenObjectByName(IN POBJECT_ATTRIBUTES ObjectAttributes, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, IN PACCESS_STATE PassedAccessState, IN ACCESS_MASK DesiredAccess, IN OUT PVOID ParseContext, OUT PHANDLE Handle)
Definition: obhandle.c:2528
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define IN
Definition: typedefs.h:39
NTSTATUS NTAPI ObCreateObjectType(IN PUNICODE_STRING TypeName, IN POBJECT_TYPE_INITIALIZER ObjectTypeInitializer, IN PVOID Reserved, OUT POBJECT_TYPE *ObjectType)
Definition: oblife.c:1048
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2654
#define STANDARD_RIGHTS_WRITE
Definition: nt_native.h:66
#define TRUE
Definition: types.h:120
NTSTATUS NTAPI NtSetHighWaitLowEventPair(IN HANDLE EventPairHandle)
Definition: evtpair.c:226
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NtCreateEventPair(OUT PHANDLE EventPairHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes)
Definition: evtpair.c:57
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:3063
#define STANDARD_RIGHTS_EXECUTE
Definition: nt_native.h:67
NTSTATUS NTAPI NtWaitLowEventPair(IN HANDLE EventPairHandle)
Definition: evtpair.c:337
struct _KEVENT_PAIR KEVENT_PAIR
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
NTSTATUS NTAPI NtSetLowWaitHighEventPair(IN HANDLE EventPairHandle)
Definition: evtpair.c:297
_SEH2_TRY
Definition: create.c:4226
#define L(x)
Definition: ntvdm.h:50
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:494
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define FALSE
Definition: types.h:117
NTSTATUS NTAPI NtOpenEventPair(OUT PHANDLE EventPairHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes)
Definition: evtpair.c:136
struct NameRec_ * Name
Definition: cdprocs.h:459
unsigned char BOOLEAN
#define EVENT_PAIR_ALL_ACCESS
Definition: extypes.h:125
NTSTATUS NTAPI ObCreateObject(IN KPROCESSOR_MODE ProbeMode OPTIONAL, IN POBJECT_TYPE Type, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN KPROCESSOR_MODE AccessMode, IN OUT PVOID ParseContext OPTIONAL, IN ULONG ObjectSize, IN ULONG PagedPoolCharge OPTIONAL, IN ULONG NonPagedPoolCharge OPTIONAL, OUT PVOID *Object)
Definition: oblife.c:952
Status
Definition: gdiplustypes.h:24
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define ObDereferenceObject
Definition: obfuncs.h:203
#define ProbeForWriteHandle(Ptr)
Definition: probe.h:43
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
PVOID *typedef PHANDLE
Definition: ntsecpkg.h:454
GENERIC_MAPPING ExEventPairMapping
Definition: evtpair.c:20
#define STANDARD_RIGHTS_READ
Definition: nt_native.h:65
#define SYNCHRONIZE
Definition: nt_native.h:61
NTSTATUS NTAPI NtSetLowEventPair(IN HANDLE EventPairHandle)
Definition: evtpair.c:265
NTSTATUS NTAPI NtWaitHighEventPair(IN HANDLE EventPairHandle)
Definition: evtpair.c:373
_SEH2_END
Definition: create.c:4400
NTSTATUS NTAPI ObInsertObject(IN PVOID Object, IN PACCESS_STATE AccessState OPTIONAL, IN ACCESS_MASK DesiredAccess, IN ULONG ObjectPointerBias, OUT PVOID *NewObject OPTIONAL, OUT PHANDLE Handle)
Definition: obhandle.c:2931
#define EVENT_INCREMENT
Definition: iotypes.h:597
LONG NTAPI ExSystemExceptionFilter(VOID)
Definition: harderr.c:351
GENERIC_MAPPING GenericMapping
Definition: obtypes.h:358
#define NULL
Definition: types.h:112
#define DPRINT1
Definition: precomp.h:8
#define OUT
Definition: typedefs.h:40
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
POBJECT_TYPE ExEventPairObjectType
Definition: evtpair.c:18
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
BOOLEAN NTAPI ExpInitializeEventPairImplementation(VOID)
Definition: evtpair.c:33
NTSTATUS NTAPI NtSetHighEventPair(IN HANDLE EventPairHandle)
Definition: evtpair.c:194
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:168
#define DPRINT
Definition: sndvol32.h:71
static CODE_SEG("PAGE")
Definition: isapnp.c:1482
VOID NTAPI KeInitializeEventPair(PKEVENT_PAIR EventPair)
ULONG DefaultNonPagedPoolCharge
Definition: obtypes.h:365
ULONG ACCESS_MASK
Definition: nt_native.h:40
#define PAGED_CODE()