2005-03-14 02:08:17 +00:00
|
|
|
/*
|
2005-01-07 06:54:27 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
2005-12-29 18:47:14 +00:00
|
|
|
* PROJECT: ReactOS Kernel
|
2005-01-07 06:54:27 +00:00
|
|
|
* FILE: ntoskrnl/ex/sem.c
|
2005-08-05 06:56:17 +00:00
|
|
|
* PURPOSE: Semaphore Implementation
|
|
|
|
* PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
|
2005-12-29 18:47:14 +00:00
|
|
|
* Thomas Weidenmueller
|
2005-01-07 06:54:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
|
|
|
#include <ntoskrnl.h>
|
|
|
|
#define NDEBUG
|
2008-08-30 16:31:06 +00:00
|
|
|
#include <debug.h>
|
2005-01-07 06:54:27 +00:00
|
|
|
|
2021-05-11 15:13:14 +00:00
|
|
|
#if defined (ALLOC_PRAGMA)
|
|
|
|
#pragma alloc_text(INIT, ExpInitializeSemaphoreImplementation)
|
|
|
|
#endif
|
|
|
|
|
2005-01-07 06:54:27 +00:00
|
|
|
/* GLOBALS ******************************************************************/
|
|
|
|
|
2014-05-11 23:11:16 +00:00
|
|
|
POBJECT_TYPE ExSemaphoreObjectType;
|
2005-01-07 06:54:27 +00:00
|
|
|
|
2005-12-29 18:47:14 +00:00
|
|
|
GENERIC_MAPPING ExSemaphoreMapping =
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2012-09-02 22:06:42 +00:00
|
|
|
STANDARD_RIGHTS_READ | SEMAPHORE_QUERY_STATE,
|
|
|
|
STANDARD_RIGHTS_WRITE | SEMAPHORE_MODIFY_STATE,
|
|
|
|
STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
|
2005-08-05 06:56:17 +00:00
|
|
|
SEMAPHORE_ALL_ACCESS
|
|
|
|
};
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-08-05 06:56:17 +00:00
|
|
|
static const INFORMATION_CLASS_INFO ExSemaphoreInfoClass[] =
|
|
|
|
{
|
2005-03-14 02:08:17 +00:00
|
|
|
/* SemaphoreBasicInformation */
|
2021-05-02 19:00:51 +00:00
|
|
|
IQS_SAME(SEMAPHORE_BASIC_INFORMATION, ULONG, ICIF_QUERY),
|
2005-01-23 22:09:27 +00:00
|
|
|
};
|
|
|
|
|
2005-12-29 18:47:14 +00:00
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
2020-10-06 19:44:01 +00:00
|
|
|
CODE_SEG("INIT")
|
2020-05-23 13:56:10 +00:00
|
|
|
BOOLEAN
|
2005-12-29 18:47:14 +00:00
|
|
|
NTAPI
|
2005-01-07 06:54:27 +00:00
|
|
|
ExpInitializeSemaphoreImplementation(VOID)
|
|
|
|
{
|
2005-05-15 17:59:33 +00:00
|
|
|
OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
|
|
|
|
UNICODE_STRING Name;
|
2012-09-03 01:12:58 +00:00
|
|
|
NTSTATUS Status;
|
2005-05-21 16:33:23 +00:00
|
|
|
DPRINT("Creating Semaphore Object Type\n");
|
2005-12-29 18:47:14 +00:00
|
|
|
|
2005-05-15 17:59:33 +00:00
|
|
|
/* Create the Event Pair Object Type */
|
|
|
|
RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
|
|
|
|
RtlInitUnicodeString(&Name, L"Semaphore");
|
|
|
|
ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
|
|
|
|
ObjectTypeInitializer.DefaultNonPagedPoolCharge = sizeof(KSEMAPHORE);
|
|
|
|
ObjectTypeInitializer.GenericMapping = ExSemaphoreMapping;
|
|
|
|
ObjectTypeInitializer.PoolType = NonPagedPool;
|
2005-08-05 06:56:17 +00:00
|
|
|
ObjectTypeInitializer.InvalidAttributes = OBJ_OPENLINK;
|
2005-05-15 17:59:33 +00:00
|
|
|
ObjectTypeInitializer.ValidAccessMask = SEMAPHORE_ALL_ACCESS;
|
2012-09-03 01:12:58 +00:00
|
|
|
Status = ObCreateObjectType(&Name, &ObjectTypeInitializer, NULL, &ExSemaphoreObjectType);
|
|
|
|
if (!NT_SUCCESS(Status)) return FALSE;
|
|
|
|
return TRUE;
|
2005-01-07 06:54:27 +00:00
|
|
|
}
|
|
|
|
|
2005-01-23 22:09:27 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-05-09 01:38:29 +00:00
|
|
|
NTSTATUS
|
2005-12-29 18:47:14 +00:00
|
|
|
NTAPI
|
2005-01-07 06:54:27 +00:00
|
|
|
NtCreateSemaphore(OUT PHANDLE SemaphoreHandle,
|
2005-03-14 02:08:17 +00:00
|
|
|
IN ACCESS_MASK DesiredAccess,
|
2005-12-29 18:47:14 +00:00
|
|
|
IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
|
2005-03-14 02:08:17 +00:00
|
|
|
IN LONG InitialCount,
|
|
|
|
IN LONG MaximumCount)
|
2005-01-07 06:54:27 +00:00
|
|
|
{
|
2005-03-14 02:08:17 +00:00
|
|
|
PKSEMAPHORE Semaphore;
|
|
|
|
HANDLE hSemaphore;
|
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
2009-08-26 17:31:02 +00:00
|
|
|
NTSTATUS Status;
|
2005-03-14 02:08:17 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Check if we were called from user-mode */
|
2009-08-26 17:31:02 +00:00
|
|
|
if (PreviousMode != KernelMode)
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Enter SEH Block */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Check handle pointer */
|
2005-08-21 17:38:07 +00:00
|
|
|
ProbeForWriteHandle(SemaphoreHandle);
|
2005-08-05 06:56:17 +00:00
|
|
|
}
|
2009-08-26 17:31:02 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2009-08-26 17:31:02 +00:00
|
|
|
/* Return the exception code */
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2005-08-05 06:56:17 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2005-03-14 02:08:17 +00:00
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Make sure the counts make sense */
|
2005-12-29 18:47:14 +00:00
|
|
|
if ((MaximumCount <= 0) ||
|
|
|
|
(InitialCount < 0) ||
|
|
|
|
(InitialCount > MaximumCount))
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-03-14 02:08:17 +00:00
|
|
|
DPRINT("Invalid Count Data!\n");
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2005-02-28 17:40:15 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Create the Semaphore Object */
|
|
|
|
Status = ObCreateObject(PreviousMode,
|
|
|
|
ExSemaphoreObjectType,
|
|
|
|
ObjectAttributes,
|
|
|
|
PreviousMode,
|
|
|
|
NULL,
|
|
|
|
sizeof(KSEMAPHORE),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
(PVOID*)&Semaphore);
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Check for Success */
|
2005-08-05 06:56:17 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Initialize it */
|
|
|
|
KeInitializeSemaphore(Semaphore,
|
|
|
|
InitialCount,
|
|
|
|
MaximumCount);
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Insert it into the Object Tree */
|
|
|
|
Status = ObInsertObject((PVOID)Semaphore,
|
|
|
|
NULL,
|
|
|
|
DesiredAccess,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&hSemaphore);
|
|
|
|
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Check for success */
|
2009-08-26 17:31:02 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Enter SEH Block for return */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Return the handle */
|
2005-03-14 02:08:17 +00:00
|
|
|
*SemaphoreHandle = hSemaphore;
|
2005-08-05 06:56:17 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(ExSystemExceptionFilter())
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2009-08-26 17:31:02 +00:00
|
|
|
/* Get the exception code */
|
2008-11-24 13:40:26 +00:00
|
|
|
Status = _SEH2_GetExceptionCode();
|
2005-08-05 06:56:17 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2005-03-14 02:08:17 +00:00
|
|
|
}
|
|
|
|
}
|
2005-02-28 17:40:15 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Return Status */
|
|
|
|
return Status;
|
2005-01-07 06:54:27 +00:00
|
|
|
}
|
|
|
|
|
2005-01-23 22:09:27 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-05-09 01:38:29 +00:00
|
|
|
NTSTATUS
|
2005-12-29 18:47:14 +00:00
|
|
|
NTAPI
|
2005-01-23 22:09:27 +00:00
|
|
|
NtOpenSemaphore(OUT PHANDLE SemaphoreHandle,
|
2005-03-14 02:08:17 +00:00
|
|
|
IN ACCESS_MASK DesiredAccess,
|
|
|
|
IN POBJECT_ATTRIBUTES ObjectAttributes)
|
2005-01-07 06:54:27 +00:00
|
|
|
{
|
2005-03-14 02:08:17 +00:00
|
|
|
HANDLE hSemaphore;
|
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
2009-08-26 17:31:02 +00:00
|
|
|
NTSTATUS Status;
|
2005-03-14 02:08:17 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Check if we were called from user-mode */
|
2009-08-26 17:31:02 +00:00
|
|
|
if (PreviousMode != KernelMode)
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Enter SEH Block */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Check handle pointer */
|
2005-08-21 17:38:07 +00:00
|
|
|
ProbeForWriteHandle(SemaphoreHandle);
|
2005-08-05 06:56:17 +00:00
|
|
|
}
|
2009-08-26 17:31:02 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2009-08-26 17:31:02 +00:00
|
|
|
/* Return the exception code */
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2005-08-05 06:56:17 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2005-03-14 02:08:17 +00:00
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Open the Object */
|
|
|
|
Status = ObOpenObjectByName(ObjectAttributes,
|
|
|
|
ExSemaphoreObjectType,
|
|
|
|
PreviousMode,
|
2006-05-25 20:50:58 +00:00
|
|
|
NULL,
|
2005-03-14 02:08:17 +00:00
|
|
|
DesiredAccess,
|
|
|
|
NULL,
|
|
|
|
&hSemaphore);
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Check for success */
|
2009-08-26 17:31:02 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Enter SEH Block for return */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Return the handle */
|
2005-03-14 02:08:17 +00:00
|
|
|
*SemaphoreHandle = hSemaphore;
|
2005-08-05 06:56:17 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(ExSystemExceptionFilter())
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2009-08-26 17:31:02 +00:00
|
|
|
/* Get the exception code */
|
2008-11-24 13:40:26 +00:00
|
|
|
Status = _SEH2_GetExceptionCode();
|
2005-12-29 18:47:14 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2005-03-14 02:08:17 +00:00
|
|
|
}
|
2005-02-28 17:40:15 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Return Status */
|
|
|
|
return Status;
|
2005-01-07 06:54:27 +00:00
|
|
|
}
|
|
|
|
|
2005-01-23 22:09:27 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-03-14 02:08:17 +00:00
|
|
|
NTSTATUS
|
2005-12-29 18:47:14 +00:00
|
|
|
NTAPI
|
2005-01-07 06:54:27 +00:00
|
|
|
NtQuerySemaphore(IN HANDLE SemaphoreHandle,
|
2005-03-14 02:08:17 +00:00
|
|
|
IN SEMAPHORE_INFORMATION_CLASS SemaphoreInformationClass,
|
|
|
|
OUT PVOID SemaphoreInformation,
|
|
|
|
IN ULONG SemaphoreInformationLength,
|
2009-08-26 17:31:02 +00:00
|
|
|
OUT PULONG ReturnLength OPTIONAL)
|
2005-01-07 06:54:27 +00:00
|
|
|
{
|
2005-03-14 02:08:17 +00:00
|
|
|
PKSEMAPHORE Semaphore;
|
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
2009-08-26 17:31:02 +00:00
|
|
|
NTSTATUS Status;
|
2005-03-14 02:08:17 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Check buffers and class validity */
|
2005-10-10 13:03:55 +00:00
|
|
|
Status = DefaultQueryInfoBufferCheck(SemaphoreInformationClass,
|
|
|
|
ExSemaphoreInfoClass,
|
2005-12-29 18:47:14 +00:00
|
|
|
sizeof(ExSemaphoreInfoClass) /
|
|
|
|
sizeof(ExSemaphoreInfoClass[0]),
|
2022-06-11 11:19:51 +00:00
|
|
|
ICIF_PROBE_READ_WRITE,
|
2005-10-10 13:03:55 +00:00
|
|
|
SemaphoreInformation,
|
|
|
|
SemaphoreInformationLength,
|
|
|
|
ReturnLength,
|
2010-01-13 22:35:43 +00:00
|
|
|
NULL,
|
2022-06-11 11:19:51 +00:00
|
|
|
PreviousMode);
|
2009-08-26 17:31:02 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Invalid buffers */
|
|
|
|
DPRINT("NtQuerySemaphore() failed, Status: 0x%x\n", Status);
|
|
|
|
return Status;
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Get the Object */
|
|
|
|
Status = ObReferenceObjectByHandle(SemaphoreHandle,
|
|
|
|
SEMAPHORE_QUERY_STATE,
|
|
|
|
ExSemaphoreObjectType,
|
|
|
|
PreviousMode,
|
|
|
|
(PVOID*)&Semaphore,
|
|
|
|
NULL);
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Check for success */
|
2009-08-26 17:31:02 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Entry SEH Block */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-12-29 18:47:14 +00:00
|
|
|
PSEMAPHORE_BASIC_INFORMATION BasicInfo =
|
|
|
|
(PSEMAPHORE_BASIC_INFORMATION)SemaphoreInformation;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Return the basic information */
|
|
|
|
BasicInfo->CurrentCount = KeReadStateSemaphore(Semaphore);
|
|
|
|
BasicInfo->MaximumCount = Semaphore->Limit;
|
|
|
|
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Return the length */
|
2009-08-26 17:31:02 +00:00
|
|
|
if (ReturnLength) *ReturnLength = sizeof(*BasicInfo);
|
2005-08-05 06:56:17 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(ExSystemExceptionFilter())
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2009-08-26 17:31:02 +00:00
|
|
|
/* Get the exception code */
|
2008-11-24 13:40:26 +00:00
|
|
|
Status = _SEH2_GetExceptionCode();
|
2005-12-29 18:47:14 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Dereference the Object */
|
|
|
|
ObDereferenceObject(Semaphore);
|
2005-01-23 22:09:27 +00:00
|
|
|
}
|
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Return status */
|
2005-01-23 22:09:27 +00:00
|
|
|
return Status;
|
2005-01-07 06:54:27 +00:00
|
|
|
}
|
|
|
|
|
2005-01-23 22:09:27 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-05-09 01:38:29 +00:00
|
|
|
NTSTATUS
|
2005-12-29 18:47:14 +00:00
|
|
|
NTAPI
|
2005-01-07 06:54:27 +00:00
|
|
|
NtReleaseSemaphore(IN HANDLE SemaphoreHandle,
|
2005-03-14 02:08:17 +00:00
|
|
|
IN LONG ReleaseCount,
|
2005-12-29 18:47:14 +00:00
|
|
|
OUT PLONG PreviousCount OPTIONAL)
|
2005-01-07 06:54:27 +00:00
|
|
|
{
|
2005-03-14 02:08:17 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
|
|
|
PKSEMAPHORE Semaphore;
|
2009-08-26 17:31:02 +00:00
|
|
|
NTSTATUS Status;
|
2005-03-14 02:08:17 +00:00
|
|
|
PAGED_CODE();
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Check if we were called from user-mode */
|
2009-08-26 17:31:02 +00:00
|
|
|
if ((PreviousCount) && (PreviousMode != KernelMode))
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Entry SEH Block */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Make sure the state pointer is valid */
|
2005-08-21 17:38:07 +00:00
|
|
|
ProbeForWriteLong(PreviousCount);
|
2005-08-05 06:56:17 +00:00
|
|
|
}
|
2009-08-26 17:31:02 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2009-08-26 17:31:02 +00:00
|
|
|
/* Return the exception code */
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2005-08-05 06:56:17 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2005-03-14 02:08:17 +00:00
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Make sure count makes sense */
|
2005-08-05 06:56:17 +00:00
|
|
|
if (ReleaseCount <= 0)
|
|
|
|
{
|
2005-03-14 02:08:17 +00:00
|
|
|
DPRINT("Invalid Release Count\n");
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Get the Object */
|
|
|
|
Status = ObReferenceObjectByHandle(SemaphoreHandle,
|
|
|
|
SEMAPHORE_MODIFY_STATE,
|
|
|
|
ExSemaphoreObjectType,
|
|
|
|
PreviousMode,
|
|
|
|
(PVOID*)&Semaphore,
|
|
|
|
NULL);
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Check for success */
|
2005-08-05 06:56:17 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Enter SEH Block */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2005-12-29 18:47:14 +00:00
|
|
|
/* Release the semaphore */
|
2005-08-05 06:56:17 +00:00
|
|
|
LONG PrevCount = KeReleaseSemaphore(Semaphore,
|
|
|
|
IO_NO_INCREMENT,
|
|
|
|
ReleaseCount,
|
|
|
|
FALSE);
|
|
|
|
|
|
|
|
/* Return the old count if requested */
|
2009-08-26 17:31:02 +00:00
|
|
|
if (PreviousCount) *PreviousCount = PrevCount;
|
2005-08-05 06:56:17 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(ExSystemExceptionFilter())
|
2005-08-05 06:56:17 +00:00
|
|
|
{
|
2009-08-26 17:31:02 +00:00
|
|
|
/* Get the exception code */
|
2008-11-24 13:40:26 +00:00
|
|
|
Status = _SEH2_GetExceptionCode();
|
2005-03-14 02:08:17 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2006-09-01 18:41:00 +00:00
|
|
|
|
2009-08-26 17:31:02 +00:00
|
|
|
/* Dereference the Semaphore */
|
2006-09-01 18:41:00 +00:00
|
|
|
ObDereferenceObject(Semaphore);
|
2005-03-14 02:08:17 +00:00
|
|
|
}
|
2005-01-23 22:09:27 +00:00
|
|
|
|
2005-03-14 02:08:17 +00:00
|
|
|
/* Return Status */
|
|
|
|
return Status;
|
2005-01-07 06:54:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|