| | | 1 | | using System; |
| | | 2 | | using System.Threading; |
| | | 3 | | |
| | | 4 | | namespace SwiftCollections.Pool; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A specialized pool for managing <see cref="SwiftQueue{T}"/> instances, |
| | | 8 | | /// providing efficient reuse to minimize memory allocations and improve performance. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <typeparam name="T">The type of elements in the queue.</typeparam> |
| | | 11 | | public sealed class SwiftQueuePool<T> : SwiftCollectionPool<SwiftQueue<T>, T>, IDisposable |
| | | 12 | | { |
| | | 13 | | #region Singleton Instance |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Shared instance of the queue pool, providing a globally accessible pool. |
| | | 17 | | /// Uses <see cref="SwiftLazyDisposable{T}"/> to ensure lazy initialization and proper disposal. |
| | | 18 | | /// </summary> |
| | 1 | 19 | | private readonly static SwiftLazyDisposable<SwiftQueuePool<T>> _lazyInstance = |
| | 2 | 20 | | new(() => new SwiftQueuePool<T>(), LazyThreadSafetyMode.ExecutionAndPublication); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the shared instance of the pool. |
| | | 24 | | /// </summary> |
| | 2 | 25 | | public static SwiftQueuePool<T> Shared => _lazyInstance.Value; |
| | | 26 | | |
| | | 27 | | #endregion |
| | | 28 | | |
| | | 29 | | #region Fields |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Tracks whether the pool has been disposed. |
| | | 33 | | /// </summary> |
| | | 34 | | private volatile bool _disposed; |
| | | 35 | | |
| | | 36 | | #endregion |
| | | 37 | | |
| | | 38 | | #region Methods |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Rents a queue instance from the pool. If the pool is empty, a new queue is created. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <returns>A <see cref="SwiftQueue{T}"/> instance.</returns> |
| | | 44 | | public override SwiftQueue<T> Rent() |
| | 4 | 45 | | { |
| | 4 | 46 | | if (_disposed) |
| | 1 | 47 | | throw new ObjectDisposedException(nameof(SwiftQueuePool<T>)); |
| | | 48 | | |
| | 3 | 49 | | return base.Rent(); |
| | 3 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Releases a queue instance back to the pool for reuse. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <param name="queue">The queue to release.</param> |
| | | 56 | | /// <remarks> |
| | | 57 | | /// The queue will be cleared before being returned to the pool to ensure it contains no stale data. |
| | | 58 | | /// </remarks> |
| | | 59 | | public override void Release(SwiftQueue<T> queue) |
| | 4 | 60 | | { |
| | 4 | 61 | | if (_disposed) |
| | 1 | 62 | | throw new ObjectDisposedException(nameof(SwiftQueuePool<T>)); |
| | | 63 | | |
| | 4 | 64 | | if (queue == null) return; |
| | | 65 | | |
| | | 66 | | // SwiftCollectionPool clears pool before releasing |
| | 2 | 67 | | base.Release(queue); |
| | 3 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Clears all pooled queues from the pool. |
| | | 72 | | /// </summary> |
| | | 73 | | public override void Clear() |
| | 4 | 74 | | { |
| | 5 | 75 | | if (_disposed) return; |
| | | 76 | | |
| | 3 | 77 | | base.Clear(); |
| | 4 | 78 | | } |
| | | 79 | | |
| | | 80 | | #endregion |
| | | 81 | | |
| | | 82 | | #region IDisposable Implementation |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Releases all resources used by the SwiftQueuePool. |
| | | 86 | | /// It is important to call Dispose() to release pooled objects, preventing potential memory leaks. |
| | | 87 | | /// </summary> |
| | | 88 | | public void Dispose() |
| | 4 | 89 | | { |
| | 4 | 90 | | if (_disposed) |
| | 1 | 91 | | return; |
| | | 92 | | |
| | 3 | 93 | | Clear(); |
| | 3 | 94 | | base.Flush(); |
| | | 95 | | |
| | 3 | 96 | | _disposed = true; |
| | | 97 | | |
| | | 98 | | // Suppress finalization to prevent unnecessary GC overhead |
| | 3 | 99 | | GC.SuppressFinalize(this); |
| | 4 | 100 | | } |
| | | 101 | | |
| | 2 | 102 | | ~SwiftQueuePool() => Dispose(); |
| | | 103 | | |
| | | 104 | | #endregion |
| | | 105 | | } |