KiAcquireThreadStateLock

VOID __fastcall KiAcquireThreadStateLock(
        _ETHREAD *Thread,
        _KPRCB **ControlPrcb,
        _KSHARED_READY_QUEUE **ControlReadyQueue){
  __int64 v6; 
  volatile signed __int32 *SharedReadyQueue; 
  __int64 NextProcessor; 
  struct _KPRCB *CurrentPrcb; 
  bool v10; 
  char v11; 
  __int64 v12; 
  struct _KPRCB *v13; 
  __int64 v14; 
  struct _KPRCB *v15; 
  __int64 v16; 
  _DWORD *SchedulerAssist; 
  _DWORD *v18; 
  struct _KPRCB *v19; 
  _DWORD *v20; 
  _DWORD *v21; 
  UINT64 SpinCount[9]; 
  UINT64 v23; 
  UINT64 v24; 

  while( 1 )
  {
    while( 1 )
    {
      while( 1 )
      {
        v6 = 0i64;
        SharedReadyQueue = 0i64;
        if( Thread->Tcb.WaitBlockFill6[68] != 2 )
          break;
LABEL_2:
        NextProcessor = Thread->Tcb.NextProcessor;
        if( (int)NextProcessor >= 0 )
        {
          CurrentPrcb = KeGetCurrentPrcb();
          v6 = (__int64)*(&KiProcessorBlock + NextProcessor);
          HIDWORD(SpinCount[0]) = 0;
          while( _interlockedbittestandset64((volatile signed __int32 *)(v6 + 48), 0i64) )
          {
            do
              KeYieldProcessorEx((UINT64 *)((char *)SpinCount + 4));
            while( *(_QWORD *)(v6 + 48) );
            SchedulerAssist = CurrentPrcb->SchedulerAssist;
            if( SchedulerAssist && CurrentPrcb->NestingLevel <= 1u )
              ++SchedulerAssist[6];
          }
          v10 = Thread == *(_ETHREAD **)(v6 + 8);
LABEL_6:
          if( v10 )
            goto LABEL_7;
          goto LABEL_19;
        }
      }
      if( Thread->Tcb.WaitBlockFill6[68] == 1 )
        break;
      if( Thread->Tcb.WaitBlockFill6[68] != 3 )
      {
        if( Thread->Tcb.WaitBlockFill6[68] != 5 )
          goto LABEL_7;
        v11 = Thread->Tcb.WaitRegister.Flags & 7;
        if( v11 == 1 || (unsigned __int8)(v11 - 3) <= 3u )
          goto LABEL_7;
        goto LABEL_2;
      }
      v12 = Thread->Tcb.NextProcessor;
      if( (int)v12 >= 0 )
      {
        v13 = KeGetCurrentPrcb();
        v6 = (__int64)*(&KiProcessorBlock + v12);
        LODWORD(v23) = 0;
        while( _interlockedbittestandset64((volatile signed __int32 *)(v6 + 48), 0i64) )
        {
          do
            KeYieldProcessorEx(&v23);
          while( *(_QWORD *)(v6 + 48) );
          v18 = v13->SchedulerAssist;
          if( v18 && v13->NestingLevel <= 1u )
            ++v18[6];
        }
        if( Thread == *(_ETHREAD **)(v6 + 16) )
          goto LABEL_7;
        if( Thread->Tcb.WaitBlockFill6[68] == 3 && Thread->Tcb._bf_0 == (_DWORD)v12 )
          __fastfail(0x1Eu);
LABEL_19:
        _InterlockedAnd64((volatile signed __int64 *)(v6 + 48), 0i64);
      }
    }
    v14 = Thread->Tcb.NextProcessor;
    if( (int)v14 >= 0 )
    {
      v19 = KeGetCurrentPrcb();
      v6 = (__int64)*(&KiProcessorBlock + v14);
      LODWORD(SpinCount[0]) = 0;
      while( _interlockedbittestandset64((volatile signed __int32 *)(v6 + 48), 0i64) )
      {
        do
          KeYieldProcessorEx(SpinCount);
        while( *(_QWORD *)(v6 + 48) );
        v20 = v19->SchedulerAssist;
        if( v20 && v19->NestingLevel <= 1u )
          ++v20[6];
      }
      if( Thread->Tcb.WaitBlockFill6[68] != 1 )
        goto LABEL_19;
      v10 = Thread->Tcb._bf_0 == (_DWORD)v14;
      goto LABEL_6;
    }
    v15 = KeGetCurrentPrcb();
    v16 = (unsigned int)v14;
    LODWORD(v16) = v14 & 0x7FFFFFFF;
    LODWORD(v24) = 0;
    SharedReadyQueue = (volatile signed __int32 *)(*(&KiProcessorBlock + v16))->SharedReadyQueue;
    while( _interlockedbittestandset64(SharedReadyQueue, 0i64) )
    {
      do
        KeYieldProcessorEx(&v24);
      while( *(_QWORD *)SharedReadyQueue );
      v21 = v15->SchedulerAssist;
      if( v21 && v15->NestingLevel <= 1u )
        ++v21[6];
    }
    if( Thread->Tcb.WaitBlockFill6[68] == 1 && Thread->Tcb._bf_0 == (_DWORD)v14 )
      break;
    _InterlockedAnd64((volatile signed __int64 *)SharedReadyQueue, 0i64);
  }
  v6 = 0i64;
LABEL_7:
  *ControlPrcb = (_KPRCB *)v6;
  *ControlReadyQueue = (_KSHARED_READY_QUEUE *)SharedReadyQueue;
}

Referenced by:

KeAbCrossThreadDelete
KeQueryTotalCycleTimeThread
KeSetActualBasePriorityThread
KeSetIdealProcessorThreadEx
KeSetThreadSchedulerAssist
KeTryToFreezeThreadStack
KeUpdateThreadTag
KiApplyForegroundBoostThread
KiRemoveThreadFromSchedulingGroup
KiSendHeteroRescheduleIntRequestHelper
KiSetAffinityThread
KiSetHeteroPolicyThread
KiSetPriorityThread
KiSetThreadSchedulingGroup
KiUpdateThreadCpuSets