TaskContinuation.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // TaskContinuation.cs
  3. //
  4. // Authors:
  5. // Jérémie Laval <jeremie dot laval at xamarin dot com>
  6. // Marek Safar <[email protected]>
  7. //
  8. // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. // THE SOFTWARE.
  27. //
  28. //
  29. #if NET_4_0 || MOBILE
  30. namespace System.Threading.Tasks
  31. {
  32. interface IContinuation
  33. {
  34. void Execute ();
  35. }
  36. class TaskContinuation : IContinuation
  37. {
  38. readonly Task task;
  39. readonly TaskContinuationOptions continuationOptions;
  40. public TaskContinuation (Task task, TaskContinuationOptions continuationOptions)
  41. {
  42. this.task = task;
  43. this.continuationOptions = continuationOptions;
  44. }
  45. bool ContinuationStatusCheck (TaskContinuationOptions kind)
  46. {
  47. if (kind == TaskContinuationOptions.None)
  48. return true;
  49. int kindCode = (int) kind;
  50. var status = task.Parent.Status;
  51. if (kindCode >= ((int) TaskContinuationOptions.NotOnRanToCompletion)) {
  52. // Remove other options
  53. kind &= ~(TaskContinuationOptions.PreferFairness
  54. | TaskContinuationOptions.LongRunning
  55. | TaskContinuationOptions.AttachedToParent
  56. | TaskContinuationOptions.ExecuteSynchronously);
  57. if (status == TaskStatus.Canceled) {
  58. if (kind == TaskContinuationOptions.NotOnCanceled)
  59. return false;
  60. if (kind == TaskContinuationOptions.OnlyOnFaulted)
  61. return false;
  62. if (kind == TaskContinuationOptions.OnlyOnRanToCompletion)
  63. return false;
  64. } else if (status == TaskStatus.Faulted) {
  65. if (kind == TaskContinuationOptions.NotOnFaulted)
  66. return false;
  67. if (kind == TaskContinuationOptions.OnlyOnCanceled)
  68. return false;
  69. if (kind == TaskContinuationOptions.OnlyOnRanToCompletion)
  70. return false;
  71. } else if (status == TaskStatus.RanToCompletion) {
  72. if (kind == TaskContinuationOptions.NotOnRanToCompletion)
  73. return false;
  74. if (kind == TaskContinuationOptions.OnlyOnFaulted)
  75. return false;
  76. if (kind == TaskContinuationOptions.OnlyOnCanceled)
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. public void Execute ()
  83. {
  84. if (!ContinuationStatusCheck (continuationOptions)) {
  85. task.CancelReal ();
  86. task.Dispose ();
  87. return;
  88. }
  89. if ((continuationOptions & TaskContinuationOptions.ExecuteSynchronously) != 0)
  90. task.RunSynchronously (task.scheduler);
  91. else
  92. task.Schedule ();
  93. }
  94. }
  95. class ActionContinuation : IContinuation
  96. {
  97. readonly Action action;
  98. public ActionContinuation (Action action)
  99. {
  100. this.action = action;
  101. }
  102. public void Execute ()
  103. {
  104. action ();
  105. }
  106. }
  107. class SynchronizationContextContinuation : IContinuation
  108. {
  109. readonly Action action;
  110. readonly SynchronizationContext ctx;
  111. public SynchronizationContextContinuation (Action action, SynchronizationContext ctx)
  112. {
  113. this.action = action;
  114. this.ctx = ctx;
  115. }
  116. public void Execute ()
  117. {
  118. ctx.Post (l => ((Action) l) (), action);
  119. }
  120. }
  121. }
  122. #endif