ETTaskHelper.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ET
  4. {
  5. public static class ETTaskHelper
  6. {
  7. private class CoroutineBlocker
  8. {
  9. private int count;
  10. private List<ETTask> tcss = new List<ETTask>();
  11. public CoroutineBlocker(int count)
  12. {
  13. this.count = count;
  14. }
  15. public async ETTask WaitAsync()
  16. {
  17. --this.count;
  18. if (this.count < 0)
  19. {
  20. return;
  21. }
  22. if (this.count == 0)
  23. {
  24. List<ETTask> t = this.tcss;
  25. this.tcss = null;
  26. foreach (ETTask ttcs in t)
  27. {
  28. ttcs.SetResult();
  29. }
  30. return;
  31. }
  32. ETTask tcs = ETTask.Create(true);
  33. tcss.Add(tcs);
  34. await tcs;
  35. }
  36. }
  37. public static async ETTask<bool> WaitAny<T>(ETTask<T>[] tasks, ETCancellationToken cancellationToken = null)
  38. {
  39. if (tasks.Length == 0)
  40. {
  41. return false;
  42. }
  43. CoroutineBlocker coroutineBlocker = new CoroutineBlocker(2);
  44. foreach (ETTask<T> task in tasks)
  45. {
  46. RunOneTask(task).Coroutine();
  47. }
  48. async ETVoid RunOneTask(ETTask<T> task)
  49. {
  50. await task;
  51. await coroutineBlocker.WaitAsync();
  52. }
  53. await coroutineBlocker.WaitAsync();
  54. if (cancellationToken == null)
  55. {
  56. return true;
  57. }
  58. return !cancellationToken.IsCancel();
  59. }
  60. public static async ETTask<bool> WaitAny(ETTask[] tasks, ETCancellationToken cancellationToken = null)
  61. {
  62. if (tasks.Length == 0)
  63. {
  64. return false;
  65. }
  66. CoroutineBlocker coroutineBlocker = new CoroutineBlocker(2);
  67. foreach (ETTask task in tasks)
  68. {
  69. RunOneTask(task).Coroutine();
  70. }
  71. async ETVoid RunOneTask(ETTask task)
  72. {
  73. await task;
  74. await coroutineBlocker.WaitAsync();
  75. }
  76. await coroutineBlocker.WaitAsync();
  77. if (cancellationToken == null)
  78. {
  79. return true;
  80. }
  81. return !cancellationToken.IsCancel();
  82. }
  83. public static async ETTask<bool> WaitAll<T>(ETTask<T>[] tasks, ETCancellationToken cancellationToken = null)
  84. {
  85. if (tasks.Length == 0)
  86. {
  87. return false;
  88. }
  89. CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Length + 1);
  90. foreach (ETTask<T> task in tasks)
  91. {
  92. RunOneTask(task).Coroutine();
  93. }
  94. async ETVoid RunOneTask(ETTask<T> task)
  95. {
  96. await task;
  97. await coroutineBlocker.WaitAsync();
  98. }
  99. await coroutineBlocker.WaitAsync();
  100. if (cancellationToken == null)
  101. {
  102. return true;
  103. }
  104. return !cancellationToken.IsCancel();
  105. }
  106. public static async ETTask<bool> WaitAll<T>(List<ETTask<T>> tasks, ETCancellationToken cancellationToken = null)
  107. {
  108. if (tasks.Count == 0)
  109. {
  110. return false;
  111. }
  112. CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Count + 1);
  113. foreach (ETTask<T> task in tasks)
  114. {
  115. RunOneTask(task).Coroutine();
  116. }
  117. async ETVoid RunOneTask(ETTask<T> task)
  118. {
  119. await task;
  120. await coroutineBlocker.WaitAsync();
  121. }
  122. await coroutineBlocker.WaitAsync();
  123. if (cancellationToken == null)
  124. {
  125. return true;
  126. }
  127. return !cancellationToken.IsCancel();
  128. }
  129. public static async ETTask<bool> WaitAll(ETTask[] tasks, ETCancellationToken cancellationToken = null)
  130. {
  131. if (tasks.Length == 0)
  132. {
  133. return false;
  134. }
  135. CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Length + 1);
  136. foreach (ETTask task in tasks)
  137. {
  138. RunOneTask(task).Coroutine();
  139. }
  140. await coroutineBlocker.WaitAsync();
  141. async ETVoid RunOneTask(ETTask task)
  142. {
  143. await task;
  144. await coroutineBlocker.WaitAsync();
  145. }
  146. if (cancellationToken == null)
  147. {
  148. return true;
  149. }
  150. return !cancellationToken.IsCancel();
  151. }
  152. public static async ETTask<bool> WaitAll(List<ETTask> tasks, ETCancellationToken cancellationToken = null)
  153. {
  154. if (tasks.Count == 0)
  155. {
  156. return false;
  157. }
  158. CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Count + 1);
  159. foreach (ETTask task in tasks)
  160. {
  161. RunOneTask(task).Coroutine();
  162. }
  163. await coroutineBlocker.WaitAsync();
  164. async ETVoid RunOneTask(ETTask task)
  165. {
  166. await task;
  167. await coroutineBlocker.WaitAsync();
  168. }
  169. if (cancellationToken == null)
  170. {
  171. return true;
  172. }
  173. return !cancellationToken.IsCancel();
  174. }
  175. }
  176. }