3 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 using System.Threading;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Runtime.Serialization;
33 namespace System.Collections.Concurrent
36 [System.Diagnostics.DebuggerDisplay ("Count = {Count}")]
37 [System.Diagnostics.DebuggerTypeProxy (typeof (CollectionDebuggerView<>))]
38 public class ConcurrentStack<T> : IProducerConsumerCollection<T>, IEnumerable<T>,
39 ICollection, IEnumerable
43 public T Value = default (T);
44 public Node Next = null;
51 class NodeObjectPool : ObjectPool<Node> {
52 protected override Node Creator ()
57 static readonly NodeObjectPool pool = new NodeObjectPool ();
59 static Node ZeroOut (Node node)
61 node.Value = default(T);
66 public ConcurrentStack ()
70 public ConcurrentStack (IEnumerable<T> collection)
72 foreach (T item in collection)
76 bool IProducerConsumerCollection<T>.TryAdd (T elem)
82 public void Push (T item)
84 Node temp = pool.Take ();
88 } while (Interlocked.CompareExchange (ref head, temp, temp.Next) != temp.Next);
90 Interlocked.Increment (ref count);
93 public void PushRange (T[] items)
95 PushRange (items, 0, items.Length);
98 public void PushRange (T[] items, int startIndex, int count)
103 for (int i = startIndex; i < count; i++) {
104 Node temp = pool.Take ();
105 temp.Value = items[i];
115 } while (Interlocked.CompareExchange (ref head, insert, first.Next) != first.Next);
117 Interlocked.Add (ref count, count);
120 public bool TryPop (out T result)
127 result = default (T);
130 } while (Interlocked.CompareExchange (ref head, temp.Next, temp) != temp);
132 Interlocked.Decrement (ref count);
135 pool.Release (ZeroOut (temp));
140 public int TryPopRange (T[] items)
142 return TryPopRange (items, 0, items.Length);
145 public int TryPopRange (T[] items, int startIndex, int count)
155 for (int j = 0; j < count - 1; j++) {
160 } while (Interlocked.CompareExchange (ref head, end, temp) != temp);
163 for (i = startIndex; i < count && temp != null; i++) {
164 items[i] = temp.Value;
167 pool.Release (ZeroOut (end));
173 public bool TryPeek (out T result)
176 if (myHead == null) {
177 result = default (T);
180 result = myHead.Value;
186 // This is not satisfactory
191 IEnumerator IEnumerable.GetEnumerator ()
193 return (IEnumerator)InternalGetEnumerator ();
196 public IEnumerator<T> GetEnumerator ()
198 return InternalGetEnumerator ();
201 IEnumerator<T> InternalGetEnumerator ()
204 if (my_head == null) {
208 yield return my_head.Value;
209 } while ((my_head = my_head.Next) != null);
213 void ICollection.CopyTo (Array array, int index)
215 T[] dest = array as T[];
218 CopyTo (dest, index);
221 public void CopyTo (T[] array, int index)
223 IEnumerator<T> e = InternalGetEnumerator ();
225 while (e.MoveNext ()) {
226 array[i++] = e.Current;
230 bool ICollection.IsSynchronized {
234 bool IProducerConsumerCollection<T>.TryTake (out T item)
236 return TryPop (out item);
239 object syncRoot = new object ();
240 object ICollection.SyncRoot {
241 get { return syncRoot; }
244 public T[] ToArray ()
246 T[] dest = new T [count];
257 public bool IsEmpty {