HistoricalScheduler.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.Collections.Generic;
  3. using System.Reactive.Disposables;
  4. namespace System.Reactive.Concurrency
  5. {
  6. /// <summary>
  7. /// Base class for historical schedulers, which are virtual time schedulers that use DateTimeOffset for absolute time and TimeSpan for relative time.
  8. /// </summary>
  9. public abstract class HistoricalSchedulerBase : VirtualTimeSchedulerBase<DateTimeOffset, TimeSpan>
  10. {
  11. /// <summary>
  12. /// Creates a new historical scheduler with the minimum value of DateTimeOffset as the initial clock value.
  13. /// </summary>
  14. protected HistoricalSchedulerBase()
  15. : base(DateTimeOffset.MinValue, Comparer<DateTimeOffset>.Default)
  16. {
  17. }
  18. /// <summary>
  19. /// Creates a new historical scheduler with the specified initial clock value.
  20. /// </summary>
  21. /// <param name="initialClock">Initial clock value.</param>
  22. protected HistoricalSchedulerBase(DateTimeOffset initialClock)
  23. : base(initialClock, Comparer<DateTimeOffset>.Default)
  24. {
  25. }
  26. /// <summary>
  27. /// Creates a new historical scheduler with the specified initial clock value and absolute time comparer.
  28. /// </summary>
  29. /// <param name="initialClock">Initial value for the clock.</param>
  30. /// <param name="comparer">Comparer to determine causality of events based on absolute time.</param>
  31. protected HistoricalSchedulerBase(DateTimeOffset initialClock, IComparer<DateTimeOffset> comparer)
  32. : base(initialClock, comparer)
  33. {
  34. }
  35. /// <summary>
  36. /// Adds a relative time value to an absolute time value.
  37. /// </summary>
  38. /// <param name="absolute">Absolute time value.</param>
  39. /// <param name="relative">Relative time value to add.</param>
  40. /// <returns>The resulting absolute time sum value.</returns>
  41. protected override DateTimeOffset Add(DateTimeOffset absolute, TimeSpan relative)
  42. {
  43. return absolute.Add(relative);
  44. }
  45. /// <summary>
  46. /// Converts the absolute time value to a DateTimeOffset value.
  47. /// </summary>
  48. /// <param name="absolute">Absolute time value to convert.</param>
  49. /// <returns>The corresponding DateTimeOffset value.</returns>
  50. protected override DateTimeOffset ToDateTimeOffset(DateTimeOffset absolute)
  51. {
  52. return absolute;
  53. }
  54. /// <summary>
  55. /// Converts the TimeSpan value to a relative time value.
  56. /// </summary>
  57. /// <param name="timeSpan">TimeSpan value to convert.</param>
  58. /// <returns>The corresponding relative time value.</returns>
  59. protected override TimeSpan ToRelative(TimeSpan timeSpan)
  60. {
  61. return timeSpan;
  62. }
  63. }
  64. /// <summary>
  65. /// Provides a virtual time scheduler that uses DateTimeOffset for absolute time and TimeSpan for relative time.
  66. /// </summary>
  67. public class HistoricalScheduler : HistoricalSchedulerBase
  68. {
  69. private readonly SchedulerQueue<DateTimeOffset> queue = new SchedulerQueue<DateTimeOffset>();
  70. /// <summary>
  71. /// Creates a new historical scheduler with the minimum value of DateTimeOffset as the initial clock value.
  72. /// </summary>
  73. public HistoricalScheduler()
  74. : base()
  75. {
  76. }
  77. /// <summary>
  78. /// Creates a new historical scheduler with the specified initial clock value.
  79. /// </summary>
  80. /// <param name="initialClock">Initial value for the clock.</param>
  81. public HistoricalScheduler(DateTimeOffset initialClock)
  82. : base(initialClock)
  83. {
  84. }
  85. /// <summary>
  86. /// Creates a new historical scheduler with the specified initial clock value.
  87. /// </summary>
  88. /// <param name="initialClock">Initial value for the clock.</param>
  89. /// <param name="comparer">Comparer to determine causality of events based on absolute time.</param>
  90. /// <exception cref="ArgumentNullException"><paramref name="comparer"/> is null.</exception>
  91. public HistoricalScheduler(DateTimeOffset initialClock, IComparer<DateTimeOffset> comparer)
  92. : base(initialClock, comparer)
  93. {
  94. }
  95. /// <summary>
  96. /// Gets the next scheduled item to be executed.
  97. /// </summary>
  98. /// <returns>The next scheduled item.</returns>
  99. protected override IScheduledItem<DateTimeOffset> GetNext()
  100. {
  101. while (queue.Count > 0)
  102. {
  103. var next = queue.Peek();
  104. if (next.IsCanceled)
  105. queue.Dequeue();
  106. else
  107. return next;
  108. }
  109. return null;
  110. }
  111. /// <summary>
  112. /// Schedules an action to be executed at dueTime.
  113. /// </summary>
  114. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  115. /// <param name="state">State passed to the action to be executed.</param>
  116. /// <param name="action">Action to be executed.</param>
  117. /// <param name="dueTime">Absolute time at which to execute the action.</param>
  118. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  119. /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
  120. public override IDisposable ScheduleAbsolute<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action)
  121. {
  122. if (action == null)
  123. throw new ArgumentNullException("action");
  124. var si = default(ScheduledItem<DateTimeOffset, TState>);
  125. var run = new Func<IScheduler, TState, IDisposable>((scheduler, state1) =>
  126. {
  127. queue.Remove(si);
  128. return action(scheduler, state1);
  129. });
  130. si = new ScheduledItem<DateTimeOffset, TState>(this, state, run, dueTime, Comparer);
  131. queue.Enqueue(si);
  132. return Disposable.Create(si.Cancel);
  133. }
  134. }
  135. }