Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / corlib / System.Threading / CancellationTokenSource.cs
index dc0f7ef0d0e6e5b88dd5249122107e744536864c..05ca53a182f1f85e389adf6278282b35e0ff1e0c 100644 (file)
@@ -1,4 +1,3 @@
-#if NET_4_0 || BOOTSTRAP_NET_4_0
 // 
 // CancellationTokenSource.cs
 //  
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
 
+#if NET_4_0 || BOOTSTRAP_NET_4_0
 using System;
 using System.Collections.Generic;
 
 namespace System.Threading
 {
        
-       public class CancellationTokenSource : IDisposable, ICancelableOperation
+       public sealed class CancellationTokenSource : IDisposable
        {
-               volatile bool canceled;
-               volatile bool processed;
+               bool canceled;
+               bool processed;
                
                int currId = int.MinValue;
                
@@ -43,9 +43,9 @@ namespace System.Threading
                
                ManualResetEvent handle = new ManualResetEvent (false);
                
-//#if USE_MONITOR
                object syncRoot = new object ();
-//#endif
+               
+               internal static readonly CancellationTokenSource NoneSource = new CancellationTokenSource ();
                
                public void Cancel ()
                {
@@ -76,6 +76,7 @@ namespace System.Threading
                                }
                        }
                        
+                       Thread.MemoryBarrier ();
                        processed = true;
                        
                        if (exceptions != null && exceptions.Count > 0)
@@ -105,10 +106,7 @@ namespace System.Threading
                
                public CancellationToken Token {
                        get {
-                               CancellationToken token = new CancellationToken (canceled);
-                               token.Source = this;
-                               
-                               return token;
+                               return CreateToken ();
                        }
                }
                
@@ -158,7 +156,7 @@ namespace System.Threading
                                sw.SpinOnce ();
                        
                }
-               
+
                CancellationTokenRegistration GetTokenReg ()
                {
                        CancellationTokenRegistration registration
@@ -166,6 +164,14 @@ namespace System.Threading
                        
                        return registration;
                }
+               
+               CancellationToken CreateToken ()
+               {
+                       CancellationToken tk = new CancellationToken (canceled);
+                       tk.Source = this;
+                       
+                       return tk;
+               }
        }
 }
 #endif