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, ISerializable, IDeserializationCallback
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);
104 public bool TryPop (out T value)
114 } while (Interlocked.CompareExchange (ref head, temp.Next, temp) != temp);
116 Interlocked.Decrement (ref count);
122 public int TryPopRange (T[] values)
124 return TryPopRange (values, 0, values.Length);
127 public int TryPopRange (T[] values, int startIndex, int count)
129 int insertIndex = startIndex;
138 for (int j = 0; j < count - 1; j++) {
143 } while (Interlocked.CompareExchange (ref head, end, temp) != temp);
146 for (i = startIndex; i < count && temp != null; i++) {
147 values[i] = temp.Value;
154 public bool TryPeek (out T value)
157 if (myHead == null) {
161 value = myHead.Value;
167 // This is not satisfactory
172 IEnumerator IEnumerable.GetEnumerator ()
174 return (IEnumerator)InternalGetEnumerator ();
177 IEnumerator<T> IEnumerable<T>.GetEnumerator ()
179 return InternalGetEnumerator ();
182 public IEnumerator<T> GetEnumerator ()
184 return InternalGetEnumerator ();
187 IEnumerator<T> InternalGetEnumerator ()
190 if (my_head == null) {
194 yield return my_head.Value;
195 } while ((my_head = my_head.Next) != null);
199 void ICollection.CopyTo (Array array, int index)
201 T[] dest = array as T[];
204 CopyTo (dest, index);
207 public void CopyTo (T[] dest, int index)
209 IEnumerator<T> e = InternalGetEnumerator ();
211 while (e.MoveNext ()) {
212 dest[i++] = e.Current;
216 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
218 throw new NotImplementedException ();
221 bool ICollection.IsSynchronized {
225 void IDeserializationCallback.OnDeserialization (object sender)
227 throw new NotImplementedException ();
230 bool IProducerConsumerCollection<T>.TryTake (out T item)
232 return TryPop (out item);
235 object syncRoot = new object ();
236 object ICollection.SyncRoot {
237 get { return syncRoot; }
240 public T[] ToArray ()
242 T[] dest = new T [count];
253 public bool IsEmpty {