/*
- * COPYRIGHT: See COPYING in the top level directory
- * PROJECT: ReactOS project
- * FILE: ntoskrnl/ke/wait.c
- * PURPOSE: Manages non-busy waiting
- * PROGRAMMER: David Welch (welch@mcmail.com)
- * REVISION HISTORY:
- * 21/07/98: Created
- * 12/1/99: Phillip Susi: Fixed wake code in KeDispatcherObjectWake
- * so that things like KeWaitForXXX() return the correct value
- */
-
-/* NOTES ********************************************************************
- *
+ * COPYRIGHT: See COPYING in the top level directory
+ * PROJECT: ReactOS Kernel
+ * FILE: ntoskrnl/ke/wait.c
+ * PURPOSE: Manages waiting for Dispatcher Objects
+ * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
+ * Gunnar Dalsnes
*/
/* INCLUDES ******************************************************************/
/* GLOBALS ******************************************************************/
-static KSPIN_LOCK DispatcherDatabaseLock;
+KSPIN_LOCK DispatcherDatabaseLock;
-#define KeDispatcherObjectWakeOne(hdr, increment) KeDispatcherObjectWakeOneOrAll(hdr, increment, FALSE)
-#define KeDispatcherObjectWakeAll(hdr, increment) KeDispatcherObjectWakeOneOrAll(hdr, increment, TRUE)
+/* Tells us if the Timer or Event is a Syncronization or Notification Object */
+#define TIMER_OR_EVENT_TYPE 0x7L
-extern POBJECT_TYPE EXPORTED ExMutantObjectType;
-extern POBJECT_TYPE EXPORTED ExSemaphoreObjectType;
-extern POBJECT_TYPE EXPORTED ExTimerType;
+/* One of the Reserved Wait Blocks, this one is for the Thread's Timer */
+#define TIMER_WAIT_BLOCK 0x3L
/* FUNCTIONS *****************************************************************/
-VOID KeInitializeDispatcherHeader(DISPATCHER_HEADER* Header,
- ULONG Type,
- ULONG Size,
- ULONG SignalState)
-{
- Header->Type = (UCHAR)Type;
- Header->Absolute = 0;
- Header->Inserted = 0;
- Header->Size = (UCHAR)Size;
- Header->SignalState = SignalState;
- InitializeListHead(&(Header->WaitListHead));
-}
-
-
-KIRQL
-KeAcquireDispatcherDatabaseLock(VOID)
-/*
- * PURPOSE: Acquires the dispatcher database lock for the caller
- */
-{
- KIRQL OldIrql;
-
- DPRINT("KeAcquireDispatcherDatabaseLock()\n");
-
- KeAcquireSpinLock (&DispatcherDatabaseLock, &OldIrql);
- return OldIrql;
-}
-
-
-VOID
-KeAcquireDispatcherDatabaseLockAtDpcLevel(VOID)
-/*
- * PURPOSE: Acquires the dispatcher database lock for the caller
- */
+BOOLEAN
+__inline
+FASTCALL
+KiCheckAlertability(BOOLEAN Alertable,
+ PKTHREAD Thread,
+ KPROCESSOR_MODE WaitMode,
+ PNTSTATUS Status)
{
- DPRINT("KeAcquireDispatcherDatabaseLockAtDpcLevel()\n");
-
- KeAcquireSpinLockAtDpcLevel (&DispatcherDatabaseLock);
-}
-
-
-VOID
-KeReleaseDispatcherDatabaseLock(KIRQL OldIrql)
-{
- DPRINT("KeReleaseDispatcherDatabaseLock(OldIrql %x)\n",OldIrql);
- if (!KeIsExecutingDpc() &&
- OldIrql < DISPATCH_LEVEL &&
- KeGetCurrentThread() != NULL &&
- KeGetCurrentThread() == KeGetCurrentKPCR()->PrcbData.IdleThread)
- {
- PsDispatchThreadNoLock(THREAD_STATE_READY);
- KeLowerIrql(OldIrql);
- }
- else
- {
- KeReleaseSpinLock(&DispatcherDatabaseLock, OldIrql);
- }
-}
-
-
-VOID
-KeReleaseDispatcherDatabaseLockFromDpcLevel(VOID)
-{
- DPRINT("KeReleaseDispatcherDatabaseLock()\n");
+ /*
+ * At this point, we have to do a wait, so make sure we can make
+ * the thread Alertable if requested.
+ */
+ if (Alertable)
+ {
+ /* If the Thread is Alerted, set the Wait Status accordingly */
+ if (Thread->Alerted[(int)WaitMode])
+ {
+ Thread->Alerted[(int)WaitMode] = FALSE;
+ DPRINT("Thread was Alerted in the specified Mode\n");
+ *Status = STATUS_ALERTED;
+ return TRUE;
+ }
+ else if ((WaitMode != KernelMode) &&
+ (!IsListEmpty(&Thread->ApcState.ApcListHead[UserMode])))
+ {
+ /* If there are User APCs Pending, then we can't really be alertable */
+ DPRINT("APCs are Pending\n");
+ Thread->ApcState.UserApcPending = TRUE;
+ *Status = STATUS_USER_APC;
+ return TRUE;
+ }
+ else if (Thread->Alerted[KernelMode])
+ {
+ /*
+ * The thread is not alerted in the mode given, but it is alerted
+ * in kernel-mode.
+ */
+ Thread->Alerted[KernelMode] = FALSE;
+ DPRINT("Thread was Alerted in Kernel-Mode\n");
+ *Status = STATUS_ALERTED;
+ return TRUE;
+ }
+ }
+ else if ((WaitMode != KernelMode) &&
+ (Thread->ApcState.UserApcPending))
+ {
+ /*
+ * If there are User APCs Pending and we are waiting in usermode,
+ * then we must notify the caller
+ */
+ DPRINT("APCs are Pending\n");
+ *Status = STATUS_USER_APC;
+ return TRUE;
+ }
- KeReleaseSpinLockFromDpcLevel(&DispatcherDatabaseLock);
+ /* Stay in the loop */
+ return FALSE;
}
-
-static BOOLEAN
-KiSideEffectsBeforeWake(DISPATCHER_HEADER * hdr,
- PKTHREAD Thread)
/*
- * FUNCTION: Perform side effects on object before a wait for a thread is
- * satisfied
+ * @implemented
+ *
+ * FUNCTION: Puts the current thread into an alertable or nonalertable
+ * wait state for a given internal
+ * ARGUMENTS:
+ * WaitMode = Processor mode in which the caller is waiting
+ * Altertable = Specifies if the wait is alertable
+ * Interval = Specifies the interval to wait
+ * RETURNS: Status
*/
+NTSTATUS
+STDCALL
+KeDelayExecutionThread(KPROCESSOR_MODE WaitMode,
+ BOOLEAN Alertable,
+ PLARGE_INTEGER Interval)
{
- BOOLEAN Abandoned = FALSE;
-
- switch (hdr->Type)
- {
- case InternalSynchronizationEvent:
- hdr->SignalState = 0;
- break;
-
- case InternalQueueType:
- break;
-
- case InternalSemaphoreType:
- hdr->SignalState--;
- break;
-
- case InternalProcessType:
- break;
-
- case InternalThreadType:
- break;
-
- case InternalNotificationEvent:
- break;
-
- case InternalSynchronizationTimer:
- hdr->SignalState = FALSE;
- break;
-
- case InternalNotificationTimer:
- break;
-
- case InternalMutexType:
- {
- PKMUTEX Mutex;
-
- Mutex = CONTAINING_RECORD(hdr, KMUTEX, Header);
- hdr->SignalState--;
- ASSERT(hdr->SignalState <= 1);
- if (hdr->SignalState == 0)
- {
- if (Thread == NULL)
- {
- DPRINT("Thread == NULL!\n");
- KEBUGCHECK(0);
- }
- Abandoned = Mutex->Abandoned;
- if (Thread != NULL)
- InsertTailList(&Thread->MutantListHead, &Mutex->MutantListEntry);
- Mutex->OwnerThread = Thread;
- Mutex->Abandoned = FALSE;
- }
- }
- break;
-
- default:
- DbgPrint("(%s:%d) Dispatcher object %x has unknown type\n", __FILE__, __LINE__, hdr);
- KEBUGCHECK(0);
- }
-
- return Abandoned;
-}
-
-static BOOLEAN
-KiIsObjectSignalled(DISPATCHER_HEADER * hdr,
- PKTHREAD Thread)
-{
- if (hdr->Type == InternalMutexType)
- {
- PKMUTEX Mutex;
-
- Mutex = CONTAINING_RECORD(hdr, KMUTEX, Header);
-
- ASSERT(hdr->SignalState <= 1);
-
- if ((hdr->SignalState < 1 && Mutex->OwnerThread == Thread) || hdr->SignalState == 1)
- {
- return (TRUE);
- }
- else
- {
- return (FALSE);
- }
- }
-
- if (hdr->SignalState <= 0)
- {
- return (FALSE);
- }
- else
- {
- return (TRUE);
- }
-}
+ PKWAIT_BLOCK TimerWaitBlock;
+ PKTIMER ThreadTimer;
+ PKTHREAD CurrentThread = KeGetCurrentThread();
+ NTSTATUS Status;
-/* Must be called with the dispatcher lock held */
-BOOLEAN KiAbortWaitThread(PKTHREAD Thread, NTSTATUS WaitStatus)
-{
- PKWAIT_BLOCK WaitBlock;
- BOOLEAN WasWaiting;
-
- /* if we are blocked, we must be waiting on something also */
- ASSERT((Thread->State == THREAD_STATE_BLOCKED) == (Thread->WaitBlockList != NULL));
-
- WaitBlock = (PKWAIT_BLOCK)Thread->WaitBlockList;
- WasWaiting = (WaitBlock != NULL);
-
- while (WaitBlock)
- {
- RemoveEntryList(&WaitBlock->WaitListEntry);
- WaitBlock = WaitBlock->NextWaitBlock;
- }
-
- Thread->WaitBlockList = NULL;
-
- if (WasWaiting)
- {
- PsUnblockThread((PETHREAD)Thread, &WaitStatus, 0);
- }
- return WasWaiting;
-}
+ DPRINT("Entering KeDelayExecutionThread\n");
-static BOOLEAN
-KeDispatcherObjectWakeOneOrAll(DISPATCHER_HEADER * hdr,
- KPRIORITY increment,
- BOOLEAN WakeAll)
-{
- PKWAIT_BLOCK Waiter;
- PKWAIT_BLOCK WaiterHead;
- PLIST_ENTRY EnumEntry;
- NTSTATUS Status;
- BOOLEAN Abandoned;
- BOOLEAN AllSignaled;
- BOOLEAN WakedAny = FALSE;
-
- DPRINT("KeDispatcherObjectWakeOnOrAll(hdr %x)\n", hdr);
- DPRINT ("hdr->WaitListHead.Flink %x hdr->WaitListHead.Blink %x\n",
- hdr->WaitListHead.Flink, hdr->WaitListHead.Blink);
-
- if (IsListEmpty(&hdr->WaitListHead))
- {
- return (FALSE);
- }
-
- //enum waiters for this dispatcher object
- EnumEntry = hdr->WaitListHead.Flink;
- while (EnumEntry != &hdr->WaitListHead && (WakeAll || !WakedAny))
- {
- WaiterHead = CONTAINING_RECORD(EnumEntry, KWAIT_BLOCK, WaitListEntry);
- DPRINT("current_entry %x current %x\n", EnumEntry, WaiterHead);
- EnumEntry = EnumEntry->Flink;
- ASSERT(WaiterHead->Thread != NULL);
- ASSERT(WaiterHead->Thread->WaitBlockList != NULL);
-
- Abandoned = FALSE;
-
- if (WaiterHead->WaitType == WaitAny)
- {
- DPRINT("WaitAny: Remove all wait blocks.\n");
- for (Waiter = WaiterHead->Thread->WaitBlockList; Waiter; Waiter = Waiter->NextWaitBlock)
- {
- RemoveEntryList(&Waiter->WaitListEntry);
- }
-
- WaiterHead->Thread->WaitBlockList = NULL;
-
- /*
- * If a WakeAll KiSideEffectsBeforeWake(hdr,.. will be called several times,
- * but thats ok since WakeAll objects has no sideeffects.
- */
- Abandoned |= KiSideEffectsBeforeWake(hdr, WaiterHead->Thread);
- }
- else
- {
- DPRINT("WaitAll: All WaitAll objects must be signaled.\n");
-
- AllSignaled = TRUE;
-
- //all WaitAll obj. for thread need to be signaled to satisfy a wake
- for (Waiter = WaiterHead->Thread->WaitBlockList; Waiter; Waiter = Waiter->NextWaitBlock)
- {
- //no need to check hdr since it has to be signaled
- if (Waiter->WaitType == WaitAll && Waiter->Object != hdr)
- {
- if (!KiIsObjectSignalled(Waiter->Object, Waiter->Thread))
- {
- AllSignaled = FALSE;
- break;
- }
- }
- }
-
- if (AllSignaled)
- {
- for (Waiter = WaiterHead->Thread->WaitBlockList; Waiter; Waiter = Waiter->NextWaitBlock)
- {
- RemoveEntryList(&Waiter->WaitListEntry);
-
- if (Waiter->WaitType == WaitAll)
- {
- Abandoned |= KiSideEffectsBeforeWake(Waiter->Object, Waiter->Thread);
- }
-
- //no WaitAny objects can possibly be signaled since we are here
- ASSERT(!(Waiter->WaitType == WaitAny
- && KiIsObjectSignalled(Waiter->Object, Waiter->Thread)));
- }
+ /* Check if the lock is already held */
+ if (CurrentThread->WaitNext)
+ {
+ /* Lock is held, disable Wait Next */
+ DPRINT("Lock is held\n");
+ CurrentThread->WaitNext = FALSE;
+ }
+ else
+ {
+ /* Lock not held, acquire it */
+ DPRINT("Lock is not held, acquiring\n");
+ CurrentThread->WaitIrql = KeAcquireDispatcherDatabaseLock();
+ }
- WaiterHead->Thread->WaitBlockList = NULL;
- }
- }
-
- if (WaiterHead->Thread->WaitBlockList == NULL)
- {
- Status = WaiterHead->WaitKey;
- if (Abandoned)
- {
- DPRINT("Abandoned mutex among objects");
- Status += STATUS_ABANDONED_WAIT_0;
- }
-
- WakedAny = TRUE;
- DPRINT("Waking %x status = %x\n", WaiterHead->Thread, Status);
- PsUnblockThread(CONTAINING_RECORD(WaiterHead->Thread, ETHREAD, Tcb),
- &Status, increment);
- }
- }
-
- return WakedAny;
-}
+ /* Use built-in Wait block */
+ TimerWaitBlock = &CurrentThread->WaitBlock[TIMER_WAIT_BLOCK];
+ /* Start Wait Loop */
+ do
+ {
+ /* Chceck if we can do an alertable wait, if requested */
+ if (KiCheckAlertability(Alertable, CurrentThread, WaitMode, &Status)) break;
+
+ /* Set Timer */
+ ThreadTimer = &CurrentThread->Timer;
+
+ /* Setup the Wait Block */
+ CurrentThread->WaitBlockList = TimerWaitBlock;
+ TimerWaitBlock->Object = (PVOID)ThreadTimer;
+ TimerWaitBlock->Thread = CurrentThread;
+ TimerWaitBlock->WaitKey = (USHORT)STATUS_TIMEOUT;
+ TimerWaitBlock->WaitType = WaitAny;
+ TimerWaitBlock->NextWaitBlock = TimerWaitBlock;
+
+ /* Link the timer to this Wait Block */
+ InitializeListHead(&ThreadTimer->Header.WaitListHead);
+ InsertTailList(&ThreadTimer->Header.WaitListHead, &TimerWaitBlock->WaitListEntry);
+
+ /* Insert the Timer into the Timer Lists and enable it */
+ if (!KiInsertTimer(ThreadTimer, *Interval))
+ {
+ /* FIXME: The timer already expired, we should find a new ready thread */
+ Status = STATUS_SUCCESS;
+ break;
+ }
+
+ /* Handle Kernel Queues */
+ if (CurrentThread->Queue)
+ {
+ DPRINT("Waking Queue\n");
+ KiWakeQueue(CurrentThread->Queue);
+ }
+
+ /* Block the Thread */
+ DPRINT("Blocking the Thread: %d, %d, %x\n",
+ Alertable, WaitMode, KeGetCurrentThread());
+ KiBlockThread(&Status,
+ Alertable,
+ WaitMode,
+ DelayExecution);
+
+ /* Check if we were executing an APC or if we timed out */
+ if (Status != STATUS_KERNEL_APC)
+ {
+ /* This is a good thing */
+ if (Status == STATUS_TIMEOUT) Status = STATUS_SUCCESS;
+
+ /* Return Status */
+ return Status;
+ }
+
+ DPRINT("Looping Again\n"); // FIXME: Need to modify interval
+ CurrentThread->WaitIrql = KeAcquireDispatcherDatabaseLock();
+ }
+ while (TRUE);
-BOOLEAN KiDispatcherObjectWake(DISPATCHER_HEADER* hdr, KPRIORITY increment)
-/*
- * FUNCTION: Wake threads waiting on a dispatcher object
- * NOTE: The exact semantics of waking are dependant on the type of object
- */
-{
- BOOL Ret;
-
- DPRINT("Entering KeDispatcherObjectWake(hdr %x)\n",hdr);
-// DPRINT("hdr->WaitListHead %x hdr->WaitListHead.Flink %x\n",
-// &hdr->WaitListHead,hdr->WaitListHead.Flink);
- DPRINT("hdr->Type %x\n",hdr->Type);
- switch (hdr->Type)
- {
- case InternalNotificationEvent:
- return(KeDispatcherObjectWakeAll(hdr, increment));
-
- case InternalNotificationTimer:
- return(KeDispatcherObjectWakeAll(hdr, increment));
-
- case InternalSynchronizationEvent:
- return(KeDispatcherObjectWakeOne(hdr, increment));
-
- case InternalSynchronizationTimer:
- return(KeDispatcherObjectWakeOne(hdr, increment));
-
- case InternalQueueType:
- return(KeDispatcherObjectWakeOne(hdr, increment));
-
- case InternalSemaphoreType:
- DPRINT("hdr->SignalState %d\n", hdr->SignalState);
- if(hdr->SignalState>0)
- {
- do
- {
- DPRINT("Waking one semaphore waiter\n");
- Ret = KeDispatcherObjectWakeOne(hdr, increment);
- } while(hdr->SignalState > 0 && Ret) ;
- return(Ret);
- }
- else return FALSE;
-
- case InternalProcessType:
- return(KeDispatcherObjectWakeAll(hdr, increment));
-
- case InternalThreadType:
- return(KeDispatcherObjectWakeAll(hdr, increment));
-
- case InternalMutexType:
- return(KeDispatcherObjectWakeOne(hdr, increment));
- }
- DbgPrint("Dispatcher object %x has unknown type %d\n", hdr, hdr->Type);
- KEBUGCHECK(0);
- return(FALSE);
+ /* Release the Lock, we are done */
+ DPRINT("Returning from KeDelayExecutionThread(), %x. Status: %d\n",
+ KeGetCurrentThread(), Status);
+ KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
+ return Status;
}
-
/*
* @implemented
- */
-NTSTATUS STDCALL
-KeWaitForSingleObject(PVOID Object,
- KWAIT_REASON WaitReason,
- KPROCESSOR_MODE WaitMode,
- BOOLEAN Alertable,
- PLARGE_INTEGER Timeout)
-/*
+ *
* FUNCTION: Puts the current thread into a wait state until the
* given dispatcher object is set to signalled
* ARGUMENTS:
* Timeout = Optional timeout value
* RETURNS: Status
*/
+NTSTATUS
+STDCALL
+KeWaitForSingleObject(PVOID Object,
+ KWAIT_REASON WaitReason,
+ KPROCESSOR_MODE WaitMode,
+ BOOLEAN Alertable,
+ PLARGE_INTEGER Timeout)
{
- return KeWaitForMultipleObjects(1,
- &Object,
- WaitAny,
- WaitReason,
- WaitMode,
- Alertable,
- Timeout,
- NULL);
-}
+ PKMUTANT CurrentObject;
+ PKWAIT_BLOCK WaitBlock;
+ PKWAIT_BLOCK TimerWaitBlock;
+ PKTIMER ThreadTimer;
+ PKTHREAD CurrentThread = KeGetCurrentThread();
+ NTSTATUS Status;
+ NTSTATUS WaitStatus;
+ DPRINT("Entering KeWaitForSingleObject\n");
+
+ /* Check if the lock is already held */
+ if (CurrentThread->WaitNext)
+ {
+ /* Lock is held, disable Wait Next */
+ DPRINT("Lock is held\n");
+ CurrentThread->WaitNext = FALSE;
+ }
+ else
+ {
+ /* Lock not held, acquire it */
+ DPRINT("Lock is not held, acquiring\n");
+ CurrentThread->WaitIrql = KeAcquireDispatcherDatabaseLock();
+ }
+ /* Start the actual Loop */
+ do
+ {
+ /* Get the current Wait Status */
+ WaitStatus = CurrentThread->WaitStatus;
-inline
-PVOID
-KiGetWaitableObjectFromObject(PVOID Object)
-{
- //special case when waiting on file objects
- if ( ((PDISPATCHER_HEADER)Object)->Type == InternalFileType)
- {
- return &((PFILE_OBJECT)Object)->Event;
- }
+ /* Append wait block to the KTHREAD wait block list */
+ CurrentThread->WaitBlockList = WaitBlock = &CurrentThread->WaitBlock[0];
- return Object;
-}
+ /* Get the Current Object */
+ CurrentObject = (PKMUTANT)Object;
+ /* Check if it's a mutant */
+ if (CurrentObject->Header.Type == MutantObject)
+ {
+ /* Check its signal state or if we own it */
+ if ((CurrentObject->Header.SignalState > 0) ||
+ (CurrentThread == CurrentObject->OwnerThread))
+ {
+ /* Just unwait this guy and exit */
+ if (CurrentObject->Header.SignalState != (LONG)MINLONG)
+ {
+ /* It has a normal signal state, so unwait it and return */
+ KiSatisfyMutantWait(CurrentObject, CurrentThread);
+ Status = STATUS_WAIT_0;
+ goto DontWait;
+ }
+ else
+ {
+ /* According to wasm.ru, we must raise this exception (tested and true) */
+ KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
+ ExRaiseStatus(STATUS_MUTANT_LIMIT_EXCEEDED);
+ }
+ }
+ }
+ else if (CurrentObject->Header.SignalState > 0)
+ {
+ /* Another satisfied object */
+ KiSatisfyNonMutantWait(CurrentObject, CurrentThread);
+ Status = STATUS_WAIT_0;
+ goto DontWait;
+ }
+
+ /* Set up the Wait Block */
+ WaitBlock->Object = CurrentObject;
+ WaitBlock->Thread = CurrentThread;
+ WaitBlock->WaitKey = (USHORT)(STATUS_WAIT_0);
+ WaitBlock->WaitType = WaitAny;
+ WaitBlock->NextWaitBlock = WaitBlock;
+
+ /* Make sure we can satisfy the Alertable request */
+ if (KiCheckAlertability(Alertable, CurrentThread, WaitMode, &Status)) break;
+
+ /* Set the Wait Status */
+ CurrentThread->WaitStatus = Status;
+
+ /* Enable the Timeout Timer if there was any specified */
+ if (Timeout)
+ {
+ /* Fail if the timeout interval is actually 0 */
+ if (!Timeout->QuadPart)
+ {
+ /* Return a timeout */
+ Status = STATUS_TIMEOUT;
+ goto DontWait;
+ }
-inline BOOL
-KiIsObjectWaitable(PVOID Object)
-{
- POBJECT_HEADER Header;
- Header = BODY_TO_HEADER(Object);
- if (Header->ObjectType == ExEventObjectType ||
- Header->ObjectType == ExIoCompletionType ||
- Header->ObjectType == ExMutantObjectType ||
- Header->ObjectType == ExSemaphoreObjectType ||
- Header->ObjectType == ExTimerType ||
- Header->ObjectType == PsProcessType ||
- Header->ObjectType == PsThreadType ||
- Header->ObjectType == IoFileObjectType)
- {
- return TRUE;
- }
- else
- {
- return FALSE;
+ /* Point to Timer Wait Block and Thread Timer */
+ TimerWaitBlock = &CurrentThread->WaitBlock[TIMER_WAIT_BLOCK];
+ ThreadTimer = &CurrentThread->Timer;
+
+ /* Connect the Timer Wait Block */
+ WaitBlock->NextWaitBlock = TimerWaitBlock;
+
+ /* Set up the Timer Wait Block */
+ TimerWaitBlock->Object = (PVOID)ThreadTimer;
+ TimerWaitBlock->Thread = CurrentThread;
+ TimerWaitBlock->WaitKey = STATUS_TIMEOUT;
+ TimerWaitBlock->WaitType = WaitAny;
+ TimerWaitBlock->NextWaitBlock = WaitBlock;
+
+ /* Link the timer to this Wait Block */
+ InitializeListHead(&ThreadTimer->Header.WaitListHead);
+ InsertTailList(&ThreadTimer->Header.WaitListHead,
+ &TimerWaitBlock->WaitListEntry);
+
+ /* Insert the Timer into the Timer Lists and enable it */
+ if (!KiInsertTimer(ThreadTimer, *Timeout))
+ {
+ /* Return a timeout if we couldn't insert the timer */
+ Status = STATUS_TIMEOUT;
+ goto DontWait;
+ }
+ }
+
+ /* Link the Object to this Wait Block */
+ InsertTailList(&CurrentObject->Header.WaitListHead,
+ &WaitBlock->WaitListEntry);
+
+ /* Handle Kernel Queues */
+ if (CurrentThread->Queue)
+ {
+ DPRINT("Waking Queue\n");
+ KiWakeQueue(CurrentThread->Queue);
+ }
+
+ /* Block the Thread */
+ DPRINT("Blocking the Thread: %d, %d, %d, %x\n",
+ Alertable, WaitMode, WaitReason, KeGetCurrentThread());
+ KiBlockThread(&Status,
+ Alertable,
+ WaitMode,
+ (UCHAR)WaitReason);
+
+ /* Check if we were executing an APC */
+ if (Status != STATUS_KERNEL_APC)
+ {
+ /* Return Status */
+ return Status;
+ }
+
+ /* Loop again and acquire the dispatcher lock */
+ DPRINT("Looping Again\n"); // FIXME: Change interval
+ CurrentThread->WaitIrql = KeAcquireDispatcherDatabaseLock();
}
+ while (TRUE);
+
+ /* Release the Lock, we are done */
+ DPRINT("Returning from KeWaitForMultipleObjects(), %x. Status: %d\n",
+ KeGetCurrentThread(), Status);
+ KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
+ return Status;
+
+DontWait:
+ /* Adjust the Quantum */
+ KiAdjustQuantumThread(CurrentThread);
+
+ /* Release & Return */
+ DPRINT("Quick-return from KeWaitForMultipleObjects(), %x. Status: %d\n.",
+ KeGetCurrentThread(), Status);
+ KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
+ return Status;
}
/*
PLARGE_INTEGER Timeout,
PKWAIT_BLOCK WaitBlockArray)
{
- DISPATCHER_HEADER *hdr;
- PKWAIT_BLOCK blk;
- PKTHREAD CurrentThread;
- ULONG CountSignaled;
- ULONG i;
- NTSTATUS Status;
- KIRQL OldIrql;
- BOOLEAN Abandoned;
-
- DPRINT("Entering KeWaitForMultipleObjects(Count %lu Object[] %p) "
- "PsGetCurrentThread() %x\n", Count, Object, PsGetCurrentThread());
-
- ASSERT(0 < Count && Count <= MAXIMUM_WAIT_OBJECTS);
-
- CurrentThread = KeGetCurrentThread();
-
- /*
- * Work out where we are going to put the wait blocks
- */
- if (WaitBlockArray == NULL)
- {
- if (Count > THREAD_WAIT_OBJECTS)
- {
- DPRINT("(%s:%d) Too many objects!\n", __FILE__, __LINE__);
- return (STATUS_UNSUCCESSFUL);
- }
- WaitBlockArray = &CurrentThread->WaitBlock[0];
- }
- else
- {
- if (Count > MAXIMUM_WAIT_OBJECTS)
- {
- DPRINT("(%s:%d) Too many objects!\n", __FILE__, __LINE__);
- return (STATUS_UNSUCCESSFUL);
- }
- }
-
-
-
- /*
- * Set up the timeout if required
- */
- if (Timeout != NULL && Timeout->QuadPart != 0)
- {
- KeSetTimer(&CurrentThread->Timer, *Timeout, NULL);
- }
-
- do
- {
- if (CurrentThread->WaitNext)
- {
- CurrentThread->WaitNext = FALSE;
- OldIrql = CurrentThread->WaitIrql;
- }
- else
- {
- OldIrql = KeAcquireDispatcherDatabaseLock ();
- }
-
- /* Alertability 101
- * ----------------
- * A Wait can either be Alertable, or Non-Alertable.
- * An Alertable Wait means that APCs can "Wake" the Thread, also called UnWaiting
- * If an APC is Pending however, we must refuse an Alertable Wait. Such a wait would
- * be pointless since an APC is just about to be delivered.
- *
- * There are many ways to check if it's safe to be alertable, and these are the ones
- * that I could think of:
- * - The Thread is already Alerted. So someone beat us to the punch and we bail out.
- * - The Thread is Waiting in User-Mode, the APC Queue is not-empty.
- * It's defintely clear that we have incoming APCs, so we need to bail out and let the system
- * know that there are Pending User APCs (so they can be Delivered and maybe we can try again)
- *
- * Furthermore, wether or not we want to be Alertable, if the Thread is waiting in User-Mode, and there
- * are Pending User APCs, we should bail out, since APCs will be delivered any second.
- */
- if (Alertable) {
- if (CurrentThread->Alerted[(int)WaitMode]) {
- CurrentThread->Alerted[(int)WaitMode] = FALSE;
- DPRINT("Alertability failed\n");
- KeReleaseDispatcherDatabaseLock(OldIrql);
- return (STATUS_ALERTED);
- } else if ((!IsListEmpty(&CurrentThread->ApcState.ApcListHead[UserMode])) && (WaitMode == UserMode)) {
- DPRINT1("Alertability failed\n");
- CurrentThread->ApcState.UserApcPending = TRUE;
- KeReleaseDispatcherDatabaseLock(OldIrql);
- return (STATUS_USER_APC);
- }
- } else if ((CurrentThread->ApcState.UserApcPending) && (WaitMode != KernelMode)) {
- DPRINT1("Alertability failed\n");
- KeReleaseDispatcherDatabaseLock(OldIrql);
- return (STATUS_USER_APC);
- }
-
- /*
- * Check if the wait is (already) satisfied
- */
- CountSignaled = 0;
- Abandoned = FALSE;
- for (i = 0; i < Count; i++)
- {
- hdr = (DISPATCHER_HEADER *) KiGetWaitableObjectFromObject(Object[i]);
-
- if (KiIsObjectSignalled(hdr, CurrentThread))
- {
- CountSignaled++;
+ PKMUTANT CurrentObject;
+ PKWAIT_BLOCK WaitBlock;
+ PKWAIT_BLOCK TimerWaitBlock;
+ PKTIMER ThreadTimer;
+ PKTHREAD CurrentThread = KeGetCurrentThread();
+ ULONG AllObjectsSignaled;
+ ULONG WaitIndex;
+ NTSTATUS Status;
+ NTSTATUS WaitStatus;
+ DPRINT("Entering KeWaitForMultipleObjects(Count %lu Object[] %p) "
+ "PsGetCurrentThread() %x, Timeout %x\n",
+ Count, Object, PsGetCurrentThread(), Timeout);
+
+ /* Set the Current Thread */
+ CurrentThread = KeGetCurrentThread();
+
+ /* Check if the lock is already held */
+ if (CurrentThread->WaitNext)
+ {
+ /* Lock is held, disable Wait Next */
+ DPRINT("Lock is held\n");
+ CurrentThread->WaitNext = FALSE;
+ }
+ else
+ {
+ /* Lock not held, acquire it */
+ DPRINT("Lock is not held, acquiring\n");
+ CurrentThread->WaitIrql = KeAcquireDispatcherDatabaseLock();
+ }
- if (WaitType == WaitAny)
- {
- Abandoned = KiSideEffectsBeforeWake(hdr, CurrentThread) ? TRUE : Abandoned;
+ /* Make sure the Wait Count is valid for the Thread and Maximum Wait Objects */
+ if (!WaitBlockArray)
+ {
+ /* Check in regards to the Thread Object Limit */
+ if (Count > THREAD_WAIT_OBJECTS) KEBUGCHECK(MAXIMUM_WAIT_OBJECTS_EXCEEDED);
- if (Timeout != NULL && Timeout->QuadPart != 0)
- {
- KeCancelTimer(&CurrentThread->Timer);
- }
+ /* Use the Thread's Wait Block */
+ WaitBlockArray = &CurrentThread->WaitBlock[0];
+ }
+ else
+ {
+ /* Using our own Block Array. Check in regards to System Object Limit */
+ if (Count > MAXIMUM_WAIT_OBJECTS) KEBUGCHECK(MAXIMUM_WAIT_OBJECTS_EXCEEDED);
+ }
- KeReleaseDispatcherDatabaseLock(OldIrql);
+ /* Start the actual Loop */
+ do
+ {
+ /* Get the current Wait Status */
+ WaitStatus = CurrentThread->WaitStatus;
- DPRINT("One object is (already) signaled!\n");
- if (Abandoned == TRUE)
- {
- return (STATUS_ABANDONED_WAIT_0 + i);
- }
+ /* Append wait block to the KTHREAD wait block list */
+ CurrentThread->WaitBlockList = WaitBlock = WaitBlockArray;
- return (STATUS_WAIT_0 + i);
- }
- }
- }
-
- Abandoned = FALSE;
- if ((WaitType == WaitAll) && (CountSignaled == Count))
- {
- for (i = 0; i < Count; i++)
- {
- hdr = (DISPATCHER_HEADER *) KiGetWaitableObjectFromObject(Object[i]);
- Abandoned = KiSideEffectsBeforeWake(hdr, CurrentThread) ? TRUE : Abandoned;
- }
-
- if (Timeout != NULL && Timeout->QuadPart != 0)
- {
- KeCancelTimer(&CurrentThread->Timer);
- }
-
- KeReleaseDispatcherDatabaseLock(OldIrql);
- DPRINT("All objects are (already) signaled!\n");
-
- if (Abandoned == TRUE)
- {
- return (STATUS_ABANDONED_WAIT_0);
- }
-
- return (STATUS_WAIT_0);
- }
-
- //zero timeout is used for testing if the object(s) can be immediately acquired
- if (Timeout != NULL && Timeout->QuadPart == 0)
- {
- KeReleaseDispatcherDatabaseLock(OldIrql);
- return STATUS_TIMEOUT;
- }
-
- /*
- * Check if we have already timed out
- */
- if (Timeout != NULL && KiIsObjectSignalled(&CurrentThread->Timer.Header, CurrentThread))
- {
- KiSideEffectsBeforeWake(&CurrentThread->Timer.Header, CurrentThread);
- KeCancelTimer(&CurrentThread->Timer);
- KeReleaseDispatcherDatabaseLock(OldIrql);
- return (STATUS_TIMEOUT);
- }
-
- /* Append wait block to the KTHREAD wait block list */
- CurrentThread->WaitBlockList = blk = WaitBlockArray;
-
- /*
- * Set up the wait
- */
- CurrentThread->WaitStatus = STATUS_UNSUCCESSFUL;
-
- for (i = 0; i < Count; i++)
- {
- hdr = (DISPATCHER_HEADER *) KiGetWaitableObjectFromObject(Object[i]);
-
- blk->Object = KiGetWaitableObjectFromObject(Object[i]);
- blk->Thread = CurrentThread;
- blk->WaitKey = (USHORT)(STATUS_WAIT_0 + i);
- blk->WaitType = (USHORT)WaitType;
-
- if (i == (Count - 1))
- {
- if (Timeout != NULL)
+ /* Check if the wait is (already) satisfied */
+ AllObjectsSignaled = TRUE;
+
+ /* First, we'll try to satisfy the wait directly */
+ for (WaitIndex = 0; WaitIndex < Count; WaitIndex++)
+ {
+ /* Get the Current Object */
+ CurrentObject = (PKMUTANT)Object[WaitIndex];
+
+ /* Check the type of wait */
+ if (WaitType == WaitAny)
{
- blk->NextWaitBlock = &CurrentThread->WaitBlock[3];
+ /* Check if the Object is a mutant */
+ if (CurrentObject->Header.Type == MutantObject)
+ {
+ /* Check if it's signaled */
+ if ((CurrentObject->Header.SignalState > 0) ||
+ (CurrentThread == CurrentObject->OwnerThread))
+ {
+ /* This is a Wait Any, so just unwait this and exit */
+ if (CurrentObject->Header.SignalState != (LONG)MINLONG)
+ {
+ /* Normal signal state, so unwait it and return */
+ KiSatisfyMutantWait(CurrentObject, CurrentThread);
+ Status = STATUS_WAIT_0 | WaitIndex;
+ goto DontWait;
+ }
+ else
+ {
+ /* According to wasm.ru, we must raise this exception (tested and true) */
+ KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
+ ExRaiseStatus(STATUS_MUTANT_LIMIT_EXCEEDED);
+ }
+ }
+ }
+ else if (CurrentObject->Header.SignalState > 0)
+ {
+ /* Another signaled object, unwait and return */
+ KiSatisfyNonMutantWait(CurrentObject, CurrentThread);
+ Status = WaitIndex;
+ goto DontWait;
+ }
}
else
{
- blk->NextWaitBlock = NULL;
+ /* Check if we're dealing with a mutant again */
+ if (CurrentObject->Header.Type == MutantObject)
+ {
+ /* Check if it has an invalid count */
+ if ((CurrentThread == CurrentObject->OwnerThread) &&
+ (CurrentObject->Header.SignalState == MINLONG))
+ {
+ /* Raise an exception */
+ KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
+ ExRaiseStatus(STATUS_MUTANT_LIMIT_EXCEEDED);
+ }
+ else if ((CurrentObject->Header.SignalState <= 0) &&
+ (CurrentThread != CurrentObject->OwnerThread))
+ {
+ /* We don't own it, can't satisfy the wait */
+ AllObjectsSignaled = FALSE;
+ }
+ }
+ else if (CurrentObject->Header.SignalState <= 0)
+ {
+ /* Not signaled, can't satisfy */
+ AllObjectsSignaled = FALSE;
+ }
+ }
+
+ /* Set up a Wait Block for this Object */
+ WaitBlock->Object = CurrentObject;
+ WaitBlock->Thread = CurrentThread;
+ WaitBlock->WaitKey = (USHORT)WaitIndex;
+ WaitBlock->WaitType = (USHORT)WaitType;
+ WaitBlock->NextWaitBlock = WaitBlock + 1;
+
+ /* Move to the next Wait Block */
+ WaitBlock = WaitBlock->NextWaitBlock;
+ }
+
+ /* Return to the Root Wait Block */
+ WaitBlock--;
+ WaitBlock->NextWaitBlock = WaitBlockArray;
+
+ /* Check if this is a Wait All and all the objects are signaled */
+ if ((WaitType == WaitAll) && (AllObjectsSignaled))
+ {
+ /* Return to the Root Wait Block */
+ WaitBlock = CurrentThread->WaitBlockList;
+
+ /* Satisfy their Waits and return to the caller */
+ KiSatisifyMultipleObjectWaits(WaitBlock);
+ Status = STATUS_WAIT_0;
+ goto DontWait;
+ }
+
+ /* Make sure we can satisfy the Alertable request */
+ if (KiCheckAlertability(Alertable, CurrentThread, WaitMode, &Status)) break;
+
+ /* Set the Wait Status */
+ CurrentThread->WaitStatus = Status;
+
+ /* Enable the Timeout Timer if there was any specified */
+ if (Timeout)
+ {
+ /* Make sure the timeout interval isn't actually 0 */
+ if (!Timeout->QuadPart)
+ {
+ /* Return a timeout */
+ Status = STATUS_TIMEOUT;
+ goto DontWait;
}
- }
- else
- {
- blk->NextWaitBlock = blk + 1;
- }
-
- /*
- * add wait block to disp. obj. wait list
- * Use FIFO for all waits except for queues which use LIFO
- */
- if (WaitReason == WrQueue)
- {
- InsertHeadList(&hdr->WaitListHead, &blk->WaitListEntry);
- }
- else
- {
- InsertTailList(&hdr->WaitListHead, &blk->WaitListEntry);
- }
-
- blk = blk->NextWaitBlock;
- }
-
- if (Timeout != NULL)
- {
- CurrentThread->WaitBlock[3].Object = (PVOID) & CurrentThread->Timer;
- CurrentThread->WaitBlock[3].Thread = CurrentThread;
- CurrentThread->WaitBlock[3].WaitKey = STATUS_TIMEOUT;
- CurrentThread->WaitBlock[3].WaitType = WaitAny;
- CurrentThread->WaitBlock[3].NextWaitBlock = NULL;
-
- InsertTailList(&CurrentThread->Timer.Header.WaitListHead,
- &CurrentThread->WaitBlock[3].WaitListEntry);
- }
-
- //kernel queues
- if (CurrentThread->Queue && WaitReason != WrQueue)
- {
- DPRINT("queue: sleep on something else\n");
- CurrentThread->Queue->CurrentCount--;
-
- //wake another thread
- if (CurrentThread->Queue->CurrentCount < CurrentThread->Queue->MaximumCount &&
- !IsListEmpty(&CurrentThread->Queue->EntryListHead))
- {
- KiDispatcherObjectWake(&CurrentThread->Queue->Header, IO_NO_INCREMENT);
- }
- }
-
- PsBlockThread(&Status, Alertable, WaitMode, TRUE, OldIrql, (UCHAR)WaitReason);
-
- //kernel queues
- OldIrql = KeAcquireDispatcherDatabaseLock ();
- if (CurrentThread->Queue && WaitReason != WrQueue)
- {
- DPRINT("queue: wake from something else\n");
- CurrentThread->Queue->CurrentCount++;
- }
- if (Status == STATUS_KERNEL_APC)
- {
- CurrentThread->WaitNext = TRUE;
- CurrentThread->WaitIrql = OldIrql;
- }
- else
- {
- KeReleaseDispatcherDatabaseLock(OldIrql);
- }
-
- } while (Status == STATUS_KERNEL_APC);
-
-
- if (Timeout != NULL)
- {
- KeCancelTimer(&CurrentThread->Timer);
- }
-
- DPRINT("Returning from KeWaitForMultipleObjects()\n");
- return (Status);
+
+ /* Point to Timer Wait Block and Thread Timer */
+ TimerWaitBlock = &CurrentThread->WaitBlock[TIMER_WAIT_BLOCK];
+ ThreadTimer = &CurrentThread->Timer;
+
+ /* Connect the Timer Wait Block */
+ WaitBlock->NextWaitBlock = TimerWaitBlock;
+
+ /* Set up the Timer Wait Block */
+ TimerWaitBlock->Object = (PVOID)ThreadTimer;
+ TimerWaitBlock->Thread = CurrentThread;
+ TimerWaitBlock->WaitKey = STATUS_TIMEOUT;
+ TimerWaitBlock->WaitType = WaitAny;
+ TimerWaitBlock->NextWaitBlock = WaitBlockArray;
+
+ /* Link the timer to this Wait Block */
+ InitializeListHead(&ThreadTimer->Header.WaitListHead);
+
+ /* Insert the Timer into the Timer Lists and enable it */
+ if (!KiInsertTimer(ThreadTimer, *Timeout))
+ {
+ /* Return a timeout if we couldn't insert the timer */
+ Status = STATUS_TIMEOUT;
+ goto DontWait;
+ }
+ }
+
+ /* Insert into Object's Wait List*/
+ WaitBlock = CurrentThread->WaitBlockList;
+ do
+ {
+ /* Get the Current Object */
+ CurrentObject = WaitBlock->Object;
+
+ /* Link the Object to this Wait Block */
+ InsertTailList(&CurrentObject->Header.WaitListHead,
+ &WaitBlock->WaitListEntry);
+
+ /* Move to the next Wait Block */
+ WaitBlock = WaitBlock->NextWaitBlock;
+ }
+ while (WaitBlock != WaitBlockArray);
+
+ /* Handle Kernel Queues */
+ if (CurrentThread->Queue)
+ {
+ DPRINT("Waking Queue\n");
+ KiWakeQueue(CurrentThread->Queue);
+ }
+
+ /* Block the Thread */
+ DPRINT("Blocking the Thread: %d, %d, %d, %x\n",
+ Alertable, WaitMode, WaitReason, KeGetCurrentThread());
+ KiBlockThread(&Status,
+ Alertable,
+ WaitMode,
+ (UCHAR)WaitReason);
+
+ /* Check if we were executing an APC */
+ DPRINT("Thread is back\n");
+ if (Status != STATUS_KERNEL_APC)
+ {
+ /* Return Status */
+ return Status;
+ }
+
+ /* Loop again and re-acquire the dispatcher lock */
+ DPRINT("Looping Again\n"); // FIXME: Fix-up the interval */
+ CurrentThread->WaitIrql = KeAcquireDispatcherDatabaseLock();
+ }
+ while (TRUE);
+
+ /* Release the Lock, we are done */
+ DPRINT("Returning, %x. Status: %d\n", KeGetCurrentThread(), Status);
+ KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
+ return Status;
+
+DontWait:
+ /* Adjust the Quantum */
+ KiAdjustQuantumThread(CurrentThread);
+
+ /* Release & Return */
+ DPRINT("Returning, %x. Status: %d\n. We did not wait.",
+ KeGetCurrentThread(), Status);
+ KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
+ return Status;
}
-VOID KeInitializeDispatcher(VOID)
+VOID
+FASTCALL
+KiWaitTest(PVOID ObjectPointer,
+ KPRIORITY Increment)
{
- KeInitializeSpinLock(&DispatcherDatabaseLock);
+ PLIST_ENTRY WaitEntry;
+ PLIST_ENTRY WaitList;
+ PKWAIT_BLOCK CurrentWaitBlock;
+ PKWAIT_BLOCK NextWaitBlock;
+ PKTHREAD WaitThread;
+ PKMUTANT FirstObject = ObjectPointer, Object;
+
+ /* Loop the Wait Entries */
+ DPRINT("KiWaitTest for Object: %x\n", FirstObject);
+ WaitList = &FirstObject->Header.WaitListHead;
+ WaitEntry = WaitList->Flink;
+ while ((FirstObject->Header.SignalState > 0) && (WaitEntry != WaitList))
+ {
+ /* Get the current wait block */
+ CurrentWaitBlock = CONTAINING_RECORD(WaitEntry,
+ KWAIT_BLOCK,
+ WaitListEntry);
+ WaitThread = CurrentWaitBlock->Thread;
+
+ /* Check the current Wait Mode */
+ if (CurrentWaitBlock->WaitType == WaitAny)
+ {
+ /* Easy case, satisfy only this wait */
+ DPRINT("Satisfiying a Wait any\n");
+ WaitEntry = WaitEntry->Blink;
+ KiSatisfyObjectWait(FirstObject, WaitThread);
+ }
+ else
+ {
+ /* Everything must be satisfied */
+ DPRINT("Checking for a Wait All\n");
+ NextWaitBlock = CurrentWaitBlock->NextWaitBlock;
+
+ /* Loop first to make sure they are valid */
+ while (NextWaitBlock != CurrentWaitBlock)
+ {
+ /* Check if the object is signaled */
+ Object = NextWaitBlock->Object;
+ DPRINT("Checking: %p %d\n",
+ Object, Object->Header.SignalState);
+ if (NextWaitBlock->WaitKey != STATUS_TIMEOUT)
+ {
+ /* Check if this is a mutant */
+ if ((Object->Header.Type == MutantObject) &&
+ (Object->Header.SignalState <= 0) &&
+ (WaitThread == Object->OwnerThread))
+ {
+ /* It's a signaled mutant */
+ }
+ else if (Object->Header.SignalState <= 0)
+ {
+ /* Skip the unwaiting */
+ goto SkipUnwait;
+ }
+ }
+
+ /* Go to the next Wait block */
+ NextWaitBlock = NextWaitBlock->NextWaitBlock;
+ }
+
+ /* All the objects are signaled, we can satisfy */
+ DPRINT("Satisfiying a Wait All\n");
+ WaitEntry = WaitEntry->Blink;
+ KiSatisifyMultipleObjectWaits(CurrentWaitBlock);
+ }
+
+ /* All waits satisfied, unwait the thread */
+ DPRINT("Unwaiting the Thread\n");
+ KiAbortWaitThread(WaitThread, CurrentWaitBlock->WaitKey, Increment);
+
+SkipUnwait:
+ /* Next entry */
+ WaitEntry = WaitEntry->Flink;
+ }
+
+ DPRINT("Done\n");
}
-NTSTATUS STDCALL
-NtWaitForMultipleObjects(IN ULONG Count,
- IN HANDLE Object [],
- IN WAIT_TYPE WaitType,
- IN BOOLEAN Alertable,
- IN PLARGE_INTEGER UnsafeTime)
+/* Must be called with the dispatcher lock held */
+VOID
+FASTCALL
+KiAbortWaitThread(PKTHREAD Thread,
+ NTSTATUS WaitStatus,
+ KPRIORITY Increment)
{
- KWAIT_BLOCK WaitBlockArray[MAXIMUM_WAIT_OBJECTS];
- PVOID ObjectPtrArray[MAXIMUM_WAIT_OBJECTS];
- NTSTATUS Status;
- ULONG i, j;
- KPROCESSOR_MODE WaitMode;
- LARGE_INTEGER Time;
-
- DPRINT("NtWaitForMultipleObjects(Count %lu Object[] %x, Alertable %d, "
- "Time %x)\n", Count,Object,Alertable,Time);
-
- if (Count > MAXIMUM_WAIT_OBJECTS)
- return STATUS_UNSUCCESSFUL;
- if (0 == Count)
- return STATUS_INVALID_PARAMETER;
-
- if (UnsafeTime)
- {
- Status = MmCopyFromCaller(&Time, UnsafeTime, sizeof(LARGE_INTEGER));
- if (!NT_SUCCESS(Status))
- {
- return(Status);
- }
- }
-
- WaitMode = ExGetPreviousMode();
-
- /* reference all objects */
- for (i = 0; i < Count; i++)
- {
- Status = ObReferenceObjectByHandle(Object[i],
- SYNCHRONIZE,
- NULL,
- WaitMode,
- &ObjectPtrArray[i],
- NULL);
- if (!NT_SUCCESS(Status) || !KiIsObjectWaitable(ObjectPtrArray[i]))
- {
- if (NT_SUCCESS(Status))
- {
- DPRINT1("Waiting for object type '%wZ' is not supported\n",
- &BODY_TO_HEADER(ObjectPtrArray[i])->ObjectType->TypeName);
- Status = STATUS_HANDLE_NOT_WAITABLE;
- i++;
- }
- /* dereference all referenced objects */
- for (j = 0; j < i; j++)
- {
- ObDereferenceObject(ObjectPtrArray[j]);
- }
-
- return(Status);
- }
- }
-
- Status = KeWaitForMultipleObjects(Count,
- ObjectPtrArray,
- WaitType,
- UserRequest,
- WaitMode,
- Alertable,
- UnsafeTime ? &Time : NULL,
- WaitBlockArray);
-
- /* dereference all objects */
- for (i = 0; i < Count; i++)
- {
- ObDereferenceObject(ObjectPtrArray[i]);
- }
-
- return(Status);
-}
+ PKWAIT_BLOCK WaitBlock;
+ /* If we are blocked, we must be waiting on something also */
+ DPRINT("KiAbortWaitThread: %x, Status: %x, %x \n",
+ Thread, WaitStatus, Thread->WaitBlockList);
-/*
- * @implemented
- */
-NTSTATUS STDCALL
-NtWaitForSingleObject(IN HANDLE Object,
- IN BOOLEAN Alertable,
- IN PLARGE_INTEGER UnsafeTime)
-{
- PVOID ObjectPtr;
- NTSTATUS Status;
- KPROCESSOR_MODE WaitMode;
- LARGE_INTEGER Time;
-
- DPRINT("NtWaitForSingleObject(Object %x, Alertable %d, Time %x)\n",
- Object,Alertable,Time);
-
- if (UnsafeTime)
- {
- Status = MmCopyFromCaller(&Time, UnsafeTime, sizeof(LARGE_INTEGER));
- if (!NT_SUCCESS(Status))
- {
- return(Status);
- }
- }
-
- WaitMode = ExGetPreviousMode();
-
- Status = ObReferenceObjectByHandle(Object,
- SYNCHRONIZE,
- NULL,
- WaitMode,
- &ObjectPtr,
- NULL);
- if (!NT_SUCCESS(Status))
- {
- return(Status);
- }
- if (!KiIsObjectWaitable(ObjectPtr))
- {
- DPRINT1("Waiting for object type '%wZ' is not supported\n",
- &BODY_TO_HEADER(ObjectPtr)->ObjectType->TypeName);
- Status = STATUS_HANDLE_NOT_WAITABLE;
- }
- else
- {
- Status = KeWaitForSingleObject(ObjectPtr,
- UserRequest,
- WaitMode,
- Alertable,
- UnsafeTime ? &Time : NULL);
- }
-
- ObDereferenceObject(ObjectPtr);
-
- return(Status);
+ /* Remove the Wait Blocks from the list */
+ DPRINT("Removing waits\n");
+ WaitBlock = Thread->WaitBlockList;
+ do
+ {
+ /* Remove it */
+ DPRINT("Removing Waitblock: %x, %x\n",
+ WaitBlock, WaitBlock->NextWaitBlock);
+ RemoveEntryList(&WaitBlock->WaitListEntry);
+
+ /* Go to the next one */
+ WaitBlock = WaitBlock->NextWaitBlock;
+ } while (WaitBlock != Thread->WaitBlockList);
+
+ /* Check if there's a Thread Timer */
+ if (Thread->Timer.Header.Inserted)
+ {
+ /* Cancel the Thread Timer with the no-lock fastpath */
+ DPRINT("Removing the Thread's Timer\n");
+ Thread->Timer.Header.Inserted = FALSE;
+ RemoveEntryList(&Thread->Timer.TimerListEntry);
+ }
+
+ /* Increment the Queue's active threads */
+ if (Thread->Queue)
+ {
+ DPRINT("Incrementing Queue's active threads\n");
+ Thread->Queue->CurrentCount++;
+ }
+
+ /* Reschedule the Thread */
+ DPRINT("Unblocking the Thread\n");
+ KiUnblockThread(Thread, &WaitStatus, 0);
}
+VOID
+FASTCALL
+KiAcquireFastMutex(IN PFAST_MUTEX FastMutex)
+{
+ /* Increase contention count */
+ FastMutex->Contention++;
+
+ /* Wait for the event */
+ KeWaitForSingleObject(&FastMutex->Gate,
+ WrMutex,
+ KernelMode,
+ FALSE,
+ NULL);
+}
-NTSTATUS STDCALL
-NtSignalAndWaitForSingleObject(IN HANDLE SignalObject,
- IN HANDLE WaitObject,
- IN BOOLEAN Alertable,
- IN PLARGE_INTEGER Time)
+VOID
+FASTCALL
+KeReleaseDispatcherDatabaseLock(KIRQL OldIrql)
{
- KPROCESSOR_MODE WaitMode;
- DISPATCHER_HEADER* hdr;
- PVOID SignalObj;
- PVOID WaitObj;
- NTSTATUS Status;
-
- WaitMode = ExGetPreviousMode();
- Status = ObReferenceObjectByHandle(SignalObject,
- 0,
- NULL,
- WaitMode,
- &SignalObj,
- NULL);
- if (!NT_SUCCESS(Status))
- {
- return Status;
- }
-
- Status = ObReferenceObjectByHandle(WaitObject,
- SYNCHRONIZE,
- NULL,
- WaitMode,
- &WaitObj,
- NULL);
- if (!NT_SUCCESS(Status))
- {
- ObDereferenceObject(SignalObj);
- return Status;
- }
-
- hdr = (DISPATCHER_HEADER *)SignalObj;
- switch (hdr->Type)
- {
- case InternalNotificationEvent:
- case InternalSynchronizationEvent:
- KeSetEvent(SignalObj,
- EVENT_INCREMENT,
- TRUE);
- break;
-
- case InternalMutexType:
- KeReleaseMutex(SignalObj,
- TRUE);
- break;
-
- case InternalSemaphoreType:
- KeReleaseSemaphore(SignalObj,
- SEMAPHORE_INCREMENT,
- 1,
- TRUE);
- break;
-
- default:
- ObDereferenceObject(SignalObj);
- ObDereferenceObject(WaitObj);
- return STATUS_OBJECT_TYPE_MISMATCH;
- }
-
- Status = KeWaitForSingleObject(WaitObj,
- UserRequest,
- WaitMode,
- Alertable,
- Time);
-
- ObDereferenceObject(SignalObj);
- ObDereferenceObject(WaitObj);
-
- return Status;
+ /* If it's the idle thread, dispatch */
+ if (!(KeIsExecutingDpc()) &&
+ (OldIrql < DISPATCH_LEVEL) &&
+ (KeGetCurrentThread()) &&
+ (KeGetCurrentThread() == KeGetCurrentPrcb()->IdleThread))
+ {
+ KiDispatchThreadNoLock(Ready);
+ KeLowerIrql(OldIrql);
+ }
+ else
+ {
+ /* Just release the spin lock */
+#ifdef CONFIG_SMP
+ KeReleaseSpinLock(&DispatcherDatabaseLock, OldIrql);
+#else
+ KeLowerIrql(OldIrql);
+#endif
+ }
}
+
+/* EOF */