reactos操作系统实现(45)

怎么样从一个延迟就绪队列里选择合适的线程运行呢?下面就来分析这段代码,如下:

#001 //

#002 // This routine scans for an appropriate ready thread to select at the

#003 // given priority and for the given cpu.

#004 //

#005 FORCEINLINE

#006 PKTHREAD

#007 KiSelectReadyThread(IN KPRIORITY Priority,

#008 IN PKPRCB Prcb)

#009 {

#010 ULONG PrioritySet;

#011 LONG HighPriority;

#012 PLIST_ENTRY ListEntry;

#013 PKTHREAD Thread = NULL;

#014

获取当前处理器的优先级。

#015 /* Save the current mask and get the priority set for the cpu */

#016 PrioritySet = Prcb->ReadySummary >> Priority;

#017 if (!PrioritySet) goto Quickie;

#018

计算合适的优先级。

#019 /* Get the highest priority possible */

#020 BitScanReverse((PULONG)&HighPriority,PrioritySet);

#021 ASSERT((PrioritySet & PRIORITY_MASK(HighPriority)) != 0);

#022 HighPriority += Priority;

#023

#024 /* Make sure the list isn't empty at the highest priority */

#025 ASSERT(IsListempty(&Prcb->dispatcherReadyListHead[HighPriority]) == FALSE);

#026

从给出的优先级队列里找到合适的就绪线程。

#027 /* Get the first thread on the list */

#028 ListEntry = Prcb->dispatcherReadyListHead[HighPriority].Flink;

#029 Thread = CONTAINING_RECORD(ListEntry,KTHREAD,WaitListEntry);

#030

#031 /* Make sure this thread is here for a reason */

#032 ASSERT(HighPriority == Thread->Priority);

#033 ASSERT(Thread->Affinity & AFFINITY_MASK(Prcb->Number));

#034 ASSERT(Thread->NextProcessor == Prcb->Number);

#035

删除就绪队列里的线程。

#036 /* Remove it from the list */

#037 if (RemoveEntryList(&Thread->WaitListEntry))

#038 {

#039 /* The list is empty Now,reset the ready summary */

#040 Prcb->ReadySummary ^= PRIORITY_MASK(HighPriority);

#041 }

#042

#043 /* Sanity check and return the thread */

#044 Quickie:

#045 ASSERT((Thread == NULL) ||

#046 (Thread->BasePriority == 0) ||

#047 (Thread->Priority != 0));

#048 return Thread;

#049 }

通过上面函数处理,就可以从合适的优先级队列里得到下一个运行的线程。

相关文章

一、前言 在组件方面react和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...