PspRevertContainerImpersonation

NTSTATUS __stdcall PspRevertContainerImpersonation(_ETHREAD *CurrentThread){
  _ETHREAD *WorkOnBehalfThread; 
  UINT8 CurrentIrql; 
  struct _KPRCB *CurrentPrcb; 
  struct _KPRCB *v6; 
  _DWORD *SchedulerAssist; 
  UINT64 SpinCount; 

  WorkOnBehalfThread = (_ETHREAD *)CurrentThread->WorkOnBehalfThread;
  if( !WorkOnBehalfThread )
    return -1073741823;
  if( (*(&CurrentThread->Tcb.MiscFlags + 1) & 0x200) != 0 )
  {
    KeGetCurrentIrql();
    __writecr8(2ui64);
    CurrentPrcb = KeGetCurrentPrcb();
    _disable();
    KiUpdateTotalCyclesCurrentThread(CurrentPrcb, &CurrentThread->Tcb, 0i64);
    _enable();
    v6 = KeGetCurrentPrcb();
    LODWORD(SpinCount) = 0;
    while( _interlockedbittestandset64((volatile signed __int32 *)&CurrentPrcb->PrcbLock, 0i64) )
    {
      do
        KeYieldProcessorEx(&SpinCount);
      while( CurrentPrcb->PrcbLock );
      SchedulerAssist = v6->SchedulerAssist;
      if( SchedulerAssist )
      {
        if( v6->NestingLevel <= 1u )
          ++SchedulerAssist[6];
      }
    }
    _interlockedbittestandreset((volatile signed __int32 *)CurrentThread, 0x12u);
    CurrentThread->Tcb.SchedulingGroup = 0i64;
    _interlockedbittestandreset((volatile signed __int32 *)&CurrentThread->Tcb.116 + 1, 9u);
    _InterlockedAnd64((volatile signed __int64 *)&CurrentPrcb->PrcbLock, 0i64);
    __writecr8(0i64);
  }
  CurrentIrql = KeGetCurrentIrql();
  __writecr8(2ui64);
  ExpAcquireSpinLockExclusive(&PspThreadWorkOnBehalfLock, CurrentIrql);
  CurrentThread->WorkOnBehalfThread = 0i64;
  KeClearSystemPriority(CurrentThread);
  LODWORD(PspThreadWorkOnBehalfLock) = 0;
  __writecr8(CurrentIrql);
  EtwTraceThreadWorkOnBehalfUpdate(WorkOnBehalfThread, 0i64);
  ObDereferenceObjectDeferDeleteWithTag((UINT64)WorkOnBehalfThread, 1953394499i64);
  return 0;
}

Referenced by:

IopProcessWorkItem
NtRevertContainerImpersonation
NtSetInformationThread
PsImpersonateContainerOfThread
PspExitThread