tgstation-server 6.12.3
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ValueTaskExtensions.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Threading.Tasks;
5
7{
11 public static class ValueTaskExtensions
12 {
20 public static async ValueTask<T[]> WhenAll<T>(IEnumerable<ValueTask<T>> tasks, int totalTasks)
21 {
22 if (tasks == null)
23 throw new ArgumentNullException(nameof(tasks));
24
25 // We don't allocate the list if no task throws
26 Exception? exception = null;
27 int i = 0;
28 var results = new T[totalTasks];
29 foreach (var task in tasks)
30 {
31 try
32 {
33 results[i] = await task.ConfigureAwait(false);
34 }
35 catch (Exception ex)
36 {
37 exception ??= ex;
38 }
39
40 ++i;
41 }
42
43 Debug.Assert(i == totalTasks, "Incorrect totalTasks specified!");
44
45 if (exception != null)
46 throw exception;
47
48 return results;
49 }
50
58 public static async ValueTask<T[]> WhenAll<T>(IReadOnlyList<ValueTask<T>> tasks)
59 {
60 if (tasks == null)
61 throw new ArgumentNullException(nameof(tasks));
62
63 var totalTasks = tasks.Count;
64 if (totalTasks == 0)
65 return Array.Empty<T>();
66
67 // We don't allocate the list if no task throws
68 Exception? exception = null;
69 var results = new T[totalTasks];
70 for (var i = 0; i < totalTasks; i++)
71 try
72 {
73 results[i] = await tasks[i].ConfigureAwait(false);
74 }
75 catch (Exception ex)
76 {
77 exception ??= ex;
78 }
79
80 if (exception != null)
81 throw exception;
82
83 return results;
84 }
85
92 public static ValueTask<T[]> WhenAll<T>(params ValueTask<T>[] tasks) => WhenAll((IReadOnlyList<ValueTask<T>>)tasks);
93
99 public static async ValueTask WhenAll(IEnumerable<ValueTask> tasks)
100 {
101 if (tasks == null)
102 throw new ArgumentNullException(nameof(tasks));
103
104 // We don't allocate the list if no task throws
105 Exception? exception = null;
106 foreach (var task in tasks)
107 try
108 {
109 await task.ConfigureAwait(false);
110 }
111 catch (Exception ex)
112 {
113 exception ??= ex;
114 }
115
116 if (exception != null)
117 throw exception;
118 }
119
126 public static async ValueTask WhenAll(IReadOnlyList<ValueTask> tasks)
127 {
128 if (tasks == null)
129 throw new ArgumentNullException(nameof(tasks));
130
131 // We don't allocate the list if no task throws
132 List<Exception>? exceptions = null;
133 for (var i = 0; i < tasks.Count; ++i)
134 try
135 {
136 var task = tasks[i];
137 await task.ConfigureAwait(false);
138 }
139 catch (Exception ex)
140 {
141 exceptions ??= new(tasks.Count - i);
142 exceptions.Add(ex);
143 }
144
145 if (exceptions != null)
146 throw new AggregateException(exceptions);
147 }
148
155 public static ValueTask WhenAll(params ValueTask[] tasks) => WhenAll((IReadOnlyList<ValueTask>)tasks);
156 }
157}
Extension methods for the ValueTask and ValueTask<TResult> classes.
static ValueTask WhenAll(params ValueTask[] tasks)
Fully await a given list of tasks .
static async ValueTask WhenAll(IEnumerable< ValueTask > tasks)
Fully await a given list of tasks .
static async ValueTask WhenAll(IReadOnlyList< ValueTask > tasks)
Fully await a given list of tasks .
static async ValueTask< T[]> WhenAll< T >(IEnumerable< ValueTask< T > > tasks, int totalTasks)
Fully await a given list of tasks .