AsyncLazy.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  2. using System;
  3. using System.Threading;
  4. namespace Cysharp.Threading.Tasks
  5. {
  6. public class AsyncLazy
  7. {
  8. static Action<object> continuation = SetCompletionSource;
  9. Func<UniTask> taskFactory;
  10. UniTaskCompletionSource completionSource;
  11. UniTask.Awaiter awaiter;
  12. object syncLock;
  13. bool initialized;
  14. public AsyncLazy(Func<UniTask> taskFactory)
  15. {
  16. this.taskFactory = taskFactory;
  17. this.completionSource = new UniTaskCompletionSource();
  18. this.syncLock = new object();
  19. this.initialized = false;
  20. }
  21. internal AsyncLazy(UniTask task)
  22. {
  23. this.taskFactory = null;
  24. this.completionSource = new UniTaskCompletionSource();
  25. this.syncLock = null;
  26. this.initialized = true;
  27. var awaiter = task.GetAwaiter();
  28. if (awaiter.IsCompleted)
  29. {
  30. SetCompletionSource(awaiter);
  31. }
  32. else
  33. {
  34. this.awaiter = awaiter;
  35. awaiter.SourceOnCompleted(continuation, this);
  36. }
  37. }
  38. public UniTask Task
  39. {
  40. get
  41. {
  42. EnsureInitialized();
  43. return completionSource.Task;
  44. }
  45. }
  46. public UniTask.Awaiter GetAwaiter() => Task.GetAwaiter();
  47. void EnsureInitialized()
  48. {
  49. if (Volatile.Read(ref initialized))
  50. {
  51. return;
  52. }
  53. EnsureInitializedCore();
  54. }
  55. void EnsureInitializedCore()
  56. {
  57. lock (syncLock)
  58. {
  59. if (!Volatile.Read(ref initialized))
  60. {
  61. var f = Interlocked.Exchange(ref taskFactory, null);
  62. if (f != null)
  63. {
  64. var task = f();
  65. var awaiter = task.GetAwaiter();
  66. if (awaiter.IsCompleted)
  67. {
  68. SetCompletionSource(awaiter);
  69. }
  70. else
  71. {
  72. this.awaiter = awaiter;
  73. awaiter.SourceOnCompleted(continuation, this);
  74. }
  75. Volatile.Write(ref initialized, true);
  76. }
  77. }
  78. }
  79. }
  80. void SetCompletionSource(in UniTask.Awaiter awaiter)
  81. {
  82. try
  83. {
  84. awaiter.GetResult();
  85. completionSource.TrySetResult();
  86. }
  87. catch (Exception ex)
  88. {
  89. completionSource.TrySetException(ex);
  90. }
  91. }
  92. static void SetCompletionSource(object state)
  93. {
  94. var self = (AsyncLazy)state;
  95. try
  96. {
  97. self.awaiter.GetResult();
  98. self.completionSource.TrySetResult();
  99. }
  100. catch (Exception ex)
  101. {
  102. self.completionSource.TrySetException(ex);
  103. }
  104. finally
  105. {
  106. self.awaiter = default;
  107. }
  108. }
  109. }
  110. public class AsyncLazy<T>
  111. {
  112. static Action<object> continuation = SetCompletionSource;
  113. Func<UniTask<T>> taskFactory;
  114. UniTaskCompletionSource<T> completionSource;
  115. UniTask<T>.Awaiter awaiter;
  116. object syncLock;
  117. bool initialized;
  118. public AsyncLazy(Func<UniTask<T>> taskFactory)
  119. {
  120. this.taskFactory = taskFactory;
  121. this.completionSource = new UniTaskCompletionSource<T>();
  122. this.syncLock = new object();
  123. this.initialized = false;
  124. }
  125. internal AsyncLazy(UniTask<T> task)
  126. {
  127. this.taskFactory = null;
  128. this.completionSource = new UniTaskCompletionSource<T>();
  129. this.syncLock = null;
  130. this.initialized = true;
  131. var awaiter = task.GetAwaiter();
  132. if (awaiter.IsCompleted)
  133. {
  134. SetCompletionSource(awaiter);
  135. }
  136. else
  137. {
  138. this.awaiter = awaiter;
  139. awaiter.SourceOnCompleted(continuation, this);
  140. }
  141. }
  142. public UniTask<T> Task
  143. {
  144. get
  145. {
  146. EnsureInitialized();
  147. return completionSource.Task;
  148. }
  149. }
  150. public UniTask<T>.Awaiter GetAwaiter() => Task.GetAwaiter();
  151. void EnsureInitialized()
  152. {
  153. if (Volatile.Read(ref initialized))
  154. {
  155. return;
  156. }
  157. EnsureInitializedCore();
  158. }
  159. void EnsureInitializedCore()
  160. {
  161. lock (syncLock)
  162. {
  163. if (!Volatile.Read(ref initialized))
  164. {
  165. var f = Interlocked.Exchange(ref taskFactory, null);
  166. if (f != null)
  167. {
  168. var task = f();
  169. var awaiter = task.GetAwaiter();
  170. if (awaiter.IsCompleted)
  171. {
  172. SetCompletionSource(awaiter);
  173. }
  174. else
  175. {
  176. this.awaiter = awaiter;
  177. awaiter.SourceOnCompleted(continuation, this);
  178. }
  179. Volatile.Write(ref initialized, true);
  180. }
  181. }
  182. }
  183. }
  184. void SetCompletionSource(in UniTask<T>.Awaiter awaiter)
  185. {
  186. try
  187. {
  188. var result = awaiter.GetResult();
  189. completionSource.TrySetResult(result);
  190. }
  191. catch (Exception ex)
  192. {
  193. completionSource.TrySetException(ex);
  194. }
  195. }
  196. static void SetCompletionSource(object state)
  197. {
  198. var self = (AsyncLazy<T>)state;
  199. try
  200. {
  201. var result = self.awaiter.GetResult();
  202. self.completionSource.TrySetResult(result);
  203. }
  204. catch (Exception ex)
  205. {
  206. self.completionSource.TrySetException(ex);
  207. }
  208. finally
  209. {
  210. self.awaiter = default;
  211. }
  212. }
  213. }
  214. }