TaskScheduler.cs 3.9 KB

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