Replace SIZEOF_REGISTER with sizeof(mgreg_t) for consistency with sizeof(gpointer)
[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 || BOOTSTRAP_NET_4_0
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                 
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                         Thread.MemoryBarrier ();
80                         processed = true;
81                         
82                         if (exceptions != null && exceptions.Count > 0)
83                                 throw new AggregateException (exceptions);
84                 }
85                 
86                 public void Dispose ()
87                 {
88                         
89                 }
90                 
91                 public static CancellationTokenSource CreateLinkedTokenSource (CancellationToken token1, CancellationToken token2)
92                 {
93                         return CreateLinkedTokenSource (new CancellationToken[] { token1, token2 });
94                 }
95                 
96                 public static CancellationTokenSource CreateLinkedTokenSource (params CancellationToken[] tokens)
97                 {
98                         CancellationTokenSource src = new CancellationTokenSource ();
99                         Action action = src.Cancel;
100                         
101                         foreach (CancellationToken token in tokens)
102                                 token.Register (action);
103                         
104                         return src;
105                 }
106                 
107                 public CancellationToken Token {
108                         get {
109                                 return CreateToken ();
110                         }
111                 }
112                 
113                 public bool IsCancellationRequested {
114                         get {
115                                 return canceled;
116                         }
117                 }
118                 
119                 internal WaitHandle WaitHandle {
120                         get {
121                                 return handle;
122                         }
123                 }
124                 
125                 internal CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
126                 {
127                         CancellationTokenRegistration tokenReg = GetTokenReg ();
128                         if (canceled) {
129                                 callback ();
130                         } else {
131                                 bool temp = false;
132                                 lock (syncRoot) {
133                                         if (!(temp = canceled))
134                                                 callbacks.Add (tokenReg, callback);
135                                 }
136                                 if (temp)
137                                         callback ();
138                         }
139                         
140                         return tokenReg;
141                 }
142                 
143                 internal void RemoveCallback (CancellationTokenRegistration tokenReg)
144                 {
145                         if (!canceled) {
146                                 lock (syncRoot) {
147                                         if (!canceled) {
148                                                 callbacks.Remove (tokenReg);
149                                                 return;
150                                         }
151                                 }
152                         }
153                         
154                         SpinWait sw = new SpinWait ();
155                         while (!processed)
156                                 sw.SpinOnce ();
157                         
158                 }
159
160                 CancellationTokenRegistration GetTokenReg ()
161                 {
162                         CancellationTokenRegistration registration
163                                 = new CancellationTokenRegistration (Interlocked.Increment (ref currId), this);
164                         
165                         return registration;
166                 }
167                 
168                 CancellationToken CreateToken ()
169                 {
170                         CancellationToken tk = new CancellationToken (canceled);
171                         tk.Source = this;
172                         
173                         return tk;
174                 }
175         }
176 }
177 #endif