Debian Codegraph @

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

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