4 // Copyright (c) 2008 Jérémie "Garuma" Laval
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 using System.Threading;
28 using System.Collections;
29 using System.Collections.Generic;
30 using System.Runtime.Serialization;
32 namespace System.Collections.Concurrent
36 public class ConcurrentStack<T> : IProducerConsumerCollection<T>, IEnumerable<T>,
37 ICollection, IEnumerable
41 public T Value = default (T);
42 public Node Next = null;
49 public ConcurrentStack ()
53 public ConcurrentStack (IEnumerable<T> enumerable)
55 foreach (T item in enumerable)
59 bool IProducerConsumerCollection<T>.TryAdd (T elem)
65 public void Push (T element)
67 Node temp = new Node ();
71 } while (Interlocked.CompareExchange (ref head, temp, temp.Next) != temp.Next);
73 Interlocked.Increment (ref count);
76 public void PushRange (T[] values)
78 PushRange (values, 0, values.Length);
81 public void PushRange (T[] values, int start, int length)
86 for (int i = start; i < length; i++) {
87 Node temp = new Node ();
88 temp.Value = values[i];
98 } while (Interlocked.CompareExchange (ref head, insert, first.Next) != first.Next);
100 Interlocked.Add (ref count, length);
103 public bool TryPop (out T value)
113 } while (Interlocked.CompareExchange (ref head, temp.Next, temp) != temp);
115 Interlocked.Decrement (ref count);
121 public int TryPopRange (T[] values)
123 return TryPopRange (values, 0, values.Length);
126 public int TryPopRange (T[] values, int startIndex, int count)
136 for (int j = 0; j < count - 1; j++) {
141 } while (Interlocked.CompareExchange (ref head, end, temp) != temp);
144 for (i = startIndex; i < count && temp != null; i++) {
145 values[i] = temp.Value;
152 public bool TryPeek (out T value)
155 if (myHead == null) {
159 value = myHead.Value;
165 // This is not satisfactory
170 IEnumerator IEnumerable.GetEnumerator ()
172 return (IEnumerator)InternalGetEnumerator ();
175 IEnumerator<T> IEnumerable<T>.GetEnumerator ()
177 return InternalGetEnumerator ();
180 public IEnumerator<T> GetEnumerator ()
182 return InternalGetEnumerator ();
185 IEnumerator<T> InternalGetEnumerator ()
188 if (my_head == null) {
192 yield return my_head.Value;
193 } while ((my_head = my_head.Next) != null);
197 void ICollection.CopyTo (Array array, int index)
199 T[] dest = array as T[];
202 CopyTo (dest, index);
205 public void CopyTo (T[] dest, int index)
207 IEnumerator<T> e = InternalGetEnumerator ();
209 while (e.MoveNext ()) {
210 dest[i++] = e.Current;
214 bool ICollection.IsSynchronized {
218 bool IProducerConsumerCollection<T>.TryTake (out T item)
220 return TryPop (out item);
223 object syncRoot = new object ();
224 object ICollection.SyncRoot {
225 get { return syncRoot; }
228 public T[] ToArray ()
230 T[] dest = new T [count];
241 public bool IsEmpty {