Skip to content

Commit

Permalink
Split the code to eliminate compiler warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Burak Akgerman committed Aug 12, 2022
1 parent 951a9c8 commit 3b5f196
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/Shared/Internal/ReEntrantScopeLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ internal ReEntrantScopeLock(HttpContextBase context)
// If we were not, we should not unlock in the dispose code
internal bool IsLockAcquired { get; }

#if NET35
private static readonly object ReEntrantLock = new object();

private static bool TryGetLock(HttpContextBase context)
{
// If context is null leave
Expand Down Expand Up @@ -61,9 +64,6 @@ public void Dispose()
}
}

#if NET35
private static readonly object ReEntrantLock = new object();

private static bool IsLocked(HttpContextBase context)
{
return context.Items?.Contains(ReEntrantLock) == true;
Expand All @@ -81,17 +81,47 @@ private static void Unlock(HttpContextBase context)
#else
private static readonly AsyncLocal<bool> ReEntrantLock = new AsyncLocal<bool>();

private static bool IsLocked(HttpContextBase context)
private static bool TryGetLock(HttpContextBase context)
{
// If context is null leave
if (context == null)
{
return false;
}

// If already locked, return false
if (IsLocked())
{
return false;
}

// Get the lock
Lock();

// Indicate the lock was successfully acquired
return true;
}

public void Dispose()
{
// Only unlock if we were the ones who locked it
if (IsLockAcquired)
{
Unlock();
}
}

private static bool IsLocked()
{
return ReEntrantLock.Value;
}

private static void Lock(HttpContextBase context)
private static void Lock()
{
ReEntrantLock.Value = true;
}

private static void Unlock(HttpContextBase context)
private static void Unlock()
{
ReEntrantLock.Value = false;
}
Expand Down

0 comments on commit 3b5f196

Please sign in to comment.