Merge pull request #498 from Unroll-Me/master
[mono.git] / mcs / class / System.Core / System.Linq.Parallel / OrderingEnumerator.cs
index e1f35dbcef2732aba899d1d8248da16b914aff2e..40023031dccdf704700f4958b4756b3ed3d0f4b6 100644 (file)
@@ -24,7 +24,7 @@
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
 
-#if NET_4_0
+#if NET_4_0 || MOBILE
 using System;
 using System.Threading;
 using System.Collections;
@@ -37,7 +37,7 @@ namespace System.Linq.Parallel
        {
                internal class SlotBucket
                {
-                       readonly ConcurrentDictionary<long, T> temporaryArea = new ConcurrentDictionary<long, T> ();
+                       readonly TemporaryArea<long, T> temporaryArea = new TemporaryArea<long, T> ();
                        readonly KeyValuePair<long, T>[] stagingArea;
                        
                        long currentIndex;
@@ -130,31 +130,25 @@ namespace System.Linq.Parallel
                                Skim ();
 
                                while (!stagingCount.IsSet) {
-                                       if (!participantCount.IsSet)
+                                       if (!participantCount.IsSet) {
                                                try {
                                                        stagingCount.Wait (mergedToken);
                                                } catch {
                                                        Skim ();
                                                }
+                                       }
 
-
-                                       // Ok so basically we hit the case where we return null but there is actually
-                                       // every remaining element inside temporaryArea. Thing is that index are basically getting messed up (probably)
-                                       // So Skim doesn't see them and inaccurately say there is nothing remaining.
-                                       // To prove this, adding a if (temporaryArea.IsEmpty) before returning null result in infinite loop
-                                       // plus if it was a problem with something here it would show elsewhere so the problem definitely comes from
-                                       // SelectMany.
                                        if (participantCount.IsSet) {
                                                if (Skim ())
                                                        continue;
                                                // Totally finished
-                                               if (stagingArea.Any (s => s.Key != -1))
+                                               if (stagingArea[0].Key != -1)
                                                        break;
                                                else
                                                        return null;
                                        }
                                }
-                               
+
                                return stagingArea;
                        }
                }