Don't special casee adding a TaskCanceledException to the AggregateException list
[mono.git] / mcs / class / corlib / System.Threading.Tasks / TaskScheduler.cs
1 // 
2 // TaskScheduler.cs
3 //  
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // 
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
8 // 
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:
15 // 
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 // 
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
25 // THE SOFTWARE.
26
27 #if NET_4_0 || MOBILE
28 using System;
29 using System.Threading;
30 using System.Collections.Generic;
31
32 namespace System.Threading.Tasks
33 {
34         [System.Diagnostics.DebuggerDisplay ("Id={Id}")]
35         [System.Diagnostics.DebuggerTypeProxy ("System.Threading.Tasks.TaskScheduler+SystemThreadingTasks_TaskSchedulerDebugView")]
36         public abstract class TaskScheduler
37         {
38                 static TaskScheduler defaultScheduler = new TpScheduler ();
39                 
40                 [ThreadStatic]
41                 static TaskScheduler currentScheduler;
42                 
43                 int id;
44                 static int lastId = int.MinValue;
45                 
46                 public static event EventHandler<UnobservedTaskExceptionEventArgs> UnobservedTaskException;
47                 
48                 protected TaskScheduler ()
49                 {
50                         this.id = Interlocked.Increment (ref lastId);
51                 }
52
53                 ~TaskScheduler ()
54                 {
55                 }
56                 
57                 public static TaskScheduler FromCurrentSynchronizationContext ()
58                 {
59                         var syncCtx = SynchronizationContext.Current;
60                         return new SynchronizationContextScheduler (syncCtx);
61                 }
62                 
63                 public static TaskScheduler Default  {
64                         get {
65                                 return defaultScheduler;
66                         }
67                 }
68                 
69                 public static TaskScheduler Current  {
70                         get {
71                                 if (currentScheduler != null)
72                                         return currentScheduler;
73                                 
74                                 return defaultScheduler;
75                         }
76                         internal set {
77                                 currentScheduler = value;
78                         }
79                 }
80                 
81                 public int Id {
82                         get {
83                                 return id;
84                         }
85                 }
86                 
87                 public virtual int MaximumConcurrencyLevel {
88                         get {
89                                 return Environment.ProcessorCount;
90                         }
91                 }
92
93                 protected abstract IEnumerable<Task> GetScheduledTasks ();
94                 protected internal abstract void QueueTask (Task task);
95                 protected internal virtual bool TryDequeue (Task task)
96                 {
97                         throw new NotSupportedException ();
98                 }
99
100                 internal protected bool TryExecuteTask (Task task)
101                 {
102                         if (task.IsCompleted)
103                                 return false;
104
105                         if (task.Status == TaskStatus.WaitingToRun) {
106                                 task.Execute (null);
107                                 return true;
108                         }
109
110                         return false;
111                 }
112
113                 protected abstract bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued);
114
115                 internal bool RunInline (Task task)
116                 {
117                         if (!TryExecuteTaskInline (task, false))
118                                 return false;
119
120                         if (!task.IsCompleted)
121                                 throw new InvalidOperationException ("The TryExecuteTaskInline call to the underlying scheduler succeeded, but the task body was not invoked");
122                         return true;
123                 }
124
125                 internal UnobservedTaskExceptionEventArgs FireUnobservedEvent (AggregateException e)
126                 {
127                         UnobservedTaskExceptionEventArgs args = new UnobservedTaskExceptionEventArgs (e);
128                         
129                         EventHandler<UnobservedTaskExceptionEventArgs> temp = UnobservedTaskException;
130                         if (temp == null)
131                                 return args;
132                         
133                         temp (this, args);
134                         
135                         return args;
136                 }
137         }
138 }
139 #endif