Fix more argument checking for Task<T>
[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                 SchedulerProxy proxy;
40                 
41                 [ThreadStatic]
42                 static TaskScheduler currentScheduler;
43                 
44                 int id;
45                 static int lastId = int.MinValue;
46                 
47                 public static event EventHandler<UnobservedTaskExceptionEventArgs> UnobservedTaskException;
48                 
49                 protected TaskScheduler ()
50                 {
51                         this.id = Interlocked.Increment (ref lastId);
52                         this.proxy = new SchedulerProxy (this);
53                 }
54
55                 ~TaskScheduler ()
56                 {
57                 }
58                 
59                 public static TaskScheduler FromCurrentSynchronizationContext ()
60                 {
61                         var syncCtx = SynchronizationContext.Current;
62                         return new SynchronizationContextScheduler (syncCtx);
63                 }
64                 
65                 public static TaskScheduler Default  {
66                         get {
67                                 return defaultScheduler;
68                         }
69                 }
70                 
71                 public static TaskScheduler Current  {
72                         get {
73                                 if (currentScheduler != null)
74                                         return currentScheduler;
75                                 
76                                 return defaultScheduler;
77                         }
78                         internal set {
79                                 currentScheduler = value;
80                         }
81                 }
82                 
83                 public int Id {
84                         get {
85                                 return id;
86                         }
87                 }
88                 
89                 public virtual int MaximumConcurrencyLevel {
90                         get {
91                                 return Environment.ProcessorCount;
92                         }
93                 }
94
95                 internal virtual void ParticipateUntil (Task task)
96                 {
97                         proxy.ParticipateUntil (task);
98                 }
99
100                 internal virtual bool ParticipateUntil (Task task, ManualResetEventSlim predicateEvt, int millisecondsTimeout)
101                 {
102                         return proxy.ParticipateUntil (task, predicateEvt, millisecondsTimeout);
103                 }
104
105                 internal virtual void PulseAll ()
106                 {
107                         proxy.PulseAll ();
108                 }
109
110                 protected abstract IEnumerable<Task> GetScheduledTasks ();
111                 protected internal abstract void QueueTask (Task task);
112                 protected internal virtual bool TryDequeue (Task task)
113                 {
114                         throw new NotSupportedException ();
115                 }
116
117                 internal protected bool TryExecuteTask (Task task)
118                 {
119                         if (task.IsCompleted)
120                                 return false;
121
122                         if (task.Status == TaskStatus.WaitingToRun) {
123                                 task.Execute (null);
124                                 return true;
125                         }
126
127                         return false;
128                 }
129
130                 protected abstract bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued);
131
132                 internal bool RunInline (Task task)
133                 {
134                         if (!TryExecuteTaskInline (task, false))
135                                 return false;
136
137                         if (!task.IsCompleted)
138                                 throw new InvalidOperationException ("The TryExecuteTaskInline call to the underlying scheduler succeeded, but the task body was not invoked");
139                         return true;
140                 }
141
142                 internal UnobservedTaskExceptionEventArgs FireUnobservedEvent (AggregateException e)
143                 {
144                         UnobservedTaskExceptionEventArgs args = new UnobservedTaskExceptionEventArgs (e);
145                         
146                         EventHandler<UnobservedTaskExceptionEventArgs> temp = UnobservedTaskException;
147                         if (temp == null)
148                                 return args;
149                         
150                         temp (this, args);
151                         
152                         return args;
153                 }
154         }
155 }
156 #endif