2009-08-11 Jérémie Laval <jeremie.laval@gmail.com>
[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 class CancellationTokenSource : IDisposable, ICancelableOperation
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 //#if USE_MONITOR
47                 object syncRoot = new object ();
48 //#endif
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                                 CancellationToken token = new CancellationToken (canceled);
109                                 token.Source = this;
110                                 
111                                 return token;
112                         }
113                 }
114                 
115                 public bool IsCancellationRequested {
116                         get {
117                                 return canceled;
118                         }
119                 }
120                 
121                 internal WaitHandle WaitHandle {
122                         get {
123                                 return handle;
124                         }
125                 }
126                 
127                 internal CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
128                 {
129                         CancellationTokenRegistration tokenReg = GetTokenReg ();
130                         if (canceled) {
131                                 callback ();
132                         } else {
133                                 bool temp = false;
134                                 lock (syncRoot) {
135                                         if (!(temp = canceled))
136                                                 callbacks.Add (tokenReg, callback);
137                                 }
138                                 if (temp)
139                                         callback ();
140                         }
141                         
142                         return tokenReg;
143                 }
144                 
145                 internal void RemoveCallback (CancellationTokenRegistration tokenReg)
146                 {
147                         if (!canceled) {
148                                 lock (syncRoot) {
149                                         if (!canceled) {
150                                                 callbacks.Remove (tokenReg);
151                                                 return;
152                                         }
153                                 }
154                         }
155                         
156                         SpinWait sw = new SpinWait ();
157                         while (!processed)
158                                 sw.SpinOnce ();
159                         
160                 }
161                 
162                 CancellationTokenRegistration GetTokenReg ()
163                 {
164                         CancellationTokenRegistration registration
165                                 = new CancellationTokenRegistration (Interlocked.Increment (ref currId), this);
166                         
167                         return registration;
168                 }
169         }
170 }
171 #endif