[Mono.Debugger.Soft] Reverted non-working support for MethodMirror.IsGenericMethod...
[mono.git] / mcs / class / corlib / System.Collections.Concurrent / ConcurrentQueue.cs
index 5c4f1923ae7d0718a290e47c80bf0f2e3e7ffbc4..18e0f58ba448bf2aafdb92529afa7670e391c890 100644 (file)
@@ -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<T> : IProducerConsumerCollection<T>, IEnumerable<T>, ICollection,
                                          IEnumerable
        {
@@ -46,20 +48,35 @@ namespace System.Collections.Concurrent
                Node tail;
                int count;
 
+               class NodeObjectPool : ObjectPool<Node> {
+                       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<T> enumerable): this()
+               public ConcurrentQueue (IEnumerable<T> 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<T> e = InternalGetEnumerator ();
                        int i = index;
                        while (e.MoveNext ()) {
-                               dest [i++] = e.Current;
+                               array [i++] = e.Current;
                        }
                }