Beispiel #1
0
        private PoolSlot <T> Allocate()
        {
            var obj  = ObjectConstructor();
            var slot = new PoolSlot <T>(obj, this);

            HoldSlotInObject(obj, slot);
            return(slot);
        }
Beispiel #2
0
 private bool TryPop(out PoolSlot <T> slot)
 {
     if (_storage.TryPop(out slot))
     {
         Interlocked.Decrement(ref _currentCount);
         slot.SetStatus(false);
         return(true);
     }
     slot = null;
     return(false);
 }
Beispiel #3
0
        /// <summary>
        /// Attempts to create, register with status "Out of pool" and return a new instance of <see cref="T"/>
        /// </summary>
        /// <param name="slot"></param>
        /// <returns>true if the operation was successfully, otherwise, false</returns>
        protected bool TryAllocatePop(out PoolSlot <T> slot)
        {
            if (_allocSemaphore.TryTake())
            {
                slot = Allocate();
                return(true);
            }

            slot = null;
            return(false);
        }
Beispiel #4
0
        /// <summary>
        /// Puts the object's slot back to the pool.
        /// </summary>
        /// <param name="slot">The slot to return</param>
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="ArgumentException" />
        /// <exception cref="InvalidOperationException" />
        public void Release(PoolSlot <T> slot)
        {
            if (slot == null)
            {
                throw new ArgumentNullException("slot");
            }
            if (slot.GetStatus(this))
            {
                throw new InvalidOperationException("Specified object is already in the pool");
            }

            CleanUp(slot.Object);
            Push(slot);
        }
Beispiel #5
0
 private void Push(PoolSlot <T> slot)
 {
     slot.SetStatus(true);
     _storage.Push(slot);
     Interlocked.Increment(ref _currentCount);
 }
Beispiel #6
0
 protected virtual void HoldSlotInObject(T @object, PoolSlot <T> slot)
 {
 }
 protected sealed override void HoldSlotInObject(T @object, PoolSlot <T> slot)
 {
     @object.PoolSlot = slot;
 }