AsyncScheduler.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.Runtime.CompilerServices;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Reactive.Concurrency
  8. {
  9. public static class AsyncScheduler
  10. {
  11. // TODO: Implement proper RendezVous semantics.
  12. public static ConfiguredTaskAwaitable RendezVous(this Task task, IAsyncScheduler scheduler)
  13. {
  14. return task.ConfigureAwait(true);
  15. }
  16. public static ConfiguredTaskAwaitable<T> RendezVous<T>(this Task<T> task, IAsyncScheduler scheduler)
  17. {
  18. return task.ConfigureAwait(true);
  19. }
  20. public static async Task Delay(this IAsyncScheduler scheduler, TimeSpan dueTime, CancellationToken token = default(CancellationToken))
  21. {
  22. if (scheduler == null)
  23. throw new ArgumentNullException(nameof(scheduler));
  24. var tcs = new TaskCompletionSource<bool>();
  25. var task = await scheduler.ScheduleAsync(ct =>
  26. {
  27. if (ct.IsCancellationRequested)
  28. {
  29. tcs.TrySetCanceled(ct);
  30. }
  31. else
  32. {
  33. tcs.SetResult(true);
  34. }
  35. return Task.CompletedTask;
  36. }, dueTime);
  37. using (token.Register(() => task.DisposeAsync()))
  38. {
  39. await tcs.Task;
  40. }
  41. }
  42. public static async Task ExecuteAsync(this IAsyncScheduler scheduler, Func<CancellationToken, Task> action, CancellationToken token = default(CancellationToken))
  43. {
  44. var tcs = new TaskCompletionSource<object>();
  45. var d = await scheduler.ScheduleAsync(async ct =>
  46. {
  47. try
  48. {
  49. ct.ThrowIfCancellationRequested();
  50. await action(ct).RendezVous(scheduler);
  51. }
  52. catch (OperationCanceledException ex) when (ex.CancellationToken == ct)
  53. {
  54. tcs.TrySetCanceled(ct);
  55. }
  56. catch (Exception ex)
  57. {
  58. tcs.TrySetException(ex);
  59. }
  60. finally
  61. {
  62. tcs.TrySetResult(null);
  63. }
  64. });
  65. using (token.Register(() =>
  66. {
  67. try
  68. {
  69. d.DisposeAsync();
  70. }
  71. finally
  72. {
  73. tcs.TrySetCanceled(token);
  74. }
  75. }))
  76. {
  77. await tcs.Task.ConfigureAwait(false);
  78. }
  79. }
  80. public static async Task<TResult> ExecuteAsync<TResult>(this IAsyncScheduler scheduler, Func<CancellationToken, Task<TResult>> action, CancellationToken token = default(CancellationToken))
  81. {
  82. var tcs = new TaskCompletionSource<TResult>();
  83. var d = await scheduler.ScheduleAsync(async ct =>
  84. {
  85. var res = default(TResult);
  86. try
  87. {
  88. ct.ThrowIfCancellationRequested();
  89. res = await action(ct).RendezVous(scheduler);
  90. }
  91. catch (OperationCanceledException ex) when (ex.CancellationToken == ct)
  92. {
  93. tcs.TrySetCanceled(ct);
  94. }
  95. catch (Exception ex)
  96. {
  97. tcs.TrySetException(ex);
  98. }
  99. finally
  100. {
  101. tcs.TrySetResult(res);
  102. }
  103. });
  104. using (token.Register(() =>
  105. {
  106. try
  107. {
  108. d.DisposeAsync();
  109. }
  110. finally
  111. {
  112. tcs.TrySetCanceled(token);
  113. }
  114. }))
  115. {
  116. return await tcs.Task.ConfigureAwait(false);
  117. }
  118. }
  119. }
  120. }