/// <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; }
/// <summary> /// Detaches a <paramref name="pooledValue"/> from the pool. /// </summary> /// <param name="pooledValue">The pooled value.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public TValue DetachPooledValue(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 detached from it."); } if (pooledValue.HasBeenReleasedBackToPool) { throw new ArgumentOutOfRangeException(nameof(pooledValue), "Pooled values that have already been released back and returned to the pool can no longer be detached from the pool."); } if (pooledValue.HasBeenDetachedFromPool) { throw new ArgumentOutOfRangeException(nameof(pooledValue), "Detached pooled values cannot be detached a second time from the pool."); } // else cancellationToken.ThrowIfCancellationRequested(); var result = pooledValue.Value; pooledValue.HasBeenDetachedFromPool = true; Interlocked.Decrement(ref _totalInstancesCount); RaisePropertyChanged(nameof(TotalInstancesCount)); return(result); }