Skip to content

Commit 8b4a521

Browse files
committed
Modified lockasync to proritize Task over ValueTask
1 parent 664aa42 commit 8b4a521

File tree

2 files changed

+68
-14
lines changed

2 files changed

+68
-14
lines changed

src/ZirconNet.Core/Async/LockAsync.cs

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// This software is distributed under the MIT license and its code is open-source and free for use, modification, and distribution.
33
// </copyright>
44

5+
using ZirconNet.Core.Runtime;
6+
57
namespace ZirconNet.Core.Async;
68

79
/// <summary>
@@ -17,61 +19,97 @@ public LockAsync()
1719
_semaphore = new(1, 1);
1820
}
1921

20-
public async Task Lock(Func<Task> worker)
22+
public async Task Lock(Func<Task> taskWorker)
2123
{
2224
await _semaphore.WaitAsync().ConfigureAwait(false);
2325
try
2426
{
25-
await worker().ConfigureAwait(false);
27+
await taskWorker().ConfigureAwait(false);
2628
}
2729
finally
2830
{
29-
_ = _semaphore?.Release();
31+
_semaphore?.Release();
3032
}
3133
}
3234

33-
public async Task<T> Lock<T>(Func<Task<T>> worker)
35+
public async Task<T> Lock<T>(Func<Task<T>> taskWorker)
3436
{
3537
await _semaphore.WaitAsync().ConfigureAwait(false);
3638
try
3739
{
38-
return await worker().ConfigureAwait(false);
40+
return await taskWorker().ConfigureAwait(false);
3941
}
4042
finally
4143
{
42-
_ = _semaphore?.Release();
44+
_semaphore?.Release();
4345
}
4446
}
4547

46-
public async Task Lock(Func<ValueTask> worker)
48+
/// <summary>
49+
/// Acquires a lock and executes the provided asynchronous <see cref="ValueTask"/> operation.
50+
/// Use <c>Lock(valueTaskWorker: () => { actions... })</c> to specify it is a <see cref="ValueTask"/>.
51+
/// Do not use the dummy parameter.
52+
/// </summary>
53+
/// <remarks>
54+
/// The method is specifically designed for <see cref="ValueTask"/> operations. The second parameter is a dummy and should not be used.
55+
/// </remarks>
56+
/// <param name="valueTaskWorker">
57+
/// The <see cref="ValueTask"/> worker delegate that represents the asynchronous operation to be performed under the lock.
58+
/// </param>
59+
/// <param name="_">
60+
/// <b>DUMMY PARAMETER - DO NOT USE.</b> This parameter is included for prioritizing the methods.
61+
/// </param>
62+
/// <returns>
63+
/// A <see cref="Task"/> that represents the asynchronous operation.
64+
/// </returns>
65+
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1313:Parameter names should begin with lower-case letter", Justification = "Is an empty parameter")]
66+
public async Task Lock(Func<ValueTask> valueTaskWorker, DummyParameter? _ = null)
4767
{
4868
await _semaphore.WaitAsync().ConfigureAwait(false);
4969
try
5070
{
51-
await worker().ConfigureAwait(false);
71+
await valueTaskWorker().ConfigureAwait(false);
5272
}
5373
finally
5474
{
55-
_ = _semaphore?.Release();
75+
_semaphore?.Release();
5676
}
5777
}
5878

59-
public Task Lock<T>(Func<T> worker)
79+
public Task Lock<T>(Func<T> taskWorker)
6080
where T : Task
6181
{
62-
return Lock(worker);
82+
return Lock(taskWorker);
6383
}
6484

65-
public async Task<T> Lock<T>(Func<ValueTask<T>> worker)
85+
/// <summary>
86+
/// Acquires a lock and executes the provided asynchronous <see cref="ValueTask"/> operation.
87+
/// Use <c>Lock(valueTaskWorker: () => { actions... })</c> to specify it is a <see cref="ValueTask"/>.
88+
/// Do not use the dummy parameter.
89+
/// </summary>
90+
/// <remarks>
91+
/// The method is specifically designed for <see cref="ValueTask"/> operations. The second parameter is a dummy and should not be used.
92+
/// </remarks>
93+
/// <param name="valueTaskWorker">
94+
/// The <see cref="ValueTask"/> worker delegate that represents the asynchronous operation to be performed under the lock.
95+
/// </param>
96+
/// <param name="_">
97+
/// <b>DUMMY PARAMETER - DO NOT USE.</b> This parameter is included for prioritizing the methods.
98+
/// </param>
99+
/// <returns>
100+
/// A <see cref="Task"/> that represents the asynchronous operation.
101+
/// </returns>
102+
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1313:Parameter names should begin with lower-case letter", Justification = "Is an empty parameter")]
103+
public async Task<T> Lock<T>(Func<ValueTask<T>> valueTaskWorker, DummyParameter? _ = null)
66104
{
67105
await _semaphore.WaitAsync().ConfigureAwait(false);
68106
try
69107
{
70-
return await worker().ConfigureAwait(false);
108+
return await valueTaskWorker().ConfigureAwait(false);
71109
}
72110
finally
73111
{
74-
_ = _semaphore?.Release();
112+
_semaphore?.Release();
75113
}
76114
}
77115

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// <copyright file="DummyParameter.cs" company="Zircon Technology">
2+
// This software is distributed under the MIT license and its code is open-source and free for use, modification, and distribution.
3+
// </copyright>
4+
5+
namespace ZirconNet.Core.Runtime;
6+
7+
/// <summary>
8+
/// Dummy parameter to force one function or another SHOULD NOT BE USED.
9+
/// </summary>
10+
public abstract class DummyParameter
11+
{
12+
public DummyParameter()
13+
{
14+
throw new InvalidOperationException("Dummy parameter shouldn't be used or initialized.");
15+
}
16+
}

0 commit comments

Comments
 (0)