Skip to content

Commit

Permalink
Small refactoring in CyclicDependencyValidator. #956
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnetjunkie committed Sep 28, 2022
1 parent ca8322c commit 48ef4b2
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions src/SimpleInjector/Internals/CyclicDependencyValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,46 +62,44 @@ internal void Reset()

private bool IsCurrentThreadReentering()
{
if (this.enteredThreads != null)
{
int currentThreadId = Environment.CurrentManagedThreadId;
if (this.enteredThreads is null) return false;

foreach (int threadId in this.enteredThreads)
{
if (threadId == currentThreadId) return true;
}
int currentThreadId = Environment.CurrentManagedThreadId;

foreach (int threadId in this.enteredThreads)
{
if (threadId == currentThreadId) return true;
}

return false;
}

private void MarkCurrentThreadAsEntering()
{
int currentThreadId = Environment.CurrentManagedThreadId;

if (this.enteredThreads is null)
{
// Create a list with an array of one. In all but the rarest cases we'll see multiple threads
// entering at the same time.
this.enteredThreads = new(1);
}

this.enteredThreads.Add(currentThreadId);
this.enteredThreads.Add(Environment.CurrentManagedThreadId);
}

private void MarkCurrentThreadAsLeaving()
{
if (this.enteredThreads != null)
{
this.enteredThreads.Remove(Environment.CurrentManagedThreadId);
if (this.enteredThreads is null) return;

if (this.enteredThreads.Count == 0)
{
// Dereference the list. Although this causes the production of way
// more garbage during the warm-up phase when the instance producer
// is depended upon a lot, it ensures that the least amount of memory
// is used when the warmup phase is complete and all expression trees
// have been compiled.
this.enteredThreads = null;
}
this.enteredThreads.Remove(Environment.CurrentManagedThreadId);

if (this.enteredThreads.Count == 0)
{
// Dereference the list. Although this causes the production of way
// more garbage during the warm-up phase when the instance producer
// is depended upon a lot, it ensures that the least amount of memory
// is used when the warmup phase is complete and all expression trees
// have been compiled.
this.enteredThreads = null;
}
}
}
Expand Down

0 comments on commit 48ef4b2

Please sign in to comment.