TaskScheduler.cs
Go to the documentation of this file.
1 //
2 // TaskScheduler.cs
3 //
4 // Authors:
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // Marek Safar <marek.safar@gmail.com>
7 //
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // Copyright 2012 Xamarin, Inc (http://www.xamarin.com)
10 //
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:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
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
27 // THE SOFTWARE.
28 
29 #if NET_4_0
30 
32 using System.Diagnostics;
33 
34 namespace System.Threading.Tasks
35 {
36  [DebuggerDisplay ("Id={Id}")]
37  [DebuggerTypeProxy (typeof (TaskSchedulerDebuggerView))]
38  public abstract class TaskScheduler
39  {
40  sealed class TaskSchedulerDebuggerView
41  {
42  readonly TaskScheduler scheduler;
43 
44  public TaskSchedulerDebuggerView (TaskScheduler scheduler)
45  {
46  this.scheduler = scheduler;
47  }
48 
49  public IEnumerable<Task> ScheduledTasks {
50  get {
51  return scheduler.GetScheduledTasks ();
52  }
53  }
54  }
55 
56  static readonly TaskScheduler defaultScheduler = new TpScheduler ();
57 
58  [ThreadStatic]
59  static TaskScheduler currentScheduler;
60 
61  int id;
62  static int lastId = int.MinValue;
63 
64  public static event EventHandler<UnobservedTaskExceptionEventArgs> UnobservedTaskException;
65 
66  protected TaskScheduler ()
67  {
68  this.id = Interlocked.Increment (ref lastId);
69  }
70 
72  {
73  var syncCtx = SynchronizationContext.Current;
74  if (syncCtx == null)
75  throw new InvalidOperationException ("The current SynchronizationContext is null and cannot be used as a TaskScheduler");
76 
77  return new SynchronizationContextScheduler (syncCtx);
78  }
79 
80  public static TaskScheduler Default {
81  get {
82  return defaultScheduler;
83  }
84  }
85 
86  public static TaskScheduler Current {
87  get {
88  if (currentScheduler != null)
89  return currentScheduler;
90 
91  return defaultScheduler;
92  }
93  internal set {
94  currentScheduler = value;
95  }
96  }
97 
98  public int Id {
99  get {
100  return id;
101  }
102  }
103 
104  public virtual int MaximumConcurrencyLevel {
105  get {
106  return int.MaxValue;
107  }
108  }
109 
110  protected abstract IEnumerable<Task> GetScheduledTasks ();
111  protected internal abstract void QueueTask (Task task);
112 
113  protected internal virtual bool TryDequeue (Task task)
114  {
115  return false;
116  }
117 
118  internal protected bool TryExecuteTask (Task task)
119  {
120  if (task.IsCompleted)
121  return false;
122 
123  if (task.Status == TaskStatus.WaitingToRun) {
124  task.Execute ();
125  if (task.WaitOnChildren ())
126  task.Wait ();
127 
128  return true;
129  }
130 
131  return false;
132  }
133 
134  protected abstract bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued);
135 
136  internal bool RunInline (Task task, bool taskWasPreviouslyQueued)
137  {
138  if (!TryExecuteTaskInline (task, taskWasPreviouslyQueued))
139  return false;
140 
141  if (!task.IsCompleted)
142  throw new InvalidOperationException ("The TryExecuteTaskInline call to the underlying scheduler succeeded, but the task body was not invoked");
143 
144  return true;
145  }
146 
147  internal static UnobservedTaskExceptionEventArgs FireUnobservedEvent (Task task, AggregateException e)
148  {
150 
151  EventHandler<UnobservedTaskExceptionEventArgs> temp = UnobservedTaskException;
152  if (temp == null)
153  return args;
154 
155  temp (task, args);
156 
157  return args;
158  }
159  }
160 }
161 #endif
abstract IEnumerable< Task > GetScheduledTasks()
static TaskScheduler FromCurrentSynchronizationContext()
static EventHandler< UnobservedTaskExceptionEventArgs > UnobservedTaskException