Debian Codegraph @

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