TaskScheduler.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 = new Scheduler ();
  37. [ThreadStatic]
  38. static TaskScheduler currentScheduler;
  39. int id;
  40. static int lastId = int.MinValue;
  41. public static event EventHandler<UnobservedTaskExceptionEventArgs> UnobservedTaskException;
  42. protected TaskScheduler ()
  43. {
  44. this.id = Interlocked.Increment (ref lastId);
  45. }
  46. public static TaskScheduler FromCurrentSynchronizationContext ()
  47. {
  48. var syncCtx = SynchronizationContext.Current;
  49. return new SynchronizationContextScheduler (syncCtx);
  50. }
  51. public static TaskScheduler Default {
  52. get {
  53. return defaultScheduler;
  54. }
  55. }
  56. public static TaskScheduler Current {
  57. get {
  58. if (currentScheduler != null)
  59. return currentScheduler;
  60. return defaultScheduler;
  61. }
  62. internal set {
  63. currentScheduler = value;
  64. }
  65. }
  66. public int Id {
  67. get {
  68. return id;
  69. }
  70. }
  71. public virtual int MaximumConcurrencyLevel {
  72. get {
  73. return Environment.ProcessorCount;
  74. }
  75. }
  76. protected abstract IEnumerable<Task> GetScheduledTasks ();
  77. protected internal abstract void QueueTask (Task task);
  78. protected internal virtual bool TryDequeue (Task task)
  79. {
  80. throw new NotSupportedException ();
  81. }
  82. internal protected bool TryExecuteTask (Task task)
  83. {
  84. return TryExecuteTaskInline (task, false);
  85. }
  86. protected abstract bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued);
  87. internal UnobservedTaskExceptionEventArgs FireUnobservedEvent (AggregateException e)
  88. {
  89. UnobservedTaskExceptionEventArgs args = new UnobservedTaskExceptionEventArgs (e);
  90. EventHandler<UnobservedTaskExceptionEventArgs> temp = UnobservedTaskException;
  91. if (temp == null)
  92. return args;
  93. temp (this, args);
  94. return args;
  95. }
  96. }
  97. }
  98. #endif