Esempio n. 1
0
        /// <summary>
        /// Releases a <paramref name="pooledValue"/> back into the pool.
        /// </summary>
        /// <param name="pooledValue">The pooled value.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public void ReleasePooledValue(Pooled <TValue> pooledValue, CancellationToken cancellationToken = new CancellationToken())
        {
            if (IsDisposed || IsDisposing)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (pooledValue == null)
            {
                throw new ArgumentNullException(nameof(pooledValue));
            }

            if (pooledValue.OwningPool != this)
            {
                throw new ArgumentOutOfRangeException(nameof(pooledValue), "Only pooled values managed by this pool can be released back into the pool.");
            }

            if (pooledValue.HasBeenReleasedBackToPool)
            {
                throw new ArgumentOutOfRangeException(nameof(pooledValue), "Pooled values that have already been released back and returned to the pool cannot be released a second time.");
            }

            if (pooledValue.HasBeenDetachedFromPool)
            {
                throw new ArgumentOutOfRangeException(nameof(pooledValue), "Detached pooled values can no longer be released and returned back into the pool.");
            }

            // else
            cancellationToken.ThrowIfCancellationRequested();

            PooledInstances.Enqueue(pooledValue.Value);
            RaisePropertyChanged(nameof(AvailableInstancesCount));

            pooledValue.HasBeenReleasedBackToPool = true;
        }
Esempio n. 2
0
        /// <summary>
        /// Increases the total size of the pool by the amount specified.
        /// </summary>
        /// <param name="increaseBy">The amount of values to increase the pool by.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public async Task IncreasePoolSizeAsync(int increaseBy = 1, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (IsDisposed || IsDisposing)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (increaseBy < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(increaseBy));
            }

            for (int i = 0; i < increaseBy; i++)
            {
                cancellationToken.ThrowIfCancellationRequested();

                var instance = await Task.Run(() => InstanceBuilder.Invoke(cancellationToken), cancellationToken).ConfigureAwait(false);

                await Task.Run(() =>
                {
                    PooledInstances.Enqueue(instance);
                }, cancellationToken).ConfigureAwait(false);

                Interlocked.Increment(ref _totalInstancesCount);
                RaisePropertyChanged(nameof(TotalInstancesCount));
            }
        }