/// <inheritdoc/> public override T Get() { // Always just create a new instance, if pooling is disabled if (!IsPoolingEnabled) { return(PoolPolicy.FunctionOnCreate()); } T element; if (_stack.Count == 0) { element = PoolPolicy.FunctionOnCreate(); _countAll++; return(element); } try { element = _stack.Pop(); } catch { // No element could be taken from stack (emptied by another thread?) element = PoolPolicy.FunctionOnCreate(); Interlocked.Increment(ref _countAll); return(element); } PoolPolicy.ActionOnGet?.Invoke(element); return(element); }
///<inheritdoc/> public override T Get() { // Always just create a new instance, if pooling is disabled if (!IsPoolingEnabled) { return(PoolPolicy.FunctionOnCreate()); } T item; if (PoolFirst == null) { item = PoolPolicy.FunctionOnCreate(); _countAll++; return(item); } var first = PoolFirst; item = first.Value !; PoolFirst = first.PoolNext; // Add the empty node to our pool for reuse and to prevent GC first.PoolNext = NextAvailableListItem; NextAvailableListItem = first; NextAvailableListItem.Value = null; _countInactive--; PoolPolicy.ActionOnGet?.Invoke(item); return(item); }
///<inheritdoc/> public override void Clear() { if (PoolPolicy.ActionOnDestroy != null) { foreach (var item in _stack) { PoolPolicy.ActionOnDestroy(item); } } _stack.Clear(); _countAll = 0; }
///<inheritdoc/> public override void Clear() { if (PoolPolicy.ActionOnDestroy != null) { foreach (var item in GetAllPoolItems().Select(item => item.Value)) { if (item != null) { PoolPolicy.ActionOnDestroy(item); } } } PoolFirst = null; NextAvailableListItem = null; _countInactive = 0; _countAll = 0; }
///<inheritdoc/> public override T Get() { // Always just create a new instance, if pooling is disabled if (!IsPoolingEnabled) { return(PoolPolicy.FunctionOnCreate()); } if (!_stack.TryPop(out var element)) { element = PoolPolicy.FunctionOnCreate(); Interlocked.Increment(ref _countAll); return(element); } PoolPolicy.ActionOnGet?.Invoke(element); return(element); }
///<inheritdoc/> public LinkedPool(PoolPolicy <T> poolPolicy) : base(poolPolicy) { }
///<inheritdoc/> public ObjectPoolConcurrent(PoolPolicy <T> poolPolicy) : base(poolPolicy) { _stack = new ConcurrentStack <T>(); }
/// <inheritdoc/> public ObjectPoolSingleThread(PoolPolicy <T> poolPolicy) : base(poolPolicy) { _stack = new Stack <T>(poolPolicy.InitialPoolSize); }
/// <summary> /// Creates a new object pool. /// </summary> protected ObjectPool(PoolPolicy <T> poolPolicy) { PoolPolicy = poolPolicy; }