/// <summary>
        ///		Initializes a new instance of the <see cref="OnTheFlyObjectPool&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="factory">
        ///		The factory delegate to create <typeparamref name="T"/> type instance using <see cref="ObjectPoolConfiguration"/>.
        ///	</param>
        /// <param name="configuration">
        ///		The <see cref="ObjectPoolConfiguration"/> which contains various settings of this object pool.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="factory"/> is <c>null</c>.
        ///		Or <paramref name="configuration"/> is <c>null</c>.
        /// </exception>
        public OnTheFlyObjectPool(Func <ObjectPoolConfiguration, T> factory, ObjectPoolConfiguration configuration)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }

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

            Contract.EndContractBlock();

            this._factory       = factory;
            this._configuration = configuration;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StandardObjectPool&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="factory">
        ///		The factory delegate to create <typeparamref name="T"/> type instance.
        ///	</param>
        /// <param name="configuration">
        ///		The <see cref="ObjectPoolConfiguration"/> which contains various settings of this object pool.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="factory"/> is <c>null</c>.
        /// </exception>
        public StandardObjectPool(Func <T> factory, ObjectPoolConfiguration configuration)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }

            Contract.EndContractBlock();

            var safeConfiguration = (configuration ?? ObjectPoolConfiguration.Default).AsFrozen();

            if (String.IsNullOrWhiteSpace(safeConfiguration.Name))
            {
                this._source = new TraceSource(this.GetType().FullName);
                this._name   = this.GetType().FullName + "@" + this.GetHashCode().ToString("X", CultureInfo.InvariantCulture);
            }
            else
            {
                this._source = new TraceSource(safeConfiguration.Name);
                this._name   = safeConfiguration.Name;
            }

#if !API_SIGNATURE_TEST
            if (configuration == null && this._source.ShouldTrace(StandardObjectPoolTrace.InitializedWithDefaultConfiguration))
            {
                this._source.TraceEvent(
                    StandardObjectPoolTrace.InitializedWithDefaultConfiguration,
                    "Initialized with default configuration. { \"Name\" : \"{0}\", \"Type\" : \"{1}\", \"HashCode\" : 0x{2:X} }",
                    this._name,
                    this.GetType(),
                    this.GetHashCode()
                    );
            }
            else if (this._source.ShouldTrace(StandardObjectPoolTrace.InitializedWithConfiguration))
            {
                this._source.TraceEvent(
                    StandardObjectPoolTrace.InitializedWithConfiguration,
                    "Initialized with specified configuration. { \"Name\" : \"{0}\", \"Type\" : \"{1}\", \"HashCode\" : 0x{2:X}, \"Configuration\" : {3} }",
                    this._name,
                    this.GetType(),
                    this.GetHashCode(),
                    configuration
                    );
            }
#endif
            this._configuration = safeConfiguration;
            this._factory       = factory;
            this._leasesLock    = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
            this._borrowTimeout = safeConfiguration.BorrowTimeout ?? TimeSpan.FromMilliseconds(Timeout.Infinite);
            this._pool          =
                new BlockingCollection <T>(
#if !SILVERLIGHT
                    new ConcurrentStack <T>()
#else
                    new ConcurrentStack <T>()
#endif
                    );

            if (safeConfiguration.MaximumPooled == null)
            {
                this._leases = new BlockingCollection <WeakReference>(new ConcurrentQueue <WeakReference>());
            }
            else
            {
                this._leases = new BlockingCollection <WeakReference>(new ConcurrentQueue <WeakReference>(), safeConfiguration.MaximumPooled.Value);
            }

            for (int i = 0; i < safeConfiguration.MinimumReserved; i++)
            {
                if (!this.AddToPool(factory(), 0))
                {
#if !API_SIGNATURE_TEST
                    this._source.TraceEvent(
                        StandardObjectPoolTrace.FailedToAddPoolInitially,
                        "Failed to add item. {{ \"Name\" : \"{0}\", \"Type\" : \"{1}\", \"HashCode\" : 0x{2:X} }}",
                        this._name,
                        this.GetType(),
                        this.GetHashCode()
                        );
#endif
                }
            }

            this._evictionIntervalMilliseconds = safeConfiguration.EvitionInterval == null ? default(int?) : unchecked (( int )safeConfiguration.EvitionInterval.Value.TotalMilliseconds);
            if (safeConfiguration.MaximumPooled != null &&
                safeConfiguration.MinimumReserved != safeConfiguration.MaximumPooled.GetValueOrDefault() &&
                this._evictionIntervalMilliseconds != null)
            {
                this._evictionTimer = new Timer(this.OnEvictionTimerElapsed, null, this._evictionIntervalMilliseconds.Value, Timeout.Infinite);
            }
            else
            {
                this._evictionTimer = null;
            }
        }