Mercurial > pub > ImplabNet
comparison Implab/Components/DisposablePool.cs @ 153:b933ec88446e v2
docs
author | cin |
---|---|
date | Fri, 12 Feb 2016 00:59:29 +0300 |
parents | 240aa6994018 |
children | c52691faaf21 |
comparison
equal
deleted
inserted
replaced
152:240aa6994018 | 153:b933ec88446e |
---|---|
2 using Implab.Parallels; | 2 using Implab.Parallels; |
3 using System.Threading; | 3 using System.Threading; |
4 using System.Diagnostics; | 4 using System.Diagnostics; |
5 using System.Diagnostics.CodeAnalysis; | 5 using System.Diagnostics.CodeAnalysis; |
6 | 6 |
7 namespace Implab { | 7 namespace Implab.Components { |
8 /// <summary> | |
9 /// The base class for implementing pools of disposable objects. | |
10 /// </summary> | |
11 /// <remarks> | |
12 /// <para>This class maintains a set of pre-created objects and which are frequently allocated and released | |
13 /// by clients. The pool maintains maximum number of unsued object, any object above this limit is disposed, | |
14 /// if the pool is empty it will create new objects on demand.</para> | |
15 /// <para>Instances of this class are thread-safe.</para> | |
16 /// </remarks> | |
8 public abstract class DisposablePool<T> : IDisposable { | 17 public abstract class DisposablePool<T> : IDisposable { |
9 readonly int m_size; | 18 readonly int m_size; |
10 readonly AsyncQueue<T> m_queue = new AsyncQueue<T>(); | 19 readonly AsyncQueue<T> m_queue = new AsyncQueue<T>(); |
11 | 20 |
12 [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] | 21 [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] |
60 // изъят и освобожден произвольный элемен, что не повлияет на ход всего процесса. | 69 // изъят и освобожден произвольный элемен, что не повлияет на ход всего процесса. |
61 if (m_disposed && m_queue.TryDequeue(out instance) && instance is IDisposable) | 70 if (m_disposed && m_queue.TryDequeue(out instance) && instance is IDisposable) |
62 ((IDisposable)instance).Dispose() ; | 71 ((IDisposable)instance).Dispose() ; |
63 | 72 |
64 } else { | 73 } else { |
65 if (instance is IDisposable) | 74 var disposable = instance as IDisposable; |
66 ((IDisposable)instance).Dispose(); | 75 if (disposable != null) |
76 disposable.Dispose(); | |
67 } | 77 } |
68 } | 78 } |
69 | 79 |
70 protected virtual void Dispose(bool disposing) { | 80 protected virtual void Dispose(bool disposing) { |
71 if (disposing) { | 81 if (disposing) { |
72 m_disposed = true; | 82 m_disposed = true; |
73 T instance; | 83 T instance; |
74 while (m_queue.TryDequeue(out instance)) | 84 while (m_queue.TryDequeue(out instance)) { |
75 if (instance is IDisposable) | 85 var disposable = instance as IDisposable; |
76 ((IDisposable)instance).Dispose(); | 86 if (disposable != null) |
87 disposable.Dispose(); | |
88 } | |
77 } | 89 } |
78 } | 90 } |
79 | 91 |
80 #region IDisposable implementation | 92 #region IDisposable implementation |
81 | 93 |