ETTaskCompletionSource.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.ExceptionServices;
  5. using System.Threading;
  6. namespace ETModel
  7. {
  8. internal class ExceptionHolder
  9. {
  10. private readonly ExceptionDispatchInfo exception;
  11. private bool calledGet;
  12. public ExceptionHolder(ExceptionDispatchInfo exception)
  13. {
  14. this.exception = exception;
  15. }
  16. public ExceptionDispatchInfo GetException()
  17. {
  18. if (calledGet)
  19. {
  20. return this.exception;
  21. }
  22. this.calledGet = true;
  23. GC.SuppressFinalize(this);
  24. return exception;
  25. }
  26. }
  27. public interface IResolvePromise
  28. {
  29. bool TrySetResult();
  30. }
  31. public interface IResolvePromise<T>
  32. {
  33. bool TrySetResult(T value);
  34. }
  35. public interface IRejectPromise
  36. {
  37. bool TrySetException(Exception e);
  38. }
  39. public interface ICancelPromise
  40. {
  41. bool TrySetCanceled();
  42. }
  43. public interface IPromise<T>: IResolvePromise<T>, IRejectPromise, ICancelPromise
  44. {
  45. }
  46. public interface IPromise: IResolvePromise, IRejectPromise, ICancelPromise
  47. {
  48. }
  49. public class ETTaskCompletionSource: IAwaiter, IPromise
  50. {
  51. // State(= AwaiterStatus)
  52. private const int Pending = 0;
  53. private const int Succeeded = 1;
  54. private const int Faulted = 2;
  55. private const int Canceled = 3;
  56. private int state;
  57. private ExceptionHolder exception;
  58. private object continuation; // action or list
  59. AwaiterStatus IAwaiter.Status => (AwaiterStatus) state;
  60. bool IAwaiter.IsCompleted => state != Pending;
  61. public ETTask Task => new ETTask(this);
  62. void IAwaiter.GetResult()
  63. {
  64. switch (this.state)
  65. {
  66. case Succeeded:
  67. return;
  68. case Faulted:
  69. this.exception.GetException().Throw();
  70. return;
  71. case Canceled:
  72. {
  73. if (this.exception != null)
  74. {
  75. this.exception.GetException().Throw(); // guranteed operation canceled exception.
  76. }
  77. throw new OperationCanceledException();
  78. }
  79. default:
  80. throw new NotSupportedException("UniTask does not allow call GetResult directly when task not completed. Please use 'await'.");
  81. }
  82. }
  83. void ICriticalNotifyCompletion.UnsafeOnCompleted(Action action)
  84. {
  85. if (Interlocked.CompareExchange(ref continuation, action, null) == null)
  86. {
  87. if (state != Pending)
  88. {
  89. TryInvokeContinuation();
  90. }
  91. }
  92. else
  93. {
  94. object c = continuation;
  95. if (c is Action action1)
  96. {
  97. var list = new List<Action>();
  98. list.Add(action1);
  99. list.Add(action);
  100. if (Interlocked.CompareExchange(ref continuation, list, action1) == action1)
  101. {
  102. goto TRYINVOKE;
  103. }
  104. }
  105. var l = (List<Action>) continuation;
  106. lock (l)
  107. {
  108. l.Add(action);
  109. }
  110. TRYINVOKE:
  111. if (state != Pending)
  112. {
  113. TryInvokeContinuation();
  114. }
  115. }
  116. }
  117. private void TryInvokeContinuation()
  118. {
  119. object c = Interlocked.Exchange(ref continuation, null);
  120. if (c == null)
  121. {
  122. return;
  123. }
  124. if (c is Action action)
  125. {
  126. action.Invoke();
  127. }
  128. else
  129. {
  130. var l = (List<Action>) c;
  131. int cnt = l.Count;
  132. for (int i = 0; i < cnt; i++)
  133. {
  134. l[i].Invoke();
  135. }
  136. }
  137. }
  138. public void SetResult()
  139. {
  140. if (this.TrySetResult())
  141. {
  142. return;
  143. }
  144. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  145. }
  146. public void SetException(Exception e)
  147. {
  148. if (this.TrySetException(e))
  149. {
  150. return;
  151. }
  152. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  153. }
  154. public bool TrySetResult()
  155. {
  156. if (Interlocked.CompareExchange(ref this.state, Succeeded, Pending) != Pending)
  157. {
  158. return false;
  159. }
  160. this.TryInvokeContinuation();
  161. return true;
  162. }
  163. public bool TrySetException(Exception e)
  164. {
  165. if (Interlocked.CompareExchange(ref this.state, Faulted, Pending) != Pending)
  166. {
  167. return false;
  168. }
  169. this.exception = new ExceptionHolder(ExceptionDispatchInfo.Capture(e));
  170. this.TryInvokeContinuation();
  171. return true;
  172. }
  173. public bool TrySetCanceled()
  174. {
  175. if (Interlocked.CompareExchange(ref this.state, Canceled, Pending) != Pending)
  176. {
  177. return false;
  178. }
  179. this.TryInvokeContinuation();
  180. return true;
  181. }
  182. public bool TrySetCanceled(OperationCanceledException e)
  183. {
  184. if (Interlocked.CompareExchange(ref this.state, Canceled, Pending) != Pending)
  185. {
  186. return false;
  187. }
  188. this.exception = new ExceptionHolder(ExceptionDispatchInfo.Capture(e));
  189. this.TryInvokeContinuation();
  190. return true;
  191. }
  192. void INotifyCompletion.OnCompleted(Action action)
  193. {
  194. ((ICriticalNotifyCompletion) this).UnsafeOnCompleted(action);
  195. }
  196. }
  197. public class ETTaskCompletionSource<T>: IAwaiter<T>, IPromise<T>
  198. {
  199. // State(= AwaiterStatus)
  200. private const int Pending = 0;
  201. private const int Succeeded = 1;
  202. private const int Faulted = 2;
  203. private const int Canceled = 3;
  204. private int state;
  205. private T value;
  206. private ExceptionHolder exception;
  207. private object continuation; // action or list
  208. bool IAwaiter.IsCompleted => state != Pending;
  209. public ETTask<T> Task => new ETTask<T>(this);
  210. public ETTask UnitTask => new ETTask(this);
  211. AwaiterStatus IAwaiter.Status => (AwaiterStatus) state;
  212. T IAwaiter<T>.GetResult()
  213. {
  214. switch (this.state)
  215. {
  216. case Succeeded:
  217. return this.value;
  218. case Faulted:
  219. this.exception.GetException().Throw();
  220. return default;
  221. case Canceled:
  222. {
  223. if (this.exception != null)
  224. {
  225. this.exception.GetException().Throw(); // guranteed operation canceled exception.
  226. }
  227. throw new OperationCanceledException();
  228. }
  229. default:
  230. throw new NotSupportedException("UniTask does not allow call GetResult directly when task not completed. Please use 'await'.");
  231. }
  232. }
  233. void ICriticalNotifyCompletion.UnsafeOnCompleted(Action action)
  234. {
  235. if (Interlocked.CompareExchange(ref continuation, action, null) == null)
  236. {
  237. if (state != Pending)
  238. {
  239. TryInvokeContinuation();
  240. }
  241. }
  242. else
  243. {
  244. var c = continuation;
  245. if (c is Action action1)
  246. {
  247. var list = new List<Action>();
  248. list.Add(action1);
  249. list.Add(action);
  250. if (Interlocked.CompareExchange(ref continuation, list, action1) == action1)
  251. {
  252. goto TRYINVOKE;
  253. }
  254. }
  255. var l = (List<Action>) continuation;
  256. lock (l)
  257. {
  258. l.Add(action);
  259. }
  260. TRYINVOKE:
  261. if (state != Pending)
  262. {
  263. TryInvokeContinuation();
  264. }
  265. }
  266. }
  267. private void TryInvokeContinuation()
  268. {
  269. object c = Interlocked.Exchange(ref continuation, null);
  270. if (c == null)
  271. {
  272. return;
  273. }
  274. if (c is Action action)
  275. {
  276. action.Invoke();
  277. return;
  278. }
  279. var l = (List<Action>) c;
  280. int cnt = l.Count;
  281. for (int i = 0; i < cnt; i++)
  282. {
  283. l[i].Invoke();
  284. }
  285. }
  286. public void SetResult(T result)
  287. {
  288. if (this.TrySetResult(result))
  289. {
  290. return;
  291. }
  292. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  293. }
  294. public void SetException(Exception e)
  295. {
  296. if (this.TrySetException(e))
  297. {
  298. return;
  299. }
  300. throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
  301. }
  302. public bool TrySetResult(T result)
  303. {
  304. if (Interlocked.CompareExchange(ref this.state, Succeeded, Pending) != Pending)
  305. {
  306. return false;
  307. }
  308. this.value = result;
  309. this.TryInvokeContinuation();
  310. return true;
  311. }
  312. public bool TrySetException(Exception e)
  313. {
  314. if (Interlocked.CompareExchange(ref this.state, Faulted, Pending) != Pending)
  315. {
  316. return false;
  317. }
  318. this.exception = new ExceptionHolder(ExceptionDispatchInfo.Capture(e));
  319. this.TryInvokeContinuation();
  320. return true;
  321. }
  322. public bool TrySetCanceled()
  323. {
  324. if (Interlocked.CompareExchange(ref this.state, Canceled, Pending) != Pending)
  325. {
  326. return false;
  327. }
  328. this.TryInvokeContinuation();
  329. return true;
  330. }
  331. public bool TrySetCanceled(OperationCanceledException e)
  332. {
  333. if (Interlocked.CompareExchange(ref this.state, Canceled, Pending) != Pending)
  334. {
  335. return false;
  336. }
  337. this.exception = new ExceptionHolder(ExceptionDispatchInfo.Capture(e));
  338. this.TryInvokeContinuation();
  339. return true;
  340. }
  341. void IAwaiter.GetResult()
  342. {
  343. ((IAwaiter<T>) this).GetResult();
  344. }
  345. void INotifyCompletion.OnCompleted(Action action)
  346. {
  347. ((ICriticalNotifyCompletion) this).UnsafeOnCompleted(action);
  348. }
  349. }
  350. }