ETTaskCompletionSource.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.ExceptionServices;
  4. namespace ETModel
  5. {
  6. internal class ExceptionHolder
  7. {
  8. private readonly ExceptionDispatchInfo exception;
  9. private bool calledGet;
  10. public ExceptionHolder(ExceptionDispatchInfo exception)
  11. {
  12. this.exception = exception;
  13. }
  14. public ExceptionDispatchInfo GetException()
  15. {
  16. if (calledGet)
  17. {
  18. return this.exception;
  19. }
  20. this.calledGet = true;
  21. GC.SuppressFinalize(this);
  22. return exception;
  23. }
  24. }
  25. public class ETTaskCompletionSource: IAwaiter
  26. {
  27. // State(= AwaiterStatus)
  28. private const int Pending = 0;
  29. private const int Succeeded = 1;
  30. private const int Faulted = 2;
  31. private const int Canceled = 3;
  32. private int state;
  33. private ExceptionHolder exception;
  34. private Action continuation; // action or list
  35. AwaiterStatus IAwaiter.Status => (AwaiterStatus) state;
  36. bool IAwaiter.IsCompleted => state != Pending;
  37. public ETTask Task => new ETTask(this);
  38. void IAwaiter.GetResult()
  39. {
  40. switch (this.state)
  41. {
  42. case Succeeded:
  43. return;
  44. case Faulted:
  45. this.exception.GetException().Throw();
  46. return;
  47. case Canceled:
  48. {
  49. if (this.exception != null)
  50. {
  51. this.exception.GetException().Throw(); // guranteed operation canceled exception.
  52. }
  53. throw new OperationCanceledException();
  54. }
  55. default:
  56. throw new NotSupportedException("ETTask does not allow call GetResult directly when task not completed. Please use 'await'.");
  57. }
  58. }
  59. void ICriticalNotifyCompletion.UnsafeOnCompleted(Action action)
  60. {
  61. this.continuation = action;
  62. if (state != Pending)
  63. {
  64. TryInvokeContinuation();
  65. }
  66. }
  67. private void TryInvokeContinuation()
  68. {
  69. this.continuation?.Invoke();
  70. this.continuation = null;
  71. }
  72. public void SetResult()
  73. {
  74. if (this.TrySetResult())
  75. {
  76. return;
  77. }
  78. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  79. }
  80. public void SetException(Exception e)
  81. {
  82. if (this.TrySetException(e))
  83. {
  84. return;
  85. }
  86. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  87. }
  88. public bool TrySetResult()
  89. {
  90. if (this.state != Pending)
  91. {
  92. return false;
  93. }
  94. this.state = Succeeded;
  95. this.TryInvokeContinuation();
  96. return true;
  97. }
  98. public bool TrySetException(Exception e)
  99. {
  100. if (this.state != Pending)
  101. {
  102. return false;
  103. }
  104. this.state = Faulted;
  105. this.exception = new ExceptionHolder(ExceptionDispatchInfo.Capture(e));
  106. this.TryInvokeContinuation();
  107. return true;
  108. }
  109. public bool TrySetCanceled()
  110. {
  111. if (this.state != Pending)
  112. {
  113. return false;
  114. }
  115. this.state = Canceled;
  116. this.TryInvokeContinuation();
  117. return true;
  118. }
  119. public bool TrySetCanceled(OperationCanceledException e)
  120. {
  121. if (this.state != Pending)
  122. {
  123. return false;
  124. }
  125. this.state = Canceled;
  126. this.exception = new ExceptionHolder(ExceptionDispatchInfo.Capture(e));
  127. this.TryInvokeContinuation();
  128. return true;
  129. }
  130. void INotifyCompletion.OnCompleted(Action action)
  131. {
  132. ((ICriticalNotifyCompletion) this).UnsafeOnCompleted(action);
  133. }
  134. }
  135. public class ETTaskCompletionSource<T>: IAwaiter<T>
  136. {
  137. // State(= AwaiterStatus)
  138. private const int Pending = 0;
  139. private const int Succeeded = 1;
  140. private const int Faulted = 2;
  141. private const int Canceled = 3;
  142. private int state;
  143. private T value;
  144. private ExceptionHolder exception;
  145. private Action continuation; // action or list
  146. bool IAwaiter.IsCompleted => state != Pending;
  147. public ETTask<T> Task => new ETTask<T>(this);
  148. public ETTask UnitTask => new ETTask(this);
  149. AwaiterStatus IAwaiter.Status => (AwaiterStatus) state;
  150. T IAwaiter<T>.GetResult()
  151. {
  152. switch (this.state)
  153. {
  154. case Succeeded:
  155. return this.value;
  156. case Faulted:
  157. this.exception.GetException().Throw();
  158. return default;
  159. case Canceled:
  160. {
  161. if (this.exception != null)
  162. {
  163. this.exception.GetException().Throw(); // guranteed operation canceled exception.
  164. }
  165. throw new OperationCanceledException();
  166. }
  167. default:
  168. throw new NotSupportedException("ETTask does not allow call GetResult directly when task not completed. Please use 'await'.");
  169. }
  170. }
  171. void ICriticalNotifyCompletion.UnsafeOnCompleted(Action action)
  172. {
  173. this.continuation = action;
  174. if (state != Pending)
  175. {
  176. TryInvokeContinuation();
  177. }
  178. }
  179. private void TryInvokeContinuation()
  180. {
  181. this.continuation?.Invoke();
  182. this.continuation = null;
  183. }
  184. public void SetResult(T result)
  185. {
  186. if (this.TrySetResult(result))
  187. {
  188. return;
  189. }
  190. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  191. }
  192. public void SetException(Exception e)
  193. {
  194. if (this.TrySetException(e))
  195. {
  196. return;
  197. }
  198. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  199. }
  200. public bool TrySetResult(T result)
  201. {
  202. if (this.state != Pending)
  203. {
  204. return false;
  205. }
  206. this.state = Succeeded;
  207. this.value = result;
  208. this.TryInvokeContinuation();
  209. return true;
  210. }
  211. public bool TrySetException(Exception e)
  212. {
  213. if (this.state != Pending)
  214. {
  215. return false;
  216. }
  217. this.state = Faulted;
  218. this.exception = new ExceptionHolder(ExceptionDispatchInfo.Capture(e));
  219. this.TryInvokeContinuation();
  220. return true;
  221. }
  222. public bool TrySetCanceled()
  223. {
  224. if (this.state != Pending)
  225. {
  226. return false;
  227. }
  228. this.state = Canceled;
  229. this.TryInvokeContinuation();
  230. return true;
  231. }
  232. public bool TrySetCanceled(OperationCanceledException e)
  233. {
  234. if (this.state != Pending)
  235. {
  236. return false;
  237. }
  238. this.state = Canceled;
  239. this.exception = new ExceptionHolder(ExceptionDispatchInfo.Capture(e));
  240. this.TryInvokeContinuation();
  241. return true;
  242. }
  243. void IAwaiter.GetResult()
  244. {
  245. ((IAwaiter<T>) this).GetResult();
  246. }
  247. void INotifyCompletion.OnCompleted(Action action)
  248. {
  249. ((ICriticalNotifyCompletion) this).UnsafeOnCompleted(action);
  250. }
  251. }
  252. }