|
3 | 3 |
|
4 | 4 | namespace Open.Disposable |
5 | 5 | { |
6 | | - public sealed class ConcurrentQueueObjectPool<T> : TrimmableObjectPoolBase<T> |
7 | | - where T : class |
8 | | - { |
9 | | - |
10 | | - public ConcurrentQueueObjectPool(Func<T> factory, Action<T> recycler, int capacity = DEFAULT_CAPACITY) |
11 | | - : base(factory, recycler, capacity) |
12 | | - { |
13 | | - Pool = new ConcurrentQueue<T>(); |
14 | | - } |
15 | | - |
16 | | - public ConcurrentQueueObjectPool(Func<T> factory, int capacity = DEFAULT_CAPACITY) |
17 | | - : this(factory, null, capacity) |
18 | | - { |
19 | | - |
20 | | - } |
21 | | - |
22 | | - ConcurrentQueue<T> Pool; |
23 | | - |
24 | | - public override int Count => Pool?.Count ?? 0; |
25 | | - |
26 | | - // For ConcurrentQueue disable using the Pocket. It's fast enough as it is... |
27 | | - protected override bool SaveToPocket(T item) |
28 | | - { |
29 | | - return false; |
30 | | - } |
31 | | - |
32 | | - protected override T TakeFromPocket() |
33 | | - { |
34 | | - return null; |
35 | | - } |
36 | | - |
37 | | - protected override bool Receive(T item) |
38 | | - { |
39 | | - var p = Pool; |
40 | | - if (p == null) return false; |
41 | | - p.Enqueue(item); // It's possible that the count could exceed MaxSize here, but the risk is negligble as a few over the limit won't hurt. |
42 | | - return true; |
43 | | - } |
44 | | - |
45 | | - protected override T TryRelease() |
46 | | - { |
47 | | - var p = Pool; |
48 | | - if (p == null) return null; |
49 | | - p.TryDequeue(out T item); |
50 | | - return item; |
51 | | - } |
52 | | - |
53 | | - protected override void OnDispose(bool calledExplicitly) |
54 | | - { |
55 | | - Pool = null; |
56 | | - } |
57 | | - |
58 | | - } |
59 | | - |
60 | | - public static class ConcurrentQueueObjectPool |
61 | | - { |
62 | | - public static ConcurrentQueueObjectPool<T> Create<T>(Func<T> factory, int capacity = Constants.DEFAULT_CAPACITY) |
63 | | - where T : class |
64 | | - { |
65 | | - return new ConcurrentQueueObjectPool<T>(factory, capacity); |
66 | | - } |
67 | | - |
68 | | - |
69 | | - public static ConcurrentQueueObjectPool<T> Create<T>(int capacity = Constants.DEFAULT_CAPACITY) |
70 | | - where T : class, new() |
71 | | - { |
72 | | - return Create(() => new T(), capacity); |
73 | | - } |
74 | | - |
75 | | - public static ConcurrentQueueObjectPool<T> Create<T>(Func<T> factory, bool autoRecycle, int capacity = Constants.DEFAULT_CAPACITY) |
76 | | - where T : class, IRecyclable |
77 | | - { |
78 | | - Action<T> recycler = null; |
79 | | - if (autoRecycle) recycler = Recycler.Recycle; |
80 | | - return new ConcurrentQueueObjectPool<T>(factory, recycler, capacity); |
81 | | - } |
82 | | - |
83 | | - public static ConcurrentQueueObjectPool<T> Create<T>(bool autoRecycle, int capacity = Constants.DEFAULT_CAPACITY) |
84 | | - where T : class, IRecyclable, new() |
85 | | - { |
86 | | - return Create(() => new T(), autoRecycle, capacity); |
87 | | - } |
88 | | - } |
| 6 | + public sealed class ConcurrentQueueObjectPool<T> : TrimmableObjectPoolBase<T> |
| 7 | + where T : class |
| 8 | + { |
| 9 | + |
| 10 | + public ConcurrentQueueObjectPool(Func<T> factory, Action<T> recycler, int capacity = DEFAULT_CAPACITY) |
| 11 | + : base(factory, recycler, capacity) |
| 12 | + { |
| 13 | + Pool = new ConcurrentQueue<T>(); |
| 14 | + } |
| 15 | + |
| 16 | + public ConcurrentQueueObjectPool(Func<T> factory, int capacity = DEFAULT_CAPACITY) |
| 17 | + : this(factory, null, capacity) |
| 18 | + { |
| 19 | + |
| 20 | + } |
| 21 | + |
| 22 | + ConcurrentQueue<T> Pool; |
| 23 | + |
| 24 | + public override int Count => Pool?.Count ?? 0; |
| 25 | + |
| 26 | + // For ConcurrentQueue disable using the Pocket. It's fast enough as it is... |
| 27 | + protected override bool SaveToPocket(T item) |
| 28 | + { |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + protected override T TakeFromPocket() |
| 33 | + { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + protected override bool Receive(T item) |
| 38 | + { |
| 39 | + var p = Pool; |
| 40 | + if (p == null) return false; |
| 41 | + p.Enqueue(item); // It's possible that the count could exceed MaxSize here, but the risk is negligble as a few over the limit won't hurt. |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + protected override T TryRelease() |
| 46 | + { |
| 47 | + var p = Pool; |
| 48 | + if (p == null) return null; |
| 49 | + p.TryDequeue(out T item); |
| 50 | + return item; |
| 51 | + } |
| 52 | + |
| 53 | + protected override void OnDispose(bool calledExplicitly) |
| 54 | + { |
| 55 | + Pool = null; |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + public static class ConcurrentQueueObjectPool |
| 61 | + { |
| 62 | + public static ConcurrentQueueObjectPool<T> Create<T>(Func<T> factory, int capacity = Constants.DEFAULT_CAPACITY) |
| 63 | + where T : class |
| 64 | + { |
| 65 | + return new ConcurrentQueueObjectPool<T>(factory, capacity); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + public static ConcurrentQueueObjectPool<T> Create<T>(int capacity = Constants.DEFAULT_CAPACITY) |
| 70 | + where T : class, new() |
| 71 | + { |
| 72 | + return Create(() => new T(), capacity); |
| 73 | + } |
| 74 | + |
| 75 | + public static ConcurrentQueueObjectPool<T> Create<T>(Func<T> factory, bool autoRecycle, int capacity = Constants.DEFAULT_CAPACITY) |
| 76 | + where T : class, IRecyclable |
| 77 | + { |
| 78 | + Action<T> recycler = null; |
| 79 | + if (autoRecycle) recycler = Recycler.Recycle; |
| 80 | + return new ConcurrentQueueObjectPool<T>(factory, recycler, capacity); |
| 81 | + } |
| 82 | + |
| 83 | + public static ConcurrentQueueObjectPool<T> Create<T>(bool autoRecycle, int capacity = Constants.DEFAULT_CAPACITY) |
| 84 | + where T : class, IRecyclable, new() |
| 85 | + { |
| 86 | + return Create(() => new T(), autoRecycle, capacity); |
| 87 | + } |
| 88 | + } |
89 | 89 | } |
0 commit comments