5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
29 using System.Threading;
30 using System.Reflection;
32 namespace System.Threading.Tasks
34 internal class SchedulerProxy
36 TaskScheduler scheduler;
38 Action<Task> participateUntil1;
39 Func<Task, ManualResetEventSlim, int, bool> participateUntil2;
41 public SchedulerProxy (TaskScheduler scheduler)
43 this.scheduler = scheduler;
44 FindMonoSpecificImpl ();
47 void FindMonoSpecificImpl ()
50 FetchMethod<Action<Task>> ("MonoParticipateUntil",
51 new[] { typeof(Task) },
52 ref participateUntil1);
54 FetchMethod<Func<Task, ManualResetEventSlim, int, bool>> ("MonoParticipateUntil",
55 new[] { typeof(Task), typeof(ManualResetEventSlim), typeof(int) },
56 ref participateUntil2);
59 void FetchMethod<TDelegate> (string name, Type[] types, ref TDelegate field) where TDelegate : class
61 var method = scheduler.GetType ().GetMethod (name,
62 BindingFlags.Instance | BindingFlags.Public,
68 field = Delegate.CreateDelegate (typeof(TDelegate), scheduler, method) as TDelegate;
69 Console.WriteLine ("Created delegate for " + name);
72 public void ParticipateUntil (Task task)
74 if (participateUntil1 != null) {
75 participateUntil1 (task);
79 if (task.Status == TaskStatus.WaitingToRun)
85 ManualResetEventSlim evt = new ManualResetEventSlim (false);
86 task.ContinueWith (_ => evt.Set (), TaskContinuationOptions.ExecuteSynchronously);
88 ParticipateUntil (evt, -1);
91 public bool ParticipateUntil (Task task, ManualResetEventSlim evt, int millisecondsTimeout)
96 if (participateUntil2 != null)
97 return participateUntil2 (task, evt, millisecondsTimeout);
99 bool fromPredicate = true;
100 task.ContinueWith (_ => { fromPredicate = false; evt.Set (); }, TaskContinuationOptions.ExecuteSynchronously);
102 ParticipateUntil (evt, millisecondsTimeout);
104 return fromPredicate;
107 void ParticipateUntil (ManualResetEventSlim evt, int millisecondsTimeout)
109 evt.Wait (millisecondsTimeout);
112 public void PulseAll ()