JobRunner.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Avalonia.Platform;
  8. namespace Avalonia.Threading
  9. {
  10. /// <summary>
  11. /// A main loop in a <see cref="Dispatcher"/>.
  12. /// </summary>
  13. internal class JobRunner
  14. {
  15. private IPlatformThreadingInterface _platform;
  16. private Queue<Job>[] _queues = Enumerable.Range(0, (int) DispatcherPriority.MaxValue + 1)
  17. .Select(_ => new Queue<Job>()).ToArray();
  18. public JobRunner(IPlatformThreadingInterface platform)
  19. {
  20. _platform = platform;
  21. }
  22. Job GetNextJob(DispatcherPriority minimumPriority)
  23. {
  24. for (int c = (int) DispatcherPriority.MaxValue; c >= (int) minimumPriority; c--)
  25. {
  26. var q = _queues[c];
  27. lock (q)
  28. {
  29. if (q.Count > 0)
  30. return q.Dequeue();
  31. }
  32. }
  33. return null;
  34. }
  35. /// <summary>
  36. /// Runs continuations pushed on the loop.
  37. /// </summary>
  38. /// <param name="priority">Priority to execute jobs for. Pass null if platform doesn't have internal priority system</param>
  39. public void RunJobs(DispatcherPriority? priority)
  40. {
  41. var minimumPriority = priority ?? DispatcherPriority.MinValue;
  42. while (true)
  43. {
  44. var job = GetNextJob(minimumPriority);
  45. if (job == null)
  46. return;
  47. if (job.TaskCompletionSource == null)
  48. {
  49. job.Action();
  50. }
  51. else
  52. {
  53. try
  54. {
  55. job.Action();
  56. job.TaskCompletionSource.SetResult(null);
  57. }
  58. catch (Exception e)
  59. {
  60. job.TaskCompletionSource.SetException(e);
  61. }
  62. }
  63. }
  64. }
  65. /// <summary>
  66. /// Invokes a method on the main loop.
  67. /// </summary>
  68. /// <param name="action">The method.</param>
  69. /// <param name="priority">The priority with which to invoke the method.</param>
  70. /// <returns>A task that can be used to track the method's execution.</returns>
  71. public Task InvokeAsync(Action action, DispatcherPriority priority)
  72. {
  73. var job = new Job(action, priority, false);
  74. AddJob(job);
  75. return job.TaskCompletionSource.Task;
  76. }
  77. /// <summary>
  78. /// Post action that will be invoked on main thread
  79. /// </summary>
  80. /// <param name="action">The method.</param>
  81. ///
  82. /// <param name="priority">The priority with which to invoke the method.</param>
  83. internal void Post(Action action, DispatcherPriority priority)
  84. {
  85. AddJob(new Job(action, priority, true));
  86. }
  87. /// <summary>
  88. /// Allows unit tests to change the platform threading interface.
  89. /// </summary>
  90. internal void UpdateServices()
  91. {
  92. _platform = AvaloniaLocator.Current.GetService<IPlatformThreadingInterface>();
  93. }
  94. private void AddJob(Job job)
  95. {
  96. var needWake = false;
  97. var queue = _queues[(int) job.Priority];
  98. lock (queue)
  99. {
  100. needWake = queue.Count == 0;
  101. queue.Enqueue(job);
  102. }
  103. if (needWake)
  104. _platform?.Signal(job.Priority);
  105. }
  106. /// <summary>
  107. /// A job to run.
  108. /// </summary>
  109. private class Job
  110. {
  111. /// <summary>
  112. /// Initializes a new instance of the <see cref="Job"/> class.
  113. /// </summary>
  114. /// <param name="action">The method to call.</param>
  115. /// <param name="priority">The job priority.</param>
  116. /// <param name="throwOnUiThread">Do not wrap excepption in TaskCompletionSource</param>
  117. public Job(Action action, DispatcherPriority priority, bool throwOnUiThread)
  118. {
  119. Action = action;
  120. Priority = priority;
  121. TaskCompletionSource = throwOnUiThread ? null : new TaskCompletionSource<object>();
  122. }
  123. /// <summary>
  124. /// Gets the method to call.
  125. /// </summary>
  126. public Action Action { get; }
  127. /// <summary>
  128. /// Gets the job priority.
  129. /// </summary>
  130. public DispatcherPriority Priority { get; }
  131. /// <summary>
  132. /// Gets the task completion source.
  133. /// </summary>
  134. public TaskCompletionSource<object> TaskCompletionSource { get; }
  135. }
  136. }
  137. }