Skip to content

Commit 8ac46a6

Browse files
author
Oren (electricessence)
committed
Updated version and added SemaphoreSlim extensions.
1 parent 8ead6b9 commit 8ac46a6

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

Open.Threading.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Part of the "Open" set of libraries.</Description>
4242
</ItemGroup>
4343

4444
<ItemGroup>
45-
<PackageReference Include="Open.Threading.ReadWrite" Version="1.3.2" />
45+
<PackageReference Include="Open.Threading.ReadWrite" Version="1.3.3" />
4646
</ItemGroup>
4747

4848
</Project>

SemaphoreSlimExtensions.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public static async Task<T> ExecuteAsync<T>(this SemaphoreSlim target, Func<T> c
177177
/// <param name="target">The semaphore instance</param>
178178
/// <param name="task">The task being waited on.</param>
179179
/// <returns>The task provided.</returns>
180-
public static async Task<T> ExecuteAsync<T>(this SemaphoreSlim target, Task<T> task)
180+
public static async Task<T> TaskExecuteAsync<T>(this SemaphoreSlim target, Task<T> task)
181181
{
182182
if (target == null)
183183
throw new ArgumentNullException(nameof(target));
@@ -203,5 +203,36 @@ public static async Task<T> ExecuteAsync<T>(this SemaphoreSlim target, Task<T> t
203203
}
204204
}
205205

206+
207+
/// <summary>
208+
/// Excutes a task within the context of a a SemaphoreSlim.
209+
/// </summary>
210+
/// <typeparam name="T">Type of the result.</typeparam>
211+
/// <param name="target">The semaphore instance</param>
212+
/// <param name="task">The task being waited on.</param>
213+
/// <returns>The task provided.</returns>
214+
public static async Task<T> ExecuteAsync<T>(this SemaphoreSlim target, ValueTask<T> task)
215+
{
216+
if (target is null) throw new ArgumentNullException(nameof(target));
217+
Contract.EndContractBlock();
218+
219+
try
220+
{
221+
await target.WaitAsync().ConfigureAwait(false);
222+
return await task;
223+
}
224+
finally
225+
{
226+
try
227+
{
228+
target.Release();
229+
}
230+
catch (SemaphoreFullException sfex)
231+
{
232+
Debug.WriteLine(sfex.ToString());
233+
}
234+
}
235+
}
236+
206237
}
207238
}

0 commit comments

Comments
 (0)