From 80a8128e9159769024f479b6e027a5328354c469 Mon Sep 17 00:00:00 2001 From: Alessio Parma Date: Sun, 2 Apr 2017 14:49:57 +0200 Subject: [PATCH] config for timed object pool --- .../CodeProject.ObjectPool.csproj | 19 ++++++++++++++++- .../Core/PooledObjectBuffer.cs | 21 +++++++++++++------ .../ParameterizedObjectPool.cs | 6 +++--- .../TimedObjectPool.cs | 19 +++++++++++++++-- .../CodeProject.ObjectPool.Examples.csproj | 4 +--- 5 files changed, 54 insertions(+), 15 deletions(-) rename {test/CodeProject.ObjectPool.Examples/Customizations => src/CodeProject.ObjectPool}/TimedObjectPool.cs (72%) diff --git a/src/CodeProject.ObjectPool/CodeProject.ObjectPool.csproj b/src/CodeProject.ObjectPool/CodeProject.ObjectPool.csproj index cd4ea26..afedd2c 100644 --- a/src/CodeProject.ObjectPool/CodeProject.ObjectPool.csproj +++ b/src/CodeProject.ObjectPool/CodeProject.ObjectPool.csproj @@ -3,7 +3,7 @@ CodeProject.ObjectPool Generic and concurrent Object Pool 3.0.1 - netstandard1.0;netstandard1.1;netstandard1.3;net35;net40;net45 + netstandard1.0;netstandard1.1;netstandard1.2;netstandard1.3;net35;net40;net45 true ../../pomma89.snk true @@ -45,11 +45,24 @@ $(PackageTargetFallback);dnxcore50 + + $(DefineConstants);NETSTD12;LIBLOG_PORTABLE + $(PackageTargetFallback);dnxcore50 + + + + + + $(DefineConstants);NETSTD13;LIBLOG_PORTABLE $(PackageTargetFallback);dnxcore50 + + + + $(DefineConstants);NET35 @@ -75,4 +88,8 @@ + + + + \ No newline at end of file diff --git a/src/CodeProject.ObjectPool/Core/PooledObjectBuffer.cs b/src/CodeProject.ObjectPool/Core/PooledObjectBuffer.cs index 56fbd90..7c7fb29 100644 --- a/src/CodeProject.ObjectPool/Core/PooledObjectBuffer.cs +++ b/src/CodeProject.ObjectPool/Core/PooledObjectBuffer.cs @@ -62,7 +62,7 @@ public sealed class PooledObjectBuffer : IEnumerable public int Capacity => _pooledObjects.Length; /// - /// The number of items stored in this buffer. + /// The number of objects stored in this buffer. /// public int Count { @@ -101,14 +101,18 @@ public IEnumerator GetEnumerator() /// IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + /// + /// Tries to dequeue an object from the buffer. + /// + /// Output pooled object. + /// True if has a value, false otherwise. [MethodImpl(TryToInline)] public bool TryDequeue(out T pooledObject) { for (var i = 0; i < _pooledObjects.Length; i++) { - ref var itemRef = ref _pooledObjects[i]; - var item = itemRef; - if (item != null && Interlocked.CompareExchange(ref itemRef, null, item) == item) + var item = _pooledObjects[i]; + if (item != null && Interlocked.CompareExchange(ref _pooledObjects[i], null, item) == item) { pooledObject = item; return true; @@ -118,13 +122,18 @@ public bool TryDequeue(out T pooledObject) return false; } + /// + /// Tries to enqueue given object into the buffer. + /// + /// Input pooled object. + /// True if there was enough space to enqueue given object, false otherwise. [MethodImpl(TryToInline)] public bool TryEnqueue(T pooledObject) { for (var i = 0; i < _pooledObjects.Length; i++) { - ref var itemRef = ref _pooledObjects[i]; - if (itemRef == null && Interlocked.CompareExchange(ref itemRef, pooledObject, null) == null) + ref var item = ref _pooledObjects[i]; + if (item == null && Interlocked.CompareExchange(ref item, pooledObject, null) == null) { return true; } diff --git a/src/CodeProject.ObjectPool/ParameterizedObjectPool.cs b/src/CodeProject.ObjectPool/ParameterizedObjectPool.cs index 008a6d7..1b9ee4a 100644 --- a/src/CodeProject.ObjectPool/ParameterizedObjectPool.cs +++ b/src/CodeProject.ObjectPool/ParameterizedObjectPool.cs @@ -21,7 +21,7 @@ namespace CodeProject.ObjectPool /// /// The type of the pool parameter. /// The type of the objects stored in the pool. - public sealed class ParameterizedObjectPool : IParameterizedObjectPool + public class ParameterizedObjectPool : IParameterizedObjectPool where TValue : PooledObject { #region Public Properties @@ -165,7 +165,7 @@ public TValue GetObject(TKey key) #region Low-level Pooling -#if (NETSTD10 || NETSTD11) +#if (NETSTD10 || NETSTD11 || NETSTD12) private readonly System.Collections.Generic.Dictionary> _pools = new System.Collections.Generic.Dictionary>(); #else private readonly System.Collections.Hashtable _pools = new System.Collections.Hashtable(); @@ -191,7 +191,7 @@ private void ClearPools() private bool TryGetPool(TKey key, out ObjectPool objectPool) { -#if (NETSTD10 || NETSTD11) +#if (NETSTD10 || NETSTD11 || NETSTD12) // Dictionary requires locking even for readers. lock (_pools) { diff --git a/test/CodeProject.ObjectPool.Examples/Customizations/TimedObjectPool.cs b/src/CodeProject.ObjectPool/TimedObjectPool.cs similarity index 72% rename from test/CodeProject.ObjectPool.Examples/Customizations/TimedObjectPool.cs rename to src/CodeProject.ObjectPool/TimedObjectPool.cs index 92dfa6e..c966241 100644 --- a/test/CodeProject.ObjectPool.Examples/Customizations/TimedObjectPool.cs +++ b/src/CodeProject.ObjectPool/TimedObjectPool.cs @@ -21,9 +21,24 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT // OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +#if !(NETSTD10 || NETSTD11) + +using System.Threading; + namespace CodeProject.ObjectPool.Examples.Customizations { - internal class TimedObjectPool + /// + /// A pool where objects are automatically removed after a period of inactivity. + /// + /// + /// The type of the object that which will be managed by the pool. The pooled object have to be + /// a sub-class of PooledObject. + /// + internal class TimedObjectPool : ObjectPool + where T : PooledObject { + private readonly Timer Timer; } -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/test/CodeProject.ObjectPool.Examples/CodeProject.ObjectPool.Examples.csproj b/test/CodeProject.ObjectPool.Examples/CodeProject.ObjectPool.Examples.csproj index 3f63694..538a48c 100644 --- a/test/CodeProject.ObjectPool.Examples/CodeProject.ObjectPool.Examples.csproj +++ b/test/CodeProject.ObjectPool.Examples/CodeProject.ObjectPool.Examples.csproj @@ -1,5 +1,4 @@  - netcoreapp1.1 CodeProject.ObjectPool.Examples @@ -15,5 +14,4 @@ - - + \ No newline at end of file