Skip to content

Commit

Permalink
Implement ITrackedDisposable for Pool; improve usage tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Exanite committed Jan 21, 2025
1 parent 29e2e13 commit 84c83bb
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions Pooling/Pool.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using Exanite.Core.Runtime;

namespace Exanite.Core.Pooling
{
public abstract class Pool : IDisposable
public abstract class Pool : ITrackedDisposable
{
public bool IsDisposed { get; protected set; }
public abstract PoolUsageInfo UsageInfo { get; }

public abstract void Dispose();
Expand All @@ -24,8 +26,6 @@ public class Pool<T> : Pool

private PoolUsageInfo usageInfo;

public int MaxInactive { get; private set; }

public override PoolUsageInfo UsageInfo
{
get
Expand All @@ -48,7 +48,7 @@ public Pool(
}

values = new Queue<T>();
MaxInactive = maxInactive;
usageInfo.MaxInactive = maxInactive;

this.create = () =>
{
Expand Down Expand Up @@ -101,7 +101,7 @@ public void Release(T element)
onRelease.Invoke(element);

UpdateUsageInfo();
if (usageInfo.InactiveCount < MaxInactive)
if (usageInfo.InactiveCount < usageInfo.MaxInactive)
{
values.Enqueue(element);
}
Expand All @@ -116,6 +116,7 @@ public void Clear()
{
foreach (var value in values)
{
usageInfo.TotalCount--;
onDestroy.Invoke(value);
}

Expand All @@ -124,15 +125,20 @@ public void Clear()

private void UpdateUsageInfo()
{
usageInfo.MaxInactive = MaxInactive;
usageInfo.InactiveCount = values.Count;
usageInfo.ActiveCount = usageInfo.TotalCount - usageInfo.InactiveCount;
}

public override void Dispose()
{
Clear();
if (IsDisposed)
{
return;
}

IsDisposed = true;

Clear();
Pools.RemovePool(this);
}

Expand Down

0 comments on commit 84c83bb

Please sign in to comment.