Scheduler.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading;
  6. namespace UniRx
  7. {
  8. // Scheduler Extension
  9. public static partial class Scheduler
  10. {
  11. // configurable defaults
  12. public static class DefaultSchedulers
  13. {
  14. static IScheduler constantTime;
  15. public static IScheduler ConstantTimeOperations
  16. {
  17. get
  18. {
  19. return constantTime ?? (constantTime = Scheduler.Immediate);
  20. }
  21. set
  22. {
  23. constantTime = value;
  24. }
  25. }
  26. static IScheduler tailRecursion;
  27. public static IScheduler TailRecursion
  28. {
  29. get
  30. {
  31. return tailRecursion ?? (tailRecursion = Scheduler.Immediate);
  32. }
  33. set
  34. {
  35. tailRecursion = value;
  36. }
  37. }
  38. static IScheduler iteration;
  39. public static IScheduler Iteration
  40. {
  41. get
  42. {
  43. return iteration ?? (iteration = Scheduler.CurrentThread);
  44. }
  45. set
  46. {
  47. iteration = value;
  48. }
  49. }
  50. static IScheduler timeBasedOperations;
  51. public static IScheduler TimeBasedOperations
  52. {
  53. get
  54. {
  55. #if UniRxLibrary
  56. return timeBasedOperations ?? (timeBasedOperations = Scheduler.ThreadPool);
  57. #else
  58. return timeBasedOperations ?? (timeBasedOperations = Scheduler.MainThread); // MainThread as default for TimeBased Operation
  59. #endif
  60. }
  61. set
  62. {
  63. timeBasedOperations = value;
  64. }
  65. }
  66. static IScheduler asyncConversions;
  67. public static IScheduler AsyncConversions
  68. {
  69. get
  70. {
  71. #if WEB_GL
  72. // WebGL does not support threadpool
  73. return asyncConversions ?? (asyncConversions = Scheduler.MainThread);
  74. #else
  75. return asyncConversions ?? (asyncConversions = Scheduler.ThreadPool);
  76. #endif
  77. }
  78. set
  79. {
  80. asyncConversions = value;
  81. }
  82. }
  83. public static void SetDotNetCompatible()
  84. {
  85. ConstantTimeOperations = Scheduler.Immediate;
  86. TailRecursion = Scheduler.Immediate;
  87. Iteration = Scheduler.CurrentThread;
  88. TimeBasedOperations = Scheduler.ThreadPool;
  89. AsyncConversions = Scheduler.ThreadPool;
  90. }
  91. }
  92. // utils
  93. public static DateTimeOffset Now
  94. {
  95. get { return DateTimeOffset.UtcNow; }
  96. }
  97. public static TimeSpan Normalize(TimeSpan timeSpan)
  98. {
  99. return timeSpan >= TimeSpan.Zero ? timeSpan : TimeSpan.Zero;
  100. }
  101. public static IDisposable Schedule(this IScheduler scheduler, DateTimeOffset dueTime, Action action)
  102. {
  103. return scheduler.Schedule(dueTime - scheduler.Now, action);
  104. }
  105. public static IDisposable Schedule(this IScheduler scheduler, Action<Action> action)
  106. {
  107. // InvokeRec1
  108. var group = new CompositeDisposable(1);
  109. var gate = new object();
  110. Action recursiveAction = null;
  111. recursiveAction = () => action(() =>
  112. {
  113. var isAdded = false;
  114. var isDone = false;
  115. var d = default(IDisposable);
  116. d = scheduler.Schedule(() =>
  117. {
  118. lock (gate)
  119. {
  120. if (isAdded)
  121. group.Remove(d);
  122. else
  123. isDone = true;
  124. }
  125. recursiveAction();
  126. });
  127. lock (gate)
  128. {
  129. if (!isDone)
  130. {
  131. group.Add(d);
  132. isAdded = true;
  133. }
  134. }
  135. });
  136. group.Add(scheduler.Schedule(recursiveAction));
  137. return group;
  138. }
  139. public static IDisposable Schedule(this IScheduler scheduler, TimeSpan dueTime, Action<Action<TimeSpan>> action)
  140. {
  141. // InvokeRec2
  142. var group = new CompositeDisposable(1);
  143. var gate = new object();
  144. Action recursiveAction = null;
  145. recursiveAction = () => action(dt =>
  146. {
  147. var isAdded = false;
  148. var isDone = false;
  149. var d = default(IDisposable);
  150. d = scheduler.Schedule(dt, () =>
  151. {
  152. lock (gate)
  153. {
  154. if (isAdded)
  155. group.Remove(d);
  156. else
  157. isDone = true;
  158. }
  159. recursiveAction();
  160. });
  161. lock (gate)
  162. {
  163. if (!isDone)
  164. {
  165. group.Add(d);
  166. isAdded = true;
  167. }
  168. }
  169. });
  170. group.Add(scheduler.Schedule(dueTime, recursiveAction));
  171. return group;
  172. }
  173. public static IDisposable Schedule(this IScheduler scheduler, DateTimeOffset dueTime, Action<Action<DateTimeOffset>> action)
  174. {
  175. // InvokeRec3
  176. var group = new CompositeDisposable(1);
  177. var gate = new object();
  178. Action recursiveAction = null;
  179. recursiveAction = () => action(dt =>
  180. {
  181. var isAdded = false;
  182. var isDone = false;
  183. var d = default(IDisposable);
  184. d = scheduler.Schedule(dt, () =>
  185. {
  186. lock (gate)
  187. {
  188. if (isAdded)
  189. group.Remove(d);
  190. else
  191. isDone = true;
  192. }
  193. recursiveAction();
  194. });
  195. lock (gate)
  196. {
  197. if (!isDone)
  198. {
  199. group.Add(d);
  200. isAdded = true;
  201. }
  202. }
  203. });
  204. group.Add(scheduler.Schedule(dueTime, recursiveAction));
  205. return group;
  206. }
  207. }
  208. }