# HG changeset patch # User cin # Date 1411991355 -14400 # Node ID 397fe8db0806aca89c7c1094c10875f06d745ce5 # Parent 0363407ee75c3b0abb87eeefba68799d665c2d1b fixed object pool diff -r 0363407ee75c -r 397fe8db0806 Implab/ObjectPool.cs --- a/Implab/ObjectPool.cs Mon Sep 29 05:04:32 2014 +0400 +++ b/Implab/ObjectPool.cs Mon Sep 29 15:49:15 2014 +0400 @@ -9,9 +9,9 @@ readonly int m_size; readonly MTQueue m_queue = new MTQueue(); - volatile bool m_disposed; + bool m_disposed; - volatile int m_count; + int m_count; public ObjectPool(Func factory, Action cleanup, int size) { Safe.ArgumentNotNull(factory, "factory"); @@ -28,21 +28,26 @@ public ObjectPool(Func factory) : this(factory,null,Environment.ProcessorCount+1) { } - public ObjectPoolWrapper Allocate() { + public ObjectPoolWrapper AllocateAuto() { + + return new ObjectPoolWrapper(Allocate(), this); + } + + public T Allocate() { if (m_disposed) throw new ObjectDisposedException(this.ToString()); T instance; if (m_queue.TryDequeue(out instance)) { Interlocked.Decrement(ref m_count); - return instance; } else { instance = m_factory(); } - return new ObjectPoolWrapper(instance, this); + return instance; } public void Release(T instance) { + Thread.MemoryBarrier(); if (m_count < m_size && !m_disposed) { Interlocked.Increment(ref m_count); @@ -55,11 +60,12 @@ // и возможно уже законцена, в таком случае следует извлечь элемент обратно и // освободить его. Если операция освобождения кеша еще не заврешилась, то будет // изъят и освобожден произвольный элемен, что не повлияет на ход всего процесса. - if (m_disposed && m_queue.TryDequeue(out instance)) - Safe.Dispose(instance); + if (m_disposed && m_queue.TryDequeue(out instance) && instance is IDisposable) + ((IDisposable)instance).Dispose() ; } else { - Safe.Dispose(instance); + if (instance is IDisposable) + ((IDisposable)instance).Dispose(); } } @@ -68,7 +74,8 @@ m_disposed = true; T instance; while (m_queue.TryDequeue(out instance)) - Safe.Dispose(instance); + if (instance is IDisposable) + ((IDisposable)instance).Dispose(); } }