1
0

EventLoopScheduler.cs 13 KB

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