- Small semaphore code simplification/optimization.

svn path=/trunk/; revision=18202
This commit is contained in:
Alex Ionescu 2005-10-02 08:31:45 +00:00
parent 66e502bd5a
commit 794f347587
2 changed files with 22 additions and 22 deletions

View file

@ -473,7 +473,10 @@ KeRundownThread(VOID)
{ {
/* Get the Mutant */ /* Get the Mutant */
Mutant = CONTAINING_RECORD(NextEntry, KMUTANT, MutantListEntry); Mutant = CONTAINING_RECORD(NextEntry, KMUTANT, MutantListEntry);
DPRINT1("Mutant: %p. Type, Size %x %x\n", Mutant, Mutant->Header.Type, Mutant->Header.Size); DPRINT1("Mutant: %p. Type, Size %x %x\n",
Mutant,
Mutant->Header.Type,
Mutant->Header.Size);
/* Make sure it's not terminating with APCs off */ /* Make sure it's not terminating with APCs off */
if (Mutant->ApcDisable) if (Mutant->ApcDisable)

View file

@ -1,11 +1,10 @@
/* $Id$ /*
*
* COPYRIGHT: See COPYING in the top level directory * COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel * PROJECT: ReactOS kernel
* FILE: ntoskrnl/ke/sem.c * FILE: ntoskrnl/ke/sem.c
* PURPOSE: Implements kernel semaphores * PURPOSE: Implements kernel semaphores
* * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
* PROGRAMMERS: David Welch (welch@mcmail.com) * David Welch (welch@mcmail.com)
*/ */
/* INCLUDES *****************************************************************/ /* INCLUDES *****************************************************************/
@ -25,13 +24,12 @@ KeInitializeSemaphore(PKSEMAPHORE Semaphore,
LONG Count, LONG Count,
LONG Limit) LONG Limit)
{ {
DPRINT("KeInitializeSemaphore Sem: %x\n", Semaphore); DPRINT("KeInitializeSemaphore Sem: %x\n", Semaphore);
/* Simply Initialize the Header */ /* Simply Initialize the Header */
KeInitializeDispatcherHeader(&Semaphore->Header, KeInitializeDispatcherHeader(&Semaphore->Header,
SemaphoreObject, SemaphoreObject,
sizeof(KSEMAPHORE)/sizeof(ULONG), sizeof(KSEMAPHORE) / sizeof(ULONG),
Count); Count);
/* Set the Limit */ /* Set the Limit */
@ -46,7 +44,7 @@ STDCALL
KeReadStateSemaphore(PKSEMAPHORE Semaphore) KeReadStateSemaphore(PKSEMAPHORE Semaphore)
{ {
/* Just return the Signal State */ /* Just return the Signal State */
return(Semaphore->Header.SignalState); return Semaphore->Header.SignalState;
} }
/* /*
@ -77,9 +75,8 @@ KeReleaseSemaphore(PKSEMAPHORE Semaphore,
KPRIORITY Increment, KPRIORITY Increment,
LONG Adjustment, LONG Adjustment,
BOOLEAN Wait) BOOLEAN Wait)
{ {
ULONG InitialState; LONG InitialState, ULONG State;
KIRQL OldIrql; KIRQL OldIrql;
PKTHREAD CurrentThread; PKTHREAD CurrentThread;
@ -92,36 +89,36 @@ KeReleaseSemaphore(PKSEMAPHORE Semaphore,
/* Lock the Dispatcher Database */ /* Lock the Dispatcher Database */
OldIrql = KeAcquireDispatcherDatabaseLock(); OldIrql = KeAcquireDispatcherDatabaseLock();
/* Save the Old State */ /* Save the Old State and get new one */
InitialState = Semaphore->Header.SignalState; InitialState = Semaphore->Header.SignalState;
State = InitialState + Adjustement;
/* Check if the Limit was exceeded */ /* Check if the Limit was exceeded */
if (Semaphore->Limit < (LONG) InitialState + Adjustment || if ((Semaphore->Limit < State) || (InitialState > State))
InitialState > InitialState + Adjustment) { {
/* Raise an error if it was exceeded */ /* Raise an error if it was exceeded */
KeReleaseDispatcherDatabaseLock(OldIrql); KeReleaseDispatcherDatabaseLock(OldIrql);
ExRaiseStatus(STATUS_SEMAPHORE_LIMIT_EXCEEDED); ExRaiseStatus(STATUS_SEMAPHORE_LIMIT_EXCEEDED);
} }
/* Now set the new state */ /* Now set the new state */
Semaphore->Header.SignalState += Adjustment; Semaphore->Header.SignalState = State;
/* Check if we should wake it */ /* Check if we should wake it */
if (InitialState == 0 && !IsListEmpty(&Semaphore->Header.WaitListHead)) { if (!(InitialState) && !(IsListEmpty(&Semaphore->Header.WaitListHead))
{
/* Wake the Semaphore */ /* Wake the Semaphore */
KiWaitTest(&Semaphore->Header, Increment); KiWaitTest(&Semaphore->Header, Increment);
} }
/* If the Wait is true, then return with a Wait and don't unlock the Dispatcher Database */ /* If the Wait is true, then return with a Wait and don't unlock the Dispatcher Database */
if (Wait == FALSE) { if (Wait == FALSE)
{
/* Release the Lock */ /* Release the Lock */
KeReleaseDispatcherDatabaseLock(OldIrql); KeReleaseDispatcherDatabaseLock(OldIrql);
}
} else { else
{
/* Set a wait */ /* Set a wait */
CurrentThread = KeGetCurrentThread(); CurrentThread = KeGetCurrentThread();
CurrentThread->WaitNext = TRUE; CurrentThread->WaitNext = TRUE;