Skip to content

Commit b647604

Browse files
committed
Make some API to be reusable by the derived classes
1 parent 3e34c38 commit b647604

File tree

3 files changed

+39
-61
lines changed

3 files changed

+39
-61
lines changed

src/DotNext.Threading/Threading/Tasks/ManualResetCompletionSource.cs

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
using System.ComponentModel;
12
using System.Diagnostics;
23
using System.Diagnostics.CodeAnalysis;
34
using System.Runtime.CompilerServices;
45
using System.Runtime.InteropServices;
5-
using ValueTaskSourceOnCompletedFlags = System.Threading.Tasks.Sources.ValueTaskSourceOnCompletedFlags;
6+
using System.Threading.Tasks.Sources;
67

78
namespace DotNext.Threading.Tasks;
89

@@ -220,8 +221,26 @@ private void OnCompleted(in Continuation continuation, short token)
220221
throw new InvalidOperationException(errorMessage);
221222
}
222223

223-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
224-
private protected void OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags)
224+
private protected ValueTaskSourceStatus GetStatus(short token, Exception? e)
225+
{
226+
var snapshot = versionAndStatus.VolatileRead(); // barrier to avoid reordering of result read
227+
228+
if (token != snapshot.Version)
229+
throw new InvalidOperationException(ExceptionMessages.InvalidSourceToken);
230+
231+
return !snapshot.IsCompleted
232+
? ValueTaskSourceStatus.Pending
233+
: e switch
234+
{
235+
null => ValueTaskSourceStatus.Succeeded,
236+
OperationCanceledException => ValueTaskSourceStatus.Canceled,
237+
_ => ValueTaskSourceStatus.Faulted,
238+
};
239+
}
240+
241+
/// <inheritdoc cref="IValueTaskSource.OnCompleted"/>
242+
[EditorBrowsable(EditorBrowsableState.Never)]
243+
public void OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags)
225244
=> OnCompleted(new(continuation, state, flags), token);
226245

227246
/// <summary>

src/DotNext.Threading/Threading/Tasks/ValueTaskCompletionSource.T.cs

+11-35
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.ComponentModel;
12
using System.Runtime.CompilerServices;
23
using System.Threading.Tasks.Sources;
34
using Debug = System.Diagnostics.Debug;
@@ -8,7 +9,7 @@ namespace DotNext.Threading.Tasks;
89
/// Represents the producer side of <see cref="ValueTask{T}"/>.
910
/// </summary>
1011
/// <remarks>
11-
/// In constrast to <see cref="TaskCompletionSource{T}"/>, this
12+
/// In contrast to <see cref="TaskCompletionSource{T}"/>, this
1213
/// source is resettable.
1314
/// From performance point of view, the type offers minimal or zero memory allocation
1415
/// for the task itself (excluding continuations). See <see cref="CreateTask(TimeSpan, CancellationToken)"/>
@@ -37,7 +38,7 @@ private static Result<T> FromCanceled(CancellationToken token)
3738
=> new(new OperationCanceledException(token));
3839

3940
/// <summary>
40-
/// Attempts to complete the task sucessfully.
41+
/// Attempts to complete the task successfully.
4142
/// </summary>
4243
/// <param name="value">The value to be returned to the consumer.</param>
4344
/// <returns><see langword="true"/> if the result is completed successfully; <see langword="false"/> if the task has been canceled or timed out.</returns>
@@ -46,7 +47,7 @@ public bool TrySetResult(T value)
4647
=> TrySetResult(null, value);
4748

4849
/// <summary>
49-
/// Attempts to complete the task sucessfully.
50+
/// Attempts to complete the task successfully.
5051
/// </summary>
5152
/// <param name="completionData">The data to be saved in <see cref="ManualResetCompletionSource.CompletionData"/> property that can be accessed from within <see cref="ManualResetCompletionSource.AfterConsumed"/> method.</param>
5253
/// <param name="value">The value to be returned to the consumer.</param>
@@ -55,7 +56,7 @@ public unsafe bool TrySetResult(object? completionData, T value)
5556
=> SetResult(completionData, completionToken: null, &Result.FromValue, value);
5657

5758
/// <summary>
58-
/// Attempts to complete the task sucessfully.
59+
/// Attempts to complete the task successfully.
5960
/// </summary>
6061
/// <param name="completionToken">The completion token previously obtained from <see cref="CreateTask(TimeSpan, CancellationToken)"/> method.</param>
6162
/// <param name="value">The value to be returned to the consumer.</param>
@@ -65,7 +66,7 @@ public bool TrySetResult(short completionToken, T value)
6566
=> TrySetResult(null, completionToken, value);
6667

6768
/// <summary>
68-
/// Attempts to complete the task sucessfully.
69+
/// Attempts to complete the task successfully.
6970
/// </summary>
7071
/// <param name="completionData">The data to be saved in <see cref="ManualResetCompletionSource.CompletionData"/> property that can be accessed from within <see cref="ManualResetCompletionSource.AfterConsumed"/> method.</param>
7172
/// <param name="completionToken">The completion token previously obtained from <see cref="CreateTask(TimeSpan, CancellationToken)"/> method.</param>
@@ -219,7 +220,7 @@ ValueTask<T> ISupplier<TimeSpan, CancellationToken, ValueTask<T>>.Invoke(TimeSpa
219220
ValueTask ISupplier<TimeSpan, CancellationToken, ValueTask>.Invoke(TimeSpan timeout, CancellationToken token)
220221
=> Activate(timeout, token) is { } version ? new(this, version) : throw new InvalidOperationException(ExceptionMessages.InvalidSourceState);
221222

222-
private T GetResult(short token)
223+
private protected T GetResult(short token)
223224
{
224225
// ensure that instance field access before returning to the pool to avoid
225226
// concurrency with Reset()
@@ -236,35 +237,10 @@ private T GetResult(short token)
236237
/// <inheritdoc />
237238
void IValueTaskSource.GetResult(short token) => GetResult(token);
238239

239-
private ValueTaskSourceStatus GetStatus(short token)
240-
{
241-
var error = result.Error;
242-
var snapshot = versionAndStatus.VolatileRead(); // barrier to avoid reordering of result read
243-
244-
if (token != snapshot.Version)
245-
throw new InvalidOperationException(ExceptionMessages.InvalidSourceToken);
246-
247-
return !snapshot.IsCompleted ? ValueTaskSourceStatus.Pending : error switch
248-
{
249-
null => ValueTaskSourceStatus.Succeeded,
250-
OperationCanceledException => ValueTaskSourceStatus.Canceled,
251-
_ => ValueTaskSourceStatus.Faulted,
252-
};
253-
}
254-
255-
/// <inheritdoc />
256-
ValueTaskSourceStatus IValueTaskSource<T>.GetStatus(short token) => GetStatus(token);
257-
258-
/// <inheritdoc />
259-
ValueTaskSourceStatus IValueTaskSource.GetStatus(short token) => GetStatus(token);
260-
261-
/// <inheritdoc />
262-
void IValueTaskSource<T>.OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags)
263-
=> OnCompleted(continuation, state, token, flags);
264-
265-
/// <inheritdoc />
266-
void IValueTaskSource.OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags)
267-
=> OnCompleted(continuation, state, token, flags);
240+
/// <inheritdoc cref="IValueTaskSource.GetStatus"/>
241+
[EditorBrowsable(EditorBrowsableState.Never)]
242+
public ValueTaskSourceStatus GetStatus(short token)
243+
=> GetStatus(token, result.Error);
268244

269245
/// <summary>
270246
/// Creates a linked <see cref="TaskCompletionSource{TResult}"/> that can be used cooperatively to

src/DotNext.Threading/Threading/Tasks/ValueTaskCompletionSource.cs

+6-23
Original file line numberDiff line numberDiff line change
@@ -136,31 +136,31 @@ public bool TrySetException(object? completionData, short completionToken, Excep
136136
=> SetResult<ValueSupplier<Exception>>(completionData, completionToken, e);
137137

138138
/// <summary>
139-
/// Attempts to complete the task sucessfully.
139+
/// Attempts to complete the task successfully.
140140
/// </summary>
141141
/// <returns><see langword="true"/> if the result is completed successfully; <see langword="false"/> if the task has been canceled or timed out.</returns>
142142
[MethodImpl(MethodImplOptions.AggressiveInlining)]
143143
public bool TrySetResult()
144144
=> TrySetResult(null);
145145

146146
/// <summary>
147-
/// Attempts to complete the task sucessfully.
147+
/// Attempts to complete the task successfully.
148148
/// </summary>
149149
/// <param name="completionData">The data to be saved in <see cref="ManualResetCompletionSource.CompletionData"/> property that can be accessed from within <see cref="ManualResetCompletionSource.AfterConsumed"/> method.</param>
150150
/// <returns><see langword="true"/> if the result is completed successfully; <see langword="false"/> if the task has been canceled or timed out.</returns>
151151
public bool TrySetResult(object? completionData)
152152
=> SetResult(completionData, completionToken: null, ISupplier<Exception>.NullOrDefault);
153153

154154
/// <summary>
155-
/// Attempts to complete the task sucessfully.
155+
/// Attempts to complete the task successfully.
156156
/// </summary>
157157
/// <param name="completionToken">The completion token previously obtained from <see cref="CreateTask(TimeSpan, CancellationToken)"/> method.</param>
158158
/// <returns><see langword="true"/> if the result is completed successfully; <see langword="false"/> if the task has been canceled or timed out.</returns>
159159
public bool TrySetResult(short completionToken)
160160
=> TrySetResult(null, completionToken);
161161

162162
/// <summary>
163-
/// Attempts to complete the task sucessfully.
163+
/// Attempts to complete the task successfully.
164164
/// </summary>
165165
/// <param name="completionData">The data to be saved in <see cref="ManualResetCompletionSource.CompletionData"/> property that can be accessed from within <see cref="ManualResetCompletionSource.AfterConsumed"/> method.</param>
166166
/// <param name="completionToken">The completion token previously obtained from <see cref="CreateTask(TimeSpan, CancellationToken)"/> method.</param>
@@ -176,7 +176,7 @@ public bool TrySetResult(object? completionData, short completionToken)
176176
/// </remarks>
177177
/// <param name="timeout">The timeout associated with the task.</param>
178178
/// <param name="token">The cancellation token that can be used to cancel the task.</param>
179-
/// <returns>A fresh incompleted task.</returns>
179+
/// <returns>A fresh uncompleted task.</returns>
180180
/// <exception cref="ArgumentOutOfRangeException"><paramref name="timeout"/> is less than zero but not equals to <see cref="System.Threading.Timeout.InfiniteTimeSpan"/>.</exception>
181181
/// <exception cref="InvalidOperationException">The source is in invalid state.</exception>
182182
public ValueTask CreateTask(TimeSpan timeout, CancellationToken token)
@@ -200,24 +200,7 @@ void IValueTaskSource.GetResult(short token)
200200

201201
/// <inheritdoc />
202202
ValueTaskSourceStatus IValueTaskSource.GetStatus(short token)
203-
{
204-
var resultCopy = result;
205-
var snapshot = versionAndStatus.VolatileRead(); // barrier to avoid reordering of result read
206-
207-
if (token != snapshot.Version)
208-
throw new InvalidOperationException(ExceptionMessages.InvalidSourceToken);
209-
210-
return !snapshot.IsCompleted ? ValueTaskSourceStatus.Pending : resultCopy switch
211-
{
212-
null => ValueTaskSourceStatus.Succeeded,
213-
{ SourceException: OperationCanceledException } => ValueTaskSourceStatus.Canceled,
214-
_ => ValueTaskSourceStatus.Faulted,
215-
};
216-
}
217-
218-
/// <inheritdoc />
219-
void IValueTaskSource.OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags)
220-
=> OnCompleted(continuation, state, token, flags);
203+
=> GetStatus(token, result?.SourceException);
221204

222205
/// <summary>
223206
/// Creates a linked <see cref="TaskCompletionSource"/> that can be used cooperatively to

0 commit comments

Comments
 (0)