TaskCompletionSource.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // TaskCompletionSource.cs
  3. //
  4. // Authors:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. // Marek Safar <[email protected]>
  7. //
  8. // Copyright (c) 2009 Jérémie "Garuma" Laval
  9. // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in
  19. // all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. // THE SOFTWARE.
  28. #if NET_4_0 || MOBILE
  29. using System;
  30. using System.Collections.Generic;
  31. namespace System.Threading.Tasks
  32. {
  33. public class TaskCompletionSource<TResult>
  34. {
  35. readonly Task<TResult> source;
  36. SpinLock opLock = new SpinLock (false);
  37. public TaskCompletionSource ()
  38. {
  39. source = new Task<TResult> (null);
  40. source.SetupScheduler (TaskScheduler.Current);
  41. }
  42. public TaskCompletionSource (object state)
  43. {
  44. source = new Task<TResult> (null, state);
  45. source.SetupScheduler (TaskScheduler.Current);
  46. }
  47. public TaskCompletionSource (TaskCreationOptions creationOptions)
  48. {
  49. source = new Task<TResult> (null, creationOptions);
  50. source.SetupScheduler (TaskScheduler.Current);
  51. }
  52. public TaskCompletionSource (object state, TaskCreationOptions creationOptions)
  53. {
  54. source = new Task<TResult> (null, state, creationOptions);
  55. source.SetupScheduler (TaskScheduler.Current);
  56. }
  57. public void SetCanceled ()
  58. {
  59. if (!ApplyOperation (source.CancelReal))
  60. ThrowInvalidException ();
  61. }
  62. public void SetException (Exception exception)
  63. {
  64. if (exception == null)
  65. throw new ArgumentNullException ("exception");
  66. SetException (new Exception[] { exception });
  67. }
  68. public void SetException (IEnumerable<Exception> exceptions)
  69. {
  70. if (!TrySetException (exceptions))
  71. ThrowInvalidException ();
  72. }
  73. public void SetResult (TResult result)
  74. {
  75. if (!ApplyOperation (() => source.Result = result))
  76. ThrowInvalidException ();
  77. }
  78. static void ThrowInvalidException ()
  79. {
  80. throw new InvalidOperationException ("The underlying Task is already in one of the three final states: RanToCompletion, Faulted, or Canceled.");
  81. }
  82. public bool TrySetCanceled ()
  83. {
  84. return ApplyOperation (source.CancelReal);
  85. }
  86. public bool TrySetException (Exception exception)
  87. {
  88. if (exception == null)
  89. throw new ArgumentNullException ("exception");
  90. return TrySetException (new Exception[] { exception });
  91. }
  92. public bool TrySetException (IEnumerable<Exception> exceptions)
  93. {
  94. if (exceptions == null)
  95. throw new ArgumentNullException ("exceptions");
  96. var aggregate = new AggregateException (exceptions);
  97. if (aggregate.InnerExceptions.Count == 0)
  98. throw new ArgumentNullException ("exceptions");
  99. return ApplyOperation (() => source.HandleGenericException (aggregate));
  100. }
  101. public bool TrySetResult (TResult result)
  102. {
  103. return ApplyOperation (() => source.Result = result);
  104. }
  105. bool ApplyOperation (Action action)
  106. {
  107. bool taken = false;
  108. try {
  109. opLock.Enter (ref taken);
  110. if (CheckInvalidState ())
  111. return false;
  112. source.Status = TaskStatus.Running;
  113. if (action != null)
  114. action ();
  115. source.Finish ();
  116. return true;
  117. } finally {
  118. if (taken)
  119. opLock.Exit ();
  120. }
  121. }
  122. bool CheckInvalidState ()
  123. {
  124. return source.Status == TaskStatus.RanToCompletion ||
  125. source.Status == TaskStatus.Faulted ||
  126. source.Status == TaskStatus.Canceled;
  127. }
  128. public Task<TResult> Task {
  129. get {
  130. return source;
  131. }
  132. }
  133. }
  134. }
  135. #endif