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  }

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

caimouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值