Skip to content

Commit

Permalink
reduce number of private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Burak Akgerman committed Aug 12, 2022
1 parent 3b5f196 commit 7a52795
Showing 1 changed file with 6 additions and 36 deletions.
42 changes: 6 additions & 36 deletions src/Shared/Internal/ReEntrantScopeLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ private static bool TryGetLock(HttpContextBase context)
}

// If already locked, return false
if (IsLocked(context))
if (context.Items?.Contains(ReEntrantLock) == true)
{
return false;
}

// Get the lock
Lock(context);
context.Items[ReEntrantLock] = bool.TrueString;

// Indicate the lock was successfully acquired
return true;
Expand All @@ -60,24 +60,9 @@ public void Dispose()
// Only unlock if we were the ones who locked it
if (IsLockAcquired)
{
Unlock(_httpContext);
_httpContext.Items?.Remove(ReEntrantLock);
}
}

private static bool IsLocked(HttpContextBase context)
{
return context.Items?.Contains(ReEntrantLock) == true;
}

private static void Lock(HttpContextBase context)
{
context.Items[ReEntrantLock] = bool.TrueString;
}

private static void Unlock(HttpContextBase context)
{
context.Items?.Remove(ReEntrantLock);
}
#else
private static readonly AsyncLocal<bool> ReEntrantLock = new AsyncLocal<bool>();

Expand All @@ -90,13 +75,13 @@ private static bool TryGetLock(HttpContextBase context)
}

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

// Get the lock
Lock();
ReEntrantLock.Value = true;

// Indicate the lock was successfully acquired
return true;
Expand All @@ -107,24 +92,9 @@ public void Dispose()
// Only unlock if we were the ones who locked it
if (IsLockAcquired)
{
Unlock();
ReEntrantLock.Value = false;
}
}

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

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

private static void Unlock()
{
ReEntrantLock.Value = false;
}
#endif
}
}

0 comments on commit 7a52795

Please sign in to comment.