KeWaitForGate

VOID __fastcall KeWaitForGate(_KGATE *Gate, _KWAIT_REASON WaitReason, CHAR WaitMode){
  _ETHREAD *CurrentThread; 
  _KGATE *v6; 
  unsigned __int8 CurrentIrql; 
  int v8; 
  _KGATE **v9; 
  int v10; 

  CurrentThread = (_ETHREAD *)KeGetCurrentThread();
  v6 = (_KGATE *)&CurrentThread->Tcb.320;
  do
  {
    CurrentIrql = KeGetCurrentIrql();
    __writecr8(2ui64);
    CurrentThread->Tcb.WaitIrql = CurrentIrql;
    KiBeginThreadWait(CurrentThread, 0, WaitReason, 0);
    if( v8 )
      break;
    CurrentThread->Tcb.WaitBlock[0].WaitType = 1;
    CurrentThread->Tcb.WaitBlockFill4[17] = 4;
    CurrentThread->Tcb.WaitBlock[0].WaitKey = 0;
    CurrentThread->Tcb.WaitBlock[0].Object = Gate;
    KiAcquireKobjectLockSafe(Gate);
    if( *((_DWORD *)Gate + 1) )
    {
      *((_DWORD *)Gate + 1) = 0;
      _InterlockedAnd((volatile signed __int32 *)Gate, 0xFFFFFF7F);
      KiFastExitThreadWait(KeGetCurrentPrcb(), &CurrentThread->Tcb, 0);
      return;
    }
    v9 = (_KGATE **)*((_QWORD *)Gate + 2);
    if( *v9 != (_KGATE *)((char *)Gate + 8) )
      __fastfail(3u);
    *(_QWORD *)v6 = (char *)Gate + 8;
    CurrentThread->Tcb.WaitBlock[0].WaitListEntry.Blink = (_LIST_ENTRY *)v9;
    *v9 = v6;
    *((_QWORD *)Gate + 2) = v6;
    _InterlockedAnd((volatile signed __int32 *)Gate, 0xFFFFFF7F);
    CurrentThread->Tcb.WaitBlockCount = 1;
    KiCommitThreadWait(CurrentThread, CurrentThread->Tcb.WaitBlock, DueTimeNone, 0i64);
  }
  while( v10 == 256 );
}

Referenced by:

KiEpfStart
KiExecuteDpc
MiAttemptSectionDelete
MiChangingSubsectionProtos
MiCheckPurgeAndUpMapCount
MiDrainControlAreaWrites
MiFreeUnusedPfnPages
MiIncrementAweMapCount
MiIncrementLargeSubsections
MiLockControlAreaSectionExtend
MiMappedPageWriter
MiMarkSessionDeletePending
MiReferenceControlArea
MiTryUnlinkNodeLargePage
MiUnlinkNodeLargePages
MiUnlinkWorkingSet
MiWaitForExtentDeletions
MiWaitForPageWriteCompletion
MiWaitForVadDeletion
MiZeroBootLargePages
MiZeroInParallel
MiZeroNodePages
MmOutSwapProcess
PspGetContextThreadInternal
PspQueryWorkingSetWatch
PspRundownUmsThreadForApcDelivery
PspSetContextThreadInternal