SynchronizationContextScheduler.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // SynchronizationContextScheduler.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2011 Novell
  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
  27. using System;
  28. using System.Threading;
  29. namespace System.Threading.Tasks
  30. {
  31. public class SynchronizationContextScheduler : TaskScheduler, IScheduler
  32. {
  33. readonly SynchronizationContext ctx;
  34. static readonly SendOrPostCallback callback = TaskLaunchWrapper;
  35. public SynchronizationContextScheduler (SynchronizationContext ctx)
  36. {
  37. this.ctx = ctx;
  38. }
  39. public void AddWork (Task t)
  40. {
  41. ctx.Post (callback, t);
  42. }
  43. static void TaskLaunchWrapper (object obj)
  44. {
  45. ((Task)obj).Execute (null);
  46. }
  47. public void ParticipateUntil (Task task)
  48. {
  49. if (task.IsCompleted)
  50. return;
  51. ManualResetEventSlim evt = new ManualResetEventSlim (false);
  52. task.ContinueWith (_ => evt.Set (), TaskContinuationOptions.ExecuteSynchronously);
  53. if (evt.IsSet || task.IsCompleted)
  54. return;
  55. ParticipateUntilInternal (task, evt, -1);
  56. }
  57. public bool ParticipateUntil (Task task, ManualResetEventSlim evt, int millisecondsTimeout)
  58. {
  59. if (task.IsCompleted)
  60. return false;
  61. bool isFromPredicate = true;
  62. task.ContinueWith (_ => { isFromPredicate = false; evt.Set (); }, TaskContinuationOptions.ExecuteSynchronously);
  63. ParticipateUntilInternal (task, evt, millisecondsTimeout);
  64. if (task.IsCompleted)
  65. return false;
  66. return isFromPredicate;
  67. }
  68. internal void ParticipateUntilInternal (Task self, ManualResetEventSlim evt, int millisecondsTimeout)
  69. {
  70. evt.Wait (millisecondsTimeout);
  71. }
  72. public void PulseAll ()
  73. {
  74. }
  75. public void Dispose ()
  76. {
  77. }
  78. protected override System.Collections.Generic.IEnumerable<Task> GetScheduledTasks ()
  79. {
  80. throw new System.NotImplementedException();
  81. }
  82. protected internal override void QueueTask (Task task)
  83. {
  84. AddWork (task);
  85. }
  86. protected internal override bool TryDequeue (Task task)
  87. {
  88. return false;
  89. }
  90. protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
  91. {
  92. task.Execute (null);
  93. return true;
  94. }
  95. public override int MaximumConcurrencyLevel {
  96. get {
  97. return base.MaximumConcurrencyLevel;
  98. }
  99. }
  100. }
  101. }
  102. #endif