Skip to content
This repository has been archived by the owner on May 21, 2022. It is now read-only.

Commit

Permalink
config for timed object pool
Browse files Browse the repository at this point in the history
  • Loading branch information
pomma89 committed Apr 2, 2017
1 parent 3e10e53 commit 80a8128
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
19 changes: 18 additions & 1 deletion src/CodeProject.ObjectPool/CodeProject.ObjectPool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<AssemblyName>CodeProject.ObjectPool</AssemblyName>
<AssemblyTitle>Generic and concurrent Object Pool</AssemblyTitle>
<VersionPrefix>3.0.1</VersionPrefix>
<TargetFrameworks>netstandard1.0;netstandard1.1;netstandard1.3;net35;net40;net45</TargetFrameworks>
<TargetFrameworks>netstandard1.0;netstandard1.1;netstandard1.2;netstandard1.3;net35;net40;net45</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyOriginatorKeyFile>../../pomma89.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
Expand Down Expand Up @@ -45,11 +45,24 @@
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.2' ">
<DefineConstants>$(DefineConstants);NETSTD12;LIBLOG_PORTABLE</DefineConstants>
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.2' ">
<PackageReference Include="System.Threading.Timer" Version="4.3.0" />
</ItemGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<DefineConstants>$(DefineConstants);NETSTD13;LIBLOG_PORTABLE</DefineConstants>
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="System.Threading.Timer" Version="4.3.0" />
</ItemGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net35' ">
<DefineConstants>$(DefineConstants);NET35</DefineConstants>
</PropertyGroup>
Expand All @@ -75,4 +88,8 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
</ItemGroup>

<ItemGroup>
<Folder Include="Extensibility\" />
</ItemGroup>
</Project>
21 changes: 15 additions & 6 deletions src/CodeProject.ObjectPool/Core/PooledObjectBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public sealed class PooledObjectBuffer<T> : IEnumerable<T>
public int Capacity => _pooledObjects.Length;

/// <summary>
/// The number of items stored in this buffer.
/// The number of objects stored in this buffer.
/// </summary>
public int Count
{
Expand Down Expand Up @@ -101,14 +101,18 @@ public IEnumerator<T> GetEnumerator()
/// </returns>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

/// <summary>
/// Tries to dequeue an object from the buffer.
/// </summary>
/// <param name="pooledObject">Output pooled object.</param>
/// <returns>True if <paramref name="pooledObject"/> has a value, false otherwise.</returns>
[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;
Expand All @@ -118,13 +122,18 @@ public bool TryDequeue(out T pooledObject)
return false;
}

/// <summary>
/// Tries to enqueue given object into the buffer.
/// </summary>
/// <param name="pooledObject">Input pooled object.</param>
/// <returns>True if there was enough space to enqueue given object, false otherwise.</returns>
[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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/CodeProject.ObjectPool/ParameterizedObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace CodeProject.ObjectPool
/// </summary>
/// <typeparam name="TKey">The type of the pool parameter.</typeparam>
/// <typeparam name="TValue">The type of the objects stored in the pool.</typeparam>
public sealed class ParameterizedObjectPool<TKey, TValue> : IParameterizedObjectPool<TKey, TValue>
public class ParameterizedObjectPool<TKey, TValue> : IParameterizedObjectPool<TKey, TValue>
where TValue : PooledObject
{
#region Public Properties
Expand Down Expand Up @@ -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<TKey, ObjectPool<TValue>> _pools = new System.Collections.Generic.Dictionary<TKey, ObjectPool<TValue>>();
#else
private readonly System.Collections.Hashtable _pools = new System.Collections.Hashtable();
Expand All @@ -191,7 +191,7 @@ private void ClearPools()

private bool TryGetPool(TKey key, out ObjectPool<TValue> objectPool)
{
#if (NETSTD10 || NETSTD11)
#if (NETSTD10 || NETSTD11 || NETSTD12)
// Dictionary requires locking even for readers.
lock (_pools)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// <summary>
/// A pool where objects are automatically removed after a period of inactivity.
/// </summary>
/// <typeparam name="T">
/// The type of the object that which will be managed by the pool. The pooled object have to be
/// a sub-class of PooledObject.
/// </typeparam>
internal class TimedObjectPool<T> : ObjectPool<T>
where T : PooledObject
{
private readonly Timer Timer;
}
}
}

#endif
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<AssemblyName>CodeProject.ObjectPool.Examples</AssemblyName>
Expand All @@ -15,5 +14,4 @@
<ItemGroup>
<ProjectReference Include="..\..\src\CodeProject.ObjectPool\CodeProject.ObjectPool.csproj" />
</ItemGroup>

</Project>
</Project>

0 comments on commit 80a8128

Please sign in to comment.