/* * @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; } }