Skip to content
This repository has been archived by the owner on May 21, 2022. It is now read-only.

Commit

Permalink
add pooledObject validate
Browse files Browse the repository at this point in the history
  • Loading branch information
ULiiAn committed Jun 20, 2017
1 parent 5e0b131 commit 838a50c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/CodeProject.ObjectPool/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,24 +177,29 @@ public void Clear()
/// <returns>A monitored object from the pool.</returns>
public T GetObject()
{
if (PooledObjects.TryDequeue(out T pooledObject))
while (true)
{
// Object found in pool.
if (Diagnostics.Enabled) Diagnostics.IncrementPoolObjectHitCount();
}
else
{
// This should not happen normally, but could be happening when there is stress on
// the pool. No available objects in pool, create a new one and return it to the caller.
if (Diagnostics.Enabled) Diagnostics.IncrementPoolObjectMissCount();
pooledObject = CreatePooledObject();
}

// Change the state of the pooled object, marking it as reserved. We will mark it as
// available as soon as the object will return to the pool.
pooledObject.PooledObjectInfo.State = PooledObjectState.Reserved;
if (PooledObjects.TryDequeue(out T pooledObject))
{
// Object found in pool.
if (Diagnostics.Enabled) Diagnostics.IncrementPoolObjectHitCount();
}
else
{
// This should not happen normally, but could be happening when there is stress on
// the pool. No available objects in pool, create a new one and return it to the caller.
if (Diagnostics.Enabled) Diagnostics.IncrementPoolObjectMissCount();
pooledObject = CreatePooledObject();
}
if (pooledObject.ValidateObject())
{
// Change the state of the pooled object, marking it as reserved. We will mark it as
// available as soon as the object will return to the pool.
pooledObject.PooledObjectInfo.State = PooledObjectState.Reserved;

return pooledObject;
return pooledObject;
}
}
}

void IObjectPoolHandle.ReturnObjectToPool(PooledObject objectToReturnToPool, bool reRegisterForFinalization)
Expand Down
9 changes: 9 additions & 0 deletions src/CodeProject.ObjectPool/PooledObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public abstract class PooledObject : EquatableObject<PooledObject>, IDisposable

#region Internal Methods - resource and state management

/// <summary>
/// Validate wraped Object state.
/// </summary>
/// <returns></returns>
internal virtual bool ValidateObject()
{
return true;
}

/// <summary>
/// Releases the object resources. This method will be called by the pool manager when
/// there is no need for this object anymore (decreasing pooled objects count, pool is
Expand Down

0 comments on commit 838a50c

Please sign in to comment.