4 // Marek Safar <marek.safar@gmail.com>
6 // Copyright (c) 2008 Jérémie "Garuma" Laval
7 // Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 using System.Threading;
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Runtime.Serialization;
37 namespace System.Collections.Concurrent
40 [System.Diagnostics.DebuggerDisplay ("Count = {Count}")]
41 [System.Diagnostics.DebuggerTypeProxy (typeof (CollectionDebuggerView<>))]
42 public class ConcurrentStack<T> : IProducerConsumerCollection<T>, IEnumerable<T>,
43 ICollection, IEnumerable
47 public T Value = default (T);
55 public ConcurrentStack ()
59 public ConcurrentStack (IEnumerable<T> collection)
61 if (collection == null)
62 throw new ArgumentNullException ("collection");
64 foreach (T item in collection)
68 bool IProducerConsumerCollection<T>.TryAdd (T elem)
74 public void Push (T item)
76 Node temp = new Node ();
80 } while (Interlocked.CompareExchange (ref head, temp, temp.Next) != temp.Next);
82 Interlocked.Increment (ref count);
85 public void PushRange (T[] items)
88 throw new ArgumentNullException ("items");
90 PushRange (items, 0, items.Length);
93 public void PushRange (T[] items, int startIndex, int count)
95 RangeArgumentsCheck (items, startIndex, count);
100 for (int i = startIndex; i < count; i++) {
101 Node temp = new Node ();
102 temp.Value = items[i];
112 } while (Interlocked.CompareExchange (ref head, insert, first.Next) != first.Next);
114 Interlocked.Add (ref this.count, count);
117 public bool TryPop (out T result)
124 result = default (T);
127 } while (Interlocked.CompareExchange (ref head, temp.Next, temp) != temp);
129 Interlocked.Decrement (ref count);
136 public int TryPopRange (T[] items)
139 throw new ArgumentNullException ("items");
140 return TryPopRange (items, 0, items.Length);
143 public int TryPopRange (T[] items, int startIndex, int count)
145 RangeArgumentsCheck (items, startIndex, count);
155 for (int j = 0; j < count; j++) {
160 } while (Interlocked.CompareExchange (ref head, end, temp) != temp);
163 for (i = startIndex; i < startIndex + count && temp != null; i++) {
164 items[i] = temp.Value;
168 Interlocked.Add (ref this.count, -(i - startIndex));
170 return i - startIndex;
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 ICollection ic = new List<T> (this);
216 ic.CopyTo (array, index);
219 public void CopyTo (T[] array, int index)
222 throw new ArgumentNullException ("array");
224 throw new ArgumentOutOfRangeException ("index");
225 if (index > array.Length)
226 throw new ArgumentException ("index is equals or greather than array length", "index");
228 IEnumerator<T> e = InternalGetEnumerator ();
230 while (e.MoveNext ()) {
231 if (i == array.Length - index)
232 throw new ArgumentException ("The number of elememts in the collection exceeds the capacity of array", "array");
233 array[i++] = e.Current;
237 bool ICollection.IsSynchronized {
238 get { return false; }
241 bool IProducerConsumerCollection<T>.TryTake (out T item)
243 return TryPop (out item);
246 object ICollection.SyncRoot {
248 throw new NotSupportedException ();
252 public T[] ToArray ()
254 return new List<T> (this).ToArray ();
263 public bool IsEmpty {
269 static void RangeArgumentsCheck (T[] items, int startIndex, int count)
272 throw new ArgumentNullException ("items");
273 if (startIndex < 0 || startIndex >= items.Length)
274 throw new ArgumentOutOfRangeException ("startIndex");
276 throw new ArgumentOutOfRangeException ("count");
277 if (startIndex + count > items.Length)
278 throw new ArgumentException ("startIndex + count is greater than the length of items.");