Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[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
33
34 using System.Collections.Generic;
35
36 namespace System.Threading.Tasks
37 {
38         sealed class TpScheduler: TaskScheduler
39         {
40                 static readonly WaitCallback callback = TaskExecuterCallback;
41
42                 protected internal override void QueueTask (Task task)
43                 {
44                         if ((task.CreationOptions & TaskCreationOptions.LongRunning) != 0) {
45                                 var thread = new Thread (l => ((Task)l).Execute ()) {
46                                         IsBackground = true
47                                 };
48
49                                 thread.Start (task);
50                                 return;
51                         }
52
53                         ThreadPool.QueueWorkItem (callback, task);
54                 }
55
56                 static void TaskExecuterCallback (object obj)
57                 {
58                         Task task = (Task)obj;
59                         task.Execute ();
60                 }
61
62                 protected override IEnumerable<Task> GetScheduledTasks ()
63                 {
64                         throw new NotImplementedException();
65                 }
66
67                 [MonoTODO ("Tasks cannot be dequeued")]
68                 protected internal override bool TryDequeue (Task task)
69                 {
70                         return false;
71                 }
72
73                 protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
74                 {
75                         if (taskWasPreviouslyQueued && !TryDequeue (task))
76                                 return false;
77
78                     return TryExecuteTask (task);
79                 }
80         }
81 }
82 #endif