@@ -1,85 +1,92 | |||||
1 | using System; |
|
1 | using System; | |
2 | using Implab.Parallels; |
|
2 | using Implab.Parallels; | |
3 | using System.Threading; |
|
3 | using System.Threading; | |
4 |
|
4 | |||
5 | namespace Implab { |
|
5 | namespace Implab { | |
6 | public class ObjectPool<T> : IDisposable { |
|
6 | public class ObjectPool<T> : IDisposable { | |
7 | readonly Func<T> m_factory; |
|
7 | readonly Func<T> m_factory; | |
8 | readonly Action<T> m_cleanup; |
|
8 | readonly Action<T> m_cleanup; | |
9 | readonly int m_size; |
|
9 | readonly int m_size; | |
10 | readonly MTQueue<T> m_queue = new MTQueue<T>(); |
|
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 | public ObjectPool(Func<T> factory, Action<T> cleanup, int size) { |
|
16 | public ObjectPool(Func<T> factory, Action<T> cleanup, int size) { | |
17 | Safe.ArgumentNotNull(factory, "factory"); |
|
17 | Safe.ArgumentNotNull(factory, "factory"); | |
18 | Safe.ArgumentInRange(size, 1, size, "size"); |
|
18 | Safe.ArgumentInRange(size, 1, size, "size"); | |
19 |
|
19 | |||
20 | m_factory = factory; |
|
20 | m_factory = factory; | |
21 | m_cleanup = cleanup; |
|
21 | m_cleanup = cleanup; | |
22 | m_size = size; |
|
22 | m_size = size; | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | public ObjectPool(Func<T> factory, Action<T> cleanup) : this(factory,cleanup,Environment.ProcessorCount+1) { |
|
25 | public ObjectPool(Func<T> factory, Action<T> cleanup) : this(factory,cleanup,Environment.ProcessorCount+1) { | |
26 | } |
|
26 | } | |
27 |
|
27 | |||
28 | public ObjectPool(Func<T> factory) : this(factory,null,Environment.ProcessorCount+1) { |
|
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 | if (m_disposed) |
|
37 | if (m_disposed) | |
33 | throw new ObjectDisposedException(this.ToString()); |
|
38 | throw new ObjectDisposedException(this.ToString()); | |
34 |
|
39 | |||
35 | T instance; |
|
40 | T instance; | |
36 | if (m_queue.TryDequeue(out instance)) { |
|
41 | if (m_queue.TryDequeue(out instance)) { | |
37 | Interlocked.Decrement(ref m_count); |
|
42 | Interlocked.Decrement(ref m_count); | |
38 | return instance; |
|
|||
39 | } else { |
|
43 | } else { | |
40 | instance = m_factory(); |
|
44 | instance = m_factory(); | |
41 | } |
|
45 | } | |
42 |
return |
|
46 | return instance; | |
43 | } |
|
47 | } | |
44 |
|
48 | |||
45 | public void Release(T instance) { |
|
49 | public void Release(T instance) { | |
|
50 | Thread.MemoryBarrier(); | |||
46 | if (m_count < m_size && !m_disposed) { |
|
51 | if (m_count < m_size && !m_disposed) { | |
47 | Interlocked.Increment(ref m_count); |
|
52 | Interlocked.Increment(ref m_count); | |
48 |
|
53 | |||
49 | if (m_cleanup != null) |
|
54 | if (m_cleanup != null) | |
50 | m_cleanup(instance); |
|
55 | m_cleanup(instance); | |
51 |
|
56 | |||
52 | m_queue.Enqueue(instance); |
|
57 | m_queue.Enqueue(instance); | |
53 |
|
58 | |||
54 | // ΠΏΠΎΠΊΠ° ΡΠ»Π΅ΠΌΠ΅Π½Ρ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π»ΡΡ Π² ΠΊΠ΅Ρ, Π±ΡΠ»Π° Π½Π°ΡΠ°ΡΠ° ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΡ Π²ΡΠ΅Π³ΠΎ ΠΊΠ΅ΡΠ° |
|
59 | // ΠΏΠΎΠΊΠ° ΡΠ»Π΅ΠΌΠ΅Π½Ρ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π»ΡΡ Π² ΠΊΠ΅Ρ, Π±ΡΠ»Π° Π½Π°ΡΠ°ΡΠ° ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΡ Π²ΡΠ΅Π³ΠΎ ΠΊΠ΅ΡΠ° | |
55 | // ΠΈ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΡΠΆΠ΅ Π·Π°ΠΊΠΎΠ½ΡΠ΅Π½Π°, Π² ΡΠ°ΠΊΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΈΠ·Π²Π»Π΅ΡΡ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΎΠ±ΡΠ°ΡΠ½ΠΎ ΠΈ |
|
60 | // ΠΈ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΡΠΆΠ΅ Π·Π°ΠΊΠΎΠ½ΡΠ΅Π½Π°, Π² ΡΠ°ΠΊΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΈΠ·Π²Π»Π΅ΡΡ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΎΠ±ΡΠ°ΡΠ½ΠΎ ΠΈ | |
56 | // ΠΎΡΠ²ΠΎΠ±ΠΎΠ΄ΠΈΡΡ Π΅Π³ΠΎ. ΠΡΠ»ΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΡ ΠΊΠ΅ΡΠ° Π΅ΡΠ΅ Π½Π΅ Π·Π°Π²ΡΠ΅ΡΠΈΠ»Π°ΡΡ, ΡΠΎ Π±ΡΠ΄Π΅Ρ |
|
61 | // ΠΎΡΠ²ΠΎΠ±ΠΎΠ΄ΠΈΡΡ Π΅Π³ΠΎ. ΠΡΠ»ΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΡ ΠΊΠ΅ΡΠ° Π΅ΡΠ΅ Π½Π΅ Π·Π°Π²ΡΠ΅ΡΠΈΠ»Π°ΡΡ, ΡΠΎ Π±ΡΠ΄Π΅Ρ | |
57 | // ΠΈΠ·ΡΡΡ ΠΈ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ»ΡΠ½ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½, ΡΡΠΎ Π½Π΅ ΠΏΠΎΠ²Π»ΠΈΡΠ΅Ρ Π½Π° Ρ ΠΎΠ΄ Π²ΡΠ΅Π³ΠΎ ΠΏΡΠΎΡΠ΅ΡΡΠ°. |
|
62 | // ΠΈΠ·ΡΡΡ ΠΈ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ»ΡΠ½ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½, ΡΡΠΎ Π½Π΅ ΠΏΠΎΠ²Π»ΠΈΡΠ΅Ρ Π½Π° Ρ ΠΎΠ΄ Π²ΡΠ΅Π³ΠΎ ΠΏΡΠΎΡΠ΅ΡΡΠ°. | |
58 | if (m_disposed && m_queue.TryDequeue(out instance)) |
|
63 | if (m_disposed && m_queue.TryDequeue(out instance) && instance is IDisposable) | |
59 |
|
|
64 | ((IDisposable)instance).Dispose() ; | |
60 |
|
65 | |||
61 | } else { |
|
66 | } else { | |
62 |
|
|
67 | if (instance is IDisposable) | |
|
68 | ((IDisposable)instance).Dispose(); | |||
63 | } |
|
69 | } | |
64 | } |
|
70 | } | |
65 |
|
71 | |||
66 | protected virtual void Dispose(bool disposing) { |
|
72 | protected virtual void Dispose(bool disposing) { | |
67 | if (disposing) { |
|
73 | if (disposing) { | |
68 | m_disposed = true; |
|
74 | m_disposed = true; | |
69 | T instance; |
|
75 | T instance; | |
70 | while (m_queue.TryDequeue(out instance)) |
|
76 | while (m_queue.TryDequeue(out instance)) | |
71 |
|
|
77 | if (instance is IDisposable) | |
|
78 | ((IDisposable)instance).Dispose(); | |||
72 | } |
|
79 | } | |
73 | } |
|
80 | } | |
74 |
|
81 | |||
75 | #region IDisposable implementation |
|
82 | #region IDisposable implementation | |
76 |
|
83 | |||
77 | public void Dispose() { |
|
84 | public void Dispose() { | |
78 | Dispose(true); |
|
85 | Dispose(true); | |
79 | GC.SuppressFinalize(this); |
|
86 | GC.SuppressFinalize(this); | |
80 | } |
|
87 | } | |
81 |
|
88 | |||
82 | #endregion |
|
89 | #endregion | |
83 | } |
|
90 | } | |
84 | } |
|
91 | } | |
85 |
|
92 |
General Comments 0
You need to be logged in to leave comments.
Login now