ImmediateScheduler.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.Threading;
  5. using System.Reactive.Disposables;
  6. namespace System.Reactive.Concurrency
  7. {
  8. /// <summary>
  9. /// Represents an object that schedules units of work to run immediately on the current thread.
  10. /// </summary>
  11. /// <seealso cref="Scheduler.Immediate">Singleton instance of this type exposed through this static property.</seealso>
  12. public sealed class ImmediateScheduler : LocalScheduler
  13. {
  14. private static readonly Lazy<ImmediateScheduler> s_instance = new Lazy<ImmediateScheduler>(() => new ImmediateScheduler());
  15. ImmediateScheduler()
  16. {
  17. }
  18. /// <summary>
  19. /// Gets the singleton instance of the immediate scheduler.
  20. /// </summary>
  21. public static ImmediateScheduler Instance
  22. {
  23. get { return s_instance.Value; }
  24. }
  25. /// <summary>
  26. /// Schedules an action to be executed.
  27. /// </summary>
  28. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  29. /// <param name="state">State passed to the action to be executed.</param>
  30. /// <param name="action">Action to be executed.</param>
  31. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  32. /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
  33. public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
  34. {
  35. if (action == null)
  36. throw new ArgumentNullException(nameof(action));
  37. return action(new AsyncLockScheduler(), state);
  38. }
  39. /// <summary>
  40. /// Schedules an action to be executed after dueTime.
  41. /// </summary>
  42. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  43. /// <param name="state">State passed to the action to be executed.</param>
  44. /// <param name="action">Action to be executed.</param>
  45. /// <param name="dueTime">Relative time after which to execute the action.</param>
  46. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  47. /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
  48. public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
  49. {
  50. if (action == null)
  51. throw new ArgumentNullException(nameof(action));
  52. var dt = Scheduler.Normalize(dueTime);
  53. if (dt.Ticks > 0)
  54. {
  55. ConcurrencyAbstractionLayer.Current.Sleep(dt);
  56. }
  57. return action(new AsyncLockScheduler(), state);
  58. }
  59. class AsyncLockScheduler : LocalScheduler
  60. {
  61. AsyncLock asyncLock;
  62. public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
  63. {
  64. if (action == null)
  65. throw new ArgumentNullException(nameof(action));
  66. var m = new SingleAssignmentDisposable();
  67. if (asyncLock == null)
  68. asyncLock = new AsyncLock();
  69. asyncLock.Wait(() =>
  70. {
  71. if (!m.IsDisposed)
  72. m.Disposable = action(this, state);
  73. });
  74. return m;
  75. }
  76. public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
  77. {
  78. if (action == null)
  79. throw new ArgumentNullException(nameof(action));
  80. if (dueTime.Ticks <= 0)
  81. return Schedule<TState>(state, action);
  82. var timer = ConcurrencyAbstractionLayer.Current.StartStopwatch();
  83. var m = new SingleAssignmentDisposable();
  84. if (asyncLock == null)
  85. asyncLock = new AsyncLock();
  86. asyncLock.Wait(() =>
  87. {
  88. if (!m.IsDisposed)
  89. {
  90. var sleep = dueTime - timer.Elapsed;
  91. if (sleep.Ticks > 0)
  92. {
  93. ConcurrencyAbstractionLayer.Current.Sleep(sleep);
  94. }
  95. if (!m.IsDisposed)
  96. m.Disposable = action(this, state);
  97. }
  98. });
  99. return m;
  100. }
  101. }
  102. }
  103. }