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
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 ();
49 internal static readonly CancellationTokenSource CanceledSource = new CancellationTokenSource ();
51 static CancellationTokenSource ()
53 CanceledSource.processed = true;
54 CanceledSource.canceled = true;
62 // If parameter is true we throw exception as soon as they appear otherwise we aggregate them
63 public void Cancel (bool throwOnFirstException)
68 List<Exception> exceptions = null;
69 if (!throwOnFirstException)
70 exceptions = new List<Exception> ();
73 foreach (KeyValuePair<CancellationTokenRegistration, Action> item in callbacks) {
74 if (throwOnFirstException) {
79 } catch (Exception e) {
86 Thread.MemoryBarrier ();
89 if (exceptions != null && exceptions.Count > 0)
90 throw new AggregateException (exceptions);
93 public void Dispose ()
98 public static CancellationTokenSource CreateLinkedTokenSource (CancellationToken token1, CancellationToken token2)
100 return CreateLinkedTokenSource (new CancellationToken[] { token1, token2 });
103 public static CancellationTokenSource CreateLinkedTokenSource (params CancellationToken[] tokens)
105 CancellationTokenSource src = new CancellationTokenSource ();
106 Action action = src.Cancel;
108 foreach (CancellationToken token in tokens)
109 token.Register (action);
114 public CancellationToken Token {
116 return CreateToken ();
120 public bool IsCancellationRequested {
126 internal WaitHandle WaitHandle {
132 internal CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
134 CancellationTokenRegistration tokenReg = GetTokenReg ();
140 if (!(temp = canceled))
141 callbacks.Add (tokenReg, callback);
150 internal void RemoveCallback (CancellationTokenRegistration tokenReg)
155 callbacks.Remove (tokenReg);
161 SpinWait sw = new SpinWait ();
167 CancellationTokenRegistration GetTokenReg ()
169 CancellationTokenRegistration registration
170 = new CancellationTokenRegistration (Interlocked.Increment (ref currId), this);
175 CancellationToken CreateToken ()
177 CancellationToken tk = new CancellationToken (true);