##// END OF EJS Templates
Added the chaining method to the non-generic IPromise
Added the chaining method to the non-generic IPromise

File last commit:

r91:cdaaf4792c22 v2
r96:daffa72a1cec v2
Show More
ObjectPool.cs
90 lines | 3.0 KiB | text/x-csharp | CSharpLexer
cin
added object pool
r82 using System;
using Implab.Parallels;
using System.Threading;
cin
fixed JSONXmlReader disposing under ugly mono...
r85 using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
cin
added object pool
r82
namespace Implab {
cin
fixed JSONXmlReader disposing under ugly mono...
r85 public abstract class ObjectPool<T> : IDisposable {
cin
added object pool
r82 readonly int m_size;
readonly MTQueue<T> m_queue = new MTQueue<T>();
cin
fixed JSONXmlReader disposing under ugly mono...
r85 [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
static readonly bool _isValueType = typeof(T).IsValueType;
cin
fixed object pool
r83 bool m_disposed;
cin
added object pool
r82
cin
fixed object pool
r83 int m_count;
cin
added object pool
r82
cin
fixed JSONXmlReader disposing under ugly mono...
r85 protected ObjectPool(int size) {
cin
added object pool
r82 m_size = size;
}
cin
fixed JSONXmlReader disposing under ugly mono...
r85 protected ObjectPool() : this(Environment.ProcessorCount+1) {
cin
added object pool
r82 }
cin
fixed object pool
r83 public T Allocate() {
cin
added object pool
r82 if (m_disposed)
cin
fixed JSONXmlReader disposing under ugly mono...
r85 throw new ObjectDisposedException(ToString());
cin
added object pool
r82
T instance;
if (m_queue.TryDequeue(out instance)) {
Interlocked.Decrement(ref m_count);
} else {
cin
fixed JSONXmlReader disposing under ugly mono...
r85 instance = CreateInstance();
Debug.Assert(!Object.Equals(instance, default(T)) || _isValueType);
cin
added object pool
r82 }
cin
fixed object pool
r83 return instance;
cin
added object pool
r82 }
cin
fixed JSONXmlReader disposing under ugly mono...
r85 protected abstract T CreateInstance();
protected virtual void CleanupInstance(T instance) {
}
cin
added object pool
r82 public void Release(T instance) {
cin
fixed JSONXmlReader disposing under ugly mono...
r85 if ( Object.Equals(instance,default(T)) && !_isValueType)
return;
cin
fixed object pool
r83 Thread.MemoryBarrier();
cin
added object pool
r82 if (m_count < m_size && !m_disposed) {
Interlocked.Increment(ref m_count);
cin
fixed JSONXmlReader disposing under ugly mono...
r85 CleanupInstance(instance);
cin
added object pool
r82
m_queue.Enqueue(instance);
// пока элемент возвращался в кеш, была начата операция освобождения всего кеша
// и возможно уже законцена, в таком случае следует извлечь элемент обратно и
// освободить его. Если операция освобождения кеша еще не заврешилась, то будет
// изъят и освобожден произвольный элемен, что не повлияет на ход всего процесса.
cin
fixed object pool
r83 if (m_disposed && m_queue.TryDequeue(out instance) && instance is IDisposable)
((IDisposable)instance).Dispose() ;
cin
added object pool
r82
} else {
cin
fixed object pool
r83 if (instance is IDisposable)
((IDisposable)instance).Dispose();
cin
added object pool
r82 }
}
protected virtual void Dispose(bool disposing) {
if (disposing) {
m_disposed = true;
T instance;
while (m_queue.TryDequeue(out instance))
cin
fixed object pool
r83 if (instance is IDisposable)
((IDisposable)instance).Dispose();
cin
added object pool
r82 }
}
#region IDisposable implementation
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}