TaskScheduler.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // TaskScheduler.cs
  3. //
  4. // Authors:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. // Marek Safar <[email protected]>
  7. //
  8. // Copyright (c) 2009 Jérémie "Garuma" Laval
  9. // Copyright 2012 Xamarin, Inc (http://www.xamarin.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in
  19. // all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. // THE SOFTWARE.
  28. #if NET_4_0
  29. using System.Collections.Generic;
  30. using System.Diagnostics;
  31. namespace System.Threading.Tasks
  32. {
  33. [DebuggerDisplay ("Id={Id}")]
  34. [DebuggerTypeProxy (typeof (TaskSchedulerDebuggerView))]
  35. public abstract class TaskScheduler
  36. {
  37. sealed class TaskSchedulerDebuggerView
  38. {
  39. readonly TaskScheduler scheduler;
  40. public TaskSchedulerDebuggerView (TaskScheduler scheduler)
  41. {
  42. this.scheduler = scheduler;
  43. }
  44. public IEnumerable<Task> ScheduledTasks {
  45. get {
  46. return scheduler.GetScheduledTasks ();
  47. }
  48. }
  49. }
  50. static readonly TaskScheduler defaultScheduler = new TpScheduler ();
  51. [ThreadStatic]
  52. static TaskScheduler currentScheduler;
  53. int id;
  54. static int lastId = int.MinValue;
  55. public static event EventHandler<UnobservedTaskExceptionEventArgs> UnobservedTaskException;
  56. protected TaskScheduler ()
  57. {
  58. this.id = Interlocked.Increment (ref lastId);
  59. }
  60. public static TaskScheduler FromCurrentSynchronizationContext ()
  61. {
  62. var syncCtx = SynchronizationContext.Current;
  63. if (syncCtx == null)
  64. throw new InvalidOperationException ("The current SynchronizationContext is null and cannot be used as a TaskScheduler");
  65. return new SynchronizationContextScheduler (syncCtx);
  66. }
  67. public static TaskScheduler Default {
  68. get {
  69. return defaultScheduler;
  70. }
  71. }
  72. public static TaskScheduler Current {
  73. get {
  74. if (currentScheduler != null)
  75. return currentScheduler;
  76. return defaultScheduler;
  77. }
  78. internal set {
  79. currentScheduler = value;
  80. }
  81. }
  82. public int Id {
  83. get {
  84. return id;
  85. }
  86. }
  87. public virtual int MaximumConcurrencyLevel {
  88. get {
  89. return int.MaxValue;
  90. }
  91. }
  92. protected abstract IEnumerable<Task> GetScheduledTasks ();
  93. protected internal abstract void QueueTask (Task task);
  94. protected internal virtual bool TryDequeue (Task task)
  95. {
  96. return false;
  97. }
  98. internal protected bool TryExecuteTask (Task task)
  99. {
  100. if (task.IsCompleted)
  101. return false;
  102. if (task.Status == TaskStatus.WaitingToRun) {
  103. task.Execute ();
  104. if (task.WaitOnChildren ())
  105. task.Wait ();
  106. return true;
  107. }
  108. return false;
  109. }
  110. protected abstract bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued);
  111. internal bool RunInline (Task task, bool taskWasPreviouslyQueued)
  112. {
  113. if (!TryExecuteTaskInline (task, taskWasPreviouslyQueued))
  114. return false;
  115. if (!task.IsCompleted)
  116. throw new InvalidOperationException ("The TryExecuteTaskInline call to the underlying scheduler succeeded, but the task body was not invoked");
  117. return true;
  118. }
  119. internal static UnobservedTaskExceptionEventArgs FireUnobservedEvent (Task task, AggregateException e)
  120. {
  121. UnobservedTaskExceptionEventArgs args = new UnobservedTaskExceptionEventArgs (e);
  122. EventHandler<UnobservedTaskExceptionEventArgs> temp = UnobservedTaskException;
  123. if (temp == null)
  124. return args;
  125. temp (task, args);
  126. return args;
  127. }
  128. }
  129. }
  130. #endif