1 #if NET_4_0 || BOOTSTRAP_NET_4_0
3 // CancellationTokenSource.cs
6 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 using System.Collections.Generic;
31 namespace System.Threading
34 public sealed class CancellationTokenSource : IDisposable
36 volatile bool canceled;
37 volatile bool processed;
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) {
81 if (exceptions != null && exceptions.Count > 0)
82 throw new AggregateException (exceptions);
85 public void Dispose ()
90 public static CancellationTokenSource CreateLinkedTokenSource (CancellationToken token1, CancellationToken token2)
92 return CreateLinkedTokenSource (new CancellationToken[] { token1, token2 });
95 public static CancellationTokenSource CreateLinkedTokenSource (params CancellationToken[] tokens)
97 CancellationTokenSource src = new CancellationTokenSource ();
98 Action action = src.Cancel;
100 foreach (CancellationToken token in tokens)
101 token.Register (action);
106 public CancellationToken Token {
108 return CreateToken ();
112 public bool IsCancellationRequested {
118 internal WaitHandle WaitHandle {
124 internal CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
126 CancellationTokenRegistration tokenReg = GetTokenReg ();
132 if (!(temp = canceled))
133 callbacks.Add (tokenReg, callback);
142 internal void RemoveCallback (CancellationTokenRegistration tokenReg)
147 callbacks.Remove (tokenReg);
153 SpinWait sw = new SpinWait ();
159 internal void ThrowIfCancellationRequested ()
162 throw new OperationCanceledException (CreateToken ());
165 CancellationTokenRegistration GetTokenReg ()
167 CancellationTokenRegistration registration
168 = new CancellationTokenRegistration (Interlocked.Increment (ref currId), this);
173 CancellationToken CreateToken ()
175 CancellationToken tk = new CancellationToken (canceled);