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
29 using System.Collections.Generic;
31 namespace System.Threading
36 public class CancellationTokenSource : IDisposable
41 int currId = int.MinValue;
43 Dictionary<CancellationTokenRegistration, Action> callbacks
44 = new Dictionary<CancellationTokenRegistration, Action> ();
46 ManualResetEvent handle = new ManualResetEvent (false);
48 object syncRoot = new object ();
50 internal static readonly CancellationTokenSource NoneSource = new CancellationTokenSource ();
51 internal static readonly CancellationTokenSource CanceledSource = new CancellationTokenSource ();
53 static CancellationTokenSource ()
55 CanceledSource.processed = true;
56 CanceledSource.canceled = true;
64 // If parameter is true we throw exception as soon as they appear otherwise we aggregate them
65 public void Cancel (bool throwOnFirstException)
70 List<Exception> exceptions = null;
71 if (!throwOnFirstException)
72 exceptions = new List<Exception> ();
75 foreach (KeyValuePair<CancellationTokenRegistration, Action> item in callbacks) {
76 if (throwOnFirstException) {
81 } catch (Exception e) {
88 Thread.MemoryBarrier ();
91 if (exceptions != null && exceptions.Count > 0)
92 throw new AggregateException (exceptions);
95 public void Dispose ()
100 public static CancellationTokenSource CreateLinkedTokenSource (CancellationToken token1, CancellationToken token2)
102 return CreateLinkedTokenSource (new CancellationToken[] { token1, token2 });
105 public static CancellationTokenSource CreateLinkedTokenSource (params CancellationToken[] tokens)
107 CancellationTokenSource src = new CancellationTokenSource ();
108 Action action = src.Cancel;
110 foreach (CancellationToken token in tokens)
111 token.Register (action);
116 public CancellationToken Token {
118 return CreateToken ();
122 public bool IsCancellationRequested {
128 internal WaitHandle WaitHandle {
134 internal CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
136 CancellationTokenRegistration tokenReg = GetTokenReg ();
142 if (!(temp = canceled))
143 callbacks.Add (tokenReg, callback);
152 internal void RemoveCallback (CancellationTokenRegistration tokenReg)
157 callbacks.Remove (tokenReg);
163 SpinWait sw = new SpinWait ();
169 CancellationTokenRegistration GetTokenReg ()
171 CancellationTokenRegistration registration
172 = new CancellationTokenRegistration (Interlocked.Increment (ref currId), this);
177 CancellationToken CreateToken ()
179 return new CancellationToken (this);