Пример #1
0
        /// <summary>
        /// Gets an item from the pool.
        /// </summary>
        /// <remarks>
        /// <para>If the pool is empty when the request is made, a new item is instantiated and returned. Otherwise an instance from the pool will be used.</para>
        /// </remarks>
        /// <returns>Returns an instance of {T} from the pool, or a new instance if the pool is empty.</returns>
        /// <exception cref="ObjectDisposedException">Thrown if the pool has been disposed.</exception>
        public override T Take()
        {
            CheckDisposed();

            T retVal = null;

            if (_PoolItemIndex >= 0)
            {
                retVal = _PoolItems[_PoolItemIndex];
                _PoolItems[_PoolItemIndex] = null;
                _PoolItemIndex--;

                if (retVal != null && PoolPolicy.InitializationPolicy == PooledItemInitialization.Take && PoolPolicy.ReinitializeObject != null)
                {
                    PoolPolicy.ReinitializeObject(retVal);
                }
            }

            if (retVal == null)
            {
                retVal = PoolPolicy.Factory(this);
            }

            return(retVal);
        }
Пример #2
0
        /// <summary>
        /// Expands the pool up by the <paramref name="increment"/> value, but not past it's maximum size, with pre-generated instances.
        /// </summary>
        /// <param name="increment"></param>
        public override void Expand(int increment)
        {
            CheckDisposed();

            if (increment <= 0)
            {
                return;
            }

            int createdCount = 0;

            while (createdCount < increment && !IsPoolFull())
            {
                AddCore(PoolPolicy.Factory(this));
                createdCount++;
            }
        }
Пример #3
0
        /// <summary>
        /// Creates as many new items as specified by <paramref name="increment"/> and adds them to the pool, but not over it's maximum capacity.
        /// </summary>
        /// <param name="increment">The maximum number of items to pre-allocate and add to the pool.</param>
        /// <remarks>
        /// <para>This method is 'thread safe', though it is possible under certain race conditons for the pool to go beyond it's configured maximum size by a few items.</para>
        /// <para>If <paramref name="increment"/> is zero or less the method returns without doing anything</para>
        /// </remarks>
        /// <exception cref="System.ObjectDisposedException">Thrown if this method is called on a disposed pool.</exception>
        public override void Expand(int increment)
        {
            CheckDisposed();

            if (increment <= 0)
            {
                return;
            }

            int createdCount = 0;

            while (createdCount < increment && !IsPoolFull())
            {
                _Pool.Add(PoolPolicy.Factory(this));
                Interlocked.Increment(ref _PoolInstancesCount);
                createdCount++;
            }
        }
Пример #4
0
        /// <summary>
        /// Gets an item from the pool.
        /// </summary>
        /// <remarks>
        /// <para>If the pool is empty when the request is made, a new item is instantiated and returned. Otherwise an instance from the pool will be used.</para>
        /// <para>This method is thread safe.</para>
        /// </remarks>
        /// <returns>Returns an instance of {T} from the pool, or a new instance if the pool is empty.</returns>
        /// <exception cref="ObjectDisposedException">Thrown if the pool has been disposed.</exception>
        public override T Take()
        {
            CheckDisposed();

            T retVal;

            if (_Pool.TryTake(out retVal))
            {
                Interlocked.Decrement(ref _PoolInstancesCount);

                if (PoolPolicy.InitializationPolicy == PooledItemInitialization.Take && PoolPolicy.ReinitializeObject != null)
                {
                    PoolPolicy.ReinitializeObject(retVal);
                }
            }
            else
            {
                retVal = PoolPolicy.Factory(this);
            }

            return(retVal);
        }