KeRemovePriQueue

_LIST_ENTRY *__stdcall KeRemovePriQueue(_KPRIQUEUE *Queue, CHAR WaitMode, UINT8 Alertable, _LARGE_INTEGER *Timeout){
  _ETHREAD *CurrentThread; 
  int v5; 
  LONGLONG QuadPart; 
  _LIST_ENTRY *v10; 
  int v11; 
  unsigned __int8 CurrentIrql; 
  _LIST_ENTRY *result; 
  _KPRIQUEUE *v14; 
  unsigned __int64 DueTimeWithThreadTimerDelay; 
  struct _KPRCB *v16; 
  __int64 v17; 
  _LIST_ENTRY *Flink; 
  _LIST_ENTRY *p_WaitListHead; 
  struct _KPRCB *CurrentPrcb; 
  __int64 v21; 
  int v22; 
  __int64 v23; 
  struct _KPRCB *v24; 
  struct _KPRCB *v25; 
  UINT64 updated; 
  UINT64 v27; 
  INT64 v28; 
  int v29; 
  __int64 v30; 
  unsigned __int8 v31; 
  __int64 v32; 
  __int64 v33; 
  UINT64 UnbiasedInterruptTime; 
  _QWORD *v35; 
  int v36; 
  unsigned __int64 v37; 
  struct _KPRCB *v38; 
  __int64 v39; 
  int v40; 
  __int64 v41; 
  _ETHREAD **v42; 
  UINT64 SpinCount; 
  UINT64 v44[3]; 
  INT64 WorkPriority; 
  CurrentThread = (_ETHREAD *)KeGetCurrentThread();
  v5 = 0;
  LODWORD(WorkPriority) = 0;
  v44[1] = 0i64;
  QuadPart = 0i64;
  v10 = (_LIST_ENTRY *)((char *)CurrentThread + 320);
  v11 = 2;
  if( !_bittestandreset((signed __int32 *)CurrentThread + 29, 2u) )
  {
    CurrentIrql = KeGetCurrentIrql();
    __writecr8(2ui64);
    *((_BYTE *)CurrentThread + 390) = CurrentIrql;
  }
  if( Timeout )
  {
    if( Timeout->HighPart < 0 )
    {
      UnbiasedInterruptTime = KiQueryUnbiasedInterruptTime(0);
      QuadPart = UnbiasedInterruptTime - (*v35 + *((_QWORD *)CurrentThread + 31));
    }
    else
    {
      QuadPart = Timeout->QuadPart;
      v11 = 1;
    }
  }
  else
  {
    v11 = 0;
  }
  while( 1 )
  {
    KiBeginThreadWait(CurrentThread, WaitMode, WrQueue, Alertable);
    result = (_LIST_ENTRY *)(int)result;
    if( (_DWORD)result )
      return result;
    v14 = (_KPRIQUEUE *)*((_QWORD *)CurrentThread + 29);
    if( Queue != v14 )
      KiSwitchPriQueue((INT64)CurrentThread, Queue, &v14->Header);
    *((_BYTE *)CurrentThread + 336) = 3;
    *((_BYTE *)CurrentThread + 337) = 4;
    *((_WORD *)CurrentThread + 169) = 128;
    *((_QWORD *)CurrentThread + 44) = Queue;
    while( _interlockedbittestandset(&Queue->Header.Lock, 7u) )
    {
      do
      {
        ++v5;
        _mm_pause();
      }
      while( (Queue->Header.LockNV & 0x80u) != 0 );
    }
    if( Queue->Header.SignalState )
    {
      CurrentPrcb = KeGetCurrentPrcb();
      LODWORD(SpinCount) = 0;
      while( _interlockedbittestandset64((volatile signed __int32 *)CurrentThread + 16, 0i64) )
      {
        do
          KeYieldProcessorEx(&SpinCount);
        while( *((_QWORD *)CurrentThread + 8) );
        v33 = *((_QWORD *)CurrentPrcb + 4247);
        if( v33 && *((_BYTE *)CurrentPrcb + 32) <= 1u )
          ++*(_DWORD *)(v33 + 24);
      }
      v21 = (__int64)KiAttemptFastRemovePriQueue(
                       Queue,
                       &WorkPriority,
                       (unsigned __int8)*((_DWORD *)CurrentThread + 135));
      if( v21 )
      {
        v22 = WorkPriority;
        v23 = (unsigned __int8)*((_DWORD *)CurrentThread + 135);
        if( (_DWORD)v23 != (_DWORD)WorkPriority )
        {
          v29 = *((_DWORD *)CurrentThread + 135) & 0x100;
          if( !v29 )
          {
            v30 = *((_QWORD *)CurrentThread + 29);
            _InterlockedDecrement((volatile signed __int32 *)(v30 + 4 * v23 + 536));
            _InterlockedIncrement((volatile signed __int32 *)(v30 + 4i64 * (int)WorkPriority + 536));
            v22 = WorkPriority;
          }
          *((_DWORD *)CurrentThread + 135) = v29 | (unsigned __int8)v22;
        }
        *((_BYTE *)CurrentThread + 645) = 0;
        KiSetBasePriorityAndClearDecrement((_KTHREAD *)CurrentThread, &WorkPriority, 0i64);
        if( v22 != *((char *)CurrentThread + 195) )
        {
          v25 = KeGetCurrentPrcb();
          if( *((_BYTE *)v25 + 32) )
          {
            updated = *((_QWORD *)CurrentThread + 9);
          }
          else
          {
            _disable();
            updated = KiUpdateTotalCyclesCurrentThread((__int64)v25, (__int64)CurrentThread, 0i64);
            _enable();
          }
          v27 = updated + KiCyclesPerClockQuantum * (unsigned int)*((unsigned __int8 *)CurrentThread + 651);
          if( (*((_DWORD *)CurrentThread + 30) & 0x20) != 0 )
            _interlockedbittestandreset((volatile signed __int32 *)CurrentThread + 30, 5u);
          v28 = (unsigned int)WorkPriority;
          *((_QWORD *)CurrentThread + 4) = v27;
          KiSetPriorityThread((_KTHREAD *)CurrentThread, 0i64, v28);
        }
        KiReleaseThreadLockSafe((INT64)CurrentThread);
        goto LABEL_24;
      }
      KiReleaseThreadLockSafe((INT64)CurrentThread);
    }
    if( (Queue->Header.Signalling & 1) != 0 )
    {
      v38 = KeGetCurrentPrcb();
      LODWORD(v44[0]) = 0;
      while( _interlockedbittestandset64((volatile signed __int32 *)CurrentThread + 16, 0i64) )
      {
        do
          KeYieldProcessorEx(v44);
        while( *((_QWORD *)CurrentThread + 8) );
        v39 = *((_QWORD *)v38 + 4247);
        if( v39 && *((_BYTE *)v38 + 32) <= 1u )
        {
          v40 = *(_DWORD *)(v39 + 24) + 1;
          *(_DWORD *)(v39 + 24) = v40;
        }
      }
      if( *((_KPRIQUEUE **)CurrentThread + 29) == Queue )
      {
        *((_QWORD *)CurrentThread + 29) = 0i64;
        v41 = *((_QWORD *)CurrentThread + 65);
        v42 = (_ETHREAD **)*((_QWORD *)CurrentThread + 66);
        if( *(_ETHREAD **)(v41 + 8) != (_ETHREAD *)((char *)CurrentThread + 520)
          || *v42 != (_ETHREAD *)((char *)CurrentThread + 520) )
        {
LABEL_71:
          __fastfail(3u);
        }
        *v42 = (_ETHREAD *)v41;
        *(_QWORD *)(v41 + 8) = v42;
      }
      KiReleaseThreadLockSafe((INT64)CurrentThread);
      v21 = 128i64;
      goto LABEL_24;
    }
    DueTimeWithThreadTimerDelay = QuadPart;
    if( v11 == 2 )
    {
      KiQueryUnbiasedInterruptTime(0);
      DueTimeWithThreadTimerDelay = KiGetDueTimeWithThreadTimerDelay(
                                      (INT64)CurrentThread,
                                      (unsigned int)(v36 + 2),
                                      QuadPart,
                                      0i64);
    }
    else
    {
      if( !v11 )
        goto LABEL_13;
      if( !QuadPart )
        goto LABEL_42;
      v37 = *(_QWORD *)&KUSER_SHARED_DATA.SystemTime.LowPart;
    }
    if( v37 > DueTimeWithThreadTimerDelay )
    {
LABEL_42:
      v21 = 258i64;
LABEL_24:
      _InterlockedAnd(&Queue->Header.Lock, 0xFFFFFF7F);
      *((_BYTE *)CurrentThread + 643) = 0;
      v24 = KeGetCurrentPrcb();
      if( *((_QWORD *)v24 + 1441) )
        KiProcessThreadWaitList((struct KPRCB *)v24, 1, 0);
      KiFastExitThreadWait((__int64)v24, (__int64)CurrentThread, 1);
      return(_LIST_ENTRY *)v21;
    }
LABEL_13:
    v16 = KeGetCurrentPrcb();
    HIDWORD(SpinCount) = 0;
    while( _interlockedbittestandset64((volatile signed __int32 *)CurrentThread + 16, 0i64) )
    {
      do
        KeYieldProcessorEx((UINT64 *)((char *)&SpinCount + 4));
      while( *((_QWORD *)CurrentThread + 8) );
      v32 = *((_QWORD *)v16 + 4247);
      if( v32 )
      {
        if( *((_BYTE *)v16 + 32) <= 1u )
          ++*(_DWORD *)(v32 + 24);
      }
    }
    v5 = 0;
    v17 = (unsigned __int8)*((_DWORD *)CurrentThread + 135);
    *((_DWORD *)CurrentThread + 135) = (unsigned __int8)*((_DWORD *)CurrentThread + 135) | 0x100;
    _InterlockedDecrement(&Queue->CurrentCount[v17]);
    *((_QWORD *)CurrentThread + 8) = 0i64;
    Flink = Queue->Header.WaitListHead.Flink;
    p_WaitListHead = &Queue->Header.WaitListHead;
    if( Flink->Blink != &Queue->Header.WaitListHead )
      goto LABEL_71;
    v10->Flink = Flink;
    *((_QWORD *)CurrentThread + 41) = p_WaitListHead;
    Flink->Blink = v10;
    p_WaitListHead->Flink = v10;
    _InterlockedAnd(&Queue->Header.Lock, 0xFFFFFF7F);
    *((_BYTE *)CurrentThread + 587) = 1;
    result = (_LIST_ENTRY *)KiCommitThreadWait(
                              (__int64)CurrentThread,
                              (__int64 *)CurrentThread + 40,
                              v11,
                              QuadPart,
                              0i64);
    *((_BYTE *)CurrentThread + 643) = 0;
    if( result != (_LIST_ENTRY *)256 )
      return result;
    v31 = KeGetCurrentIrql();
    __writecr8(2ui64);
    *((_BYTE *)CurrentThread + 390) = v31;
  }
}

Referenced by:

ExpWorkerThread