Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / class / corlib / System.Threading.Tasks / TaskScheduler.cs
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
31 using System.Collections.Generic;
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                 
71                 public static TaskScheduler FromCurrentSynchronizationContext ()
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                 internal static bool IsDefault {
105                         get {
106                                 return currentScheduler == null || currentScheduler == defaultScheduler;
107                         }
108                 }
109                 
110                 public virtual int MaximumConcurrencyLevel {
111                         get {
112                                 return int.MaxValue;
113                         }
114                 }
115
116                 protected abstract IEnumerable<Task> GetScheduledTasks ();
117                 protected internal abstract void QueueTask (Task task);
118
119                 protected internal virtual bool TryDequeue (Task task)
120                 {
121                         return false;
122                 }
123
124                 internal protected bool TryExecuteTask (Task task)
125                 {
126                         if (task.IsCompleted)
127                                 return false;
128
129                         if (task.Status == TaskStatus.WaitingToRun) {
130                                 task.Execute ();
131                                 if (task.WaitOnChildren ())
132                                         task.Wait ();
133
134                                 return true;
135                         }
136
137                         return false;
138                 }
139
140                 protected abstract bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued);
141
142                 internal bool RunInline (Task task, bool taskWasPreviouslyQueued)
143                 {
144                         if (!TryExecuteTaskInline (task, taskWasPreviouslyQueued))
145                                 return false;
146
147                         if (!task.IsCompleted)
148                                 throw new InvalidOperationException ("The TryExecuteTaskInline call to the underlying scheduler succeeded, but the task body was not invoked");
149
150                         return true;
151                 }
152
153                 internal static UnobservedTaskExceptionEventArgs FireUnobservedEvent (Task task, AggregateException e)
154                 {
155                         UnobservedTaskExceptionEventArgs args = new UnobservedTaskExceptionEventArgs (e);
156                         
157                         EventHandler<UnobservedTaskExceptionEventArgs> temp = UnobservedTaskException;
158                         if (temp == null)
159                                 return args;
160                         
161                         temp (task, args);
162                         
163                         return args;
164                 }
165         }
166 }
167 #endif