KiSetSystemAffinityThread

VOID __stdcall KiSetSystemAffinityThread(
        _KPRCB *Prcb,
        _GROUP_AFFINITY *Affinity,
        UINT64 IdealProcessor,
        _SINGLE_LIST_ENTRY *ReadyList){
  _ETHREAD *CurrentThread; 
  _KPRCB *v7; 
  __int64 v8; 
  unsigned __int64 v9; 
  _KNODE *ParentNode; 
  int v11; 
  unsigned __int64 v12; 
  unsigned __int64 v13; 
  __int64 v14; 
  UINT64 SpinCount; 
  int v16; 

  CurrentThread = Prcb->CurrentThread;
  CurrentThread->Tcb.Affinity.Group = Affinity->Group;
  CurrentThread->Tcb.Affinity.Mask = Affinity->Mask;
  if( (unsigned int)IdealProcessor < 0x500 )
  {
    CurrentThread->Tcb.IdealProcessor = IdealProcessor;
    v14 = (unsigned int)IdealProcessor;
    v9 = 0x140000000ui64;
LABEL_7:
    v7 = *(_KPRCB **)(v9 + 8 * v14 + 13618368);
    goto LABEL_8;
  }
  v7 = *(&KiProcessorBlock + CurrentThread->Tcb.IdealProcessor);
  if( !(unsigned int)KiPrcbInGroupAffinity(v7, Affinity) )
  {
    ParentNode = v7->ParentNode;
    v11 = *(unsigned __int16 *)(v8 + 8);
    v12 = *(_QWORD *)v8;
    if( (_WORD)v11 == ParentNode->Affinity.Group )
    {
      v13 = v12 & ParentNode->Affinity.Mask;
      if( v13 )
        v12 = v13;
    }
    _BitScanReverse64(&v12, v12);
    v16 = v12;
    v14 = *(unsigned int *)(v9 + 4i64 * (unsigned int)(v12 + (v11 << 6)) + 13631696);
    CurrentThread->Tcb.IdealProcessor = v14;
    goto LABEL_7;
  }
LABEL_8:
  if( (CurrentThread->Tcb._bf_0 & 8) != 0 || !(unsigned int)KiComputeThreadAffinity(&CurrentThread->Tcb) )
  {
    KiUpdateSharedReadyQueueAffinityThread(v7, &CurrentThread->Tcb);
    KiUpdateNodeAffinitizedFlag(&CurrentThread->Tcb);
  }
  if( !(unsigned int)KiPrcbInGroupAffinity(Prcb, &CurrentThread->Tcb.Affinity) )
  {
    _interlockedbittestandset((volatile signed __int32 *)&CurrentThread->Tcb.116 + 1, 0xCu);
    if( !Prcb->NextThread )
    {
      LODWORD(SpinCount) = 0;
      while( _interlockedbittestandset64((volatile signed __int32 *)&Prcb->PrcbLock, 0i64) )
      {
        do
          KeYieldProcessorEx(&SpinCount);
        while( Prcb->PrcbLock );
      }
      if( !Prcb->NextThread )
        KiSelectNextThread(Prcb, ReadyList);
      _InterlockedAnd64((volatile signed __int64 *)&Prcb->PrcbLock, 0i64);
    }
  }
}

Referenced by:

KeRevertToUserGroupAffinityThread
KeSetSystemGroupAffinityThread