KeRemovePriQueue

_LIST_ENTRY *__stdcall KeRemovePriQueue(_KPRIQUEUE *Queue, _LARGE_INTEGER *Timeout){
  UINT8 v2; 
  __int64 v3; 
  _ETHREAD *CurrentThread; 
  int v5; 
  UINT8 v6; 
  CHAR v7; 
  UINT64 v9; 
  $BA6907B96C27CBFB414D9A454222AF19 *v10; 
  _KDUE_TIME_TYPE v11; 
  unsigned __int8 CurrentIrql; 
  _LIST_ENTRY *result; 
  _DISPATCHER_HEADER *v14; 
  UINT64 v15; 
  struct _KPRCB *v16; 
  __int64 v17; 
  __int64 v18; 
  _LIST_ENTRY *v19; 
  struct _KPRCB *CurrentPrcb; 
  __int64 v21; 
  int v22; 
  __int64 QueuePriority; 
  struct _KPRCB *v24; 
  struct _KPRCB *v25; 
  UINT64 CycleTime; 
  unsigned __int64 v27; 
  INT64 v28; 
  int v29; 
  _DISPATCHER_HEADER *volatile v30; 
  unsigned __int8 v31; 
  _DWORD *v32; 
  _DWORD *SchedulerAssist; 
  UINT64 UnbiasedInterruptTime; 
  _QWORD *v35; 
  int v36; 
  UINT64 v37; 
  struct _KPRCB *v38; 
  _DWORD *v39; 
  int v40; 
  _LIST_ENTRY *Flink; 
  _LIST_ENTRY *Blink; 
  UINT64 SpinCount; 
  __int64 v44[3]; 
  INT64 WorkPriority; 

  CurrentThread = (_ETHREAD *)KeGetCurrentThread();
  v5 = 0;
  LODWORD(WorkPriority) = 0;
  v44[1] = 0i64;
  v6 = v2;
  v7 = (char)Timeout;
  v9 = 0i64;
  v10 = &CurrentThread->Tcb.320;
  v11 = DueTimeRelative;
  if( !_bittestandreset((signed __int32 *)&CurrentThread->Tcb.116, 2u) )
  {
    CurrentIrql = KeGetCurrentIrql();
    __writecr8(2ui64);
    CurrentThread->Tcb.WaitIrql = CurrentIrql;
  }
  if( v3 )
  {
    if( *(int *)(v3 + 4) < 0 )
    {
      UnbiasedInterruptTime = KiQueryUnbiasedInterruptTime(0);
      v9 = UnbiasedInterruptTime - (*v35 + CurrentThread->Tcb.RelativeTimerBias);
    }
    else
    {
      v9 = *(_QWORD *)v3;
      v11 = DueTimeAbsolute;
    }
  }
  else
  {
    v11 = DueTimeNone;
  }
  while( 1 )
  {
    KiBeginThreadWait(CurrentThread, v7, WrQueue, v6);
    result = (_LIST_ENTRY *)(int)result;
    if( (_DWORD)result )
      return result;
    v14 = CurrentThread->Tcb.Queue;
    if( Queue != (_KPRIQUEUE *)v14 )
      KiSwitchPriQueue(&CurrentThread->Tcb, Queue, v14);
    CurrentThread->Tcb.WaitBlock[0].WaitType = 3;
    CurrentThread->Tcb.WaitBlockFill4[17] = 4;
    CurrentThread->Tcb.WaitBlock[0].WaitKey = 128;
    CurrentThread->Tcb.WaitBlock[0].Object = Queue;
    while( _interlockedbittestandset((volatile signed __int32 *)Queue, 7u) )
    {
      do
      {
        ++v5;
        _mm_pause();
      }
      while( (*(_DWORD *)Queue & 0x80u) != 0 );
    }
    if( *((_DWORD *)Queue + 1) )
    {
      CurrentPrcb = KeGetCurrentPrcb();
      LODWORD(SpinCount) = 0;
      while( _interlockedbittestandset64((volatile signed __int32 *)&CurrentThread->Tcb.ThreadLock, 0i64) )
      {
        do
          KeYieldProcessorEx(&SpinCount);
        while( CurrentThread->Tcb.ThreadLock );
        SchedulerAssist = CurrentPrcb->SchedulerAssist;
        if( SchedulerAssist && CurrentPrcb->NestingLevel <= 1u )
          ++SchedulerAssist[6];
      }
      v21 = (__int64)KiAttemptFastRemovePriQueue(
                       Queue,
                       &WorkPriority,
                       (unsigned __int8)CurrentThread->Tcb.QueuePriority);
      if( v21 )
      {
        v22 = WorkPriority;
        QueuePriority = (unsigned __int8)CurrentThread->Tcb.QueuePriority;
        if( (_DWORD)QueuePriority != (_DWORD)WorkPriority )
        {
          v29 = CurrentThread->Tcb.QueuePriority & 0x100;
          if( !v29 )
          {
            v30 = CurrentThread->Tcb.Queue;
            _InterlockedDecrement((volatile signed __int32 *)v30 + QueuePriority + 134);
            _InterlockedIncrement((volatile signed __int32 *)v30 + (int)WorkPriority + 134);
            v22 = WorkPriority;
          }
          CurrentThread->Tcb.QueuePriority = v29 | (unsigned __int8)v22;
        }
        CurrentThread->Tcb.Saturation = 0;
        KiSetBasePriorityAndClearDecrement(&CurrentThread->Tcb, &WorkPriority, 0i64);
        if( v22 != CurrentThread->Tcb.Priority )
        {
          v25 = KeGetCurrentPrcb();
          if( v25->NestingLevel )
          {
            CycleTime = CurrentThread->Tcb.CycleTime;
          }
          else
          {
            _disable();
            CycleTime = KiUpdateTotalCyclesCurrentThread(v25, &CurrentThread->Tcb, 0i64);
            _enable();
          }
          v27 = CycleTime + KiCyclesPerClockQuantum * (unsigned int)CurrentThread->Tcb.SchedulerApc.SpareByte1;
          if( (*(&CurrentThread->Tcb.MiscFlags + 1) & 0x20) != 0 )
            _interlockedbittestandreset((volatile signed __int32 *)&CurrentThread->Tcb.116 + 1, 5u);
          v28 = (unsigned int)WorkPriority;
          CurrentThread->Tcb.QuantumTarget = v27;
          KiSetPriorityThread(&CurrentThread->Tcb, 0i64, v28);
        }
        KiReleaseThreadLockSafe((INT64)CurrentThread);
        goto LABEL_24;
      }
      KiReleaseThreadLockSafe((INT64)CurrentThread);
    }
    if( (*((_BYTE *)Queue + 1) & 1) != 0 )
    {
      v38 = KeGetCurrentPrcb();
      LODWORD(v44[0]) = 0;
      while( _interlockedbittestandset64((volatile signed __int32 *)&CurrentThread->Tcb.ThreadLock, 0i64) )
      {
        do
          KeYieldProcessorEx((UINT64 *)v44);
        while( CurrentThread->Tcb.ThreadLock );
        v39 = v38->SchedulerAssist;
        if( v39 && v38->NestingLevel <= 1u )
        {
          v40 = v39[6] + 1;
          v39[6] = v40;
        }
      }
      if( (_KPRIQUEUE *)CurrentThread->Tcb.Queue == Queue )
      {
        CurrentThread->Tcb.Queue = 0i64;
        Flink = CurrentThread->Tcb.QueueListEntry.Flink;
        Blink = CurrentThread->Tcb.QueueListEntry.Blink;
        if( Flink->Blink != &CurrentThread->Tcb.QueueListEntry || Blink->Flink != &CurrentThread->Tcb.QueueListEntry )
LABEL_71:
          __fastfail(3u);
        Blink->Flink = Flink;
        Flink->Blink = Blink;
      }
      KiReleaseThreadLockSafe((INT64)CurrentThread);
      v21 = 128i64;
      goto LABEL_24;
    }
    v15 = v9;
    if( v11 == DueTimeRelative )
    {
      KiQueryUnbiasedInterruptTime(0);
      LODWORD(v15) = KiGetDueTimeWithThreadTimerDelay((INT64)CurrentThread, (unsigned int)(v36 + 2), v9, 0i64);
    }
    else
    {
      if( v11 == DueTimeNone )
        goto LABEL_13;
      if( !v9 )
        goto LABEL_42;
      v37 = *(_QWORD *)&KUSER_SHARED_DATA.SystemTime.LowPart;
    }
    if( v37 > v15 )
    {
LABEL_42:
      v21 = 258i64;
LABEL_24:
      _InterlockedAnd((volatile signed __int32 *)Queue, 0xFFFFFF7F);
      CurrentThread->Tcb.WaitReason = 0;
      v24 = KeGetCurrentPrcb();
      if( v24->DeferredReadyListHead.Next )
        KiProcessThreadWaitList(v24, AdjustUnwait, 0i64, 2ui64);
      KiFastExitThreadWait(v24, &CurrentThread->Tcb, 1u);
      return(_LIST_ENTRY *)v21;
    }
LABEL_13:
    v16 = KeGetCurrentPrcb();
    HIDWORD(SpinCount) = 0;
    while( _interlockedbittestandset64((volatile signed __int32 *)&CurrentThread->Tcb.ThreadLock, 0i64) )
    {
      do
        KeYieldProcessorEx((UINT64 *)((char *)&SpinCount + 4));
      while( CurrentThread->Tcb.ThreadLock );
      v32 = v16->SchedulerAssist;
      if( v32 )
      {
        if( v16->NestingLevel <= 1u )
          ++v32[6];
      }
    }
    v5 = 0;
    v17 = (unsigned __int8)CurrentThread->Tcb.QueuePriority;
    CurrentThread->Tcb.QueuePriority = (unsigned __int8)CurrentThread->Tcb.QueuePriority | 0x100;
    _InterlockedDecrement((volatile signed __int32 *)Queue + v17 + 134);
    CurrentThread->Tcb.ThreadLock = 0i64;
    v18 = *((_QWORD *)Queue + 1);
    v19 = (_LIST_ENTRY *)((char *)Queue + 8);
    if( *(_KPRIQUEUE **)(v18 + 8) != (_KPRIQUEUE *)((char *)Queue + 8) )
      goto LABEL_71;
    v10->WaitBlock[0].WaitListEntry.Flink = (_LIST_ENTRY *)v18;
    CurrentThread->Tcb.WaitBlock[0].WaitListEntry.Blink = v19;
    *(_QWORD *)(v18 + 8) = v10;
    v19->Flink = (_LIST_ENTRY *)v10;
    _InterlockedAnd((volatile signed __int32 *)Queue, 0xFFFFFF7F);
    CurrentThread->Tcb.WaitBlockCount = 1;
    KiCommitThreadWait(CurrentThread, CurrentThread->Tcb.WaitBlock, v11, v9);
    CurrentThread->Tcb.WaitReason = 0;
    if( result != (_LIST_ENTRY *)256 )
      return result;
    v31 = KeGetCurrentIrql();
    __writecr8(2ui64);
    CurrentThread->Tcb.WaitIrql = v31;
  }
}

Referenced by:

ExpWorkerThread