UniTask.WhenAny.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using Cysharp.Threading.Tasks.Internal;
  6. namespace Cysharp.Threading.Tasks
  7. {
  8. public partial struct UniTask
  9. {
  10. public static UniTask<(bool hasResultLeft, T result)> WhenAny<T>(UniTask<T> leftTask, UniTask rightTask)
  11. {
  12. return new UniTask<(bool, T)>(new WhenAnyLRPromise<T>(leftTask, rightTask), 0);
  13. }
  14. public static UniTask<(int winArgumentIndex, T result)> WhenAny<T>(params UniTask<T>[] tasks)
  15. {
  16. return new UniTask<(int, T)>(new WhenAnyPromise<T>(tasks, tasks.Length), 0);
  17. }
  18. public static UniTask<(int winArgumentIndex, T result)> WhenAny<T>(IEnumerable<UniTask<T>> tasks)
  19. {
  20. using (var span = ArrayPoolUtil.Materialize(tasks))
  21. {
  22. return new UniTask<(int, T)>(new WhenAnyPromise<T>(span.Array, span.Length), 0);
  23. }
  24. }
  25. /// <summary>Return value is winArgumentIndex</summary>
  26. public static UniTask<int> WhenAny(params UniTask[] tasks)
  27. {
  28. return new UniTask<int>(new WhenAnyPromise(tasks, tasks.Length), 0);
  29. }
  30. /// <summary>Return value is winArgumentIndex</summary>
  31. public static UniTask<int> WhenAny(IEnumerable<UniTask> tasks)
  32. {
  33. using (var span = ArrayPoolUtil.Materialize(tasks))
  34. {
  35. return new UniTask<int>(new WhenAnyPromise(span.Array, span.Length), 0);
  36. }
  37. }
  38. sealed class WhenAnyLRPromise<T> : IUniTaskSource<(bool, T)>
  39. {
  40. int completedCount;
  41. UniTaskCompletionSourceCore<(bool, T)> core;
  42. public WhenAnyLRPromise(UniTask<T> leftTask, UniTask rightTask)
  43. {
  44. TaskTracker.TrackActiveTask(this, 3);
  45. {
  46. UniTask<T>.Awaiter awaiter;
  47. try
  48. {
  49. awaiter = leftTask.GetAwaiter();
  50. }
  51. catch (Exception ex)
  52. {
  53. core.TrySetException(ex);
  54. goto RIGHT;
  55. }
  56. if (awaiter.IsCompleted)
  57. {
  58. TryLeftInvokeContinuation(this, awaiter);
  59. }
  60. else
  61. {
  62. awaiter.SourceOnCompleted(state =>
  63. {
  64. using (var t = (StateTuple<WhenAnyLRPromise<T>, UniTask<T>.Awaiter>)state)
  65. {
  66. TryLeftInvokeContinuation(t.Item1, t.Item2);
  67. }
  68. }, StateTuple.Create(this, awaiter));
  69. }
  70. }
  71. RIGHT:
  72. {
  73. UniTask.Awaiter awaiter;
  74. try
  75. {
  76. awaiter = rightTask.GetAwaiter();
  77. }
  78. catch (Exception ex)
  79. {
  80. core.TrySetException(ex);
  81. return;
  82. }
  83. if (awaiter.IsCompleted)
  84. {
  85. TryRightInvokeContinuation(this, awaiter);
  86. }
  87. else
  88. {
  89. awaiter.SourceOnCompleted(state =>
  90. {
  91. using (var t = (StateTuple<WhenAnyLRPromise<T>, UniTask.Awaiter>)state)
  92. {
  93. TryRightInvokeContinuation(t.Item1, t.Item2);
  94. }
  95. }, StateTuple.Create(this, awaiter));
  96. }
  97. }
  98. }
  99. static void TryLeftInvokeContinuation(WhenAnyLRPromise<T> self, in UniTask<T>.Awaiter awaiter)
  100. {
  101. T result;
  102. try
  103. {
  104. result = awaiter.GetResult();
  105. }
  106. catch (Exception ex)
  107. {
  108. self.core.TrySetException(ex);
  109. return;
  110. }
  111. if (Interlocked.Increment(ref self.completedCount) == 1)
  112. {
  113. self.core.TrySetResult((true, result));
  114. }
  115. }
  116. static void TryRightInvokeContinuation(WhenAnyLRPromise<T> self, in UniTask.Awaiter awaiter)
  117. {
  118. try
  119. {
  120. awaiter.GetResult();
  121. }
  122. catch (Exception ex)
  123. {
  124. self.core.TrySetException(ex);
  125. return;
  126. }
  127. if (Interlocked.Increment(ref self.completedCount) == 1)
  128. {
  129. self.core.TrySetResult((false, default));
  130. }
  131. }
  132. public (bool, T) GetResult(short token)
  133. {
  134. TaskTracker.RemoveTracking(this);
  135. GC.SuppressFinalize(this);
  136. return core.GetResult(token);
  137. }
  138. public UniTaskStatus GetStatus(short token)
  139. {
  140. return core.GetStatus(token);
  141. }
  142. public void OnCompleted(Action<object> continuation, object state, short token)
  143. {
  144. core.OnCompleted(continuation, state, token);
  145. }
  146. public UniTaskStatus UnsafeGetStatus()
  147. {
  148. return core.UnsafeGetStatus();
  149. }
  150. void IUniTaskSource.GetResult(short token)
  151. {
  152. GetResult(token);
  153. }
  154. }
  155. sealed class WhenAnyPromise<T> : IUniTaskSource<(int, T)>
  156. {
  157. int completedCount;
  158. UniTaskCompletionSourceCore<(int, T)> core;
  159. public WhenAnyPromise(UniTask<T>[] tasks, int tasksLength)
  160. {
  161. if (tasksLength == 0)
  162. {
  163. throw new ArgumentException("The tasks argument contains no tasks.");
  164. }
  165. TaskTracker.TrackActiveTask(this, 3);
  166. for (int i = 0; i < tasksLength; i++)
  167. {
  168. UniTask<T>.Awaiter awaiter;
  169. try
  170. {
  171. awaiter = tasks[i].GetAwaiter();
  172. }
  173. catch (Exception ex)
  174. {
  175. core.TrySetException(ex);
  176. continue; // consume others.
  177. }
  178. if (awaiter.IsCompleted)
  179. {
  180. TryInvokeContinuation(this, awaiter, i);
  181. }
  182. else
  183. {
  184. awaiter.SourceOnCompleted(state =>
  185. {
  186. using (var t = (StateTuple<WhenAnyPromise<T>, UniTask<T>.Awaiter, int>)state)
  187. {
  188. TryInvokeContinuation(t.Item1, t.Item2, t.Item3);
  189. }
  190. }, StateTuple.Create(this, awaiter, i));
  191. }
  192. }
  193. }
  194. static void TryInvokeContinuation(WhenAnyPromise<T> self, in UniTask<T>.Awaiter awaiter, int i)
  195. {
  196. T result;
  197. try
  198. {
  199. result = awaiter.GetResult();
  200. }
  201. catch (Exception ex)
  202. {
  203. self.core.TrySetException(ex);
  204. return;
  205. }
  206. if (Interlocked.Increment(ref self.completedCount) == 1)
  207. {
  208. self.core.TrySetResult((i, result));
  209. }
  210. }
  211. public (int, T) GetResult(short token)
  212. {
  213. TaskTracker.RemoveTracking(this);
  214. GC.SuppressFinalize(this);
  215. return core.GetResult(token);
  216. }
  217. public UniTaskStatus GetStatus(short token)
  218. {
  219. return core.GetStatus(token);
  220. }
  221. public void OnCompleted(Action<object> continuation, object state, short token)
  222. {
  223. core.OnCompleted(continuation, state, token);
  224. }
  225. public UniTaskStatus UnsafeGetStatus()
  226. {
  227. return core.UnsafeGetStatus();
  228. }
  229. void IUniTaskSource.GetResult(short token)
  230. {
  231. GetResult(token);
  232. }
  233. }
  234. sealed class WhenAnyPromise : IUniTaskSource<int>
  235. {
  236. int completedCount;
  237. UniTaskCompletionSourceCore<int> core;
  238. public WhenAnyPromise(UniTask[] tasks, int tasksLength)
  239. {
  240. if (tasksLength == 0)
  241. {
  242. throw new ArgumentException("The tasks argument contains no tasks.");
  243. }
  244. TaskTracker.TrackActiveTask(this, 3);
  245. for (int i = 0; i < tasksLength; i++)
  246. {
  247. UniTask.Awaiter awaiter;
  248. try
  249. {
  250. awaiter = tasks[i].GetAwaiter();
  251. }
  252. catch (Exception ex)
  253. {
  254. core.TrySetException(ex);
  255. continue; // consume others.
  256. }
  257. if (awaiter.IsCompleted)
  258. {
  259. TryInvokeContinuation(this, awaiter, i);
  260. }
  261. else
  262. {
  263. awaiter.SourceOnCompleted(state =>
  264. {
  265. using (var t = (StateTuple<WhenAnyPromise, UniTask.Awaiter, int>)state)
  266. {
  267. TryInvokeContinuation(t.Item1, t.Item2, t.Item3);
  268. }
  269. }, StateTuple.Create(this, awaiter, i));
  270. }
  271. }
  272. }
  273. static void TryInvokeContinuation(WhenAnyPromise self, in UniTask.Awaiter awaiter, int i)
  274. {
  275. try
  276. {
  277. awaiter.GetResult();
  278. }
  279. catch (Exception ex)
  280. {
  281. self.core.TrySetException(ex);
  282. return;
  283. }
  284. if (Interlocked.Increment(ref self.completedCount) == 1)
  285. {
  286. self.core.TrySetResult(i);
  287. }
  288. }
  289. public int GetResult(short token)
  290. {
  291. TaskTracker.RemoveTracking(this);
  292. GC.SuppressFinalize(this);
  293. return core.GetResult(token);
  294. }
  295. public UniTaskStatus GetStatus(short token)
  296. {
  297. return core.GetStatus(token);
  298. }
  299. public void OnCompleted(Action<object> continuation, object state, short token)
  300. {
  301. core.OnCompleted(continuation, state, token);
  302. }
  303. public UniTaskStatus UnsafeGetStatus()
  304. {
  305. return core.UnsafeGetStatus();
  306. }
  307. void IUniTaskSource.GetResult(short token)
  308. {
  309. GetResult(token);
  310. }
  311. }
  312. }
  313. }