changeset 82:0363407ee75c v2

added object pool
author cin
date Mon, 29 Sep 2014 05:04:32 +0400
parents 2c5631b43c7d
children 397fe8db0806
files Implab/Implab.csproj Implab/ObjectPool.cs Implab/ObjectPoolWrapper.cs Implab/SyncPool.cs Implab/SyncPoolWrapper.cs
diffstat 5 files changed, 111 insertions(+), 113 deletions(-) [+]
line wrap: on
line diff
--- a/Implab/Implab.csproj	Fri Sep 26 20:44:01 2014 +0400
+++ b/Implab/Implab.csproj	Mon Sep 29 05:04:32 2014 +0400
@@ -7,8 +7,6 @@
     <OutputType>Library</OutputType>
     <RootNamespace>Implab</RootNamespace>
     <AssemblyName>Implab</AssemblyName>
-    <ProductVersion>8.0.30703</ProductVersion>
-    <SchemaVersion>2.0</SchemaVersion>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
@@ -123,8 +121,8 @@
     <Compile Include="PromiseExtensions.cs" />
     <Compile Include="TransientPromiseException.cs" />
     <Compile Include="SyncContextPromise.cs" />
-    <Compile Include="SyncPool.cs" />
-    <Compile Include="SyncPoolWrapper.cs" />
+    <Compile Include="ObjectPool.cs" />
+    <Compile Include="ObjectPoolWrapper.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup />
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab/ObjectPool.cs	Mon Sep 29 05:04:32 2014 +0400
@@ -0,0 +1,85 @@
+using System;
+using Implab.Parallels;
+using System.Threading;
+
+namespace Implab {
+    public class ObjectPool<T> : IDisposable {
+        readonly Func<T> m_factory;
+        readonly Action<T> m_cleanup;
+        readonly int m_size;
+        readonly MTQueue<T> m_queue = new MTQueue<T>();
+
+        volatile bool m_disposed;
+
+        volatile int m_count;
+
+        public ObjectPool(Func<T> factory, Action<T> cleanup, int size) {
+            Safe.ArgumentNotNull(factory, "factory");
+            Safe.ArgumentInRange(size, 1, size, "size");
+
+            m_factory = factory;
+            m_cleanup = cleanup;
+            m_size = size;
+        }
+
+        public ObjectPool(Func<T> factory, Action<T> cleanup) : this(factory,cleanup,Environment.ProcessorCount+1) {
+        }
+
+        public ObjectPool(Func<T> factory) : this(factory,null,Environment.ProcessorCount+1) {
+        }
+
+        public ObjectPoolWrapper<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<T>(instance, this);
+        }
+
+        public void Release(T instance) {
+            if (m_count < m_size && !m_disposed) {
+                Interlocked.Increment(ref m_count);
+
+                if (m_cleanup != null)
+                    m_cleanup(instance);
+
+                m_queue.Enqueue(instance);
+
+                // пока элемент возвращался в кеш, была начата операция освобождения всего кеша
+                // и возможно уже законцена, в таком случае следует извлечь элемент обратно и
+                // освободить его. Если операция освобождения кеша еще не заврешилась, то будет
+                // изъят и освобожден произвольный элемен, что не повлияет на ход всего процесса.
+                if (m_disposed && m_queue.TryDequeue(out instance))
+                    Safe.Dispose(instance);
+
+            } else {
+                Safe.Dispose(instance);
+            }
+        }
+
+        protected virtual void Dispose(bool disposing) {
+            if (disposing) {
+                m_disposed = true;
+                T instance;
+                while (m_queue.TryDequeue(out instance))
+                    Safe.Dispose(instance);
+            }
+        }
+
+        #region IDisposable implementation
+
+        public void Dispose() {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        #endregion
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab/ObjectPoolWrapper.cs	Mon Sep 29 05:04:32 2014 +0400
@@ -0,0 +1,24 @@
+using System;
+
+namespace Implab {
+    public struct ObjectPoolWrapper<T> : IDisposable {
+        readonly T m_value;
+        readonly ObjectPool<T> m_pool;
+
+        internal ObjectPoolWrapper(T value, ObjectPool<T> pool) {
+            m_value = value;
+            m_pool = pool;
+        }
+
+        public T Value {
+            get { return m_value; }
+        }
+
+        #region IDisposable implementation
+        public void Dispose() {
+            m_pool.Release(m_value);
+        }
+        #endregion
+    }
+}
+
--- a/Implab/SyncPool.cs	Fri Sep 26 20:44:01 2014 +0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,85 +0,0 @@
-using System;
-using Implab.Parallels;
-using System.Threading;
-
-namespace Implab {
-    /*public class SyncPool<T> : IDisposable {
-        readonly Func<T> m_factory;
-        readonly Action<T> m_cleanup;
-        readonly int m_size;
-        readonly MTQueue<T> m_queue = new MTQueue<T>();
-
-        volatile bool m_disposed;
-
-        volatile int m_count;
-
-        public SyncPool(Func<T> factory, Action<T> cleanup, int size) {
-            Safe.ArgumentNotNull(factory, "factory");
-            Safe.ArgumentInRange(size, 1, size, "size");
-
-            m_factory = factory;
-            m_cleanup = cleanup;
-            m_size = size;
-        }
-
-        public SyncPool(Func<T> factory, Action<T> cleanup) : this(factory,cleanup,Environment.ProcessorCount+1) {
-        }
-
-        public SyncPool(Func<T> factory) : this(factory,null,Environment.ProcessorCount+1) {
-        }
-
-        public SyncPoolWrapper<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 SyncPoolWrapper<T>(instance, this);
-        }
-
-        public void Release(T instance) {
-            if (m_count < m_size && !m_disposed) {
-                Interlocked.Increment(ref m_count);
-
-                if (m_cleanup != null)
-                    m_cleanup(instance);
-
-                m_queue.Enqueue(instance);
-
-                // пока элемент возвращался в кеш, была начата операция освобождения всего кеша
-                // и возможно уже законцена, в таком случае следует извлечь элемент обратно и
-                // освободить его. Если операция освобождения кеша еще не заврешилась, то будет
-                // изъят и освобожден произвольный элемен, что не повлияет на ход всего процесса.
-                if (m_disposed && m_queue.TryDequeue(out instance))
-                    Safe.Dispose(instance);
-
-            } else {
-                Safe.Dispose(instance);
-            }
-        }
-
-        protected virtual void Dispose(bool disposing) {
-            if (disposing) {
-                m_disposed = true;
-                T instance;
-                while (m_queue.TryDequeue(out instance))
-                    Safe.Dispose(instance);
-            }
-        }
-
-        #region IDisposable implementation
-
-        public void Dispose() {
-            Dispose(true);
-            GC.SuppressFinalize(this);
-        }
-
-        #endregion
-    }*/
-}
-
--- a/Implab/SyncPoolWrapper.cs	Fri Sep 26 20:44:01 2014 +0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-using System;
-
-namespace Implab {
-    /*public struct SyncPoolWrapper<T> : IDisposable {
-        readonly T m_value;
-        readonly SyncPool<T> m_pool;
-
-        internal SyncPoolWrapper(T value, SyncPool<T> pool) {
-            m_value = value;
-            m_pool = pool;
-        }
-
-        public T Value {
-            get { return m_value; }
-        }
-
-        #region IDisposable implementation
-        public void Dispose() {
-            m_pool.Release(m_value);
-        }
-        #endregion
-    }*/
-}
-