ETTask.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.ExceptionServices;
  7. namespace ET
  8. {
  9. [AsyncMethodBuilder(typeof (ETAsyncTaskMethodBuilder))]
  10. public class ETTask: ICriticalNotifyCompletion
  11. {
  12. public static Action<Exception> ExceptionHandler;
  13. public static ETTaskCompleted CompletedTask
  14. {
  15. get
  16. {
  17. return new ETTaskCompleted();
  18. }
  19. }
  20. private static readonly ConcurrentQueue<ETTask> queue = new();
  21. /// <summary>
  22. /// 请不要随便使用ETTask的对象池,除非你完全搞懂了ETTask!!!
  23. /// 假如开启了池,await之后不能再操作ETTask,否则可能操作到再次从池中分配出来的ETTask,产生灾难性的后果
  24. /// SetResult的时候请现将tcs置空,避免多次对同一个ETTask SetResult
  25. /// </summary>
  26. public static ETTask Create(bool fromPool = false)
  27. {
  28. if (!fromPool)
  29. {
  30. return new ETTask();
  31. }
  32. if (!queue.TryDequeue(out ETTask task))
  33. {
  34. return new ETTask() {fromPool = true};
  35. }
  36. return task;
  37. }
  38. private void Recycle()
  39. {
  40. if (!this.fromPool)
  41. {
  42. return;
  43. }
  44. this.state = AwaiterStatus.Pending;
  45. this.callback = null;
  46. // 太多了
  47. if (queue.Count > 1000)
  48. {
  49. return;
  50. }
  51. queue.Enqueue(this);
  52. }
  53. private bool fromPool;
  54. private AwaiterStatus state;
  55. private object callback; // Action or ExceptionDispatchInfo
  56. private ETTask()
  57. {
  58. }
  59. [DebuggerHidden]
  60. private async ETVoid InnerCoroutine()
  61. {
  62. await this;
  63. }
  64. [DebuggerHidden]
  65. public void Coroutine()
  66. {
  67. InnerCoroutine().Coroutine();
  68. }
  69. [DebuggerHidden]
  70. public ETTask GetAwaiter()
  71. {
  72. return this;
  73. }
  74. public bool IsCompleted
  75. {
  76. [DebuggerHidden]
  77. get
  78. {
  79. return this.state != AwaiterStatus.Pending;
  80. }
  81. }
  82. [DebuggerHidden]
  83. public void UnsafeOnCompleted(Action action)
  84. {
  85. if (this.state != AwaiterStatus.Pending)
  86. {
  87. action?.Invoke();
  88. return;
  89. }
  90. this.callback = action;
  91. }
  92. [DebuggerHidden]
  93. public void OnCompleted(Action action)
  94. {
  95. this.UnsafeOnCompleted(action);
  96. }
  97. [DebuggerHidden]
  98. public void GetResult()
  99. {
  100. switch (this.state)
  101. {
  102. case AwaiterStatus.Succeeded:
  103. this.Recycle();
  104. break;
  105. case AwaiterStatus.Faulted:
  106. ExceptionDispatchInfo c = this.callback as ExceptionDispatchInfo;
  107. this.callback = null;
  108. this.Recycle();
  109. c?.Throw();
  110. break;
  111. default:
  112. throw new NotSupportedException("ETTask does not allow call GetResult directly when task not completed. Please use 'await'.");
  113. }
  114. }
  115. [DebuggerHidden]
  116. public void SetResult()
  117. {
  118. if (this.state != AwaiterStatus.Pending)
  119. {
  120. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  121. }
  122. this.state = AwaiterStatus.Succeeded;
  123. Action c = this.callback as Action;
  124. this.callback = null;
  125. c?.Invoke();
  126. }
  127. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  128. [DebuggerHidden]
  129. public void SetException(Exception e)
  130. {
  131. if (this.state != AwaiterStatus.Pending)
  132. {
  133. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  134. }
  135. this.state = AwaiterStatus.Faulted;
  136. Action c = this.callback as Action;
  137. this.callback = ExceptionDispatchInfo.Capture(e);
  138. c?.Invoke();
  139. }
  140. }
  141. [AsyncMethodBuilder(typeof (ETAsyncTaskMethodBuilder<>))]
  142. public class ETTask<T>: ICriticalNotifyCompletion
  143. {
  144. private static readonly ConcurrentQueue<ETTask<T>> queue = new();
  145. /// <summary>
  146. /// 请不要随便使用ETTask的对象池,除非你完全搞懂了ETTask!!!
  147. /// 假如开启了池,await之后不能再操作ETTask,否则可能操作到再次从池中分配出来的ETTask,产生灾难性的后果
  148. /// SetResult的时候请现将tcs置空,避免多次对同一个ETTask SetResult
  149. /// </summary>
  150. public static ETTask<T> Create(bool fromPool = false)
  151. {
  152. if (!fromPool)
  153. {
  154. return new ETTask<T>();
  155. }
  156. if (!queue.TryDequeue(out ETTask<T> task))
  157. {
  158. return new ETTask<T>() {fromPool = true};
  159. }
  160. return task;
  161. }
  162. private void Recycle()
  163. {
  164. if (!this.fromPool)
  165. {
  166. return;
  167. }
  168. this.callback = null;
  169. this.value = default;
  170. this.state = AwaiterStatus.Pending;
  171. // 太多了
  172. if (queue.Count > 1000)
  173. {
  174. return;
  175. }
  176. queue.Enqueue(this);
  177. }
  178. private bool fromPool;
  179. private AwaiterStatus state;
  180. private T value;
  181. private object callback; // Action or ExceptionDispatchInfo
  182. private ETTask()
  183. {
  184. }
  185. [DebuggerHidden]
  186. private async ETVoid InnerCoroutine()
  187. {
  188. await this;
  189. }
  190. [DebuggerHidden]
  191. public void Coroutine()
  192. {
  193. InnerCoroutine().Coroutine();
  194. }
  195. [DebuggerHidden]
  196. public ETTask<T> GetAwaiter()
  197. {
  198. return this;
  199. }
  200. [DebuggerHidden]
  201. public T GetResult()
  202. {
  203. switch (this.state)
  204. {
  205. case AwaiterStatus.Succeeded:
  206. T v = this.value;
  207. this.Recycle();
  208. return v;
  209. case AwaiterStatus.Faulted:
  210. ExceptionDispatchInfo c = this.callback as ExceptionDispatchInfo;
  211. this.callback = null;
  212. this.Recycle();
  213. c?.Throw();
  214. return default;
  215. default:
  216. throw new NotSupportedException("ETask does not allow call GetResult directly when task not completed. Please use 'await'.");
  217. }
  218. }
  219. public bool IsCompleted
  220. {
  221. [DebuggerHidden]
  222. get
  223. {
  224. return state != AwaiterStatus.Pending;
  225. }
  226. }
  227. [DebuggerHidden]
  228. public void UnsafeOnCompleted(Action action)
  229. {
  230. if (this.state != AwaiterStatus.Pending)
  231. {
  232. action?.Invoke();
  233. return;
  234. }
  235. this.callback = action;
  236. }
  237. [DebuggerHidden]
  238. public void OnCompleted(Action action)
  239. {
  240. this.UnsafeOnCompleted(action);
  241. }
  242. [DebuggerHidden]
  243. public void SetResult(T result)
  244. {
  245. if (this.state != AwaiterStatus.Pending)
  246. {
  247. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  248. }
  249. this.state = AwaiterStatus.Succeeded;
  250. this.value = result;
  251. Action c = this.callback as Action;
  252. this.callback = null;
  253. c?.Invoke();
  254. }
  255. [DebuggerHidden]
  256. public void SetException(Exception e)
  257. {
  258. if (this.state != AwaiterStatus.Pending)
  259. {
  260. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  261. }
  262. this.state = AwaiterStatus.Faulted;
  263. Action c = this.callback as Action;
  264. this.callback = ExceptionDispatchInfo.Capture(e);
  265. c?.Invoke();
  266. }
  267. }
  268. }