TaskCompletionSource.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. static readonly Func<TResult> emptyFunction = () => default (TResult);
  36. static readonly Func<object, TResult> emptyParamFunction = (_) => default (TResult);
  37. static readonly Action<Task<TResult>, TResult> setResultAction = SetResultAction;
  38. static readonly Action<Task<TResult>, AggregateException> setExceptionAction = SetExceptionAction;
  39. static readonly Action<Task<TResult>, object> setCanceledAction = SetCanceledAction;
  40. readonly Task<TResult> source;
  41. SpinLock opLock = new SpinLock (false);
  42. public TaskCompletionSource ()
  43. {
  44. source = new Task<TResult> (emptyFunction);
  45. source.SetupScheduler (TaskScheduler.Current);
  46. }
  47. public TaskCompletionSource (object state)
  48. {
  49. source = new Task<TResult> (emptyParamFunction, state);
  50. source.SetupScheduler (TaskScheduler.Current);
  51. }
  52. public TaskCompletionSource (TaskCreationOptions creationOptions)
  53. : this (null, creationOptions)
  54. {
  55. }
  56. public TaskCompletionSource (object state, TaskCreationOptions creationOptions)
  57. {
  58. if ((creationOptions & System.Threading.Tasks.Task.WorkerTaskNotSupportedOptions) != 0)
  59. throw new ArgumentOutOfRangeException ("creationOptions");
  60. source = new Task<TResult> (emptyParamFunction, state, creationOptions);
  61. source.SetupScheduler (TaskScheduler.Current);
  62. }
  63. public void SetCanceled ()
  64. {
  65. if (!TrySetCanceled ())
  66. ThrowInvalidException ();
  67. }
  68. public void SetException (Exception exception)
  69. {
  70. if (exception == null)
  71. throw new ArgumentNullException ("exception");
  72. SetException (new Exception[] { exception });
  73. }
  74. public void SetException (IEnumerable<Exception> exceptions)
  75. {
  76. if (!TrySetException (exceptions))
  77. ThrowInvalidException ();
  78. }
  79. public void SetResult (TResult result)
  80. {
  81. if (!TrySetResult (result))
  82. ThrowInvalidException ();
  83. }
  84. static void ThrowInvalidException ()
  85. {
  86. throw new InvalidOperationException ("The underlying Task is already in one of the three final states: RanToCompletion, Faulted, or Canceled.");
  87. }
  88. public bool TrySetCanceled ()
  89. {
  90. return ApplyOperation (setCanceledAction, null);
  91. }
  92. public bool TrySetException (Exception exception)
  93. {
  94. if (exception == null)
  95. throw new ArgumentNullException ("exception");
  96. return TrySetException (new Exception[] { exception });
  97. }
  98. public bool TrySetException (IEnumerable<Exception> exceptions)
  99. {
  100. if (exceptions == null)
  101. throw new ArgumentNullException ("exceptions");
  102. var aggregate = new AggregateException (exceptions);
  103. if (aggregate.InnerExceptions.Count == 0)
  104. throw new ArgumentNullException ("exceptions");
  105. return ApplyOperation (setExceptionAction, aggregate);
  106. }
  107. public bool TrySetResult (TResult result)
  108. {
  109. return ApplyOperation (setResultAction, result);
  110. }
  111. bool ApplyOperation<TState> (Action<Task<TResult>, TState> action, TState state)
  112. {
  113. bool taken = false;
  114. try {
  115. opLock.Enter (ref taken);
  116. if (CheckInvalidState ())
  117. return false;
  118. source.Status = TaskStatus.Running;
  119. if (action != null)
  120. action (source, state);
  121. source.Finish ();
  122. return true;
  123. } finally {
  124. if (taken)
  125. opLock.Exit ();
  126. }
  127. }
  128. bool CheckInvalidState ()
  129. {
  130. return source.Status == TaskStatus.RanToCompletion ||
  131. source.Status == TaskStatus.Faulted ||
  132. source.Status == TaskStatus.Canceled;
  133. }
  134. static void SetResultAction (Task<TResult> source, TResult result)
  135. {
  136. source.Result = result;
  137. }
  138. static void SetExceptionAction (Task<TResult> source, AggregateException aggregate)
  139. {
  140. source.HandleGenericException (aggregate);
  141. }
  142. static void SetCanceledAction (Task<TResult> source, object unused)
  143. {
  144. source.CancelReal ();
  145. }
  146. public Task<TResult> Task {
  147. get {
  148. return source;
  149. }
  150. }
  151. }
  152. }
  153. #endif