private PoolSlot <T> Allocate() { var obj = ObjectConstructor(); var slot = new PoolSlot <T>(obj, this); HoldSlotInObject(obj, slot); return(slot); }
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); }
/// <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); }
/// <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); }
protected sealed override void HoldSlotInObject(T @object, PoolSlot <T> slot) { @object.PoolSlot = slot; }
private void Push(PoolSlot <T> slot) { slot.SetStatus(true); _storage.Push(slot); Interlocked.Increment(ref _currentCount); }
protected virtual void HoldSlotInObject(T @object, PoolSlot <T> slot) { }