2 // CancellationTokenSource.cs
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
27 #if NET_4_0 || BOOTSTRAP_NET_4_0
29 using System.Collections.Generic;
31 namespace System.Threading
34 public sealed class CancellationTokenSource : IDisposable
39 int currId = int.MinValue;
41 Dictionary<CancellationTokenRegistration, Action> callbacks
42 = new Dictionary<CancellationTokenRegistration, Action> ();
44 ManualResetEvent handle = new ManualResetEvent (false);
46 object syncRoot = new object ();
48 internal static readonly CancellationTokenSource NoneSource = new CancellationTokenSource ();
55 // If parameter is true we throw exception as soon as they appear otherwise we aggregate them
56 public void Cancel (bool throwOnFirst)
61 List<Exception> exceptions = null;
63 exceptions = new List<Exception> ();
66 foreach (KeyValuePair<CancellationTokenRegistration, Action> item in callbacks) {
72 } catch (Exception e) {
79 Thread.MemoryBarrier ();
82 if (exceptions != null && exceptions.Count > 0)
83 throw new AggregateException (exceptions);
86 public void Dispose ()
91 public static CancellationTokenSource CreateLinkedTokenSource (CancellationToken token1, CancellationToken token2)
93 return CreateLinkedTokenSource (new CancellationToken[] { token1, token2 });
96 public static CancellationTokenSource CreateLinkedTokenSource (params CancellationToken[] tokens)
98 CancellationTokenSource src = new CancellationTokenSource ();
99 Action action = src.Cancel;
101 foreach (CancellationToken token in tokens)
102 token.Register (action);
107 public CancellationToken Token {
109 return CreateToken ();
113 public bool IsCancellationRequested {
119 internal WaitHandle WaitHandle {
125 internal CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
127 CancellationTokenRegistration tokenReg = GetTokenReg ();
133 if (!(temp = canceled))
134 callbacks.Add (tokenReg, callback);
143 internal void RemoveCallback (CancellationTokenRegistration tokenReg)
148 callbacks.Remove (tokenReg);
154 SpinWait sw = new SpinWait ();
160 CancellationTokenRegistration GetTokenReg ()
162 CancellationTokenRegistration registration
163 = new CancellationTokenRegistration (Interlocked.Increment (ref currId), this);
168 CancellationToken CreateToken ()
170 CancellationToken tk = new CancellationToken (canceled);