Example #1
0
        private IItemStore CreateItemStore(PoolAccessModeEnum mode, int capacity)
        {
            if (mode == PoolAccessModeEnum.FIFO)
            {
                return(new QueueStore(capacity));
            }
            else if (mode == PoolAccessModeEnum.LIFO)
            {
                return(new StackStore(capacity));
            }
            else if (mode == PoolAccessModeEnum.Circular)
            {
                return(new CircularStore(capacity));
            }

            throw new NotSupportedException(string.Format(
                                                "The '{0}' PoolAccessModeEnum encountered in CreateItemStore method is not supported",
                                                mode));
        }
Example #2
0
        /// <summary>
        /// Creates a pool.
        /// </summary>
        ///
        /// <param name="poolSize">
        /// Size of the pool.
        /// </param>
        ///
        /// <param name="factory">
        /// Func used to create PooledObjects.
        /// </param>
        ///
        /// <param name="poolLoadingMode">
        /// Loading mode of the pool.
        /// </param>
        ///
        /// <param name="poolAccessMode">
        /// Access mode of the pool.
        /// </param>
        ///
        /// <param name="initializeFunc">
        /// Func used to initialize pool's intances (if the class implements IPoolableObject).
        /// </param>
        public Pool(int poolSize, Func <Pool <T>, T> factory, PoolLoadingModeEnum poolLoadingMode, PoolAccessModeEnum poolAccessMode, Func <object> initializeFunc = null)
        {
            if (poolSize <= 0)
            {
                throw new ArgumentOutOfRangeException("poolSize", poolSize, "The 'poolSize' value must be greater than zero.");
            }

            if (factory == null)
            {
                throw new ArgumentNullException("_factory");
            }

            _poolSize        = poolSize;
            _poolLoadingMode = poolLoadingMode;

            _factory        = factory;
            _initializeFunc = initializeFunc;

            _sync      = new Semaphore(poolSize, poolSize);
            _itemStore = CreateItemStore(poolAccessMode, poolSize);

            if (poolLoadingMode == PoolLoadingModeEnum.Eager)
            {
                PreloadItems();
            }
        }