Synchronization
Represents task synchronization methods.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace DotNext.Threading.Tasks
{
[NullableContext(1)]
[Nullable(0)]
public static class Synchronization
{
[return: Nullable(new byte[] {
0,
1
})]
public static Result<TResult> GetResult<[Nullable(2)] TResult>(this Task<TResult> task, TimeSpan timeout)
{
try {
return task.Wait(timeout) ? new Result<TResult>(task.Result) : new Result<TResult>((Exception)new TimeoutException());
} catch (AggregateException ex) when (ex.InnerExceptions.Count == 1) {
return new Result<TResult>(ex.InnerExceptions[0]);
} catch (Exception error) {
return new Result<TResult>(error);
}
}
[return: Nullable(new byte[] {
0,
1
})]
public static Result<TResult> GetResult<[Nullable(2)] TResult>(this Task<TResult> task, CancellationToken token)
{
try {
task.Wait(token);
return task.Result;
} catch (AggregateException ex) when (ex.InnerExceptions.Count == 1) {
return new Result<TResult>(ex.InnerExceptions[0]);
} catch (Exception error) {
return new Result<TResult>(error);
}
}
[NullableContext(2)]
[return: Nullable(new byte[] {
0,
1
})]
public static Result<TResult>? TryGetResult<TResult>([Nullable(new byte[] {
2,
1
})] this Task<TResult> task)
{
if (task == null || !task.IsCompleted)
return null;
return task.GetResult(CancellationToken.None);
}
[RequiresUnreferencedCode("Runtime binding may be incompatible with IL trimming")]
[return: Nullable(new byte[] {
0,
2
})]
public static Result<dynamic> GetResult(this Task task, CancellationToken token)
{
try {
task.Wait(token);
return new Result<object>(new DynamicTaskAwaitable.Awaiter(task, 0).GetRawResult());
} catch (AggregateException ex) when (ex.InnerExceptions.Count == 1) {
return new Result<object>(ex.InnerExceptions[0]);
} catch (Exception error) {
return new Result<object>(error);
}
}
[RequiresUnreferencedCode("Runtime binding may be incompatible with IL trimming")]
[return: Nullable(new byte[] {
0,
2
})]
public static Result<dynamic> GetResult(this Task task, TimeSpan timeout)
{
try {
if (!task.Wait(timeout))
throw new TimeoutException();
return new Result<object>(new DynamicTaskAwaitable.Awaiter(task, 0).GetRawResult());
} catch (AggregateException ex) when (ex.InnerExceptions.Count == 1) {
return new Result<object>(ex.InnerExceptions[0]);
} catch (Exception error) {
return new Result<object>(error);
}
}
[AsyncStateMachine(typeof(<WhenAll>d__5<, >))]
[return: Nullable(new byte[] {
1,
0,
0,
1,
0,
1
})]
public static Task<(Result<T1>, Result<T2>)> WhenAll<[Nullable(2)] T1, [Nullable(2)] T2>(Task<T1> task1, Task<T2> task2)
{
<WhenAll>d__5<T1, T2> stateMachine = default(<WhenAll>d__5<T1, T2>);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<(Result<T1>, Result<T2>)>.Create();
stateMachine.task1 = task1;
stateMachine.task2 = task2;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<WhenAll>d__6<, , >))]
[return: Nullable(new byte[] {
1,
0,
0,
1,
0,
1,
0,
1
})]
public static Task<(Result<T1>, Result<T2>, Result<T3>)> WhenAll<[Nullable(2)] T1, [Nullable(2)] T2, [Nullable(2)] T3>(Task<T1> task1, Task<T2> task2, Task<T3> task3)
{
<WhenAll>d__6<T1, T2, T3> stateMachine = default(<WhenAll>d__6<T1, T2, T3>);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<(Result<T1>, Result<T2>, Result<T3>)>.Create();
stateMachine.task1 = task1;
stateMachine.task2 = task2;
stateMachine.task3 = task3;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<WhenAll>d__7<, , , >))]
[return: Nullable(new byte[] {
1,
0,
0,
1,
0,
1,
0,
1,
0,
1
})]
public static Task<(Result<T1>, Result<T2>, Result<T3>, Result<T4>)> WhenAll<[Nullable(2)] T1, [Nullable(2)] T2, [Nullable(2)] T3, [Nullable(2)] T4>(Task<T1> task1, Task<T2> task2, Task<T3> task3, Task<T4> task4)
{
<WhenAll>d__7<T1, T2, T3, T4> stateMachine = default(<WhenAll>d__7<T1, T2, T3, T4>);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<(Result<T1>, Result<T2>, Result<T3>, Result<T4>)>.Create();
stateMachine.task1 = task1;
stateMachine.task2 = task2;
stateMachine.task3 = task3;
stateMachine.task4 = task4;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<WhenAll>d__8<, , , , >))]
[return: Nullable(new byte[] {
1,
0,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1
})]
public static Task<(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>)> WhenAll<[Nullable(2)] T1, [Nullable(2)] T2, [Nullable(2)] T3, [Nullable(2)] T4, [Nullable(2)] T5>(Task<T1> task1, Task<T2> task2, Task<T3> task3, Task<T4> task4, Task<T5> task5)
{
<WhenAll>d__8<T1, T2, T3, T4, T5> stateMachine = default(<WhenAll>d__8<T1, T2, T3, T4, T5>);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>)>.Create();
stateMachine.task1 = task1;
stateMachine.task2 = task2;
stateMachine.task3 = task3;
stateMachine.task4 = task4;
stateMachine.task5 = task5;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<WhenAll>d__9<>))]
private static ValueTask WhenAll<T>(T tasks) where T : struct, ITuple
{
<WhenAll>d__9<T> stateMachine = default(<WhenAll>d__9<T>);
stateMachine.<>t__builder = AsyncValueTaskMethodBuilder.Create();
stateMachine.tasks = tasks;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
public static ValueTask WhenAll(ValueTask task1, ValueTask task2)
{
return Synchronization.WhenAll<(ValueTask, ValueTask)>((task1, task2));
}
[NullableContext(2)]
[AsyncStateMachine(typeof(<WhenAll>d__11<, >))]
[return: Nullable(new byte[] {
0,
0,
0,
1,
0,
1
})]
public static ValueTask<(Result<T1>, Result<T2>)> WhenAll<T1, T2>([Nullable(new byte[] {
0,
1
})] ValueTask<T1> task1, [Nullable(new byte[] {
0,
1
})] ValueTask<T2> task2)
{
<WhenAll>d__11<T1, T2> stateMachine = default(<WhenAll>d__11<T1, T2>);
stateMachine.<>t__builder = AsyncValueTaskMethodBuilder<(Result<T1>, Result<T2>)>.Create();
stateMachine.task1 = task1;
stateMachine.task2 = task2;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
public static ValueTask WhenAll(ValueTask task1, ValueTask task2, ValueTask task3)
{
return Synchronization.WhenAll<(ValueTask, ValueTask, ValueTask)>((task1, task2, task3));
}
[NullableContext(2)]
[AsyncStateMachine(typeof(<WhenAll>d__13<, , >))]
[return: Nullable(new byte[] {
0,
0,
0,
1,
0,
1,
0,
1
})]
public static ValueTask<(Result<T1>, Result<T2>, Result<T3>)> WhenAll<T1, T2, T3>([Nullable(new byte[] {
0,
1
})] ValueTask<T1> task1, [Nullable(new byte[] {
0,
1
})] ValueTask<T2> task2, [Nullable(new byte[] {
0,
1
})] ValueTask<T3> task3)
{
<WhenAll>d__13<T1, T2, T3> stateMachine = default(<WhenAll>d__13<T1, T2, T3>);
stateMachine.<>t__builder = AsyncValueTaskMethodBuilder<(Result<T1>, Result<T2>, Result<T3>)>.Create();
stateMachine.task1 = task1;
stateMachine.task2 = task2;
stateMachine.task3 = task3;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
public static ValueTask WhenAll(ValueTask task1, ValueTask task2, ValueTask task3, ValueTask task4)
{
return Synchronization.WhenAll<(ValueTask, ValueTask, ValueTask, ValueTask)>((task1, task2, task3, task4));
}
[NullableContext(2)]
[AsyncStateMachine(typeof(<WhenAll>d__15<, , , >))]
[return: Nullable(new byte[] {
0,
0,
0,
1,
0,
1,
0,
1,
0,
1
})]
public static ValueTask<(Result<T1>, Result<T2>, Result<T3>, Result<T4>)> WhenAll<T1, T2, T3, T4>([Nullable(new byte[] {
0,
1
})] ValueTask<T1> task1, [Nullable(new byte[] {
0,
1
})] ValueTask<T2> task2, [Nullable(new byte[] {
0,
1
})] ValueTask<T3> task3, [Nullable(new byte[] {
0,
1
})] ValueTask<T4> task4)
{
<WhenAll>d__15<T1, T2, T3, T4> stateMachine = default(<WhenAll>d__15<T1, T2, T3, T4>);
stateMachine.<>t__builder = AsyncValueTaskMethodBuilder<(Result<T1>, Result<T2>, Result<T3>, Result<T4>)>.Create();
stateMachine.task1 = task1;
stateMachine.task2 = task2;
stateMachine.task3 = task3;
stateMachine.task4 = task4;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
public static ValueTask WhenAll(ValueTask task1, ValueTask task2, ValueTask task3, ValueTask task4, ValueTask task5)
{
return Synchronization.WhenAll<(ValueTask, ValueTask, ValueTask, ValueTask, ValueTask)>((task1, task2, task3, task4, task5));
}
[NullableContext(2)]
[AsyncStateMachine(typeof(<WhenAll>d__17<, , , , >))]
[return: Nullable(new byte[] {
0,
0,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1
})]
public static ValueTask<(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>)> WhenAll<T1, T2, T3, T4, T5>([Nullable(new byte[] {
0,
1
})] ValueTask<T1> task1, [Nullable(new byte[] {
0,
1
})] ValueTask<T2> task2, [Nullable(new byte[] {
0,
1
})] ValueTask<T3> task3, [Nullable(new byte[] {
0,
1
})] ValueTask<T4> task4, [Nullable(new byte[] {
0,
1
})] ValueTask<T5> task5)
{
<WhenAll>d__17<T1, T2, T3, T4, T5> stateMachine = default(<WhenAll>d__17<T1, T2, T3, T4, T5>);
stateMachine.<>t__builder = AsyncValueTaskMethodBuilder<(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>)>.Create();
stateMachine.task1 = task1;
stateMachine.task2 = task2;
stateMachine.task3 = task3;
stateMachine.task4 = task4;
stateMachine.task5 = task5;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
}
}