VOID FASTCALL KiQueueReadyThread(IN PKTHREAD Thread, IN PKPRCB Prcb) { /* Call the macro. We keep the API for compatibility with ASM code */ KxQueueReadyThread(Thread, Prcb); }
VOID NTAPI KiDispatchInterrupt(VOID) { PKIPCR Pcr = (PKIPCR)KeGetPcr(); PKPRCB Prcb = &Pcr->PrcbData; PVOID OldHandler; PKTHREAD NewThread, OldThread; /* Disable interrupts */ _disable(); /* Check for pending timers, pending DPCs, or pending ready threads */ if ((Prcb->DpcData[0].DpcQueueDepth) || (Prcb->TimerRequest) || (Prcb->DeferredReadyListHead.Next)) { /* Switch to safe execution context */ OldHandler = Pcr->NtTib.ExceptionList; Pcr->NtTib.ExceptionList = EXCEPTION_CHAIN_END; /* Retire DPCs while under the DPC stack */ //KiRetireDpcListInDpcStack(Prcb, Prcb->DpcStack); // FIXME!!! // KiRetireDpcList(Prcb); /* Restore context */ Pcr->NtTib.ExceptionList = OldHandler; } /* Re-enable interrupts */ _enable(); /* Check for quantum end */ if (Prcb->QuantumEnd) { /* Handle quantum end */ Prcb->QuantumEnd = FALSE; KiQuantumEnd(); } else if (Prcb->NextThread) { /* Capture current thread data */ OldThread = Prcb->CurrentThread; NewThread = Prcb->NextThread; /* Set new thread data */ Prcb->NextThread = NULL; Prcb->CurrentThread = NewThread; /* The thread is now running */ NewThread->State = Running; OldThread->WaitReason = WrDispatchInt; /* Make the old thread ready */ KxQueueReadyThread(OldThread, Prcb); /* Swap to the new thread */ KiSwapContext(APC_LEVEL, OldThread); } }
VOID NTAPI KiQuantumEnd(VOID) { PKPRCB Prcb = KeGetCurrentPrcb(); PKTHREAD NextThread, Thread = Prcb->CurrentThread; /* Check if a DPC Event was requested to be signaled */ if (InterlockedExchange(&Prcb->DpcSetEventRequest, 0)) { /* Signal it */ KeSetEvent(&Prcb->DpcEvent, 0, 0); } /* Raise to synchronization level and lock the PRCB and thread */ KeRaiseIrqlToSynchLevel(); KiAcquireThreadLock(Thread); KiAcquirePrcbLock(Prcb); /* Check if Quantum expired */ if (Thread->Quantum <= 0) { /* Check if we're real-time and with quantums disabled */ if ((Thread->Priority >= LOW_REALTIME_PRIORITY) && (Thread->ApcState.Process->DisableQuantum)) { /* Otherwise, set maximum quantum */ Thread->Quantum = MAX_QUANTUM; } else { /* Reset the new Quantum */ Thread->Quantum = Thread->QuantumReset; /* Calculate new priority */ Thread->Priority = KiComputeNewPriority(Thread, 1); /* Check if a new thread is scheduled */ if (!Prcb->NextThread) { /* Get a new ready thread */ NextThread = KiSelectReadyThread(Thread->Priority, Prcb); if (NextThread) { /* Found one, set it on standby */ NextThread->State = Standby; Prcb->NextThread = NextThread; } } else { /* Otherwise, make sure that this thread doesn't get preempted */ Thread->Preempted = FALSE; } } } /* Release the thread lock */ KiReleaseThreadLock(Thread); /* Check if there's no thread scheduled */ if (!Prcb->NextThread) { /* Just leave now */ KiReleasePrcbLock(Prcb); KeLowerIrql(DISPATCH_LEVEL); return; } /* Get the next thread now */ NextThread = Prcb->NextThread; /* Set current thread's swap busy to true */ KiSetThreadSwapBusy(Thread); /* Switch threads in PRCB */ Prcb->NextThread = NULL; Prcb->CurrentThread = NextThread; /* Set thread to running and the switch reason to Quantum End */ NextThread->State = Running; Thread->WaitReason = WrQuantumEnd; /* Queue it on the ready lists */ KxQueueReadyThread(Thread, Prcb); /* Set wait IRQL to APC_LEVEL */ Thread->WaitIrql = APC_LEVEL; /* Swap threads */ KiSwapContext(APC_LEVEL, Thread); /* Lower IRQL back to DISPATCH_LEVEL */ KeLowerIrql(DISPATCH_LEVEL); }
/* * @implemented */ NTSTATUS NTAPI NtYieldExecution(VOID) { NTSTATUS Status; KIRQL OldIrql; PKPRCB Prcb; PKTHREAD Thread, NextThread; /* NB: No instructions (other than entry code) should preceed this line */ /* Fail if there's no ready summary */ if (!KiGetCurrentReadySummary()) return STATUS_NO_YIELD_PERFORMED; /* Now get the current thread, set the status... */ Status = STATUS_NO_YIELD_PERFORMED; Thread = KeGetCurrentThread(); /* Raise IRQL to synch and get the KPRCB now */ OldIrql = KeRaiseIrqlToSynchLevel(); Prcb = KeGetCurrentPrcb(); /* Now check if there's still a ready summary */ if (Prcb->ReadySummary) { /* Acquire thread and PRCB lock */ KiAcquireThreadLock(Thread); KiAcquirePrcbLock(Prcb); /* Find a new thread to run if none was selected */ if (!Prcb->NextThread) Prcb->NextThread = KiSelectReadyThread(1, Prcb); /* Make sure we still have a next thread to schedule */ NextThread = Prcb->NextThread; if (NextThread) { /* Reset quantum and recalculate priority */ Thread->Quantum = Thread->QuantumReset; Thread->Priority = KiComputeNewPriority(Thread, 1); /* Release the thread lock */ KiReleaseThreadLock(Thread); /* Set context swap busy */ KiSetThreadSwapBusy(Thread); /* Set the new thread as running */ Prcb->NextThread = NULL; Prcb->CurrentThread = NextThread; NextThread->State = Running; /* Setup a yield wait and queue the thread */ Thread->WaitReason = WrYieldExecution; KxQueueReadyThread(Thread, Prcb); /* Make it wait at APC_LEVEL */ Thread->WaitIrql = APC_LEVEL; /* Sanity check */ ASSERT(OldIrql <= DISPATCH_LEVEL); /* Swap to new thread */ KiSwapContext(APC_LEVEL, Thread); Status = STATUS_SUCCESS; } else { /* Release the PRCB and thread lock */ KiReleasePrcbLock(Prcb); KiReleaseThreadLock(Thread); } } /* Lower IRQL and return */ KeLowerIrql(OldIrql); return Status; }
NTSTATUS NtYieldExecution ( VOID ) /*++ Routine Description: This function yields execution to any ready thread for up to one quantum. Arguments: None. Return Value: None. --*/ { KIRQL OldIrql; PKTHREAD NewThread; PRKPRCB Prcb; NTSTATUS Status; PKTHREAD Thread; // // If no other threads are ready, then return immediately. Otherwise, // attempt to yield execution. // // N.B. The test for ready threads is made outside any synchonization. // Since this code cannot be perfectly synchronized under any // conditions the lack of synchronization is of no consequence. // if (KiGetCurrentReadySummary() == 0) { return STATUS_NO_YIELD_PERFORMED; } else { Status = STATUS_NO_YIELD_PERFORMED; Thread = KeGetCurrentThread(); OldIrql = KeRaiseIrqlToSynchLevel(); Prcb = KeGetCurrentPrcb(); if (Prcb->ReadySummary != 0) { // // Acquire the thread lock and the PRCB lock. // // If a thread has not already been selected for execution, then // attempt to select another thread for execution. // KiAcquireThreadLock(Thread); KiAcquirePrcbLock(Prcb); if (Prcb->NextThread == NULL) { Prcb->NextThread = KiSelectReadyThread(1, Prcb); } // // If a new thread has been selected for execution, then switch // immediately to the selected thread. // if ((NewThread = Prcb->NextThread) != NULL) { Thread->Quantum = Thread->QuantumReset; // // Compute the new thread priority. // // N.B. The new priority will never be greater than the previous // priority. // Thread->Priority = KiComputeNewPriority(Thread, 1); // // Release the thread lock, set swap busy for the old thread, // set the next thread to NULL, set the current thread to the // new thread, set the new thread state to running, set the // wait reason, queue the old running thread, and release the // PRCB lock, and swp context to the new thread. // KiReleaseThreadLock(Thread); KiSetContextSwapBusy(Thread); Prcb->NextThread = NULL; Prcb->CurrentThread = NewThread; NewThread->State = Running; Thread->WaitReason = WrYieldExecution; KxQueueReadyThread(Thread, Prcb); Thread->WaitIrql = APC_LEVEL; ASSERT(OldIrql <= DISPATCH_LEVEL); KiSwapContext(Thread, NewThread); Status = STATUS_SUCCESS; } else { KiReleasePrcbLock(Prcb); KiReleaseThreadLock(Thread); } } // // Lower IRQL to its previous level and return. // KeLowerIrql(OldIrql); return Status; } }