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 ();
42 static TaskScheduler currentScheduler;
45 static int lastId = int.MinValue;
47 public static event EventHandler<UnobservedTaskExceptionEventArgs> UnobservedTaskException;
49 protected TaskScheduler ()
51 this.id = Interlocked.Increment (ref lastId);
52 this.proxy = new SchedulerProxy (this);
59 public static TaskScheduler FromCurrentSynchronizationContext ()
61 var syncCtx = SynchronizationContext.Current;
62 return new SynchronizationContextScheduler (syncCtx);
65 public static TaskScheduler Default {
67 return defaultScheduler;
71 public static TaskScheduler Current {
73 if (currentScheduler != null)
74 return currentScheduler;
76 return defaultScheduler;
79 currentScheduler = value;
89 public virtual int MaximumConcurrencyLevel {
91 return Environment.ProcessorCount;
95 internal virtual void ParticipateUntil (Task task)
97 proxy.ParticipateUntil (task);
100 internal virtual bool ParticipateUntil (Task task, ManualResetEventSlim predicateEvt, int millisecondsTimeout)
102 return proxy.ParticipateUntil (task, predicateEvt, millisecondsTimeout);
105 internal virtual void PulseAll ()
110 protected abstract IEnumerable<Task> GetScheduledTasks ();
111 protected internal abstract void QueueTask (Task task);
112 protected internal virtual bool TryDequeue (Task task)
114 throw new NotSupportedException ();
117 internal protected bool TryExecuteTask (Task task)
119 if (task.IsCompleted)
122 if (task.Status == TaskStatus.WaitingToRun) {
130 protected abstract bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued);
132 internal bool RunInline (Task task)
134 if (!TryExecuteTaskInline (task, false))
137 if (!task.IsCompleted)
138 throw new InvalidOperationException ("The TryExecuteTaskInline call to the underlying scheduler succeeded, but the task body was not invoked");
142 internal UnobservedTaskExceptionEventArgs FireUnobservedEvent (AggregateException e)
144 UnobservedTaskExceptionEventArgs args = new UnobservedTaskExceptionEventArgs (e);
146 EventHandler<UnobservedTaskExceptionEventArgs> temp = UnobservedTaskException;