CancellationTokenSource.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // CancellationTokenSource.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2009 Jérémie "Garuma" Laval
  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.Collections.Generic;
  29. namespace System.Threading
  30. {
  31. public sealed class CancellationTokenSource : IDisposable
  32. {
  33. bool canceled;
  34. bool processed;
  35. int currId = int.MinValue;
  36. Dictionary<CancellationTokenRegistration, Action> callbacks
  37. = new Dictionary<CancellationTokenRegistration, Action> ();
  38. ManualResetEvent handle = new ManualResetEvent (false);
  39. object syncRoot = new object ();
  40. internal static readonly CancellationTokenSource NoneSource = new CancellationTokenSource ();
  41. public void Cancel ()
  42. {
  43. Cancel (false);
  44. }
  45. // If parameter is true we throw exception as soon as they appear otherwise we aggregate them
  46. public void Cancel (bool throwOnFirstException)
  47. {
  48. canceled = true;
  49. handle.Set ();
  50. List<Exception> exceptions = null;
  51. if (!throwOnFirstException)
  52. exceptions = new List<Exception> ();
  53. lock (callbacks) {
  54. foreach (KeyValuePair<CancellationTokenRegistration, Action> item in callbacks) {
  55. if (throwOnFirstException) {
  56. item.Value ();
  57. } else {
  58. try {
  59. item.Value ();
  60. } catch (Exception e) {
  61. exceptions.Add (e);
  62. }
  63. }
  64. }
  65. }
  66. Thread.MemoryBarrier ();
  67. processed = true;
  68. if (exceptions != null && exceptions.Count > 0)
  69. throw new AggregateException (exceptions);
  70. }
  71. public void Dispose ()
  72. {
  73. }
  74. public static CancellationTokenSource CreateLinkedTokenSource (CancellationToken token1, CancellationToken token2)
  75. {
  76. return CreateLinkedTokenSource (new CancellationToken[] { token1, token2 });
  77. }
  78. public static CancellationTokenSource CreateLinkedTokenSource (params CancellationToken[] tokens)
  79. {
  80. CancellationTokenSource src = new CancellationTokenSource ();
  81. Action action = src.Cancel;
  82. foreach (CancellationToken token in tokens)
  83. token.Register (action);
  84. return src;
  85. }
  86. public CancellationToken Token {
  87. get {
  88. return CreateToken ();
  89. }
  90. }
  91. public bool IsCancellationRequested {
  92. get {
  93. return canceled;
  94. }
  95. }
  96. internal WaitHandle WaitHandle {
  97. get {
  98. return handle;
  99. }
  100. }
  101. internal CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
  102. {
  103. CancellationTokenRegistration tokenReg = GetTokenReg ();
  104. if (canceled) {
  105. callback ();
  106. } else {
  107. bool temp = false;
  108. lock (syncRoot) {
  109. if (!(temp = canceled))
  110. callbacks.Add (tokenReg, callback);
  111. }
  112. if (temp)
  113. callback ();
  114. }
  115. return tokenReg;
  116. }
  117. internal void RemoveCallback (CancellationTokenRegistration tokenReg)
  118. {
  119. if (!canceled) {
  120. lock (syncRoot) {
  121. if (!canceled) {
  122. callbacks.Remove (tokenReg);
  123. return;
  124. }
  125. }
  126. }
  127. SpinWait sw = new SpinWait ();
  128. while (!processed)
  129. sw.SpinOnce ();
  130. }
  131. CancellationTokenRegistration GetTokenReg ()
  132. {
  133. CancellationTokenRegistration registration
  134. = new CancellationTokenRegistration (Interlocked.Increment (ref currId), this);
  135. return registration;
  136. }
  137. CancellationToken CreateToken ()
  138. {
  139. CancellationToken tk = new CancellationToken (canceled);
  140. tk.Source = this;
  141. return tk;
  142. }
  143. }
  144. }
  145. #endif