ETTaskHelper.cs 5.8 KB

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