5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
29 using System.Threading;
30 using System.Collections.Generic;
32 namespace System.Threading.Tasks
34 [System.Diagnostics.DebuggerDisplay ("Id={Id}")]
35 [System.Diagnostics.DebuggerTypeProxy ("System.Threading.Tasks.TaskScheduler+SystemThreadingTasks_TaskSchedulerDebugView")]
36 public abstract class TaskScheduler
38 static TaskScheduler defaultScheduler = new TpScheduler ();
41 static TaskScheduler currentScheduler;
44 static int lastId = int.MinValue;
46 public static event EventHandler<UnobservedTaskExceptionEventArgs> UnobservedTaskException;
48 protected TaskScheduler ()
50 this.id = Interlocked.Increment (ref lastId);
57 public static TaskScheduler FromCurrentSynchronizationContext ()
59 var syncCtx = SynchronizationContext.Current;
60 return new SynchronizationContextScheduler (syncCtx);
63 public static TaskScheduler Default {
65 return defaultScheduler;
69 public static TaskScheduler Current {
71 if (currentScheduler != null)
72 return currentScheduler;
74 return defaultScheduler;
77 currentScheduler = value;
87 public virtual int MaximumConcurrencyLevel {
89 return Environment.ProcessorCount;
93 protected abstract IEnumerable<Task> GetScheduledTasks ();
94 protected internal abstract void QueueTask (Task task);
95 protected internal virtual bool TryDequeue (Task task)
97 throw new NotSupportedException ();
100 internal protected bool TryExecuteTask (Task task)
102 if (task.IsCompleted)
105 if (task.Status == TaskStatus.WaitingToRun) {
113 protected abstract bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued);
115 internal bool RunInline (Task task)
117 if (!TryExecuteTaskInline (task, false))
120 if (!task.IsCompleted)
121 throw new InvalidOperationException ("The TryExecuteTaskInline call to the underlying scheduler succeeded, but the task body was not invoked");
125 internal UnobservedTaskExceptionEventArgs FireUnobservedEvent (AggregateException e)
127 UnobservedTaskExceptionEventArgs args = new UnobservedTaskExceptionEventArgs (e);
129 EventHandler<UnobservedTaskExceptionEventArgs> temp = UnobservedTaskException;