2006-11-08 11:47:44 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS Kernel
|
|
|
|
* FILE: ntoskrnl/ex/keyedevt.c
|
|
|
|
* PURPOSE: Support for keyed events
|
2012-04-20 19:32:36 +00:00
|
|
|
* PROGRAMMERS: Timo Kreuzer (timo.kreuzer@reactos.org)
|
2006-11-08 11:47:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
|
|
|
#include <ntoskrnl.h>
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-04-20 19:31:07 +00:00
|
|
|
/* INTERNAL TYPES *************************************************************/
|
|
|
|
|
|
|
|
#define NUM_KEY_HASH_BUCKETS 23
|
|
|
|
typedef struct _EX_KEYED_EVENT
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
EX_PUSH_LOCK Lock;
|
|
|
|
LIST_ENTRY WaitListHead;
|
|
|
|
LIST_ENTRY ReleaseListHead;
|
|
|
|
} HashTable[NUM_KEY_HASH_BUCKETS];
|
|
|
|
} EX_KEYED_EVENT, *PEX_KEYED_EVENT;
|
|
|
|
|
2006-11-08 11:47:44 +00:00
|
|
|
/* GLOBALS *******************************************************************/
|
|
|
|
|
2012-04-20 19:31:07 +00:00
|
|
|
PEX_KEYED_EVENT ExpCritSecOutOfMemoryEvent;
|
|
|
|
POBJECT_TYPE ExKeyedEventObjectType;
|
|
|
|
|
|
|
|
static
|
|
|
|
GENERIC_MAPPING ExpKeyedEventMapping =
|
|
|
|
{
|
2015-02-01 13:59:09 +00:00
|
|
|
STANDARD_RIGHTS_READ | KEYEDEVENT_WAIT,
|
|
|
|
STANDARD_RIGHTS_WRITE | KEYEDEVENT_WAKE,
|
2012-09-02 22:06:42 +00:00
|
|
|
STANDARD_RIGHTS_EXECUTE,
|
2015-02-01 13:59:09 +00:00
|
|
|
KEYEDEVENT_ALL_ACCESS
|
2012-04-20 19:31:07 +00:00
|
|
|
};
|
|
|
|
|
2006-11-08 11:47:44 +00:00
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
2012-12-19 23:49:13 +00:00
|
|
|
_IRQL_requires_max_(APC_LEVEL)
|
2012-09-03 01:12:58 +00:00
|
|
|
BOOLEAN
|
|
|
|
INIT_FUNCTION
|
2012-04-20 19:31:07 +00:00
|
|
|
NTAPI
|
|
|
|
ExpInitializeKeyedEventImplementation(VOID)
|
|
|
|
{
|
|
|
|
OBJECT_TYPE_INITIALIZER ObjectTypeInitializer = {0};
|
|
|
|
UNICODE_STRING TypeName = RTL_CONSTANT_STRING(L"KeyedEvent");
|
2012-09-03 00:33:05 +00:00
|
|
|
UNICODE_STRING Name = RTL_CONSTANT_STRING(L"\\KernelObjects\\CritSecOutOfMemoryEvent");
|
2012-04-20 19:31:07 +00:00
|
|
|
NTSTATUS Status;
|
2012-09-03 00:33:05 +00:00
|
|
|
HANDLE EventHandle;
|
|
|
|
OBJECT_ATTRIBUTES ObjectAttributes;
|
2012-04-20 19:31:07 +00:00
|
|
|
|
|
|
|
/* Set up the object type initializer */
|
|
|
|
ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
|
|
|
|
ObjectTypeInitializer.GenericMapping = ExpKeyedEventMapping;
|
2012-09-02 22:06:42 +00:00
|
|
|
ObjectTypeInitializer.PoolType = PagedPool;
|
2015-02-01 13:59:09 +00:00
|
|
|
ObjectTypeInitializer.ValidAccessMask = KEYEDEVENT_ALL_ACCESS;
|
2012-09-03 00:33:05 +00:00
|
|
|
ObjectTypeInitializer.UseDefaultObject = TRUE;
|
2012-04-20 19:31:07 +00:00
|
|
|
|
|
|
|
/* Create the keyed event object type */
|
|
|
|
Status = ObCreateObjectType(&TypeName,
|
|
|
|
&ObjectTypeInitializer,
|
|
|
|
NULL,
|
|
|
|
&ExKeyedEventObjectType);
|
2012-09-03 01:12:58 +00:00
|
|
|
if (!NT_SUCCESS(Status)) return FALSE;
|
2012-09-03 00:33:05 +00:00
|
|
|
|
|
|
|
/* Create the out of memory event for critical sections */
|
|
|
|
InitializeObjectAttributes(&ObjectAttributes, &Name, OBJ_PERMANENT, NULL, NULL);
|
|
|
|
Status = ZwCreateKeyedEvent(&EventHandle,
|
2015-02-01 13:59:09 +00:00
|
|
|
KEYEDEVENT_ALL_ACCESS,
|
2012-09-03 00:33:05 +00:00
|
|
|
&ObjectAttributes,
|
|
|
|
0);
|
|
|
|
if (NT_SUCCESS(Status))
|
2012-04-20 19:31:07 +00:00
|
|
|
{
|
2012-09-03 00:33:05 +00:00
|
|
|
/* Take a reference so we can get rid of the handle */
|
|
|
|
Status = ObReferenceObjectByHandle(EventHandle,
|
2015-02-01 13:59:09 +00:00
|
|
|
KEYEDEVENT_ALL_ACCESS,
|
2012-09-03 00:33:05 +00:00
|
|
|
ExKeyedEventObjectType,
|
|
|
|
KernelMode,
|
|
|
|
(PVOID*)&ExpCritSecOutOfMemoryEvent,
|
|
|
|
NULL);
|
|
|
|
ZwClose(EventHandle);
|
2012-09-03 01:12:58 +00:00
|
|
|
return TRUE;
|
2012-04-20 19:31:07 +00:00
|
|
|
}
|
2012-09-03 01:12:58 +00:00
|
|
|
|
|
|
|
return FALSE;
|
2012-04-20 19:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
ExpInitializeKeyedEvent(
|
|
|
|
_Out_ PEX_KEYED_EVENT KeyedEvent)
|
|
|
|
{
|
|
|
|
ULONG i;
|
|
|
|
|
|
|
|
/* Loop all hash buckets */
|
|
|
|
for (i = 0; i < NUM_KEY_HASH_BUCKETS; i++)
|
|
|
|
{
|
|
|
|
/* Initialize the mutex and the wait lists */
|
|
|
|
ExInitializePushLock(&KeyedEvent->HashTable[i].Lock);
|
|
|
|
InitializeListHead(&KeyedEvent->HashTable[i].WaitListHead);
|
|
|
|
InitializeListHead(&KeyedEvent->HashTable[i].ReleaseListHead);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-19 23:49:13 +00:00
|
|
|
_IRQL_requires_max_(APC_LEVEL)
|
2006-11-08 11:47:44 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2012-04-20 19:31:07 +00:00
|
|
|
ExpReleaseOrWaitForKeyedEvent(
|
|
|
|
_Inout_ PEX_KEYED_EVENT KeyedEvent,
|
|
|
|
_In_ PVOID KeyedWaitValue,
|
|
|
|
_In_ BOOLEAN Alertable,
|
|
|
|
_In_ PLARGE_INTEGER Timeout,
|
|
|
|
_In_ BOOLEAN Release)
|
2006-11-08 11:47:44 +00:00
|
|
|
{
|
2012-04-20 19:31:07 +00:00
|
|
|
PETHREAD Thread, CurrentThread;
|
2015-09-25 18:40:56 +00:00
|
|
|
PEPROCESS CurrentProcess;
|
2012-04-20 19:31:07 +00:00
|
|
|
PLIST_ENTRY ListEntry, WaitListHead1, WaitListHead2;
|
|
|
|
NTSTATUS Status;
|
|
|
|
ULONG_PTR HashIndex;
|
2015-02-15 22:20:34 +00:00
|
|
|
PVOID PreviousKeyedWaitValue;
|
2012-04-20 19:31:07 +00:00
|
|
|
|
|
|
|
/* Get the current process */
|
2015-09-25 18:40:56 +00:00
|
|
|
CurrentProcess = PsGetCurrentProcess();
|
2012-04-20 19:31:07 +00:00
|
|
|
|
|
|
|
/* Calculate the hash index */
|
|
|
|
HashIndex = (ULONG_PTR)KeyedWaitValue >> 5;
|
|
|
|
HashIndex ^= (ULONG_PTR)CurrentProcess >> 6;
|
|
|
|
HashIndex %= NUM_KEY_HASH_BUCKETS;
|
|
|
|
|
|
|
|
/* Lock the lists */
|
2013-01-21 20:58:23 +00:00
|
|
|
KeEnterCriticalRegion();
|
2012-04-20 19:31:07 +00:00
|
|
|
ExAcquirePushLockExclusive(&KeyedEvent->HashTable[HashIndex].Lock);
|
|
|
|
|
|
|
|
/* Get the lists for search and wait, depending on whether
|
|
|
|
we want to wait for the event or signal it */
|
|
|
|
if (Release)
|
|
|
|
{
|
|
|
|
WaitListHead1 = &KeyedEvent->HashTable[HashIndex].WaitListHead;
|
|
|
|
WaitListHead2 = &KeyedEvent->HashTable[HashIndex].ReleaseListHead;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WaitListHead1 = &KeyedEvent->HashTable[HashIndex].ReleaseListHead;
|
|
|
|
WaitListHead2 = &KeyedEvent->HashTable[HashIndex].WaitListHead;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* loop the first wait list */
|
|
|
|
ListEntry = WaitListHead1->Flink;
|
|
|
|
while (ListEntry != WaitListHead1)
|
|
|
|
{
|
2013-01-21 20:58:23 +00:00
|
|
|
/* Get the waiting thread. Note that this thread cannot be terminated
|
|
|
|
as long as we hold the list lock, since it either needs to wait to
|
|
|
|
be signaled by this thread or, when the wait is aborted due to thread
|
|
|
|
termination, then it first needs to acquire the list lock. */
|
2012-04-20 19:31:07 +00:00
|
|
|
Thread = CONTAINING_RECORD(ListEntry, ETHREAD, KeyedWaitChain);
|
2015-02-15 22:20:34 +00:00
|
|
|
ListEntry = ListEntry->Flink;
|
2012-04-20 19:31:07 +00:00
|
|
|
|
|
|
|
/* Check if this thread is a correct waiter */
|
2015-09-25 18:40:56 +00:00
|
|
|
if ((Thread->Tcb.Process == &CurrentProcess->Pcb) &&
|
2012-04-20 19:31:07 +00:00
|
|
|
(Thread->KeyedWaitValue == KeyedWaitValue))
|
|
|
|
{
|
|
|
|
/* Remove the thread from the list */
|
|
|
|
RemoveEntryList(&Thread->KeyedWaitChain);
|
|
|
|
|
2013-01-21 20:58:23 +00:00
|
|
|
/* Initialize the list entry to show that it was removed */
|
|
|
|
InitializeListHead(&Thread->KeyedWaitChain);
|
|
|
|
|
2012-04-20 19:31:07 +00:00
|
|
|
/* Wake the thread */
|
2015-02-15 22:20:34 +00:00
|
|
|
KeReleaseSemaphore(&Thread->KeyedWaitSemaphore,
|
|
|
|
IO_NO_INCREMENT,
|
|
|
|
1,
|
|
|
|
FALSE);
|
2013-01-21 20:58:23 +00:00
|
|
|
Thread = NULL;
|
2012-04-20 19:31:07 +00:00
|
|
|
|
2013-01-21 20:58:23 +00:00
|
|
|
/* Unlock the list. After this it is not safe to access Thread */
|
2012-04-20 19:31:07 +00:00
|
|
|
ExReleasePushLockExclusive(&KeyedEvent->HashTable[HashIndex].Lock);
|
2013-01-21 20:58:23 +00:00
|
|
|
KeLeaveCriticalRegion();
|
2012-04-20 19:31:07 +00:00
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the current thread */
|
|
|
|
CurrentThread = PsGetCurrentThread();
|
|
|
|
|
2015-02-15 22:20:34 +00:00
|
|
|
/* Set the wait key and remember the old value */
|
|
|
|
PreviousKeyedWaitValue = CurrentThread->KeyedWaitValue;
|
2012-04-20 19:31:07 +00:00
|
|
|
CurrentThread->KeyedWaitValue = KeyedWaitValue;
|
|
|
|
|
|
|
|
/* Initialize the wait semaphore */
|
|
|
|
KeInitializeSemaphore(&CurrentThread->KeyedWaitSemaphore, 0, 1);
|
|
|
|
|
|
|
|
/* Insert the current thread into the secondary wait list */
|
|
|
|
InsertTailList(WaitListHead2, &CurrentThread->KeyedWaitChain);
|
|
|
|
|
2013-01-21 20:58:23 +00:00
|
|
|
/* Unlock the list */
|
2012-04-20 19:31:07 +00:00
|
|
|
ExReleasePushLockExclusive(&KeyedEvent->HashTable[HashIndex].Lock);
|
2013-01-21 20:58:23 +00:00
|
|
|
KeLeaveCriticalRegion();
|
2012-04-20 19:31:07 +00:00
|
|
|
|
|
|
|
/* Wait for the keyed wait semaphore */
|
|
|
|
Status = KeWaitForSingleObject(&CurrentThread->KeyedWaitSemaphore,
|
|
|
|
WrKeyedEvent,
|
|
|
|
KernelMode,
|
|
|
|
Alertable,
|
|
|
|
Timeout);
|
|
|
|
|
2013-01-21 20:58:23 +00:00
|
|
|
/* Check if the wait was aborted or timed out */
|
|
|
|
if (Status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Lock the lists to make sure no one else messes with the entry */
|
|
|
|
KeEnterCriticalRegion();
|
|
|
|
ExAcquirePushLockExclusive(&KeyedEvent->HashTable[HashIndex].Lock);
|
|
|
|
|
|
|
|
/* Check if the wait list entry is still in the list */
|
2015-02-15 22:20:34 +00:00
|
|
|
if (!IsListEmpty(&CurrentThread->KeyedWaitChain))
|
2013-01-21 20:58:23 +00:00
|
|
|
{
|
|
|
|
/* Remove the thread from the list */
|
|
|
|
RemoveEntryList(&CurrentThread->KeyedWaitChain);
|
2015-02-15 22:20:34 +00:00
|
|
|
InitializeListHead(&CurrentThread->KeyedWaitChain);
|
2013-01-21 20:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock the list */
|
|
|
|
ExReleasePushLockExclusive(&KeyedEvent->HashTable[HashIndex].Lock);
|
|
|
|
KeLeaveCriticalRegion();
|
|
|
|
}
|
|
|
|
|
2015-02-15 22:20:34 +00:00
|
|
|
/* Restore the previous KeyedWaitValue, since this is a union member */
|
|
|
|
CurrentThread->KeyedWaitValue = PreviousKeyedWaitValue;
|
|
|
|
|
2013-01-21 20:58:23 +00:00
|
|
|
return Status;
|
2012-04-20 19:31:07 +00:00
|
|
|
}
|
|
|
|
|
2012-12-19 23:49:13 +00:00
|
|
|
_IRQL_requires_max_(APC_LEVEL)
|
2012-04-20 19:31:07 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
ExpWaitForKeyedEvent(
|
|
|
|
_Inout_ PEX_KEYED_EVENT KeyedEvent,
|
|
|
|
_In_ PVOID KeyedWaitValue,
|
|
|
|
_In_ BOOLEAN Alertable,
|
|
|
|
_In_ PLARGE_INTEGER Timeout)
|
|
|
|
{
|
|
|
|
/* Call the generic internal function */
|
|
|
|
return ExpReleaseOrWaitForKeyedEvent(KeyedEvent,
|
|
|
|
KeyedWaitValue,
|
|
|
|
Alertable,
|
|
|
|
Timeout,
|
|
|
|
FALSE);
|
2006-11-08 11:47:44 +00:00
|
|
|
}
|
|
|
|
|
2012-12-19 23:49:13 +00:00
|
|
|
_IRQL_requires_max_(APC_LEVEL)
|
2006-11-08 11:47:44 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2012-04-20 19:31:07 +00:00
|
|
|
ExpReleaseKeyedEvent(
|
|
|
|
_Inout_ PEX_KEYED_EVENT KeyedEvent,
|
|
|
|
_In_ PVOID KeyedWaitValue,
|
2015-02-01 13:59:09 +00:00
|
|
|
_In_ BOOLEAN Alertable,
|
|
|
|
_In_ PLARGE_INTEGER Timeout)
|
2006-11-08 11:47:44 +00:00
|
|
|
{
|
2012-04-20 19:31:07 +00:00
|
|
|
/* Call the generic internal function */
|
|
|
|
return ExpReleaseOrWaitForKeyedEvent(KeyedEvent,
|
|
|
|
KeyedWaitValue,
|
|
|
|
Alertable,
|
|
|
|
Timeout,
|
|
|
|
TRUE);
|
2006-11-08 11:47:44 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 13:55:12 +00:00
|
|
|
_IRQL_requires_max_(PASSIVE_LEVEL)
|
2006-11-08 11:47:44 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2012-04-20 19:31:07 +00:00
|
|
|
NtCreateKeyedEvent(
|
|
|
|
_Out_ PHANDLE OutHandle,
|
|
|
|
_In_ ACCESS_MASK AccessMask,
|
|
|
|
_In_ POBJECT_ATTRIBUTES ObjectAttributes,
|
|
|
|
_In_ ULONG Flags)
|
2006-11-08 11:47:44 +00:00
|
|
|
{
|
2012-04-20 19:31:07 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
|
|
|
|
PEX_KEYED_EVENT KeyedEvent;
|
|
|
|
HANDLE KeyedEventHandle;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
/* Check flags */
|
|
|
|
if (Flags != 0)
|
|
|
|
{
|
|
|
|
/* We don't support any flags yet */
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the object */
|
|
|
|
Status = ObCreateObject(PreviousMode,
|
|
|
|
ExKeyedEventObjectType,
|
|
|
|
ObjectAttributes,
|
|
|
|
PreviousMode,
|
|
|
|
NULL,
|
|
|
|
sizeof(EX_KEYED_EVENT),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
(PVOID*)&KeyedEvent);
|
|
|
|
|
|
|
|
/* Check for success */
|
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
|
|
|
|
2013-01-21 20:58:23 +00:00
|
|
|
/* Initialize the keyed event */
|
2012-04-20 19:31:07 +00:00
|
|
|
ExpInitializeKeyedEvent(KeyedEvent);
|
|
|
|
|
|
|
|
/* Insert it */
|
|
|
|
Status = ObInsertObject(KeyedEvent,
|
|
|
|
NULL,
|
|
|
|
AccessMask,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&KeyedEventHandle);
|
|
|
|
|
|
|
|
/* Check for success (ObInsertObject dereferences!) */
|
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
|
|
|
|
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
|
|
|
/* Enter SEH for return */
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
/* Return the handle to the caller */
|
|
|
|
ProbeForWrite(OutHandle, sizeof(HANDLE), sizeof(HANDLE));
|
|
|
|
*OutHandle = KeyedEventHandle;
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(ExSystemExceptionFilter())
|
|
|
|
{
|
|
|
|
/* Get the exception code */
|
|
|
|
Status = _SEH2_GetExceptionCode();
|
|
|
|
|
|
|
|
/* Cleanup */
|
|
|
|
ObCloseHandle(KeyedEventHandle, PreviousMode);
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*OutHandle = KeyedEventHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return Status */
|
|
|
|
return Status;
|
2006-11-08 11:47:44 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 13:55:12 +00:00
|
|
|
_IRQL_requires_max_(PASSIVE_LEVEL)
|
2006-11-08 11:47:44 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2012-04-20 19:31:07 +00:00
|
|
|
NtOpenKeyedEvent(
|
|
|
|
_Out_ PHANDLE OutHandle,
|
|
|
|
_In_ ACCESS_MASK AccessMask,
|
|
|
|
_In_ POBJECT_ATTRIBUTES ObjectAttributes)
|
2006-11-08 11:47:44 +00:00
|
|
|
{
|
2012-04-20 19:31:07 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
|
|
|
|
HANDLE KeyedEventHandle;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
/* Open the object */
|
|
|
|
Status = ObOpenObjectByName(ObjectAttributes,
|
|
|
|
ExKeyedEventObjectType,
|
|
|
|
PreviousMode,
|
|
|
|
NULL,
|
|
|
|
AccessMask,
|
|
|
|
NULL,
|
|
|
|
&KeyedEventHandle);
|
|
|
|
|
|
|
|
/* Check for success */
|
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
|
|
|
|
|
|
|
/* Enter SEH for return */
|
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
/* Return the handle to the caller */
|
|
|
|
ProbeForWrite(OutHandle, sizeof(HANDLE), sizeof(HANDLE));
|
|
|
|
*OutHandle = KeyedEventHandle;
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(ExSystemExceptionFilter())
|
|
|
|
{
|
|
|
|
/* Get the exception code */
|
|
|
|
Status = _SEH2_GetExceptionCode();
|
2013-01-21 20:58:23 +00:00
|
|
|
|
|
|
|
/* Cleanup */
|
|
|
|
ObCloseHandle(KeyedEventHandle, PreviousMode);
|
2012-04-20 19:31:07 +00:00
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*OutHandle = KeyedEventHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return status */
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2013-01-14 13:55:12 +00:00
|
|
|
_IRQL_requires_max_(PASSIVE_LEVEL)
|
2012-04-20 19:31:07 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtWaitForKeyedEvent(
|
2013-01-14 13:55:12 +00:00
|
|
|
_In_opt_ HANDLE Handle,
|
2012-04-20 19:31:07 +00:00
|
|
|
_In_ PVOID Key,
|
|
|
|
_In_ BOOLEAN Alertable,
|
2013-01-14 13:55:12 +00:00
|
|
|
_In_opt_ PLARGE_INTEGER Timeout)
|
2012-04-20 19:31:07 +00:00
|
|
|
{
|
|
|
|
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
|
|
|
|
PEX_KEYED_EVENT KeyedEvent;
|
|
|
|
NTSTATUS Status;
|
2013-01-14 13:55:12 +00:00
|
|
|
LARGE_INTEGER TimeoutCopy;
|
|
|
|
|
2015-02-15 22:20:34 +00:00
|
|
|
/* Key must always be two-byte aligned */
|
|
|
|
if ((ULONG_PTR)Key & 1)
|
|
|
|
{
|
|
|
|
return STATUS_INVALID_PARAMETER_1;
|
|
|
|
}
|
|
|
|
|
2013-01-14 13:55:12 +00:00
|
|
|
/* Check if the caller passed a timeout value and this is from user mode */
|
|
|
|
if ((Timeout != NULL) && (PreviousMode != KernelMode))
|
|
|
|
{
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
ProbeForRead(Timeout, sizeof(*Timeout), 1);
|
|
|
|
TimeoutCopy = *Timeout;
|
|
|
|
Timeout = &TimeoutCopy;
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
}
|
2012-04-20 19:31:07 +00:00
|
|
|
|
|
|
|
/* Check if the caller provided a handle */
|
|
|
|
if (Handle != NULL)
|
|
|
|
{
|
|
|
|
/* Get the keyed event object */
|
|
|
|
Status = ObReferenceObjectByHandle(Handle,
|
2015-02-01 13:59:09 +00:00
|
|
|
KEYEDEVENT_WAIT,
|
2012-04-20 19:31:07 +00:00
|
|
|
ExKeyedEventObjectType,
|
|
|
|
PreviousMode,
|
|
|
|
(PVOID*)&KeyedEvent,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Check for success */
|
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Use the default keyed event for low memory critical sections */
|
|
|
|
KeyedEvent = ExpCritSecOutOfMemoryEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the wait */
|
|
|
|
Status = ExpWaitForKeyedEvent(KeyedEvent, Key, Alertable, Timeout);
|
|
|
|
|
2017-10-22 08:57:40 +00:00
|
|
|
if (Handle != NULL)
|
|
|
|
{
|
|
|
|
/* Dereference the keyed event */
|
|
|
|
ObDereferenceObject(KeyedEvent);
|
|
|
|
}
|
2012-04-20 19:31:07 +00:00
|
|
|
|
|
|
|
/* Return the status */
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2013-01-14 13:55:12 +00:00
|
|
|
_IRQL_requires_max_(PASSIVE_LEVEL)
|
2012-04-20 19:31:07 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtReleaseKeyedEvent(
|
2013-01-14 13:55:12 +00:00
|
|
|
_In_opt_ HANDLE Handle,
|
2012-04-20 19:31:07 +00:00
|
|
|
_In_ PVOID Key,
|
|
|
|
_In_ BOOLEAN Alertable,
|
2013-01-14 13:55:12 +00:00
|
|
|
_In_opt_ PLARGE_INTEGER Timeout)
|
2012-04-20 19:31:07 +00:00
|
|
|
{
|
|
|
|
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
|
|
|
|
PEX_KEYED_EVENT KeyedEvent;
|
|
|
|
NTSTATUS Status;
|
2013-01-14 13:55:12 +00:00
|
|
|
LARGE_INTEGER TimeoutCopy;
|
|
|
|
|
2015-02-15 22:20:34 +00:00
|
|
|
/* Key must always be two-byte aligned */
|
|
|
|
if ((ULONG_PTR)Key & 1)
|
|
|
|
{
|
|
|
|
return STATUS_INVALID_PARAMETER_1;
|
|
|
|
}
|
|
|
|
|
2013-01-14 13:55:12 +00:00
|
|
|
/* Check if the caller passed a timeout value and this is from user mode */
|
|
|
|
if ((Timeout != NULL) && (PreviousMode != KernelMode))
|
|
|
|
{
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
ProbeForRead(Timeout, sizeof(*Timeout), 1);
|
|
|
|
TimeoutCopy = *Timeout;
|
|
|
|
Timeout = &TimeoutCopy;
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
}
|
2012-04-20 19:31:07 +00:00
|
|
|
|
|
|
|
/* Check if the caller provided a handle */
|
|
|
|
if (Handle != NULL)
|
|
|
|
{
|
|
|
|
/* Get the keyed event object */
|
|
|
|
Status = ObReferenceObjectByHandle(Handle,
|
2015-02-01 13:59:09 +00:00
|
|
|
KEYEDEVENT_WAKE,
|
2012-04-20 19:31:07 +00:00
|
|
|
ExKeyedEventObjectType,
|
|
|
|
PreviousMode,
|
|
|
|
(PVOID*)&KeyedEvent,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Check for success */
|
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Use the default keyed event for low memory critical sections */
|
|
|
|
KeyedEvent = ExpCritSecOutOfMemoryEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the wait */
|
|
|
|
Status = ExpReleaseKeyedEvent(KeyedEvent, Key, Alertable, Timeout);
|
|
|
|
|
2017-10-22 08:57:40 +00:00
|
|
|
if (Handle != NULL)
|
|
|
|
{
|
|
|
|
/* Dereference the keyed event */
|
|
|
|
ObDereferenceObject(KeyedEvent);
|
|
|
|
}
|
2012-04-20 19:31:07 +00:00
|
|
|
|
|
|
|
/* Return the status */
|
|
|
|
return Status;
|
2006-11-08 11:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|