ScheduledItem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.Collections.Generic;
  5. using System.Reactive.Disposables;
  6. namespace System.Reactive.Concurrency
  7. {
  8. /// <summary>
  9. /// Abstract base class for scheduled work items.
  10. /// </summary>
  11. /// <typeparam name="TAbsolute">Absolute time representation type.</typeparam>
  12. public abstract class ScheduledItem<TAbsolute> : IScheduledItem<TAbsolute>, IComparable<ScheduledItem<TAbsolute>>
  13. where TAbsolute : IComparable<TAbsolute>
  14. {
  15. private readonly SingleAssignmentDisposable _disposable = new SingleAssignmentDisposable();
  16. private readonly TAbsolute _dueTime;
  17. private readonly IComparer<TAbsolute> _comparer;
  18. /// <summary>
  19. /// Creates a new scheduled work item to run at the specified time.
  20. /// </summary>
  21. /// <param name="dueTime">Absolute time at which the work item has to be executed.</param>
  22. /// <param name="comparer">Comparer used to compare work items based on their scheduled time.</param>
  23. /// <exception cref="ArgumentNullException"><paramref name="comparer"/> is null.</exception>
  24. protected ScheduledItem(TAbsolute dueTime, IComparer<TAbsolute> comparer)
  25. {
  26. if (comparer == null)
  27. throw new ArgumentNullException(nameof(comparer));
  28. _dueTime = dueTime;
  29. _comparer = comparer;
  30. }
  31. /// <summary>
  32. /// Gets the absolute time at which the item is due for invocation.
  33. /// </summary>
  34. public TAbsolute DueTime
  35. {
  36. get { return _dueTime; }
  37. }
  38. /// <summary>
  39. /// Invokes the work item.
  40. /// </summary>
  41. public void Invoke()
  42. {
  43. if (!_disposable.IsDisposed)
  44. _disposable.Disposable = InvokeCore();
  45. }
  46. /// <summary>
  47. /// Implement this method to perform the work item invocation, returning a disposable object for deep cancellation.
  48. /// </summary>
  49. /// <returns>Disposable object used to cancel the work item and/or derived work items.</returns>
  50. protected abstract IDisposable InvokeCore();
  51. #region Inequality
  52. /// <summary>
  53. /// Compares the work item with another work item based on absolute time values.
  54. /// </summary>
  55. /// <param name="other">Work item to compare the current work item to.</param>
  56. /// <returns>Relative ordering between this and the specified work item.</returns>
  57. /// <remarks>The inequality operators are overloaded to provide results consistent with the IComparable implementation. Equality operators implement traditional reference equality semantics.</remarks>
  58. public int CompareTo(ScheduledItem<TAbsolute> other)
  59. {
  60. // MSDN: By definition, any object compares greater than null, and two null references compare equal to each other.
  61. if (object.ReferenceEquals(other, null))
  62. return 1;
  63. return _comparer.Compare(DueTime, other.DueTime);
  64. }
  65. /// <summary>
  66. /// Determines whether one specified ScheduledItem&lt;TAbsolute&gt; object is due before a second specified ScheduledItem&lt;TAbsolute&gt; object.
  67. /// </summary>
  68. /// <param name="left">The first object to compare.</param>
  69. /// <param name="right">The second object to compare.</param>
  70. /// <returns>true if the DueTime value of left is earlier than the DueTime value of right; otherwise, false.</returns>
  71. /// <remarks>This operator provides results consistent with the IComparable implementation.</remarks>
  72. public static bool operator <(ScheduledItem<TAbsolute> left, ScheduledItem<TAbsolute> right)
  73. {
  74. return Comparer<ScheduledItem<TAbsolute>>.Default.Compare(left, right) < 0;
  75. }
  76. /// <summary>
  77. /// Determines whether one specified ScheduledItem&lt;TAbsolute&gt; object is due before or at the same of a second specified ScheduledItem&lt;TAbsolute&gt; object.
  78. /// </summary>
  79. /// <param name="left">The first object to compare.</param>
  80. /// <param name="right">The second object to compare.</param>
  81. /// <returns>true if the DueTime value of left is earlier than or simultaneous with the DueTime value of right; otherwise, false.</returns>
  82. /// <remarks>This operator provides results consistent with the IComparable implementation.</remarks>
  83. public static bool operator <=(ScheduledItem<TAbsolute> left, ScheduledItem<TAbsolute> right)
  84. {
  85. return Comparer<ScheduledItem<TAbsolute>>.Default.Compare(left, right) <= 0;
  86. }
  87. /// <summary>
  88. /// Determines whether one specified ScheduledItem&lt;TAbsolute&gt; object is due after a second specified ScheduledItem&lt;TAbsolute&gt; object.
  89. /// </summary>
  90. /// <param name="left">The first object to compare.</param>
  91. /// <param name="right">The second object to compare.</param>
  92. /// <returns>true if the DueTime value of left is later than the DueTime value of right; otherwise, false.</returns>
  93. /// <remarks>This operator provides results consistent with the IComparable implementation.</remarks>
  94. public static bool operator >(ScheduledItem<TAbsolute> left, ScheduledItem<TAbsolute> right)
  95. {
  96. return Comparer<ScheduledItem<TAbsolute>>.Default.Compare(left, right) > 0;
  97. }
  98. /// <summary>
  99. /// Determines whether one specified ScheduledItem&lt;TAbsolute&gt; object is due after or at the same time of a second specified ScheduledItem&lt;TAbsolute&gt; object.
  100. /// </summary>
  101. /// <param name="left">The first object to compare.</param>
  102. /// <param name="right">The second object to compare.</param>
  103. /// <returns>true if the DueTime value of left is later than or simultaneous with the DueTime value of right; otherwise, false.</returns>
  104. /// <remarks>This operator provides results consistent with the IComparable implementation.</remarks>
  105. public static bool operator >=(ScheduledItem<TAbsolute> left, ScheduledItem<TAbsolute> right)
  106. {
  107. return Comparer<ScheduledItem<TAbsolute>>.Default.Compare(left, right) >= 0;
  108. }
  109. #endregion
  110. #region Equality
  111. /// <summary>
  112. /// Determines whether two specified ScheduledItem&lt;TAbsolute, TValue&gt; objects are equal.
  113. /// </summary>
  114. /// <param name="left">The first object to compare.</param>
  115. /// <param name="right">The second object to compare.</param>
  116. /// <returns>true if both ScheduledItem&lt;TAbsolute, TValue&gt; are equal; otherwise, false.</returns>
  117. /// <remarks>This operator does not provide results consistent with the IComparable implementation. Instead, it implements reference equality.</remarks>
  118. public static bool operator ==(ScheduledItem<TAbsolute> left, ScheduledItem<TAbsolute> right)
  119. {
  120. return object.ReferenceEquals(left, right);
  121. }
  122. /// <summary>
  123. /// Determines whether two specified ScheduledItem&lt;TAbsolute, TValue&gt; objects are inequal.
  124. /// </summary>
  125. /// <param name="left">The first object to compare.</param>
  126. /// <param name="right">The second object to compare.</param>
  127. /// <returns>true if both ScheduledItem&lt;TAbsolute, TValue&gt; are inequal; otherwise, false.</returns>
  128. /// <remarks>This operator does not provide results consistent with the IComparable implementation. Instead, it implements reference equality.</remarks>
  129. public static bool operator !=(ScheduledItem<TAbsolute> left, ScheduledItem<TAbsolute> right)
  130. {
  131. return !(left == right);
  132. }
  133. /// <summary>
  134. /// Determines whether a ScheduledItem&lt;TAbsolute&gt; object is equal to the specified object.
  135. /// </summary>
  136. /// <param name="obj">The object to compare to the current ScheduledItem&lt;TAbsolute&gt; object.</param>
  137. /// <returns>true if the obj parameter is a ScheduledItem&lt;TAbsolute&gt; object and is equal to the current ScheduledItem&lt;TAbsolute&gt; object; otherwise, false.</returns>
  138. public override bool Equals(object obj)
  139. {
  140. return object.ReferenceEquals(this, obj);
  141. }
  142. /// <summary>
  143. /// Returns the hash code for the current ScheduledItem&lt;TAbsolute&gt; object.
  144. /// </summary>
  145. /// <returns>A 32-bit signed integer hash code.</returns>
  146. public override int GetHashCode()
  147. {
  148. return base.GetHashCode();
  149. }
  150. #endregion
  151. /// <summary>
  152. /// Cancels the work item by disposing the resource returned by InvokeCore as soon as possible.
  153. /// </summary>
  154. public void Cancel()
  155. {
  156. _disposable.Dispose();
  157. }
  158. /// <summary>
  159. /// Gets whether the work item has received a cancellation request.
  160. /// </summary>
  161. public bool IsCanceled
  162. {
  163. get { return _disposable.IsDisposed; }
  164. }
  165. }
  166. /// <summary>
  167. /// Represents a scheduled work item based on the materialization of an IScheduler.Schedule method call.
  168. /// </summary>
  169. /// <typeparam name="TAbsolute">Absolute time representation type.</typeparam>
  170. /// <typeparam name="TValue">Type of the state passed to the scheduled action.</typeparam>
  171. public sealed class ScheduledItem<TAbsolute, TValue> : ScheduledItem<TAbsolute>
  172. where TAbsolute : IComparable<TAbsolute>
  173. {
  174. private readonly IScheduler _scheduler;
  175. private readonly TValue _state;
  176. private readonly Func<IScheduler, TValue, IDisposable> _action;
  177. /// <summary>
  178. /// Creates a materialized work item.
  179. /// </summary>
  180. /// <param name="scheduler">Recursive scheduler to invoke the scheduled action with.</param>
  181. /// <param name="state">State to pass to the scheduled action.</param>
  182. /// <param name="action">Scheduled action.</param>
  183. /// <param name="dueTime">Time at which to run the scheduled action.</param>
  184. /// <param name="comparer">Comparer used to compare work items based on their scheduled time.</param>
  185. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> or <paramref name="comparer"/> is null.</exception>
  186. public ScheduledItem(IScheduler scheduler, TValue state, Func<IScheduler, TValue, IDisposable> action, TAbsolute dueTime, IComparer<TAbsolute> comparer)
  187. : base(dueTime, comparer)
  188. {
  189. if (scheduler == null)
  190. throw new ArgumentNullException(nameof(scheduler));
  191. if (action == null)
  192. throw new ArgumentNullException(nameof(action));
  193. _scheduler = scheduler;
  194. _state = state;
  195. _action = action;
  196. }
  197. /// <summary>
  198. /// Creates a materialized work item.
  199. /// </summary>
  200. /// <param name="scheduler">Recursive scheduler to invoke the scheduled action with.</param>
  201. /// <param name="state">State to pass to the scheduled action.</param>
  202. /// <param name="action">Scheduled action.</param>
  203. /// <param name="dueTime">Time at which to run the scheduled action.</param>
  204. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
  205. public ScheduledItem(IScheduler scheduler, TValue state, Func<IScheduler, TValue, IDisposable> action, TAbsolute dueTime)
  206. : this(scheduler, state, action, dueTime, Comparer<TAbsolute>.Default)
  207. {
  208. }
  209. /// <summary>
  210. /// Invokes the scheduled action with the supplied recursive scheduler and state.
  211. /// </summary>
  212. /// <returns>Cancellation resource returned by the scheduled action.</returns>
  213. protected override IDisposable InvokeCore()
  214. {
  215. return _action(_scheduler, _state);
  216. }
  217. }
  218. }