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 =
39 Environment.GetEnvironmentVariable ("USE_OLD_TASK_SCHED") != null ? (TaskScheduler)new Scheduler () : (TaskScheduler)new TpScheduler ();
43 static TaskScheduler currentScheduler;
46 static int lastId = int.MinValue;
48 public static event EventHandler<UnobservedTaskExceptionEventArgs> UnobservedTaskException;
50 protected TaskScheduler ()
52 this.id = Interlocked.Increment (ref lastId);
53 this.proxy = new SchedulerProxy (this);
56 public static TaskScheduler FromCurrentSynchronizationContext ()
58 var syncCtx = SynchronizationContext.Current;
59 return new SynchronizationContextScheduler (syncCtx);
62 public static TaskScheduler Default {
64 return defaultScheduler;
68 public static TaskScheduler Current {
70 if (currentScheduler != null)
71 return currentScheduler;
73 return defaultScheduler;
76 currentScheduler = value;
86 public virtual int MaximumConcurrencyLevel {
88 return Environment.ProcessorCount;
92 internal virtual void ParticipateUntil (Task task)
94 proxy.ParticipateUntil (task);
97 internal virtual bool ParticipateUntil (Task task, ManualResetEventSlim predicateEvt, int millisecondsTimeout)
99 return proxy.ParticipateUntil (task, predicateEvt, millisecondsTimeout);
102 internal virtual void PulseAll ()
107 protected abstract IEnumerable<Task> GetScheduledTasks ();
108 protected internal abstract void QueueTask (Task task);
109 protected internal virtual bool TryDequeue (Task task)
111 throw new NotSupportedException ();
114 internal protected bool TryExecuteTask (Task task)
116 if (task.IsCompleted)
119 if (task.Status == TaskStatus.WaitingToRun) {
127 protected abstract bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued);
129 internal UnobservedTaskExceptionEventArgs FireUnobservedEvent (AggregateException e)
131 UnobservedTaskExceptionEventArgs args = new UnobservedTaskExceptionEventArgs (e);
133 EventHandler<UnobservedTaskExceptionEventArgs> temp = UnobservedTaskException;