public void TestResetWorks()
        {
            UsedElementTracker testInst = new UsedElementTracker(1000);

            Assert.AreEqual(int.MaxValue, testInst.MinFreeElementsCount);

            testInst.UpdateMinFreeElementCount(100);
            Assert.AreEqual(100, testInst.MinFreeElementsCount);

            testInst.Reset();
            Assert.AreEqual(int.MaxValue, testInst.MinFreeElementsCount);
        }
示例#2
0
        /// <summary>
        /// Attempts to create a new element and registers it in pool.
        /// Returns 'null' in case of failure.
        /// </summary>
        /// <param name="timeout">Creation timeout in milliseconds</param>
        /// <param name="token">Cancellation token</param>
        /// <returns>Created element in case of success, otherwise null</returns>
        private PoolElementWrapper <TElem> TryCreateNewElement(int timeout, CancellationToken token)
        {
            if (_disposeCancellation.IsCancellationRequested || token.IsCancellationRequested)
            {
                return(null);
            }
            if (Volatile.Read(ref _reservedCount) >= _maxElementCount)
            {
                return(null);
            }

            _usedElementTracker.Reset();

            PoolElementWrapper <TElem> result = null;

            try
            {
                if (Interlocked.Increment(ref _reservedCount) > _maxElementCount)
                {
                    return(null);
                }

                TElem element = default(TElem);
                if (CreateElement(out element, timeout, token))
                {
                    result = _elementsContainer.Add(element, this, false);
                    result.SetPoolName(this.Name);
                }
                else
                {
                    Profiling.Profiler.ObjectPoolElementFaulted(this.Name, this.ElementCount);
                }
            }
            finally
            {
                if (result == null)
                {
                    Interlocked.Decrement(ref _reservedCount);
                }
            }


            if (result != null)
            {
                Profiling.Profiler.ObjectPoolElementCreated(this.Name, this.ElementCount);
            }

            return(result);
        }