Debian Codegraph @

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

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if HAS_AWAIT
4 using System.Reactive.Disposables;
5 using System.Threading;
6 using System.Threading.Tasks;
7
8 namespace System.Reactive.Concurrency
9 {
10 public static partial class Scheduler
11 {
12 /// <summary>
13 /// Yields execution of the current work item on the scheduler to another work item on the scheduler.
14 /// The caller should await the result of calling Yield to schedule the remainder of the current work item (known as the continuation).
15 /// </summary>
16 /// <param name="scheduler">Scheduler to yield work on.</param>
17 /// <returns>Scheduler operation object to await in order to schedule the continuation.</returns>
18 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
19 public static SchedulerOperation Yield(this IScheduler scheduler)
20 {
21 if (scheduler == null)
22 throw new ArgumentNullException("scheduler");
23
24 return new SchedulerOperation(a => scheduler.Schedule(a), scheduler.GetCancellationToken());
25 }
26
27 /// <summary>
28 /// Yields execution of the current work item on the scheduler to another work item on the scheduler.
29 /// The caller should await the result of calling Yield to schedule the remainder of the current work item (known as the continuation).
30 /// </summary>
31 /// <param name="scheduler">Scheduler to yield work on.</param>
32 /// <param name="cancellationToken">Cancellation token to cancel the continuation to run.</param>
33 /// <returns>Scheduler operation object to await in order to schedule the continuation.</returns>
34 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
35 public static SchedulerOperation Yield(this IScheduler scheduler, CancellationToken cancellationToken)
36 {
37 if (scheduler == null)
38 throw new ArgumentNullException("scheduler");
39
40 return new SchedulerOperation(a => scheduler.Schedule(a), cancellationToken);
41 }
42
43 /// <summary>
44 /// Suspends execution of the current work item on the scheduler for the specified duration.
45 /// The caller should await the result of calling Sleep to schedule the remainder of the current work item (known as the continuation) after the specified duration.
46 /// </summary>
47 /// <param name="scheduler">Scheduler to yield work on.</param>
48 /// <param name="dueTime">Time when the continuation should run.</param>
49 /// <returns>Scheduler operation object to await in order to schedule the continuation.</returns>
50 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
51 public static SchedulerOperation Sleep(this IScheduler scheduler, TimeSpan dueTime)
52 {
53 if (scheduler == null)
54 throw new ArgumentNullException("scheduler");
55
56 return new SchedulerOperation(a => scheduler.Schedule(dueTime, a), scheduler.GetCancellationToken());
57 }
58
59 /// <summary>
60 /// Suspends execution of the current work item on the scheduler for the specified duration.
61 /// The caller should await the result of calling Sleep to schedule the remainder of the current work item (known as the continuation) after the specified duration.
62 /// </summary>
63 /// <param name="scheduler">Scheduler to yield work on.</param>
64 /// <param name="dueTime">Time when the continuation should run.</param>
65 /// <param name="cancellationToken">Cancellation token to cancel the continuation to run.</param>
66 /// <returns>Scheduler operation object to await in order to schedule the continuation.</returns>
67 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
68 public static SchedulerOperation Sleep(this IScheduler scheduler, TimeSpan dueTime, CancellationToken cancellationToken)
69 {
70 if (scheduler == null)
71 throw new ArgumentNullException("scheduler");
72
73 return new SchedulerOperation(a => scheduler.Schedule(dueTime, a), cancellationToken);
74 }
75
76 /// <summary>
77 /// Suspends execution of the current work item on the scheduler until the specified due time.
78 /// The caller should await the result of calling Sleep to schedule the remainder of the current work item (known as the continuation) at the specified due time.
79 /// </summary>
80 /// <param name="scheduler">Scheduler to yield work on.</param>
81 /// <param name="dueTime">Time when the continuation should run.</param>
82 /// <returns>Scheduler operation object to await in order to schedule the continuation.</returns>
83 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
84 public static SchedulerOperation Sleep(this IScheduler scheduler, DateTimeOffset dueTime)
85 {
86 if (scheduler == null)
87 throw new ArgumentNullException("scheduler");
88
89 return new SchedulerOperation(a => scheduler.Schedule(dueTime, a), scheduler.GetCancellationToken());
90 }
91
92 /// <summary>
93 /// Suspends execution of the current work item on the scheduler until the specified due time.
94 /// The caller should await the result of calling Sleep to schedule the remainder of the current work item (known as the continuation) at the specified due time.
95 /// </summary>
96 /// <param name="scheduler">Scheduler to yield work on.</param>
97 /// <param name="dueTime">Time when the continuation should run.</param>
98 /// <param name="cancellationToken">Cancellation token to cancel the continuation to run.</param>
99 /// <returns>Scheduler operation object to await in order to schedule the continuation.</returns>
100 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
101 public static SchedulerOperation Sleep(this IScheduler scheduler, DateTimeOffset dueTime, CancellationToken cancellationToken)
102 {
103 if (scheduler == null)
104 throw new ArgumentNullException("scheduler");
105
106 return new SchedulerOperation(a => scheduler.Schedule(dueTime, a), cancellationToken);
107 }
108
109 /// <summary>
110 /// Schedules work using an asynchronous method, allowing for cooperative scheduling in an imperative coding style.
111 /// </summary>
112 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
113 /// <param name="scheduler">Scheduler to schedule work on.</param>
114 /// <param name="state">State to pass to the asynchronous method.</param>
115 /// <param name="action">Asynchronous method to run the work, using Yield and Sleep operations for cooperative scheduling and injection of cancellation points.</param>
116 /// <returns>Disposable object that allows to cancel outstanding work on cooperative cancellation points or through the cancellation token passed to the asynchronous method.</returns>
117 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
118 public static IDisposable ScheduleAsync<TState>(this IScheduler scheduler, TState state, Func<IScheduler, TState, CancellationToken, Task> action)
119 {
120 if (scheduler == null)
121 throw new ArgumentNullException("scheduler");
122 if (action == null)
123 throw new ArgumentNullException("action");
124
125 return ScheduleAsync_<TState>(scheduler, state, action);
126 }
127
128 /// <summary>
129 /// Schedules work using an asynchronous method, allowing for cooperative scheduling in an imperative coding style.
130 /// </summary>
131 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
132 /// <param name="scheduler">Scheduler to schedule work on.</param>
133 /// <param name="state">State to pass to the asynchronous method.</param>
134 /// <param name="action">Asynchronous method to run the work, using Yield and Sleep operations for cooperative scheduling and injection of cancellation points.</param>
135 /// <returns>Disposable object that allows to cancel outstanding work on cooperative cancellation points or through the cancellation token passed to the asynchronous method.</returns>
136 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
137 public static IDisposable ScheduleAsync<TState>(this IScheduler scheduler, TState state, Func<IScheduler, TState, CancellationToken, Task<IDisposable>> action)
138 {
139 if (scheduler == null)
140 throw new ArgumentNullException("scheduler");
141 if (action == null)
142 throw new ArgumentNullException("action");
143
144 return ScheduleAsync_<TState>(scheduler, state, action);
145 }
146
147 /// <summary>
148 /// Schedules work using an asynchronous method, allowing for cooperative scheduling in an imperative coding style.
149 /// </summary>
150 /// <param name="scheduler">Scheduler to schedule work on.</param>
151 /// <param name="action">Asynchronous method to run the work, using Yield and Sleep operations for cooperative scheduling and injection of cancellation points.</param>
152 /// <returns>Disposable object that allows to cancel outstanding work on cooperative cancellation points or through the cancellation token passed to the asynchronous method.</returns>
153 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
154 public static IDisposable ScheduleAsync(this IScheduler scheduler, Func<IScheduler, CancellationToken, Task> action)
155 {
156 if (scheduler == null)
157 throw new ArgumentNullException("scheduler");
158 if (action == null)
159 throw new ArgumentNullException("action");
160
161 return ScheduleAsync_(scheduler, default(object), (self, o, ct) => action(self, ct));
162 }
163
164 /// <summary>
165 /// Schedules work using an asynchronous method, allowing for cooperative scheduling in an imperative coding style.
166 /// </summary>
167 /// <param name="scheduler">Scheduler to schedule work on.</param>
168 /// <param name="action">Asynchronous method to run the work, using Yield and Sleep operations for cooperative scheduling and injection of cancellation points.</param>
169 /// <returns>Disposable object that allows to cancel outstanding work on cooperative cancellation points or through the cancellation token passed to the asynchronous method.</returns>
170 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
171 public static IDisposable ScheduleAsync(this IScheduler scheduler, Func<IScheduler, CancellationToken, Task<IDisposable>> action)
172 {
173 if (scheduler == null)
174 throw new ArgumentNullException("scheduler");
175 if (action == null)
176 throw new ArgumentNullException("action");
177
178 return ScheduleAsync_(scheduler, default(object), (self, o, ct) => action(self, ct));
179 }
180
181 /// <summary>
182 /// Schedules work using an asynchronous method, allowing for cooperative scheduling in an imperative coding style.
183 /// </summary>
184 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
185 /// <param name="scheduler">Scheduler to schedule work on.</param>
186 /// <param name="state">State to pass to the asynchronous method.</param>
187 /// <param name="dueTime">Relative time after which to execute the action.</param>
188 /// <param name="action">Asynchronous method to run the work, using Yield and Sleep operations for cooperative scheduling and injection of cancellation points.</param>
189 /// <returns>Disposable object that allows to cancel outstanding work on cooperative cancellation points or through the cancellation token passed to the asynchronous method.</returns>
190 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
191 public static IDisposable ScheduleAsync<TState>(this IScheduler scheduler, TState state, TimeSpan dueTime, Func<IScheduler, TState, CancellationToken, Task> action)
192 {
193 if (scheduler == null)
194 throw new ArgumentNullException("scheduler");
195 if (action == null)
196 throw new ArgumentNullException("action");
197
198 return ScheduleAsync_(scheduler, state, dueTime, action);
199 }
200
201 /// <summary>
202 /// Schedules work using an asynchronous method, allowing for cooperative scheduling in an imperative coding style.
203 /// </summary>
204 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
205 /// <param name="scheduler">Scheduler to schedule work on.</param>
206 /// <param name="state">State to pass to the asynchronous method.</param>
207 /// <param name="dueTime">Relative time after which to execute the action.</param>
208 /// <param name="action">Asynchronous method to run the work, using Yield and Sleep operations for cooperative scheduling and injection of cancellation points.</param>
209 /// <returns>Disposable object that allows to cancel outstanding work on cooperative cancellation points or through the cancellation token passed to the asynchronous method.</returns>
210 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
211 public static IDisposable ScheduleAsync<TState>(this IScheduler scheduler, TState state, TimeSpan dueTime, Func<IScheduler, TState, CancellationToken, Task<IDisposable>> action)
212 {
213 if (scheduler == null)
214 throw new ArgumentNullException("scheduler");
215 if (action == null)
216 throw new ArgumentNullException("action");
217
218 return ScheduleAsync_(scheduler, state, dueTime, action);
219 }
220
221 /// <summary>
222 /// Schedules work using an asynchronous method, allowing for cooperative scheduling in an imperative coding style.
223 /// </summary>
224 /// <param name="scheduler">Scheduler to schedule work on.</param>
225 /// <param name="dueTime">Relative time after which to execute the action.</param>
226 /// <param name="action">Asynchronous method to run the work, using Yield and Sleep operations for cooperative scheduling and injection of cancellation points.</param>
227 /// <returns>Disposable object that allows to cancel outstanding work on cooperative cancellation points or through the cancellation token passed to the asynchronous method.</returns>
228 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
229 public static IDisposable ScheduleAsync(this IScheduler scheduler, TimeSpan dueTime, Func<IScheduler, CancellationToken, Task> action)
230 {
231 if (scheduler == null)
232 throw new ArgumentNullException("scheduler");
233 if (action == null)
234 throw new ArgumentNullException("action");
235
236 return ScheduleAsync_(scheduler, default(object), dueTime, (self, o, ct) => action(self, ct));
237 }
238
239 /// <summary>
240 /// Schedules work using an asynchronous method, allowing for cooperative scheduling in an imperative coding style.
241 /// </summary>
242 /// <param name="scheduler">Scheduler to schedule work on.</param>
243 /// <param name="dueTime">Relative time after which to execute the action.</param>
244 /// <param name="action">Asynchronous method to run the work, using Yield and Sleep operations for cooperative scheduling and injection of cancellation points.</param>
245 /// <returns>Disposable object that allows to cancel outstanding work on cooperative cancellation points or through the cancellation token passed to the asynchronous method.</returns>
246 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
247 public static IDisposable ScheduleAsync(this IScheduler scheduler, TimeSpan dueTime, Func<IScheduler, CancellationToken, Task<IDisposable>> action)
248 {
249 if (scheduler == null)
250 throw new ArgumentNullException("scheduler");
251 if (action == null)
252 throw new ArgumentNullException("action");
253
254 return ScheduleAsync_(scheduler, default(object), dueTime, (self, o, ct) => action(self, ct));
255 }
256
257 /// <summary>
258 /// Schedules work using an asynchronous method, allowing for cooperative scheduling in an imperative coding style.
259 /// </summary>
260 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
261 /// <param name="scheduler">Scheduler to schedule work on.</param>
262 /// <param name="state">State to pass to the asynchronous method.</param>
263 /// <param name="dueTime">Absolute time at which to execute the action.</param>
264 /// <param name="action">Asynchronous method to run the work, using Yield and Sleep operations for cooperative scheduling and injection of cancellation points.</param>
265 /// <returns>Disposable object that allows to cancel outstanding work on cooperative cancellation points or through the cancellation token passed to the asynchronous method.</returns>
266 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
267 public static IDisposable ScheduleAsync<TState>(this IScheduler scheduler, TState state, DateTimeOffset dueTime, Func<IScheduler, TState, CancellationToken, Task> action)
268 {
269 if (scheduler == null)
270 throw new ArgumentNullException("scheduler");
271 if (action == null)
272 throw new ArgumentNullException("action");
273
274 return ScheduleAsync_(scheduler, state, dueTime, action);
275 }
276
277 /// <summary>
278 /// Schedules work using an asynchronous method, allowing for cooperative scheduling in an imperative coding style.
279 /// </summary>
280 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
281 /// <param name="scheduler">Scheduler to schedule work on.</param>
282 /// <param name="state">State to pass to the asynchronous method.</param>
283 /// <param name="dueTime">Absolute time at which to execute the action.</param>
284 /// <param name="action">Asynchronous method to run the work, using Yield and Sleep operations for cooperative scheduling and injection of cancellation points.</param>
285 /// <returns>Disposable object that allows to cancel outstanding work on cooperative cancellation points or through the cancellation token passed to the asynchronous method.</returns>
286 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
287 public static IDisposable ScheduleAsync<TState>(this IScheduler scheduler, TState state, DateTimeOffset dueTime, Func<IScheduler, TState, CancellationToken, Task<IDisposable>> action)
288 {
289 if (scheduler == null)
290 throw new ArgumentNullException("scheduler");
291 if (action == null)
292 throw new ArgumentNullException("action");
293
294 return ScheduleAsync_(scheduler, state, dueTime, action);
295 }
296
297 /// <summary>
298 /// Schedules work using an asynchronous method, allowing for cooperative scheduling in an imperative coding style.
299 /// </summary>
300 /// <param name="scheduler">Scheduler to schedule work on.</param>
301 /// <param name="dueTime">Absolute time at which to execute the action.</param>
302 /// <param name="action">Asynchronous method to run the work, using Yield and Sleep operations for cooperative scheduling and injection of cancellation points.</param>
303 /// <returns>Disposable object that allows to cancel outstanding work on cooperative cancellation points or through the cancellation token passed to the asynchronous method.</returns>
304 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
305 public static IDisposable ScheduleAsync(this IScheduler scheduler, DateTimeOffset dueTime, Func<IScheduler, CancellationToken, Task> action)
306 {
307 if (scheduler == null)
308 throw new ArgumentNullException("scheduler");
309 if (action == null)
310 throw new ArgumentNullException("action");
311
312 return ScheduleAsync_(scheduler, default(object), dueTime, (self, o, ct) => action(self, ct));
313 }
314
315 /// <summary>
316 /// Schedules work using an asynchronous method, allowing for cooperative scheduling in an imperative coding style.
317 /// </summary>
318 /// <param name="scheduler">Scheduler to schedule work on.</param>
319 /// <param name="dueTime">Absolute time at which to execute the action.</param>
320 /// <param name="action">Asynchronous method to run the work, using Yield and Sleep operations for cooperative scheduling and injection of cancellation points.</param>
321 /// <returns>Disposable object that allows to cancel outstanding work on cooperative cancellation points or through the cancellation token passed to the asynchronous method.</returns>
322 /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
323 public static IDisposable ScheduleAsync(this IScheduler scheduler, DateTimeOffset dueTime, Func<IScheduler, CancellationToken, Task<IDisposable>> action)
324 {
325 if (scheduler == null)
326 throw new ArgumentNullException("scheduler");
327 if (action == null)
328 throw new ArgumentNullException("action");
329
330 return ScheduleAsync_(scheduler, default(object), dueTime, (self, o, ct) => action(self, ct));
331 }
332
333 private static IDisposable ScheduleAsync_<TState>(IScheduler scheduler, TState state, Func<IScheduler, TState, CancellationToken, Task> action)
334 {
335 return scheduler.Schedule(state, (self, s) => InvokeAsync(self, s, action));
336 }
337
338 private static IDisposable ScheduleAsync_<TState>(IScheduler scheduler, TState state, Func<IScheduler, TState, CancellationToken, Task<IDisposable>> action)
339 {
340 return scheduler.Schedule(state, (self, s) => InvokeAsync(self, s, action));
341 }
342
343 private static IDisposable ScheduleAsync_<TState>(IScheduler scheduler, TState state, TimeSpan dueTime, Func<IScheduler, TState, CancellationToken, Task> action)
344 {
345 return scheduler.Schedule(state, dueTime, (self, s) => InvokeAsync(self, s, action));
346 }
347
348 private static IDisposable ScheduleAsync_<TState>(IScheduler scheduler, TState state, TimeSpan dueTime, Func<IScheduler, TState, CancellationToken, Task<IDisposable>> action)
349 {
350 return scheduler.Schedule(state, dueTime, (self, s) => InvokeAsync(self, s, action));
351 }
352
353 private static IDisposable ScheduleAsync_<TState>(IScheduler scheduler, TState state, DateTimeOffset dueTime, Func<IScheduler, TState, CancellationToken, Task> action)
354 {
355 return scheduler.Schedule(state, dueTime, (self, s) => InvokeAsync(self, s, action));
356 }
357
358 private static IDisposable ScheduleAsync_<TState>(IScheduler scheduler, TState state, DateTimeOffset dueTime, Func<IScheduler, TState, CancellationToken, Task<IDisposable>> action)
359 {
360 return scheduler.Schedule(state, dueTime, (self, s) => InvokeAsync(self, s, action));
361 }
362
363 private static IDisposable InvokeAsync<TState>(IScheduler self, TState s, Func<IScheduler, TState, CancellationToken, Task<IDisposable>> action)
364 {
365 var c = new CancellationDisposable();
366 var d = new SingleAssignmentDisposable();
367
368 action(new CancelableScheduler(self, c.Token), s, c.Token).ContinueWith(t =>
369 {
370 if (t.IsCanceled)
371 return;
372
373 if (t.Exception != null)
374 t.Exception.Handle(e => e is OperationCanceledException);
375
376 d.Disposable = t.Result;
377 }, TaskContinuationOptions.ExecuteSynchronously);
378
379 return new CompositeDisposable(c, d);
380 }
381
382 private static IDisposable InvokeAsync<TState>(IScheduler self, TState s, Func<IScheduler, TState, CancellationToken, Task> action)
383 {
384 return InvokeAsync(self, s, (self_, state, ct) => action(self_, state, ct).ContinueWith(_ => Disposable.Empty));
385 }
386
387 private static CancellationToken GetCancellationToken(this IScheduler scheduler)
388 {
389 var cs = scheduler as CancelableScheduler;
390 return cs != null ? cs.Token : CancellationToken.None;
391 }
392
393 class CancelableScheduler : IScheduler
394 {
395 private readonly IScheduler _scheduler;
396 private readonly CancellationToken _cancellationToken;
397
398 public CancelableScheduler(IScheduler scheduler, CancellationToken cancellationToken)
399 {
400 _scheduler = scheduler;
401 _cancellationToken = cancellationToken;
402 }
403
404 public CancellationToken Token
405 {
406 get { return _cancellationToken; }
407 }
408
409 public DateTimeOffset Now
410 {
411 get { return _scheduler.Now; }
412 }
413
414 public IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
415 {
416 return _scheduler.Schedule(state, action);
417 }
418
419 public IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
420 {
421 return _scheduler.Schedule(state, dueTime, action);
422 }
423
424 public IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action)
425 {
426 return _scheduler.Schedule(state, dueTime, action);
427 }
428 }
429
430 }
431 }
432 #endif