Skip to content

Commit e58aaf1

Browse files
author
Oren Ferrari
committed
There is simply no identifiable reason object pools should be asynchronous.
1 parent 521f1df commit e58aaf1

5 files changed

Lines changed: 35 additions & 85 deletions

File tree

Open.Disposable.ObjectPools.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27130.2036
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Open.Disposable.ObjectPools.Benchmarking", "benchmarking\Open.Disposable.ObjectPools.Benchmarking.csproj", "{8404875C-ED46-4057-8D97-598B4D7B8040}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Open.Disposable.ObjectPools", "source\Open.Disposable.ObjectPools.csproj", "{7DF5DA35-AB11-479D-BD87-27461AC3F5A8}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{8404875C-ED46-4057-8D97-598B4D7B8040}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{8404875C-ED46-4057-8D97-598B4D7B8040}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{8404875C-ED46-4057-8D97-598B4D7B8040}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{8404875C-ED46-4057-8D97-598B4D7B8040}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{7DF5DA35-AB11-479D-BD87-27461AC3F5A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{7DF5DA35-AB11-479D-BD87-27461AC3F5A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{7DF5DA35-AB11-479D-BD87-27461AC3F5A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{7DF5DA35-AB11-479D-BD87-27461AC3F5A8}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {86DED392-9553-4DB9-9EC3-BEBE4F470390}
30+
EndGlobalSection
31+
EndGlobal

source/Channels/ChannelObjectPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public ChannelObjectPool(Func<T> factory, int capacity = DEFAULT_CAPACITY)
2020

2121
protected override void OnDispose(bool calledExplicitly)
2222
{
23-
Pool?.Writer.Complete();
23+
Pool?.Writer.TryComplete();
2424
Pool = null;
2525
}
2626

source/IObjectPool.cs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Threading.Tasks;
43

54
namespace Open.Disposable
65
{
@@ -46,21 +45,7 @@ public interface IObjectPool<T> : IDisposable
4645
/// <returns>An item removed from the pool or generated. Should never be null.</returns>
4746
T Take();
4847

49-
50-
51-
/// <summary>
52-
/// Asynchronously receives an item and adds it to the pool. Ignores null references.
53-
/// WARNING: The item is considered 'dead' but resurrectable so be sure not to hold on to the item's reference.
54-
/// </summary>
55-
/// <param name="item">The item to give up to the pool.</param>
56-
/// <param name="recycler">An optional action exectue on the item only if it's possible to return to the pool.</param>
57-
Task GiveAsync(T item);
58-
59-
/// <summary>
60-
/// Awaits an available item from the pool. If none are available it generates one.
61-
/// </summary>
62-
/// <returns>An item removed from the pool or generated.</returns>
63-
Task<T> TakeAsync();
48+
6449
}
6550

6651
public static partial class ObjectPoolExtensions
@@ -92,31 +77,6 @@ public static void Give<T>(this IObjectPool<T> target, T item1, T item2, params
9277
target.Give(item2);
9378
target.Give(items);
9479
}
95-
96-
97-
98-
/// <summary>
99-
/// Asynchronously receives items and iteratively adds them to the pool.
100-
/// WARNING: These items are considered 'dead' but resurrectable so be sure not to hold on to their reference.
101-
/// </summary>
102-
/// <param name="items">The items to give up to the pool.</param>
103-
public static Task GiveAsync<T>(this IObjectPool<T> target, IEnumerable<T> items)
104-
where T : class
105-
{
106-
return Task.Run(() => target.Give(items));
107-
}
108-
109-
/// <summary>
110-
/// Asynchronously receives items and iteratively adds them to the pool.
111-
/// WARNING: These items are considered 'dead' but resurrectable so be sure not to hold on to their reference.
112-
/// </summary>
113-
/// <param name="item2">The first item to give up to the pool.</param>
114-
/// <param name="item2">The second item to give up to the pool.</param>
115-
/// <param name="items">The remaining items to give up to the pool.</param>
116-
public static Task GiveAsync<T>(this IObjectPool<T> target, T item1, T item2, params T[] items)
117-
where T : class
118-
{
119-
return Task.Run(() => target.Give(item1, item2, items));
120-
}
80+
12181
}
12282
}

source/ObjectPoolBase.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Threading.Tasks;
32

43
namespace Open.Disposable
54
{
@@ -74,27 +73,6 @@ public void Give(T item)
7473
&& (SaveToPocket(item) || Receive(item)))
7574
OnReceived();
7675
}
77-
78-
protected virtual Task<bool> ReceiveAsync(T item)
79-
{
80-
return Task.Run(() => Receive(item)); // Default implemnation, can be overridden.
81-
}
82-
83-
public Task GiveAsync(T item)
84-
{
85-
// We need to pre-check CanReceive because excessive tasks could build up if not.
86-
if (item == null || !CanReceive)
87-
return Task.CompletedTask;
88-
89-
return ReceiveConditionalAsync(item);
90-
}
91-
92-
async Task ReceiveConditionalAsync(T item)
93-
{
94-
if (PrepareToReceive(item)
95-
&& (SaveToPocket(item) || await ReceiveAsync(item).ConfigureAwait(false))) // Doesn't need original context. Just needs to trigger OnReceived().
96-
OnReceived();
97-
}
9876
#endregion
9977

10078
#region Release (.Take())
@@ -103,20 +81,6 @@ public virtual T Take()
10381
return TryTake() ?? Factory();
10482
}
10583

106-
protected virtual Task<T> ReleaseAsync()
107-
{
108-
return Task.Run((Func<T>)Take);
109-
}
110-
111-
public Task<T> TakeAsync()
112-
{
113-
// See if there's one available already.
114-
if (TryTake(out T firstTry))
115-
return Task.FromResult(firstTry);
116-
117-
return ReleaseAsync();
118-
}
119-
12084
public bool TryTake(out T item)
12185
{
12286
item = TryTake();

source/Open.Disposable.ObjectPools.csproj

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,14 @@ Part of the "Open" set of libraries.</Description>
2222
<PackageReleaseNotes>Updated Open.Threading.Tasks reference (only used for trimming).</PackageReleaseNotes>
2323
</PropertyGroup>
2424

25-
<ItemGroup>
26-
<Compile Remove="Channels\**" />
27-
<EmbeddedResource Remove="Channels\**" />
28-
<None Remove="Channels\**" />
29-
</ItemGroup>
30-
3125
<ItemGroup>
3226
<None Remove=".git" />
3327
</ItemGroup>
3428

3529
<ItemGroup>
3630
<PackageReference Include="Open.Disposable" Version="1.1.0" />
3731
<PackageReference Include="Open.Threading.Tasks" Version="1.1.2" />
32+
<PackageReference Include="System.Threading.Channels" Version="4.5.0-preview2-26406-04" />
3833
</ItemGroup>
3934

4035
</Project>

0 commit comments

Comments
 (0)