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