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