예제 #1
0
        /// <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);
        }
예제 #2
0
        ///<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);
        }
예제 #3
0
        ///<inheritdoc/>
        public override void Clear()
        {
            if (PoolPolicy.ActionOnDestroy != null)
            {
                foreach (var item in _stack)
                {
                    PoolPolicy.ActionOnDestroy(item);
                }
            }

            _stack.Clear();
            _countAll = 0;
        }
예제 #4
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;
        }
예제 #5
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);
        }
예제 #6
0
 ///<inheritdoc/>
 public LinkedPool(PoolPolicy <T> poolPolicy) : base(poolPolicy)
 {
 }
예제 #7
0
 ///<inheritdoc/>
 public ObjectPoolConcurrent(PoolPolicy <T> poolPolicy) : base(poolPolicy)
 {
     _stack = new ConcurrentStack <T>();
 }
예제 #8
0
 /// <inheritdoc/>
 public ObjectPoolSingleThread(PoolPolicy <T> poolPolicy) : base(poolPolicy)
 {
     _stack = new Stack <T>(poolPolicy.InitialPoolSize);
 }
예제 #9
0
 /// <summary>
 /// Creates a new object pool.
 /// </summary>
 protected ObjectPool(PoolPolicy <T> poolPolicy)
 {
     PoolPolicy = poolPolicy;
 }