X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FSystem.Collections.Concurrent%2FConcurrentQueue.cs;h=18e0f58ba448bf2aafdb92529afa7670e391c890;hb=5245b9c97a3fddb27943cbcc88d5d80a6ebc33e1;hp=5c4f1923ae7d0718a290e47c80bf0f2e3e7ffbc4;hpb=9e35d37fb66cc8f16ca213ddfdbff4eafc9596f9;p=mono.git diff --git a/mcs/class/corlib/System.Collections.Concurrent/ConcurrentQueue.cs b/mcs/class/corlib/System.Collections.Concurrent/ConcurrentQueue.cs index 5c4f1923ae7..18e0f58ba44 100644 --- a/mcs/class/corlib/System.Collections.Concurrent/ConcurrentQueue.cs +++ b/mcs/class/corlib/System.Collections.Concurrent/ConcurrentQueue.cs @@ -22,7 +22,7 @@ // // -#if NET_4_0 || BOOTSTRAP_NET_4_0 +#if NET_4_0 || MOBILE using System; using System.Threading; @@ -32,7 +32,9 @@ using System.Runtime.Serialization; namespace System.Collections.Concurrent { - + + [System.Diagnostics.DebuggerDisplay ("Count={Count}")] + [System.Diagnostics.DebuggerTypeProxy (typeof (CollectionDebuggerView<>))] public class ConcurrentQueue : IProducerConsumerCollection, IEnumerable, ICollection, IEnumerable { @@ -46,20 +48,35 @@ namespace System.Collections.Concurrent Node tail; int count; + class NodeObjectPool : ObjectPool { + protected override Node Creator () + { + return new Node (); + } + } + static readonly NodeObjectPool pool = new NodeObjectPool (); + + static Node ZeroOut (Node node) + { + node.Value = default(T); + node.Next = null; + return node; + } + public ConcurrentQueue () { tail = head; } - public ConcurrentQueue (IEnumerable enumerable): this() + public ConcurrentQueue (IEnumerable collection): this() { - foreach (T item in enumerable) + foreach (T item in collection) Enqueue (item); } public void Enqueue (T item) { - Node node = new Node (); + Node node = pool.Take (); node.Value = item; Node oldTail = null; @@ -93,9 +110,9 @@ namespace System.Collections.Concurrent return true; } - public bool TryDequeue (out T value) + public bool TryDequeue (out T result) { - value = default (T); + result = default (T); bool advanced = false; while (!advanced) { @@ -111,11 +128,13 @@ namespace System.Collections.Concurrent // If not then the linked list is mal formed, update tail Interlocked.CompareExchange (ref tail, oldNext, oldTail); } - value = default (T); + result = default (T); return false; } else { - value = oldNext.Value; + result = oldNext.Value; advanced = Interlocked.CompareExchange (ref head, oldNext, oldHead) == oldHead; + if (advanced) + pool.Release (ZeroOut (oldHead)); } } } @@ -125,15 +144,15 @@ namespace System.Collections.Concurrent return true; } - public bool TryPeek (out T value) + public bool TryPeek (out T result) { if (IsEmpty) { - value = default (T); + result = default (T); return false; } Node first = head.Next; - value = first.Value; + result = first.Value; return true; } @@ -169,12 +188,12 @@ namespace System.Collections.Concurrent CopyTo (dest, index); } - public void CopyTo (T[] dest, int index) + public void CopyTo (T[] array, int index) { IEnumerator e = InternalGetEnumerator (); int i = index; while (e.MoveNext ()) { - dest [i++] = e.Current; + array [i++] = e.Current; } }