TaskExtensionsImpl.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // TaskExtensionsImpl.cs
  3. //
  4. // Authors:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. // Marek Safar ([email protected])
  7. //
  8. // Copyright (c) 2010 Jérémie "Garuma" Laval
  9. // Copyright (C) 2013 Xamarin, Inc (http://www.xamarin.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if NET_4_0
  31. namespace System.Threading.Tasks
  32. {
  33. static class TaskExtensionsImpl
  34. {
  35. const TaskContinuationOptions options = TaskContinuationOptions.ExecuteSynchronously;
  36. public static Task<TResult> Unwrap<TResult> (Task<Task<TResult>> task)
  37. {
  38. var src = new TaskCompletionSource<TResult> ();
  39. task.ContinueWith ((t, arg) => Cont (t, (TaskCompletionSource<TResult>) arg), src, CancellationToken.None, options, TaskScheduler.Current);
  40. return src.Task;
  41. }
  42. public static Task Unwrap (Task<Task> task)
  43. {
  44. var src = new TaskCompletionSource<object> ();
  45. task.ContinueWith ((t, arg) => Cont (t, (TaskCompletionSource<object>) arg), src, CancellationToken.None, options, TaskScheduler.Current);
  46. return src.Task;
  47. }
  48. static void SetResult (Task source, TaskCompletionSource<object> dest)
  49. {
  50. if (source.IsCanceled)
  51. dest.SetCanceled ();
  52. else if (source.IsFaulted)
  53. dest.SetException (source.Exception.InnerExceptions);
  54. else
  55. dest.SetResult (null);
  56. }
  57. static void Cont (Task<Task> source, TaskCompletionSource<object> dest)
  58. {
  59. if (source.IsCanceled)
  60. dest.SetCanceled ();
  61. else if (source.IsFaulted)
  62. dest.SetException (source.Exception.InnerExceptions);
  63. else
  64. source.Result.ContinueWith ((t, arg) => SetResult (t, (TaskCompletionSource<object>) arg), dest, CancellationToken.None, options, TaskScheduler.Current);
  65. }
  66. static void SetResult<TResult> (Task<TResult> source, TaskCompletionSource<TResult> dest)
  67. {
  68. if (source.IsCanceled)
  69. dest.SetCanceled ();
  70. else if (source.IsFaulted)
  71. dest.SetException (source.Exception.InnerExceptions);
  72. else
  73. dest.SetResult (source.Result);
  74. }
  75. static void Cont<TResult> (Task<Task<TResult>> source, TaskCompletionSource<TResult> dest)
  76. {
  77. if (source.IsCanceled)
  78. dest.SetCanceled ();
  79. else if (source.IsFaulted)
  80. dest.SetException (source.Exception.InnerExceptions);
  81. else
  82. source.Result.ContinueWith ((t, arg) => SetResult (t, (TaskCompletionSource<TResult>) arg), dest, CancellationToken.None, options, TaskScheduler.Current);
  83. }
  84. }
  85. }
  86. #endif