Esempio n. 1
0
        private PoolInstance <T> Allocate()
        {
            var obj      = ObjectConstructor();
            var instance = new PoolInstance <T>(obj, this);

            return(instance);
        }
Esempio n. 2
0
 private bool TryPop(out PoolInstance <T> instance)
 {
     if (_storage.TryPop(out instance))
     {
         Interlocked.Decrement(ref _currentCount);
         instance.SetStatus(false);
         return(true);
     }
     instance = null;
     return(false);
 }
Esempio n. 3
0
        /// <summary>
        /// Попытка создать, зарегистрировать статус «Вне пула» и вернуть новый экземпляр
        /// </summary>
        /// <returns>true, если операция была успешно выполнена, в противном случае - false</returns>
        protected bool TryAllocatePop(out PoolInstance <T> instance)
        {
            if (_allocSemaphore.TryTake())
            {
                instance = Allocate();
                return(true);
            }

            instance = null;
            return(false);
        }
Esempio n. 4
0
        /// <summary>
        /// Помещает объект обратно в пул.
        /// </summary>
        public void Release(PoolInstance <T> instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (instance.GetStatus(this))
            {
                throw new InvalidOperationException("Указанный объект уже находится в пуле");
            }

            CleanUp(instance.Object);
            Push(instance);
        }
Esempio n. 5
0
 private void Push(PoolInstance <T> instance)
 {
     instance.SetStatus(true);
     _storage.Push(instance);
     Interlocked.Increment(ref _currentCount);
 }