Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / class / corlib / System.Threading.Tasks / TaskFactory_T.cs
index 0c4ba2a09263a74e61af5ef1db8b4d973f6d10f7..0ac088c9058160dc52cc14bbc0d73ca6d066ea2c 100644 (file)
@@ -339,14 +339,13 @@ namespace System.Threading.Tasks
                                throw new ArgumentOutOfRangeException ("creationOptions");
 
                        var tcs = new TaskCompletionSource<TResult> (state, creationOptions);
-                       var alreadyInvoked = false;
+                       var alreadyInvoked = new AtomicBoolean ();
                        var iar = beginMethod (l => {
-                               alreadyInvoked = true;
-                               InnerInvoke (tcs, endMethod, l);
+                               if (alreadyInvoked.TryRelaxedSet ())
+                                       InnerInvoke (tcs, endMethod, l);
                        }, state);
-                       if (iar != null && !alreadyInvoked && iar.CompletedSynchronously) {
+                       if (iar != null && iar.CompletedSynchronously && alreadyInvoked.TryRelaxedSet ())
                                InnerInvoke (tcs, endMethod, iar);
-                       }
 
                        return tcs.Task;
                }
@@ -377,14 +376,13 @@ namespace System.Threading.Tasks
                                throw new ArgumentOutOfRangeException ("creationOptions");
 
                        var tcs = new TaskCompletionSource<TResult> (state, creationOptions);
-                       var alreadyInvoked = false;
+                       var alreadyInvoked = new AtomicBoolean ();
                        var iar = beginMethod (arg1, l => {
-                               alreadyInvoked = true;
-                               InnerInvoke (tcs, endMethod, l);
+                               if (alreadyInvoked.TryRelaxedSet ())
+                                       InnerInvoke (tcs, endMethod, l);
                        }, state);
-                       if (iar != null && !alreadyInvoked && iar.CompletedSynchronously) {
+                       if (iar != null && iar.CompletedSynchronously && alreadyInvoked.TryRelaxedSet ())
                                InnerInvoke (tcs, endMethod, iar);
-                       }
 
                        return tcs.Task;
                }
@@ -414,14 +412,13 @@ namespace System.Threading.Tasks
                                throw new ArgumentOutOfRangeException ("creationOptions");
 
                        var tcs = new TaskCompletionSource<TResult> (state, creationOptions);
-                       var alreadyInvoked = false;
+                       var alreadyInvoked = new AtomicBoolean ();
                        var iar = beginMethod (arg1, arg2, l => {
-                               alreadyInvoked = true;
-                               InnerInvoke (tcs, endMethod, l);
+                               if (alreadyInvoked.TryRelaxedSet ())
+                                       InnerInvoke (tcs, endMethod, l);
                        }, state);
-                       if (iar != null && !alreadyInvoked && iar.CompletedSynchronously) {
+                       if (iar != null && iar.CompletedSynchronously && alreadyInvoked.TryRelaxedSet ())
                                InnerInvoke (tcs, endMethod, iar);
-                       }
 
                        return tcs.Task;
                }
@@ -452,14 +449,13 @@ namespace System.Threading.Tasks
                                throw new ArgumentOutOfRangeException ("creationOptions");
 
                        var tcs = new TaskCompletionSource<TResult> (state, creationOptions);
-                       bool alreadyInvoked = false;
+                       var alreadyInvoked = new AtomicBoolean ();
                        var iar = beginMethod (arg1, arg2, arg3, l => {
-                               alreadyInvoked = true;
-                               InnerInvoke (tcs, endMethod, l);
+                               if (alreadyInvoked.TryRelaxedSet ())
+                                       InnerInvoke (tcs, endMethod, l);
                        }, state);
-                       if (iar != null && !alreadyInvoked && iar.CompletedSynchronously) {
+                       if (iar != null && iar.CompletedSynchronously && alreadyInvoked.TryRelaxedSet ())
                                InnerInvoke (tcs, endMethod, iar);
-                       }
 
                        return tcs.Task;
                }