Author: Jérémie Laval <jeremie.laval@gmail.com>
[mono.git] / mcs / class / corlib / System.Threading.Tasks / TaskScheduler.cs
1 #if NET_4_0
2 // 
3 // TaskScheduler.cs
4 //  
5 // Author:
6 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // 
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // 
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27
28 using System;
29 using System.Threading;
30 using System.Collections.Generic;
31
32 namespace System.Threading.Tasks
33 {
34         public abstract class TaskScheduler
35         {
36                 static TaskScheduler defaultScheduler = new Scheduler ();
37                 
38                 [ThreadStatic]
39                 static TaskScheduler currentScheduler;
40                 
41                 int id;
42                 static int lastId = int.MinValue;
43                 
44                 protected TaskScheduler ()
45                 {
46                         this.id = Interlocked.Increment (ref lastId);
47                 }
48
49           // FIXME: Probably not correct
50                 public static TaskScheduler FromCurrentSynchronizationContext ()
51                 {
52                         return Current;
53                 }
54                 
55                 public static TaskScheduler Default  {
56                         get {
57                                 return defaultScheduler;
58                         }
59                 }
60                 
61                 public static TaskScheduler Current  {
62                         get {
63                                 if (Task.Current != null && currentScheduler != null)
64                                         return currentScheduler;
65                                 
66                                 return defaultScheduler;
67                         }
68                         internal set {
69                                 currentScheduler = value;
70                         }
71                 }
72                 
73                 public int Id {
74                         get {
75                                 return id;
76                         }
77                 }
78                 
79                 public virtual int MaximumConcurrencyLevel {
80                         get {
81                                 return Environment.ProcessorCount;
82                         }
83                 }
84                 
85                 protected abstract IEnumerable<Task> GetScheduledTasks ();
86                 protected internal abstract void QueueTask (Task task);
87                 protected internal virtual bool TryDequeue (Task task)
88                 {
89                         throw new NotSupportedException ();
90                 }
91
92                 internal protected bool TryExecuteTask (Task task)
93                 {
94                         throw new NotSupportedException ();
95                 }
96
97                 protected abstract bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued);
98         }
99 }
100 #endif