From 9972bf87b4f27d9c8f358ef8414ac1ab957a2f0f Mon Sep 17 00:00:00 2001 From: Jo Shields Date: Wed, 19 Feb 2014 22:12:43 +0000 Subject: Imported Upstream version 3.2.8+dfsg --- .../BlockingCollection.cs | 44 +++++++++++++++------- 1 file changed, 30 insertions(+), 14 deletions(-) (limited to 'mcs/class/System/System.Collections.Concurrent/BlockingCollection.cs') diff --git a/mcs/class/System/System.Collections.Concurrent/BlockingCollection.cs b/mcs/class/System/System.Collections.Concurrent/BlockingCollection.cs index 26dc84a078..cc7e04ecdd 100644 --- a/mcs/class/System/System.Collections.Concurrent/BlockingCollection.cs +++ b/mcs/class/System/System.Collections.Concurrent/BlockingCollection.cs @@ -370,13 +370,19 @@ namespace System.Collections.Concurrent { item = default (T); CheckArray (collections); - int index = 0; - foreach (var coll in collections) { - try { - item = coll.Take (); - return index; - } catch {} - index++; + WaitHandle[] wait_table = null; + while (true) { + int index = 0; + for (int i = 0; i < collections.Length; ++i) { + if (collections [i].TryTake (out item)) + return i; + } + if (wait_table == null) { + wait_table = new WaitHandle [collections.Length]; + for (int i = 0; i < collections.Length; ++i) + wait_table [i] = collections [i].mreRemove.WaitHandle; + } + WaitHandle.WaitAny (wait_table); } return -1; } @@ -385,14 +391,24 @@ namespace System.Collections.Concurrent { item = default (T); CheckArray (collections); - int index = 0; - foreach (var coll in collections) { - try { - item = coll.Take (cancellationToken); - return index; - } catch {} - index++; + WaitHandle[] wait_table = null; + while (true) { + int index = 0; + for (int i = 0; i < collections.Length; ++i) { + if (collections [i].TryTake (out item)) + return i; + } + cancellationToken.ThrowIfCancellationRequested (); + if (wait_table == null) { + wait_table = new WaitHandle [collections.Length + 1]; + for (int i = 0; i < collections.Length; ++i) + wait_table [i] = collections [i].mreRemove.WaitHandle; + wait_table [collections.Length] = cancellationToken.WaitHandle; + } + WaitHandle.WaitAny (wait_table); + cancellationToken.ThrowIfCancellationRequested (); } + return -1; } -- cgit v1.2.3