SchedulerProxy.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // SchedulerProxy.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  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. #if NET_4_0 || MOBILE
  27. using System;
  28. using System.Threading;
  29. using System.Reflection;
  30. namespace System.Threading.Tasks
  31. {
  32. internal class SchedulerProxy : IScheduler
  33. {
  34. TaskScheduler scheduler;
  35. Action<Task> participateUntil1;
  36. Func<Task, ManualResetEventSlim, int, bool> participateUntil2;
  37. public SchedulerProxy (TaskScheduler scheduler)
  38. {
  39. this.scheduler = scheduler;
  40. FindMonoSpecificImpl ();
  41. }
  42. void FindMonoSpecificImpl ()
  43. {
  44. Console.WriteLine (scheduler.GetType ());
  45. // participateUntil1
  46. FetchMethod<Action<Task>> ("ParticipateUntil",
  47. new[] { typeof(Task) },
  48. ref participateUntil1);
  49. // participateUntil2
  50. FetchMethod<Func<Task, ManualResetEventSlim, int, bool>> ("ParticipateUntil",
  51. new[] { typeof(Task), typeof(ManualResetEventSlim), typeof(int) },
  52. ref participateUntil2);
  53. }
  54. void FetchMethod<TDelegate> (string name, Type[] types, ref TDelegate field) where TDelegate : class
  55. {
  56. var method = scheduler.GetType ().GetMethod (name,
  57. BindingFlags.Instance | BindingFlags.Public,
  58. null,
  59. types,
  60. null);
  61. if (method == null)
  62. return;
  63. field = Delegate.CreateDelegate (typeof(TDelegate), scheduler, method) as TDelegate;
  64. Console.WriteLine ("Created delegate for " + name);
  65. }
  66. #region IScheduler implementation
  67. public void AddWork (Task t)
  68. {
  69. scheduler.QueueTask (t);
  70. }
  71. public void ParticipateUntil (Task task)
  72. {
  73. if (participateUntil1 != null) {
  74. participateUntil1 (task);
  75. return;
  76. }
  77. ManualResetEventSlim evt = new ManualResetEventSlim (false);
  78. task.ContinueWith (_ => evt.Set (), TaskContinuationOptions.ExecuteSynchronously);
  79. ParticipateUntil (evt, -1);
  80. }
  81. public bool ParticipateUntil (Task task, ManualResetEventSlim evt, int millisecondsTimeout)
  82. {
  83. if (participateUntil2 != null)
  84. return participateUntil2 (task, evt, millisecondsTimeout);
  85. bool fromPredicate = true;
  86. task.ContinueWith (_ => { fromPredicate = false; evt.Set (); }, TaskContinuationOptions.ExecuteSynchronously);
  87. ParticipateUntil (evt, millisecondsTimeout);
  88. return fromPredicate;
  89. }
  90. void ParticipateUntil (ManualResetEventSlim evt, int millisecondsTimeout)
  91. {
  92. evt.Wait (millisecondsTimeout);
  93. }
  94. public void PulseAll ()
  95. {
  96. }
  97. #endregion
  98. #region IDisposable implementation
  99. public void Dispose ()
  100. {
  101. scheduler = null;
  102. }
  103. #endregion
  104. }
  105. }
  106. #endif