KiResortScbQueue

VOID __stdcall KiResortScbQueue(_KPRCB *Prcb, _KSCB *Scb, UINT8 Increase){
  _RTL_BALANCED_NODE *p_QueueNode; 
  unsigned __int64 v6; 
  unsigned __int64 i; 
  _RTL_BALANCED_NODE *v8; 
  unsigned __int64 k; 
  _RTL_BALANCED_NODE *j; 
  signed int v11; 
  unsigned int v12; 
  unsigned int v13; 
  int v14; 
  int v15; 
  unsigned int Rank; 
  int v17; 
  unsigned int ReadySummary; 
  unsigned int v19; 
  int v20; 
  int v21; 

  p_QueueNode = &Scb->QueueNode;
  if( !Increase )
  {
    v6 = (unsigned __int64)p_QueueNode->Children[0];
    if( p_QueueNode->Children[0] )
    {
      for( ; *(_QWORD *)(v6 + 8); v6 = *(_QWORD *)(v6 + 8) )
        ;
    }
    else
    {
      for( i = Scb->QueueNode.ParentValue; ; i = *(_QWORD *)(v6 + 16) )
      {
        v6 = i & 0xFFFFFFFFFFFFFFFCui64;
        if( !v6 || *(_RTL_BALANCED_NODE **)(v6 + 8) == p_QueueNode )
          break;
        p_QueueNode = (_RTL_BALANCED_NODE *)v6;
      }
    }
    if( !v6 )
      return;
    Rank = Scb->Rank;
    v17 = Rank - *(_DWORD *)(v6 + 28);
    if( Rank != *(_DWORD *)(v6 + 28) )
      goto LABEL_31;
    ReadySummary = Scb->ReadySummary;
    if( (_WORD)ReadySummary )
    {
      v19 = *(unsigned __int16 *)(v6 + 26);
      _BitScanReverse((unsigned int *)&v20, ReadySummary);
      v21 = 0;
      if( (_WORD)v19 )
        _BitScanReverse((unsigned int *)&v21, v19);
      v17 = v21 - v20;
LABEL_31:
      if( v17 >= 0 )
        return;
      goto LABEL_32;
    }
    if( Rank )
    {
      v17 = 1;
      goto LABEL_31;
    }
    if( Scb->GenerationCycles > *(_QWORD *)(v6 - 88) )
      return;
LABEL_32:
    KiRemoveSchedulingGroupQueue(Prcb, Scb, 0);
    KiInsertSchedulingGroupQueue(Prcb, Scb, 0);
    return;
  }
  v8 = Scb->QueueNode.Children[1];
  if( v8 )
  {
    for( j = v8->Children[0]; j; j = j->Children[0] )
      v8 = j;
  }
  else
  {
    for( k = Scb->QueueNode.ParentValue; ; k = v8->ParentValue )
    {
      v8 = (_RTL_BALANCED_NODE *)(k & 0xFFFFFFFFFFFFFFFCui64);
      if( !v8 || v8->Children[0] == p_QueueNode )
        break;
      p_QueueNode = v8;
    }
  }
  if( v8 )
  {
    v11 = Scb->Rank - HIDWORD(v8[1].Left);
    if( !v11 )
    {
      v12 = Scb->ReadySummary;
      if( (_WORD)v12 )
      {
        v13 = WORD1(v8[1].Left);
        _BitScanReverse((unsigned int *)&v14, v12);
        v15 = 0;
        if( (_WORD)v13 )
          _BitScanReverse((unsigned int *)&v15, v13);
        v11 = v15 - v14;
      }
      else
      {
        if( !Scb->Rank )
        {
          if( (_RTL_BALANCED_NODE *)Scb->GenerationCycles <= v8[-4].Children[1] )
            return;
          goto LABEL_32;
        }
        v11 = 1;
      }
    }
    if( v11 <= 0 )
      return;
    goto LABEL_32;
  }
}

Referenced by:

KeSetSchedulingGroupRankBias
KiComputeGroupSchedulingRank
KiInsertNonMaxOverQuotaScb
KiRecomputeGroupSchedulingRank