Merge pull request #121 from LogosBible/processfixes
[mono.git] / mcs / class / corlib / System.Threading.Tasks / SchedulerProxy.cs
1 // 
2 // SchedulerProxy.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.Reflection;
31
32 namespace System.Threading.Tasks
33 {
34         internal class SchedulerProxy
35         {
36                 TaskScheduler scheduler;
37
38                 Action<Task> participateUntil1;
39                 Func<Task, ManualResetEventSlim, int, bool> participateUntil2;
40
41                 public SchedulerProxy (TaskScheduler scheduler)
42                 {
43                         this.scheduler = scheduler;
44                         FindMonoSpecificImpl ();
45                 }
46
47                 void FindMonoSpecificImpl ()
48                 {
49                         // participateUntil1
50                         FetchMethod<Action<Task>> ("MonoParticipateUntil",
51                                                    new[] { typeof(Task) },
52                                                    ref participateUntil1);
53                         // participateUntil2
54                         FetchMethod<Func<Task, ManualResetEventSlim, int, bool>> ("MonoParticipateUntil",
55                                                                                   new[] { typeof(Task), typeof(ManualResetEventSlim), typeof(int) },
56                                                                                   ref participateUntil2);
57                 }
58
59                 void FetchMethod<TDelegate> (string name, Type[] types, ref TDelegate field) where TDelegate : class
60                 {
61                         var method = scheduler.GetType ().GetMethod (name,
62                                                                      BindingFlags.Instance | BindingFlags.Public,
63                                                                      null,
64                                                                      types,
65                                                                      null);
66                         if (method == null)
67                                 return;
68                         field = Delegate.CreateDelegate (typeof(TDelegate), scheduler, method) as TDelegate;
69                         Console.WriteLine ("Created delegate for " + name);
70                 }
71
72                 public void ParticipateUntil (Task task)
73                 {
74                         if (participateUntil1 != null) {
75                                 participateUntil1 (task);
76                                 return;
77                         }
78
79                         if (task.Status == TaskStatus.WaitingToRun)
80                                 task.Execute (null);
81
82                         if (task.IsCompleted)
83                                 return;
84
85                         ManualResetEventSlim evt = new ManualResetEventSlim (false);
86                         task.ContinueWith (_ => evt.Set (), TaskContinuationOptions.ExecuteSynchronously);
87
88                         ParticipateUntil (evt, -1);
89                 }
90                 
91                 public bool ParticipateUntil (Task task, ManualResetEventSlim evt, int millisecondsTimeout)
92                 {
93                         if (task.IsCompleted)
94                                 return false;
95
96                         if (participateUntil2 != null)
97                                 return participateUntil2 (task, evt, millisecondsTimeout);
98
99                         bool fromPredicate = true;
100                         task.ContinueWith (_ => { fromPredicate = false; evt.Set (); }, TaskContinuationOptions.ExecuteSynchronously);
101
102                         ParticipateUntil (evt, millisecondsTimeout);
103
104                         return fromPredicate;
105                 }
106
107                 void ParticipateUntil (ManualResetEventSlim evt, int millisecondsTimeout)
108                 {
109                         evt.Wait (millisecondsTimeout);
110                 }
111
112                 public void PulseAll ()
113                 {
114                         
115                 }
116         }
117 }
118 #endif