EventLoopScheduler.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 td = new TernaryDisposable();
  164. var gate = new AsyncLock();
  165. td.Extra = gate;
  166. var tick = default(Func<IScheduler, object, IDisposable>);
  167. tick = (self_, _) =>
  168. {
  169. next += period;
  170. td.Next = self_.Schedule(null, next - _stopwatch.Elapsed, tick);
  171. gate.Wait(() =>
  172. {
  173. state1 = action(state1);
  174. });
  175. return Disposable.Empty;
  176. };
  177. td.First = Schedule(null, next - _stopwatch.Elapsed, tick);
  178. return td;
  179. }
  180. private sealed class TernaryDisposable : IDisposable
  181. {
  182. private IDisposable _task;
  183. private IDisposable _extra;
  184. // If Next was called before this assignment is executed, it won't overwrite
  185. // a more fresh IDisposable task
  186. public IDisposable First { set { Disposable.TrySetSingle(ref _task, value); } }
  187. // It is fine to overwrite the first or previous IDisposable task
  188. public IDisposable Next { set { Disposable.TrySetMultiple(ref _task, value); } }
  189. public IDisposable Extra { set { Disposable.SetSingle(ref _extra, value); } }
  190. public void Dispose()
  191. {
  192. Disposable.TryDispose(ref _task);
  193. Disposable.TryDispose(ref _extra);
  194. }
  195. }
  196. /// <summary>
  197. /// Starts a new stopwatch object.
  198. /// </summary>
  199. /// <returns>New stopwatch object; started at the time of the request.</returns>
  200. public override IStopwatch StartStopwatch()
  201. {
  202. //
  203. // Strictly speaking, this explicit override is not necessary because the base implementation calls into
  204. // the enlightenment module to obtain the CAL, which would circle back to System.Reactive.PlatformServices
  205. // where we're currently running. This is merely a short-circuit to avoid the additional roundtrip.
  206. //
  207. return new StopwatchImpl();
  208. }
  209. /// <summary>
  210. /// Ends the thread associated with this scheduler. All remaining work in the scheduler queue is abandoned.
  211. /// </summary>
  212. public void Dispose()
  213. {
  214. lock (_gate)
  215. {
  216. if (!_disposed)
  217. {
  218. _disposed = true;
  219. _nextTimer.Dispose();
  220. _evt.Release();
  221. }
  222. }
  223. }
  224. #endregion
  225. #region Private implementation
  226. /// <summary>
  227. /// Ensures there is an event loop thread running. Should be called under the gate.
  228. /// </summary>
  229. private void EnsureThread()
  230. {
  231. if (_thread == null)
  232. {
  233. _thread = _threadFactory(Run);
  234. _thread.Start();
  235. }
  236. }
  237. /// <summary>
  238. /// Event loop scheduled on the designated event loop thread. The loop is suspended/resumed using the event
  239. /// which gets set by calls to Schedule, the next item timer, or calls to Dispose.
  240. /// </summary>
  241. private void Run()
  242. {
  243. while (true)
  244. {
  245. _evt.Wait();
  246. var ready = default(ScheduledItem<TimeSpan>[]);
  247. lock (_gate)
  248. {
  249. //
  250. // Bug fix that ensures the number of calls to Release never greatly exceeds the number of calls to Wait.
  251. // See work item #37: https://rx.codeplex.com/workitem/37
  252. //
  253. while (_evt.CurrentCount > 0) _evt.Wait();
  254. //
  255. // The event could have been set by a call to Dispose. This takes priority over anything else. We quit the
  256. // loop immediately. Subsequent calls to Schedule won't ever create a new thread.
  257. //
  258. if (_disposed)
  259. {
  260. ((IDisposable)_evt).Dispose();
  261. return;
  262. }
  263. while (_queue.Count > 0 && _queue.Peek().DueTime <= _stopwatch.Elapsed)
  264. {
  265. var item = _queue.Dequeue();
  266. _readyList.Enqueue(item);
  267. }
  268. if (_queue.Count > 0)
  269. {
  270. var next = _queue.Peek();
  271. if (next != _nextItem)
  272. {
  273. _nextItem = next;
  274. var due = next.DueTime - _stopwatch.Elapsed;
  275. _nextTimer.Disposable = ConcurrencyAbstractionLayer.Current.StartTimer(Tick, next, due);
  276. }
  277. }
  278. if (_readyList.Count > 0)
  279. {
  280. ready = _readyList.ToArray();
  281. _readyList.Clear();
  282. }
  283. }
  284. if (ready != null)
  285. {
  286. foreach (var item in ready)
  287. {
  288. if (!item.IsCanceled)
  289. {
  290. item.Invoke();
  291. }
  292. }
  293. }
  294. if (ExitIfEmpty)
  295. {
  296. lock (_gate)
  297. {
  298. if (_readyList.Count == 0 && _queue.Count == 0)
  299. {
  300. _thread = null;
  301. return;
  302. }
  303. }
  304. }
  305. }
  306. }
  307. private void Tick(object state)
  308. {
  309. lock (_gate)
  310. {
  311. if (!_disposed)
  312. {
  313. var item = (ScheduledItem<TimeSpan>)state;
  314. if (item == _nextItem)
  315. {
  316. _nextItem = null;
  317. }
  318. if (_queue.Remove(item))
  319. {
  320. _readyList.Enqueue(item);
  321. }
  322. _evt.Release();
  323. }
  324. }
  325. }
  326. #endregion
  327. }
  328. }