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 public ConcurrentStack ()
55 public ConcurrentStack (IEnumerable<T> collection)
57 foreach (T item in collection)
61 bool IProducerConsumerCollection<T>.TryAdd (T elem)
67 public void Push (T item)
69 Node temp = new Node ();
73 } while (Interlocked.CompareExchange (ref head, temp, temp.Next) != temp.Next);
75 Interlocked.Increment (ref count);
78 public void PushRange (T[] items)
80 PushRange (items, 0, items.Length);
83 public void PushRange (T[] items, int startIndex, int count)
88 for (int i = startIndex; i < count; i++) {
89 Node temp = new Node ();
90 temp.Value = items[i];
100 } while (Interlocked.CompareExchange (ref head, insert, first.Next) != first.Next);
102 Interlocked.Add (ref count, count);
105 public bool TryPop (out T result)
112 result = default (T);
115 } while (Interlocked.CompareExchange (ref head, temp.Next, temp) != temp);
117 Interlocked.Decrement (ref count);
124 public int TryPopRange (T[] items)
127 throw new ArgumentNullException ("items");
128 return TryPopRange (items, 0, items.Length);
131 public int TryPopRange (T[] items, int startIndex, int count)
134 throw new ArgumentNullException ("items");
135 if (startIndex < 0 || startIndex >= items.Length)
136 throw new ArgumentOutOfRangeException ("startIndex");
138 throw new ArgumentOutOfRangeException ("count");
139 if (startIndex + count > items.Length)
140 throw new ArgumentException ("startIndex + count is greater than the length of items.");
150 for (int j = 0; j < count; j++) {
155 } while (Interlocked.CompareExchange (ref head, end, temp) != temp);
158 for (i = startIndex; i < startIndex + count && temp != null; i++) {
159 items[i] = temp.Value;
163 Interlocked.Add (ref this.count, -(i - startIndex));
165 return i - startIndex;
168 public bool TryPeek (out T result)
171 if (myHead == null) {
172 result = default (T);
175 result = myHead.Value;
181 // This is not satisfactory
186 IEnumerator IEnumerable.GetEnumerator ()
188 return (IEnumerator)InternalGetEnumerator ();
191 public IEnumerator<T> GetEnumerator ()
193 return InternalGetEnumerator ();
196 IEnumerator<T> InternalGetEnumerator ()
199 if (my_head == null) {
203 yield return my_head.Value;
204 } while ((my_head = my_head.Next) != null);
208 void ICollection.CopyTo (Array array, int index)
211 throw new ArgumentNullException ("array");
213 throw new ArgumentException ("The array can't be multidimensional");
214 if (array.GetLowerBound (0) != 0)
215 throw new ArgumentException ("The array needs to be 0-based");
217 T[] dest = array as T[];
219 throw new ArgumentException ("The array cannot be cast to the collection element type", "array");
220 CopyTo (dest, index);
223 public void CopyTo (T[] array, int index)
226 throw new ArgumentNullException ("array");
228 throw new ArgumentOutOfRangeException ("index");
229 if (index >= array.Length)
230 throw new ArgumentException ("index is equals or greather than array length", "index");
232 IEnumerator<T> e = InternalGetEnumerator ();
234 while (e.MoveNext ()) {
235 if (i == array.Length - index)
236 throw new ArgumentException ("The number of elememts in the collection exceeds the capacity of array", "array");
237 array[i++] = e.Current;
241 bool ICollection.IsSynchronized {
245 bool IProducerConsumerCollection<T>.TryTake (out T item)
247 return TryPop (out item);
250 object syncRoot = new object ();
251 object ICollection.SyncRoot {
252 get { return syncRoot; }
255 public T[] ToArray ()
257 return new List<T> (this).ToArray ();
266 public bool IsEmpty {