@@ -9,9 +9,9 namespace Implab { | |||
|
9 | 9 | readonly int m_size; |
|
10 | 10 | readonly MTQueue<T> m_queue = new MTQueue<T>(); |
|
11 | 11 | |
|
12 |
|
|
|
12 | bool m_disposed; | |
|
13 | 13 | |
|
14 |
|
|
|
14 | int m_count; | |
|
15 | 15 | |
|
16 | 16 | public ObjectPool(Func<T> factory, Action<T> cleanup, int size) { |
|
17 | 17 | Safe.ArgumentNotNull(factory, "factory"); |
@@ -28,21 +28,26 namespace Implab { | |||
|
28 | 28 | public ObjectPool(Func<T> factory) : this(factory,null,Environment.ProcessorCount+1) { |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | public ObjectPoolWrapper<T> Allocate() { | |
|
31 | public ObjectPoolWrapper<T> AllocateAuto() { | |
|
32 | ||
|
33 | return new ObjectPoolWrapper<T>(Allocate(), this); | |
|
34 | } | |
|
35 | ||
|
36 | public T Allocate() { | |
|
32 | 37 | if (m_disposed) |
|
33 | 38 | throw new ObjectDisposedException(this.ToString()); |
|
34 | 39 | |
|
35 | 40 | T instance; |
|
36 | 41 | if (m_queue.TryDequeue(out instance)) { |
|
37 | 42 | Interlocked.Decrement(ref m_count); |
|
38 | return instance; | |
|
39 | 43 | } else { |
|
40 | 44 | instance = m_factory(); |
|
41 | 45 | } |
|
42 |
return |
|
|
46 | return instance; | |
|
43 | 47 | } |
|
44 | 48 | |
|
45 | 49 | public void Release(T instance) { |
|
50 | Thread.MemoryBarrier(); | |
|
46 | 51 | if (m_count < m_size && !m_disposed) { |
|
47 | 52 | Interlocked.Increment(ref m_count); |
|
48 | 53 | |
@@ -55,11 +60,12 namespace Implab { | |||
|
55 | 60 | // и возможно уже законцена, в таком случае следует извлечь элемент обратно и |
|
56 | 61 | // освободить его. Если операция освобождения кеша еще не заврешилась, то будет |
|
57 | 62 | // изъят и освобожден произвольный элемен, что не повлияет на ход всего процесса. |
|
58 | if (m_disposed && m_queue.TryDequeue(out instance)) | |
|
59 |
|
|
|
63 | if (m_disposed && m_queue.TryDequeue(out instance) && instance is IDisposable) | |
|
64 | ((IDisposable)instance).Dispose() ; | |
|
60 | 65 | |
|
61 | 66 | } else { |
|
62 |
|
|
|
67 | if (instance is IDisposable) | |
|
68 | ((IDisposable)instance).Dispose(); | |
|
63 | 69 | } |
|
64 | 70 | } |
|
65 | 71 | |
@@ -68,7 +74,8 namespace Implab { | |||
|
68 | 74 | m_disposed = true; |
|
69 | 75 | T instance; |
|
70 | 76 | while (m_queue.TryDequeue(out instance)) |
|
71 |
|
|
|
77 | if (instance is IDisposable) | |
|
78 | ((IDisposable)instance).Dispose(); | |
|
72 | 79 | } |
|
73 | 80 | } |
|
74 | 81 |
General Comments 0
You need to be logged in to leave comments.
Login now