5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // Marek Safar <marek.safar@gmail.com>
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // Copyright 2012 Xamarin, Inc (http://www.xamarin.com)
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 using System.Collections.Generic;
32 using System.Diagnostics;
34 namespace System.Threading.Tasks
36 [DebuggerDisplay ("Id={Id}")]
37 [DebuggerTypeProxy (typeof (TaskSchedulerDebuggerView))]
38 public abstract class TaskScheduler
40 sealed class TaskSchedulerDebuggerView
42 readonly TaskScheduler scheduler;
44 public TaskSchedulerDebuggerView (TaskScheduler scheduler)
46 this.scheduler = scheduler;
49 public IEnumerable<Task> ScheduledTasks {
51 return scheduler.GetScheduledTasks ();
56 static readonly TaskScheduler defaultScheduler = new TpScheduler ();
59 static TaskScheduler currentScheduler;
62 static int lastId = int.MinValue;
64 public static event EventHandler<UnobservedTaskExceptionEventArgs> UnobservedTaskException;
66 protected TaskScheduler ()
68 this.id = Interlocked.Increment (ref lastId);
71 public static TaskScheduler FromCurrentSynchronizationContext ()
73 var syncCtx = SynchronizationContext.Current;
75 throw new InvalidOperationException ("The current SynchronizationContext is null and cannot be used as a TaskScheduler");
77 return new SynchronizationContextScheduler (syncCtx);
80 public static TaskScheduler Default {
82 return defaultScheduler;
86 public static TaskScheduler Current {
88 if (currentScheduler != null)
89 return currentScheduler;
91 return defaultScheduler;
94 currentScheduler = value;
104 internal static bool IsDefault {
106 return currentScheduler == null || currentScheduler == defaultScheduler;
110 public virtual int MaximumConcurrencyLevel {
116 protected abstract IEnumerable<Task> GetScheduledTasks ();
117 protected internal abstract void QueueTask (Task task);
119 protected internal virtual bool TryDequeue (Task task)
124 internal protected bool TryExecuteTask (Task task)
126 if (task.IsCompleted)
129 if (task.Status == TaskStatus.WaitingToRun) {
131 if (task.WaitOnChildren ())
140 protected abstract bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued);
142 internal bool RunInline (Task task, bool taskWasPreviouslyQueued)
144 if (!TryExecuteTaskInline (task, taskWasPreviouslyQueued))
147 if (!task.IsCompleted)
148 throw new InvalidOperationException ("The TryExecuteTaskInline call to the underlying scheduler succeeded, but the task body was not invoked");
153 internal static UnobservedTaskExceptionEventArgs FireUnobservedEvent (Task task, AggregateException e)
155 UnobservedTaskExceptionEventArgs args = new UnobservedTaskExceptionEventArgs (e);
157 EventHandler<UnobservedTaskExceptionEventArgs> temp = UnobservedTaskException;