Class SafeObjectPool<T>
A fast, dynamically-sized and thread-safe object pool to store objects.
This pool can be configured to automatically call IDisposable of elements that are free (for example during trimming, when pool is full or when the pool is disposed itself).
public sealed class SafeObjectPool<T> : ObjectPool<T>, IDisposable
Type Parameters
TType of object to pool
- Inheritance
-
ObjectPool<T>SafeObjectPool<T>
- Implements
- Inherited Members
Constructors
SafeObjectPool(Func<T>)
Creates a pool of objects.
public SafeObjectPool(Func<T> factory)
Parameters
factoryFunc<T>Delegate used to construct instances of the pooled objects.
For a default delegate use CreateDefault<T>() or get_Factory<T>().
Exceptions
- ArgumentNullException
Thrown when
factoryis null.
- See Also
-
CreateDefault<T>()get_Factory<T>()
Properties
Capacity
Capacity of the pool.
This region of the pool support concurrent access.
The capacity should preferably be not greater than ProcessorCount * 2, since it's fully iterated before accessing the reserve.
public int Capacity { get; init; }
Property Value
Exceptions
- ArgumentOutOfRangeException
Throw when value is lower than
1.
Factory
Delegate which instantiates new objects.
public Func<T> Factory { get; }
Property Value
- Func<T>
FreeCallback
If this value is not null, the callback will be executed on each element which is free from the pool.
That is, it will be called in elements not being stored during Return(T), or during elements free by Trim(bool) or its automatic cleaning.
If no value is specified, by default it will include a callback which executes Dispose() on elements which can be casted to IDisposable.
public Action<T>? FreeCallback { get; init; }
Property Value
- Action<T>
Remarks
If no value is specified, by default it will include a callback, but we actually don't call it.
Instead we run the behaviour inline. This is to avoid the delegate call.
IsReserveFixed
Determines if the reserve pool is not allowed to grow nor shrink given its usage.
public bool IsReserveFixed { get; init; }
Property Value
Reserve
Current capacity of the reserve.
This reserve pool is only acceded when the non-reserve capacity gets full or empty.
This is because this region can only be acceded by a single thread
This pool has a dynamic size so this value represent the initial size of the pool which may enlarge or shrink over time.
public int Reserve { get; init; }
Property Value
Exceptions
- ArgumentOutOfRangeException
Throw when value is negative.
Methods
ApproximateCount()
Gets an approximate count of the objects stored in the pool.
This value is not accurate and may be lower or higher than the actual count.
This is primarily used for debugging purposes.
public override int ApproximateCount()
Returns
- int
Approximate count of elements in the pool. If this operation is not supported, return
-1instead of throwing.
Dispose()
Calls the Dispose() of all the pooled elements.
public void Dispose()
~SafeObjectPool()
Calls the Dispose() of all the pooled elements.
protected ~SafeObjectPool()
Rent()
Rent an element from the pool.
If the pool is empty, instantiate a new element.
Implementors of this class can choose how elements are instantiated and initialized, or throw if instantiation of new elements is not supported.
public override T Rent()
Returns
- T
Rented element.
Return(T)
Return rented object to pool.
public override void Return(T element)
Parameters
elementTObject to return.
Trim(bool)
Trim the content of the pool.
public override void Trim(bool force = false)
Parameters
forceboolIf true, the pool is forced to clear all elements inside. Otherwise, the pool may only clear partially or not clear at all if the heuristic says so.