##// END OF EJS Templates
docs
cin -
r154:2dcdee4c0810 v2
parent child
Show More
@@ -1,4 +1,5
1 namespace Implab.Components {
1 namespace Implab.Components {
2
2 public enum ExecutionState {
3 public enum ExecutionState {
3 Uninitialized,
4 Uninitialized,
4 Initial,
5 Initial,
@@ -4,18 +4,17 using System.Threading;
4
4
5 namespace Implab.Components {
5 namespace Implab.Components {
6 /// <summary>
6 /// <summary>
7 /// Π‘Π°Π·ΠΎΠ²Ρ‹ΠΉ класс для создания ΠΏΡƒΠ»ΠΎΠ² ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ².
7 /// The base class for creating object pools.
8 /// </summary>
8 /// </summary>
9 /// <remarks>
9 /// <remarks>
10 /// <para>ΠŸΡƒΠ» ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² позволяСт ΠΌΠ½ΠΎΠ³ΠΎΠΊΡ€Π°Ρ‚Π½ΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ ΠΎΠ΄ΠΈΠ½ ΠΈ Ρ‚ΠΎΡ‚ΠΆΠ΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚,
10 /// <para>The objects pool is offers frequently requested objects to be reused, this gives
11 /// Ρ‡Ρ‚ΠΎ Π°ΠΊΡ‚ΡƒΠ°Π»ΡŒΠ½ΠΎ для ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ², созданиС ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Ρ‚Ρ€Π΅Π±ΡƒΠ΅Ρ‚ сущСствСнных рСсурсов.
11 /// a gool speed improvement for the 'heavy' objects. To avoid memory overhead the pool uses
12 /// ΠŸΡƒΠ» ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ слабыС ссылки, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π½Π΅ ΠΏΡ€Π΅ΠΏΡΡ‚ΡΡ‚Π²ΠΎΠ²Π°Ρ‚ΡŒ освобоТдСнию
12 /// weak references allowing CG to do it's work. If there are no free objects in the pool
13 /// рСсурсов ΠΈ создаСт Π½ΠΎΠ²Ρ‹Π΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Ρ‹ ΠΏΡ€ΠΈ нСобходимости.</para>
13 /// they are created on demand. </para>
14 /// <para>
14 /// <para>
15 /// НаслСдники Π΄ΠΎΠ»ΠΆΠ½Ρ‹ Ρ€Π΅Π°Π»ΠΈΠ·ΠΎΠ²Ρ‹Π²Π°Ρ‚ΡŒ ΠΌΠ΅Ρ‚ΠΎΠ΄ <see cref="CreateInstance()"/> для создания
15 /// Implementors need to defined a <see cref="CreateInstance()"/> method
16 /// Π½ΠΎΠ²Ρ‹Ρ… экзСмпляров.
17 /// </para>
16 /// </para>
18 /// <para>ΠŸΡƒΠ» ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚ обращСния сразу ΠΈΠ· Π½Π΅ΡΠΊΠΎΠ»ΡŒΠΊΠΈΡ… ΠΏΠΎΡ‚ΠΎΠΊΠΎΠ².</para>
17 /// <para>The instances of this class are thred-safe.</para>
19 /// </remarks>
18 /// </remarks>
20 public abstract class ObjectPool<T> where T : class {
19 public abstract class ObjectPool<T> where T : class {
21 readonly AsyncQueue<WeakReference> m_queue = new AsyncQueue<WeakReference>();
20 readonly AsyncQueue<WeakReference> m_queue = new AsyncQueue<WeakReference>();
@@ -32,11 +31,22 namespace Implab.Components {
32 m_size = size;
31 m_size = size;
33 }
32 }
34
33
34 /// <summary>
35 /// Creates the instance if there are no free ones in the pool.
36 /// </summary>
37 /// <returns>The new instance.</returns>
35 protected abstract T CreateInstance();
38 protected abstract T CreateInstance();
36
39
40 /// <summary>
41 /// Cleanups the instance.
42 /// </summary>
43 /// <param name="instance">The instance to cleanup and prepare it for the next use.</param>
37 protected virtual void CleanupInstance(T instance) {
44 protected virtual void CleanupInstance(T instance) {
38 }
45 }
39
46
47 /// <summary>
48 /// Allocate free instance from the pool or reates a new one.
49 /// </summary>
40 public T Allocate() {
50 public T Allocate() {
41 WeakReference reference;
51 WeakReference reference;
42 while (m_queue.TryDequeue(out reference)) {
52 while (m_queue.TryDequeue(out reference)) {
@@ -49,6 +59,11 namespace Implab.Components {
49 return CreateInstance();
59 return CreateInstance();
50 }
60 }
51
61
62 /// <summary>
63 /// Release the specified instance and returns it to the pool of free instances.
64 /// </summary>
65 /// <param name="instance">The instance to return to the pool.</param>
66 /// <remarks>Before the instance is returned to the pool the <see cref="CleanupInstance(T)"/> is called.</remarks>
52 public void Release(T instance) {
67 public void Release(T instance) {
53 if (m_count < m_size && instance != null) {
68 if (m_count < m_size && instance != null) {
54 Interlocked.Increment(ref m_count);
69 Interlocked.Increment(ref m_count);
General Comments 0
You need to be logged in to leave comments. Login now