BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / corlib / System.Threading.Tasks / TpScheduler.cs
1 //
2 // TpScheduler.cs
3 //
4 // Authors:
5 //    Jérémie Laval <jeremie dot laval at xamarin dot com>
6 //    Marek Safar  <marek.safar@gmail.com>
7 //
8 // Copyright (c) 2011 Jérémie "Garuma" Laval
9 // Copyright 2012 Xamarin Inc (http://www.xamarin.com).
10 //
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy
13 // of this software and associated documentation files (the "Software"), to deal
14 // in the Software without restriction, including without limitation the rights
15 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 // copies of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be included in
20 // all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 // THE SOFTWARE.
29 //
30 //
31
32 #if NET_4_0 || MOBILE
33
34 namespace System.Threading.Tasks
35 {
36         sealed class TpScheduler: TaskScheduler
37         {
38                 static readonly WaitCallback callback = TaskExecuterCallback;
39
40                 protected internal override void QueueTask (Task task)
41                 {
42                         if ((task.CreationOptions & TaskCreationOptions.LongRunning) != 0) {
43                                 var thread = new Thread (l => ((Task)l).Execute ()) {
44                                         IsBackground = true
45                                 };
46
47                                 thread.Start (task);
48                                 return;
49                         }
50
51                         ThreadPool.UnsafeQueueUserWorkItem (callback, task);
52                 }
53
54                 static void TaskExecuterCallback (object obj)
55                 {
56                         Task task = (Task)obj;
57                         task.Execute ();
58                 }
59
60                 protected override System.Collections.Generic.IEnumerable<Task> GetScheduledTasks ()
61                 {
62                         throw new System.NotImplementedException();
63                 }
64
65                 protected internal override bool TryDequeue (Task task)
66                 {
67                         throw new System.NotImplementedException();
68                 }
69
70                 protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
71                 {
72                     return TryExecuteTask(task);
73                 }
74         }
75 }
76 #endif