Merge pull request #1229 from andreiagaita/master
[mono.git] / mcs / class / corlib / System.Threading.Tasks / TaskFactory_T.cs
index 0c4ba2a09263a74e61af5ef1db8b4d973f6d10f7..e8ce5276346c9ff259a1b5a950c539f61a5a861b 100644 (file)
@@ -26,7 +26,7 @@
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
 
-#if NET_4_0 || MOBILE
+#if NET_4_0
 
 namespace System.Threading.Tasks
 {
@@ -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;
                }