Honors correctly canceled ctor parameter of CancellationToken
[mono.git] / mcs / class / corlib / System.Threading / CancellationTokenSource.cs
1 // 
2 // CancellationTokenSource.cs
3 //  
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
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
27 #if NET_4_0 || MOBILE
28 using System;
29 using System.Collections.Generic;
30
31 namespace System.Threading
32 {
33         
34         public sealed class CancellationTokenSource : IDisposable
35         {
36                 bool canceled;
37                 bool processed;
38                 
39                 int currId = int.MinValue;
40                 
41                 Dictionary<CancellationTokenRegistration, Action> callbacks
42                         = new Dictionary<CancellationTokenRegistration, Action> ();
43                 
44                 ManualResetEvent handle = new ManualResetEvent (false);
45                 
46                 object syncRoot = new object ();
47                 
48                 internal static readonly CancellationTokenSource NoneSource = new CancellationTokenSource ();
49                 internal static readonly CancellationTokenSource CanceledSource = new CancellationTokenSource ();
50
51                 static CancellationTokenSource ()
52                 {
53                         CanceledSource.processed = true;
54                         CanceledSource.canceled = true;
55                 }
56                 
57                 public void Cancel ()
58                 {
59                         Cancel (false);
60                 }
61                 
62                 // If parameter is true we throw exception as soon as they appear otherwise we aggregate them
63                 public void Cancel (bool throwOnFirstException)
64                 {
65                         canceled = true;
66                         handle.Set ();
67                         
68                         List<Exception> exceptions = null;
69                         if (!throwOnFirstException)
70                                 exceptions = new List<Exception> ();
71                         
72                         lock (callbacks) {
73                                 foreach (KeyValuePair<CancellationTokenRegistration, Action> item in callbacks) {
74                                         if (throwOnFirstException) {
75                                                 item.Value ();
76                                         } else {
77                                                 try {
78                                                         item.Value ();
79                                                 } catch (Exception e) {
80                                                         exceptions.Add (e);
81                                                 }
82                                         }
83                                 }
84                         }
85                         
86                         Thread.MemoryBarrier ();
87                         processed = true;
88                         
89                         if (exceptions != null && exceptions.Count > 0)
90                                 throw new AggregateException (exceptions);
91                 }
92                 
93                 public void Dispose ()
94                 {
95                         
96                 }
97                 
98                 public static CancellationTokenSource CreateLinkedTokenSource (CancellationToken token1, CancellationToken token2)
99                 {
100                         return CreateLinkedTokenSource (new CancellationToken[] { token1, token2 });
101                 }
102                 
103                 public static CancellationTokenSource CreateLinkedTokenSource (params CancellationToken[] tokens)
104                 {
105                         CancellationTokenSource src = new CancellationTokenSource ();
106                         Action action = src.Cancel;
107                         
108                         foreach (CancellationToken token in tokens)
109                                 token.Register (action);
110                         
111                         return src;
112                 }
113                 
114                 public CancellationToken Token {
115                         get {
116                                 return CreateToken ();
117                         }
118                 }
119                 
120                 public bool IsCancellationRequested {
121                         get {
122                                 return canceled;
123                         }
124                 }
125                 
126                 internal WaitHandle WaitHandle {
127                         get {
128                                 return handle;
129                         }
130                 }
131                 
132                 internal CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
133                 {
134                         CancellationTokenRegistration tokenReg = GetTokenReg ();
135                         if (canceled) {
136                                 callback ();
137                         } else {
138                                 bool temp = false;
139                                 lock (syncRoot) {
140                                         if (!(temp = canceled))
141                                                 callbacks.Add (tokenReg, callback);
142                                 }
143                                 if (temp)
144                                         callback ();
145                         }
146                         
147                         return tokenReg;
148                 }
149                 
150                 internal void RemoveCallback (CancellationTokenRegistration tokenReg)
151                 {
152                         if (!canceled) {
153                                 lock (syncRoot) {
154                                         if (!canceled) {
155                                                 callbacks.Remove (tokenReg);
156                                                 return;
157                                         }
158                                 }
159                         }
160                         
161                         SpinWait sw = new SpinWait ();
162                         while (!processed)
163                                 sw.SpinOnce ();
164                         
165                 }
166
167                 CancellationTokenRegistration GetTokenReg ()
168                 {
169                         CancellationTokenRegistration registration
170                                 = new CancellationTokenRegistration (Interlocked.Increment (ref currId), this);
171                         
172                         return registration;
173                 }
174                 
175                 CancellationToken CreateToken ()
176                 {
177                         CancellationToken tk = new CancellationToken (true);
178                         tk.Source = this;
179                         
180                         return tk;
181                 }
182         }
183 }
184 #endif