﻿// Decompiled with JetBrains decompiler
// Type: System.Web.HttpApplicationStateLock
// Assembly: System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// MVID: 7E68A73E-4066-4F24-AB0A-F147209F50EC
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Web.dll

using System.Runtime;
using System.Web.Util;

namespace System.Web
{
  internal class HttpApplicationStateLock : ReadWriteObjectLock
  {
    private int _recursionCount;
    private int _threadId;

    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    internal HttpApplicationStateLock()
    {
    }

    internal override void AcquireRead()
    {
      if (this._threadId == SafeNativeMethods.GetCurrentThreadId())
        return;
      base.AcquireRead();
    }

    internal override void ReleaseRead()
    {
      if (this._threadId == SafeNativeMethods.GetCurrentThreadId())
        return;
      base.ReleaseRead();
    }

    internal override void AcquireWrite()
    {
      int currentThreadId = SafeNativeMethods.GetCurrentThreadId();
      if (this._threadId == currentThreadId)
      {
        ++this._recursionCount;
      }
      else
      {
        base.AcquireWrite();
        this._threadId = currentThreadId;
        this._recursionCount = 1;
      }
    }

    internal override void ReleaseWrite()
    {
      if (this._threadId != SafeNativeMethods.GetCurrentThreadId() || --this._recursionCount != 0)
        return;
      this._threadId = 0;
      base.ReleaseWrite();
    }

    internal void EnsureReleaseWrite()
    {
      if (this._threadId != SafeNativeMethods.GetCurrentThreadId())
        return;
      this._threadId = 0;
      this._recursionCount = 0;
      base.ReleaseWrite();
    }
  }
}
