1
0

EventLoopScheduler.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. using System.Threading;
  7. namespace System.Reactive.Concurrency
  8. {
  9. /// <summary>
  10. /// Represents an object that schedules units of work on a designated thread.
  11. /// </summary>
  12. public sealed class EventLoopScheduler : LocalScheduler, ISchedulerPeriodic, IDisposable
  13. {
  14. #region Fields
  15. /// <summary>
  16. /// Counter for diagnostic purposes, to name the threads.
  17. /// </summary>
  18. private static int s_counter;
  19. /// <summary>
  20. /// Thread factory function.
  21. /// </summary>
  22. private readonly Func<ThreadStart, Thread> _threadFactory;
  23. /// <summary>
  24. /// Stopwatch for timing free of absolute time dependencies.
  25. /// </summary>
  26. private IStopwatch _stopwatch;
  27. /// <summary>
  28. /// Thread used by the event loop to run work items on. No work should be run on any other thread.
  29. /// If ExitIfEmpty is set, the thread can quit and a new thread will be created when new work is scheduled.
  30. /// </summary>
  31. private Thread _thread;
  32. /// <summary>
  33. /// Gate to protect data structures, including the work queue and the ready list.
  34. /// </summary>
  35. private readonly object _gate;
  36. /// <summary>
  37. /// Semaphore to count requests to re-evaluate the queue, from either Schedule requests or when a timer
  38. /// expires and moves on to the next item in the queue.
  39. /// </summary>
  40. private readonly SemaphoreSlim _evt;
  41. /// <summary>
  42. /// Queue holding work items. Protected by the gate.
  43. /// </summary>
  44. private readonly SchedulerQueue<TimeSpan> _queue;
  45. /// <summary>
  46. /// Queue holding items that are ready to be run as soon as possible. Protected by the gate.
  47. /// </summary>
  48. private readonly Queue<ScheduledItem<TimeSpan>> _readyList;
  49. /// <summary>
  50. /// Work item that will be scheduled next. Used upon reevaluation of the queue to check whether the next
  51. /// item is still the same. If not, a new timer needs to be started (see below).
  52. /// </summary>
  53. private ScheduledItem<TimeSpan> _nextItem;
  54. /// <summary>
  55. /// Disposable that always holds the timer to dispatch the first element in the queue.
  56. /// </summary>
  57. private readonly SerialDisposable _nextTimer;
  58. /// <summary>
  59. /// Flag indicating whether the event loop should quit. When set, the event should be signaled as well to
  60. /// wake up the event loop thread, which will subsequently abandon all work.
  61. /// </summary>
  62. private bool _disposed;
  63. #endregion
  64. #region Constructors
  65. /// <summary>
  66. /// Creates an object that schedules units of work on a designated thread.
  67. /// </summary>
  68. public EventLoopScheduler()
  69. : this(a => new Thread(a) { Name = "Event Loop " + Interlocked.Increment(ref s_counter), IsBackground = true })
  70. {
  71. }
  72. #if !NO_THREAD
  73. /// <summary>
  74. /// Creates an object that schedules units of work on a designated thread, using the specified factory to control thread creation options.
  75. /// </summary>
  76. /// <param name="threadFactory">Factory function for thread creation.</param>
  77. /// <exception cref="ArgumentNullException"><paramref name="threadFactory"/> is <c>null</c>.</exception>
  78. public EventLoopScheduler(Func<ThreadStart, Thread> threadFactory)
  79. {
  80. if (threadFactory == null)
  81. throw new ArgumentNullException(nameof(threadFactory));
  82. #else
  83. internal EventLoopScheduler(Func<ThreadStart, Thread> threadFactory)
  84. {
  85. #endif
  86. _threadFactory = threadFactory;
  87. _stopwatch = ConcurrencyAbstractionLayer.Current.StartStopwatch();
  88. _gate = new object();
  89. _evt = new SemaphoreSlim(0);
  90. _queue = new SchedulerQueue<TimeSpan>();
  91. _readyList = new Queue<ScheduledItem<TimeSpan>>();
  92. _nextTimer = new SerialDisposable();
  93. ExitIfEmpty = false;
  94. }
  95. #endregion
  96. #region Properties
  97. /// <summary>
  98. /// Indicates whether the event loop thread is allowed to quit when no work is left. If new work
  99. /// is scheduled afterwards, a new event loop thread is created. This property is used by the
  100. /// NewThreadScheduler which uses an event loop for its recursive invocations.
  101. /// </summary>
  102. internal bool ExitIfEmpty
  103. {
  104. get;
  105. set;
  106. }
  107. #endregion
  108. #region Public methods
  109. /// <summary>
  110. /// Schedules an action to be executed after dueTime.
  111. /// </summary>
  112. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  113. /// <param name="state">State passed to the action to be executed.</param>
  114. /// <param name="action">Action to be executed.</param>
  115. /// <param name="dueTime">Relative time after which to execute the action.</param>
  116. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  117. /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
  118. /// <exception cref="ObjectDisposedException">The scheduler has been disposed and doesn't accept new work.</exception>
  119. public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
  120. {
  121. if (action == null)
  122. throw new ArgumentNullException(nameof(action));
  123. var due = _stopwatch.Elapsed + dueTime;
  124. var si = new ScheduledItem<TimeSpan, TState>(this, state, action, due);
  125. lock (_gate)
  126. {
  127. if (_disposed)
  128. throw new ObjectDisposedException("");
  129. if (dueTime <= TimeSpan.Zero)
  130. {
  131. _readyList.Enqueue(si);
  132. _evt.Release();
  133. }
  134. else
  135. {
  136. _queue.Enqueue(si);
  137. _evt.Release();
  138. }
  139. EnsureThread();
  140. }
  141. return Disposable.Create(si.Cancel);
  142. }
  143. /// <summary>
  144. /// Schedules a periodic piece of work on the designated thread.
  145. /// </summary>
  146. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  147. /// <param name="state">Initial state passed to the action upon the first iteration.</param>
  148. /// <param name="period">Period for running the work periodically.</param>
  149. /// <param name="action">Action to be executed, potentially updating the state.</param>
  150. /// <returns>The disposable object used to cancel the scheduled recurring action (best effort).</returns>
  151. /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
  152. /// <exception cref="ArgumentOutOfRangeException"><paramref name="period"/> is less than <see cref="TimeSpan.Zero"/>.</exception>
  153. /// <exception cref="ObjectDisposedException">The scheduler has been disposed and doesn't accept new work.</exception>
  154. public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action)
  155. {
  156. if (period < TimeSpan.Zero)
  157. throw new ArgumentOutOfRangeException(nameof(period));
  158. if (action == null)
  159. throw new ArgumentNullException(nameof(action));
  160. var start = _stopwatch.Elapsed;
  161. var next = start + period;
  162. var state1 = state;
  163. var d = new MultipleAssignmentDisposable();
  164. var gate = new AsyncLock();
  165. var tick = default(Func<IScheduler, object, IDisposable>);
  166. tick = (self_, _) =>
  167. {
  168. next += period;
  169. d.Disposable = self_.Schedule(null, next - _stopwatch.Elapsed, tick);
  170. gate.Wait(() =>
  171. {
  172. state1 = action(state1);
  173. });
  174. return Disposable.Empty;
  175. };
  176. d.Disposable = Schedule(null, next - _stopwatch.Elapsed, tick);
  177. return StableCompositeDisposable.Create(d, gate);
  178. }
  179. /// <summary>
  180. /// Starts a new stopwatch object.
  181. /// </summary>
  182. /// <returns>New stopwatch object; started at the time of the request.</returns>
  183. public override IStopwatch StartStopwatch()
  184. {
  185. //
  186. // Strictly speaking, this explicit override is not necessary because the base implementation calls into
  187. // the enlightenment module to obtain the CAL, which would circle back to System.Reactive.PlatformServices
  188. // where we're currently running. This is merely a short-circuit to avoid the additional roundtrip.
  189. //
  190. return new StopwatchImpl();
  191. }
  192. /// <summary>
  193. /// Ends the thread associated with this scheduler. All remaining work in the scheduler queue is abandoned.
  194. /// </summary>
  195. public void Dispose()
  196. {
  197. lock (_gate)
  198. {
  199. if (!_disposed)
  200. {
  201. _disposed = true;
  202. _nextTimer.Dispose();
  203. _evt.Release();
  204. }
  205. }
  206. }
  207. #endregion
  208. #region Private implementation
  209. /// <summary>
  210. /// Ensures there is an event loop thread running. Should be called under the gate.
  211. /// </summary>
  212. private void EnsureThread()
  213. {
  214. if (_thread == null)
  215. {
  216. _thread = _threadFactory(Run);
  217. _thread.Start();
  218. }
  219. }
  220. /// <summary>
  221. /// Event loop scheduled on the designated event loop thread. The loop is suspended/resumed using the event
  222. /// which gets set by calls to Schedule, the next item timer, or calls to Dispose.
  223. /// </summary>
  224. private void Run()
  225. {
  226. while (true)
  227. {
  228. _evt.Wait();
  229. var ready = default(ScheduledItem<TimeSpan>[]);
  230. lock (_gate)
  231. {
  232. //
  233. // Bug fix that ensures the number of calls to Release never greatly exceeds the number of calls to Wait.
  234. // See work item #37: https://rx.codeplex.com/workitem/37
  235. //
  236. while (_evt.CurrentCount > 0) _evt.Wait();
  237. //
  238. // The event could have been set by a call to Dispose. This takes priority over anything else. We quit the
  239. // loop immediately. Subsequent calls to Schedule won't ever create a new thread.
  240. //
  241. if (_disposed)
  242. {
  243. ((IDisposable)_evt).Dispose();
  244. return;
  245. }
  246. while (_queue.Count > 0 && _queue.Peek().DueTime <= _stopwatch.Elapsed)
  247. {
  248. var item = _queue.Dequeue();
  249. _readyList.Enqueue(item);
  250. }
  251. if (_queue.Count > 0)
  252. {
  253. var next = _queue.Peek();
  254. if (next != _nextItem)
  255. {
  256. _nextItem = next;
  257. var due = next.DueTime - _stopwatch.Elapsed;
  258. _nextTimer.Disposable = ConcurrencyAbstractionLayer.Current.StartTimer(Tick, next, due);
  259. }
  260. }
  261. if (_readyList.Count > 0)
  262. {
  263. ready = _readyList.ToArray();
  264. _readyList.Clear();
  265. }
  266. }
  267. if (ready != null)
  268. {
  269. foreach (var item in ready)
  270. {
  271. if (!item.IsCanceled)
  272. {
  273. item.Invoke();
  274. }
  275. }
  276. }
  277. if (ExitIfEmpty)
  278. {
  279. lock (_gate)
  280. {
  281. if (_readyList.Count == 0 && _queue.Count == 0)
  282. {
  283. _thread = null;
  284. return;
  285. }
  286. }
  287. }
  288. }
  289. }
  290. private void Tick(object state)
  291. {
  292. lock (_gate)
  293. {
  294. if (!_disposed)
  295. {
  296. var item = (ScheduledItem<TimeSpan>)state;
  297. if (_queue.Remove(item))
  298. {
  299. _readyList.Enqueue(item);
  300. }
  301. _evt.Release();
  302. }
  303. }
  304. }
  305. #endregion
  306. }
  307. }