Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Core/Reactive/Concurrency/Scheduler.Recursive.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 using System;
4 using System.Reactive.Disposables;
5
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("scheduler");
21 if (action == null)
22 throw new ArgumentNullException("action");
23
24 return scheduler.Schedule(action, (_action, self) => _action(() => self(_action)));
25 }
26
27 /// <summary>
28 /// Schedules an action to be executed recursively.
29 /// </summary>
30 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
31 /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
32 /// <param name="state">State passed to the action to be executed.</param>
33 /// <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>
34 /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
35 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
36 public static IDisposable Schedule<TState>(this IScheduler scheduler, TState state, Action<TState, Action<TState>> action)
37 {
38 if (scheduler == null)
39 throw new ArgumentNullException("scheduler");
40 if (action == null)
41 throw new ArgumentNullException("action");
42
43 return scheduler.Schedule(new Pair<TState, Action<TState, Action<TState>>> { First = state, Second = action }, InvokeRec1);
44 }
45
46 static IDisposable InvokeRec1<TState>(IScheduler scheduler, Pair<TState, Action<TState, Action<TState>>> pair)
47 {
48 var group = new CompositeDisposable(1);
49 var gate = new object();
50 var state = pair.First;
51 var action = pair.Second;
52
53 Action<TState> recursiveAction = null;
54 recursiveAction = state1 => action(state1, state2 =>
55 {
56 var isAdded = false;
57 var isDone = false;
58 var d = default(IDisposable);
59 d = scheduler.Schedule(state2, (scheduler1, state3) =>
60 {
61 lock (gate)
62 {
63 if (isAdded)
64 group.Remove(d);
65 else
66 isDone = true;
67 }
68 recursiveAction(state3);
69 return Disposable.Empty;
70 });
71
72 lock (gate)
73 {
74 if (!isDone)
75 {
76 group.Add(d);
77 isAdded = true;
78 }
79 }
80 });
81
82 recursiveAction(state);
83
84 return group;
85 }
86
87 /// <summary>
88 /// Schedules an action to be executed recursively after a specified relative due time.
89 /// </summary>
90 /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
91 /// <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>
92 /// <param name="dueTime">Relative time after which to execute the action for the first time.</param>
93 /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
94 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
95 public static IDisposable Schedule(this IScheduler scheduler, TimeSpan dueTime, Action<Action<TimeSpan>> action)
96 {
97 if (scheduler == null)
98 throw new ArgumentNullException("scheduler");
99 if (action == null)
100 throw new ArgumentNullException("action");
101
102 return scheduler.Schedule(action, dueTime, (_action, self) => _action(dt => self(_action, dt)));
103 }
104
105 /// <summary>
106 /// Schedules an action to be executed recursively after a specified relative due time.
107 /// </summary>
108 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
109 /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
110 /// <param name="state">State passed to the action to be executed.</param>
111 /// <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>
112 /// <param name="dueTime">Relative time after which to execute the action for the first time.</param>
113 /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
114 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
115 public static IDisposable Schedule<TState>(this IScheduler scheduler, TState state, TimeSpan dueTime, Action<TState, Action<TState, TimeSpan>> action)
116 {
117 if (scheduler == null)
118 throw new ArgumentNullException("scheduler");
119 if (action == null)
120 throw new ArgumentNullException("action");
121
122 return scheduler.Schedule(new Pair<TState, Action<TState, Action<TState, TimeSpan>>> { First = state, Second = action }, dueTime, InvokeRec2);
123 }
124
125 static IDisposable InvokeRec2<TState>(IScheduler scheduler, Pair<TState, Action<TState, Action<TState, TimeSpan>>> pair)
126 {
127 var group = new CompositeDisposable(1);
128 var gate = new object();
129 var state = pair.First;
130 var action = pair.Second;
131
132 Action<TState> recursiveAction = null;
133 recursiveAction = state1 => action(state1, (state2, dueTime1) =>
134 {
135 var isAdded = false;
136 var isDone = false;
137 var d = default(IDisposable);
138 d = scheduler.Schedule(state2, dueTime1, (scheduler1, state3) =>
139 {
140 lock (gate)
141 {
142 if (isAdded)
143 group.Remove(d);
144 else
145 isDone = true;
146 }
147 recursiveAction(state3);
148 return Disposable.Empty;
149 });
150
151 lock (gate)
152 {
153 if (!isDone)
154 {
155 group.Add(d);
156 isAdded = true;
157 }
158 }
159 });
160
161 recursiveAction(state);
162
163 return group;
164 }
165
166 /// <summary>
167 /// Schedules an action to be executed recursively at a specified absolute due time.
168 /// </summary>
169 /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
170 /// <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>
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(this IScheduler scheduler, DateTimeOffset dueTime, Action<Action<DateTimeOffset>> action)
175 {
176 if (scheduler == null)
177 throw new ArgumentNullException("scheduler");
178 if (action == null)
179 throw new ArgumentNullException("action");
180
181 return scheduler.Schedule(action, dueTime, (_action, self) => _action(dt => self(_action, dt)));
182 }
183
184 /// <summary>
185 /// Schedules an action to be executed recursively at a specified absolute due time.
186 /// </summary>
187 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
188 /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
189 /// <param name="state">State passed to the action to be executed.</param>
190 /// <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>
191 /// <param name="dueTime">Absolute time at which to execute the action for the first time.</param>
192 /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
193 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
194 public static IDisposable Schedule<TState>(this IScheduler scheduler, TState state, DateTimeOffset dueTime, Action<TState, Action<TState, DateTimeOffset>> action)
195 {
196 if (scheduler == null)
197 throw new ArgumentNullException("scheduler");
198 if (action == null)
199 throw new ArgumentNullException("action");
200
201 return scheduler.Schedule(new Pair<TState, Action<TState, Action<TState, DateTimeOffset>>> { First = state, Second = action }, dueTime, InvokeRec3);
202 }
203
204 static IDisposable InvokeRec3<TState>(IScheduler scheduler, Pair<TState, Action<TState, Action<TState, DateTimeOffset>>> pair)
205 {
206 var group = new CompositeDisposable(1);
207 var gate = new object();
208 var state = pair.First;
209 var action = pair.Second;
210
211 Action<TState> recursiveAction = null;
212 recursiveAction = state1 => action(state1, (state2, dueTime1) =>
213 {
214 var isAdded = false;
215 var isDone = false;
216 var d = default(IDisposable);
217 d = scheduler.Schedule(state2, dueTime1, (scheduler1, state3) =>
218 {
219 lock (gate)
220 {
221 if (isAdded)
222 group.Remove(d);
223 else
224 isDone = true;
225 }
226 recursiveAction(state3);
227 return Disposable.Empty;
228 });
229
230 lock (gate)
231 {
232 if (!isDone)
233 {
234 group.Add(d);
235 isAdded = true;
236 }
237 }
238 });
239
240 recursiveAction(state);
241
242 return group;
243 }
244
245 #if !NO_SERIALIZABLE
246 [Serializable]
247 #endif
248 struct Pair<T1, T2>
249 {
250 public T1 First;
251 public T2 Second;
252 }
253 }
254 }