Scheduler.Recursive.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Reactive.Disposables;
  6. namespace System.Reactive.Concurrency
  7. {
  8. public static partial class Scheduler
  9. {
  10. /// <summary>
  11. /// Schedules an action to be executed recursively.
  12. /// </summary>
  13. /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
  14. /// <param name="action">Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action.</param>
  15. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  16. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
  17. public static IDisposable Schedule(this IScheduler scheduler, Action<Action> action)
  18. {
  19. if (scheduler == null)
  20. throw new ArgumentNullException(nameof(scheduler));
  21. if (action == null)
  22. throw new ArgumentNullException(nameof(action));
  23. return scheduler.Schedule(action, (_action, self) => _action(() => self(_action)));
  24. }
  25. /// <summary>
  26. /// Schedules an action to be executed recursively.
  27. /// </summary>
  28. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  29. /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
  30. /// <param name="state">State passed to the action to be executed.</param>
  31. /// <param name="action">Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in recursive invocation state.</param>
  32. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  33. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
  34. public static IDisposable Schedule<TState>(this IScheduler scheduler, TState state, Action<TState, Action<TState>> action)
  35. {
  36. if (scheduler == null)
  37. throw new ArgumentNullException(nameof(scheduler));
  38. if (action == null)
  39. throw new ArgumentNullException(nameof(action));
  40. return scheduler.Schedule(new Pair<TState, Action<TState, Action<TState>>> { First = state, Second = action }, InvokeRec1);
  41. }
  42. static IDisposable InvokeRec1<TState>(IScheduler scheduler, Pair<TState, Action<TState, Action<TState>>> pair)
  43. {
  44. var group = new CompositeDisposable(1);
  45. var gate = new object();
  46. var state = pair.First;
  47. var action = pair.Second;
  48. Action<TState> recursiveAction = null;
  49. recursiveAction = state1 => action(state1, state2 =>
  50. {
  51. var isAdded = false;
  52. var isDone = false;
  53. var d = default(IDisposable);
  54. d = scheduler.Schedule(state2, (scheduler1, state3) =>
  55. {
  56. lock (gate)
  57. {
  58. if (isAdded)
  59. group.Remove(d);
  60. else
  61. isDone = true;
  62. }
  63. recursiveAction(state3);
  64. return Disposable.Empty;
  65. });
  66. lock (gate)
  67. {
  68. if (!isDone)
  69. {
  70. group.Add(d);
  71. isAdded = true;
  72. }
  73. }
  74. });
  75. recursiveAction(state);
  76. return group;
  77. }
  78. /// <summary>
  79. /// Schedules an action to be executed recursively after a specified relative due time.
  80. /// </summary>
  81. /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
  82. /// <param name="action">Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action at the specified relative time.</param>
  83. /// <param name="dueTime">Relative time after which to execute the action for the first time.</param>
  84. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  85. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
  86. public static IDisposable Schedule(this IScheduler scheduler, TimeSpan dueTime, Action<Action<TimeSpan>> action)
  87. {
  88. if (scheduler == null)
  89. throw new ArgumentNullException(nameof(scheduler));
  90. if (action == null)
  91. throw new ArgumentNullException(nameof(action));
  92. return scheduler.Schedule(action, dueTime, (_action, self) => _action(dt => self(_action, dt)));
  93. }
  94. /// <summary>
  95. /// Schedules an action to be executed recursively after a specified relative due time.
  96. /// </summary>
  97. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  98. /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
  99. /// <param name="state">State passed to the action to be executed.</param>
  100. /// <param name="action">Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in the recursive due time and invocation state.</param>
  101. /// <param name="dueTime">Relative time after which to execute the action for the first time.</param>
  102. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  103. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
  104. public static IDisposable Schedule<TState>(this IScheduler scheduler, TState state, TimeSpan dueTime, Action<TState, Action<TState, TimeSpan>> action)
  105. {
  106. if (scheduler == null)
  107. throw new ArgumentNullException(nameof(scheduler));
  108. if (action == null)
  109. throw new ArgumentNullException(nameof(action));
  110. return scheduler.Schedule(new Pair<TState, Action<TState, Action<TState, TimeSpan>>> { First = state, Second = action }, dueTime, InvokeRec2);
  111. }
  112. static IDisposable InvokeRec2<TState>(IScheduler scheduler, Pair<TState, Action<TState, Action<TState, TimeSpan>>> pair)
  113. {
  114. var group = new CompositeDisposable(1);
  115. var gate = new object();
  116. var state = pair.First;
  117. var action = pair.Second;
  118. Action<TState> recursiveAction = null;
  119. recursiveAction = state1 => action(state1, (state2, dueTime1) =>
  120. {
  121. var isAdded = false;
  122. var isDone = false;
  123. var d = default(IDisposable);
  124. d = scheduler.Schedule(state2, dueTime1, (scheduler1, state3) =>
  125. {
  126. lock (gate)
  127. {
  128. if (isAdded)
  129. group.Remove(d);
  130. else
  131. isDone = true;
  132. }
  133. recursiveAction(state3);
  134. return Disposable.Empty;
  135. });
  136. lock (gate)
  137. {
  138. if (!isDone)
  139. {
  140. group.Add(d);
  141. isAdded = true;
  142. }
  143. }
  144. });
  145. recursiveAction(state);
  146. return group;
  147. }
  148. /// <summary>
  149. /// Schedules an action to be executed recursively at a specified absolute due time.
  150. /// </summary>
  151. /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
  152. /// <param name="action">Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action at the specified absolute time.</param>
  153. /// <param name="dueTime">Absolute time at which to execute the action for the first time.</param>
  154. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  155. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
  156. public static IDisposable Schedule(this IScheduler scheduler, DateTimeOffset dueTime, Action<Action<DateTimeOffset>> action)
  157. {
  158. if (scheduler == null)
  159. throw new ArgumentNullException(nameof(scheduler));
  160. if (action == null)
  161. throw new ArgumentNullException(nameof(action));
  162. return scheduler.Schedule(action, dueTime, (_action, self) => _action(dt => self(_action, dt)));
  163. }
  164. /// <summary>
  165. /// Schedules an action to be executed recursively at a specified absolute due time.
  166. /// </summary>
  167. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  168. /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
  169. /// <param name="state">State passed to the action to be executed.</param>
  170. /// <param name="action">Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in the recursive due time and invocation state.</param>
  171. /// <param name="dueTime">Absolute time at which to execute the action for the first time.</param>
  172. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  173. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
  174. public static IDisposable Schedule<TState>(this IScheduler scheduler, TState state, DateTimeOffset dueTime, Action<TState, Action<TState, DateTimeOffset>> action)
  175. {
  176. if (scheduler == null)
  177. throw new ArgumentNullException(nameof(scheduler));
  178. if (action == null)
  179. throw new ArgumentNullException(nameof(action));
  180. return scheduler.Schedule(new Pair<TState, Action<TState, Action<TState, DateTimeOffset>>> { First = state, Second = action }, dueTime, InvokeRec3);
  181. }
  182. static IDisposable InvokeRec3<TState>(IScheduler scheduler, Pair<TState, Action<TState, Action<TState, DateTimeOffset>>> pair)
  183. {
  184. var group = new CompositeDisposable(1);
  185. var gate = new object();
  186. var state = pair.First;
  187. var action = pair.Second;
  188. Action<TState> recursiveAction = null;
  189. recursiveAction = state1 => action(state1, (state2, dueTime1) =>
  190. {
  191. var isAdded = false;
  192. var isDone = false;
  193. var d = default(IDisposable);
  194. d = scheduler.Schedule(state2, dueTime1, (scheduler1, state3) =>
  195. {
  196. lock (gate)
  197. {
  198. if (isAdded)
  199. group.Remove(d);
  200. else
  201. isDone = true;
  202. }
  203. recursiveAction(state3);
  204. return Disposable.Empty;
  205. });
  206. lock (gate)
  207. {
  208. if (!isDone)
  209. {
  210. group.Add(d);
  211. isAdded = true;
  212. }
  213. }
  214. });
  215. recursiveAction(state);
  216. return group;
  217. }
  218. #if !NO_SERIALIZABLE
  219. [Serializable]
  220. #endif
  221. struct Pair<T1, T2>
  222. {
  223. public T1 First;
  224. public T2 Second;
  225. }
  226. }
  227. }