Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.PlatformServices/Reactive/Concurrency/ThreadPoolScheduler.Windows.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if WINDOWS
4 using System.Reactive.Concurrency;
5 using System.Reactive.Disposables;
6 using Windows.System.Threading;
7
8 namespace System.Reactive.Concurrency
9 {
10 /// <summary>
11 /// Represents an object that schedules units of work on the Windows Runtime thread pool.
12 /// </summary>
13 /// <seealso cref="ThreadPoolScheduler.Default">Singleton instance of this type exposed through this static property.</seealso>
14 [CLSCompliant(false)]
15 public sealed class ThreadPoolScheduler : LocalScheduler, ISchedulerPeriodic
16 {
17 private readonly WorkItemPriority _priority;
18 private readonly WorkItemOptions _options;
19 private static Lazy<ThreadPoolScheduler> s_default = new Lazy<ThreadPoolScheduler>(() => new ThreadPoolScheduler());
20
21 /// <summary>
22 /// Constructs a ThreadPoolScheduler that schedules units of work on the Windows ThreadPool.
23 /// </summary>
24 public ThreadPoolScheduler()
25 {
26 }
27
28 /// <summary>
29 /// Constructs a ThreadPoolScheduler that schedules units of work on the Windows ThreadPool with the given priority.
30 /// </summary>
31 /// <param name="priority">Priority for scheduled units of work.</param>
32 public ThreadPoolScheduler(WorkItemPriority priority)
33 {
34 _priority = priority;
35 _options = WorkItemOptions.None;
36 }
37
38 /// <summary>
39 /// Constructs a ThreadPoolScheduler that schedules units of work on the Windows ThreadPool with the given priority.
40 /// </summary>
41 /// <param name="priority">Priority for scheduled units of work.</param>
42 /// <param name="options">Options that configure how work is scheduled.</param>
43 public ThreadPoolScheduler(WorkItemPriority priority, WorkItemOptions options)
44 {
45 _priority = priority;
46 _options = options;
47 }
48
49 /// <summary>
50 /// Gets the singleton instance of the Windows Runtime thread pool scheduler.
51 /// </summary>
52 public static ThreadPoolScheduler Default
53 {
54 get
55 {
56 return s_default.Value;
57 }
58 }
59
60 /// <summary>
61 /// Gets the priority at which work is scheduled.
62 /// </summary>
63 public WorkItemPriority Priority
64 {
65 get { return _priority; }
66 }
67
68 /// <summary>
69 /// Gets the options that configure how work is scheduled.
70 /// </summary>
71 public WorkItemOptions Options
72 {
73 get { return _options; }
74 }
75
76 /// <summary>
77 /// Schedules an action to be executed.
78 /// </summary>
79 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
80 /// <param name="state">State passed to the action to be executed.</param>
81 /// <param name="action">Action to be executed.</param>
82 /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
83 /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
84 public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
85 {
86 if (action == null)
87 throw new ArgumentNullException("action");
88
89 var d = new SingleAssignmentDisposable();
90
91 var res = global::Windows.System.Threading.ThreadPool.RunAsync(iaa =>
92 {
93 if (!d.IsDisposed)
94 d.Disposable = action(this, state);
95 }, _priority, _options);
96
97 return new CompositeDisposable(
98 d,
99 Disposable.Create(res.Cancel)
100 );
101 }
102
103 /// <summary>
104 /// Schedules an action to be executed after dueTime, using a Windows.System.Threading.ThreadPoolTimer object.
105 /// </summary>
106 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
107 /// <param name="state">State passed to the action to be executed.</param>
108 /// <param name="action">Action to be executed.</param>
109 /// <param name="dueTime">Relative time after which to execute the action.</param>
110 /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
111 /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
112 public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
113 {
114 if (action == null)
115 throw new ArgumentNullException("action");
116
117 var dt = Scheduler.Normalize(dueTime);
118
119 if (dt.Ticks == 0)
120 return Schedule(state, action);
121
122 var d = new SingleAssignmentDisposable();
123
124 var res = global::Windows.System.Threading.ThreadPoolTimer.CreateTimer(
125 tpt =>
126 {
127 if (!d.IsDisposed)
128 d.Disposable = action(this, state);
129 },
130 dt
131 );
132
133 return new CompositeDisposable(
134 d,
135 Disposable.Create(res.Cancel)
136 );
137 }
138
139 /// <summary>
140 /// Schedules a periodic piece of work, using a Windows.System.Threading.ThreadPoolTimer object.
141 /// </summary>
142 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
143 /// <param name="state">Initial state passed to the action upon the first iteration.</param>
144 /// <param name="period">Period for running the work periodically.</param>
145 /// <param name="action">Action to be executed, potentially updating the state.</param>
146 /// <returns>The disposable object used to cancel the scheduled recurring action (best effort).</returns>
147 /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
148 /// <exception cref="ArgumentOutOfRangeException"><paramref name="period"/> is less than one millisecond.</exception>
149 public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action)
150 {
151 //
152 // The WinRT thread pool is based on the Win32 thread pool and cannot handle
153 // sub-1ms resolution. When passing a lower period, we get single-shot
154 // timer behavior instead. See MSDN documentation for CreatePeriodicTimer
155 // for more information.
156 //
157 if (period < TimeSpan.FromMilliseconds(1))
158 throw new ArgumentOutOfRangeException("period", Strings_PlatformServices.WINRT_NO_SUB1MS_TIMERS);
159 if (action == null)
160 throw new ArgumentNullException("action");
161
162 var state1 = state;
163 var gate = new AsyncLock();
164
165 var res = global::Windows.System.Threading.ThreadPoolTimer.CreatePeriodicTimer(
166 tpt =>
167 {
168 gate.Wait(() =>
169 {
170 state1 = action(state1);
171 });
172 },
173 period
174 );
175
176 return Disposable.Create(() =>
177 {
178 res.Cancel();
179 gate.Dispose();
180 action = Stubs<TState>.I;
181 });
182 }
183 }
184 }
185 #endif