Wrap always_inline and noinline attributes in compiler checks and use MSVC equivalent.
[mono.git] / mcs / class / corlib / System.Collections.Concurrent / ConcurrentStack.cs
1 // ConcurrentStack.cs
2 //
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
4 //
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:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
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
21 // THE SOFTWARE.
22 //
23 //
24
25 #if NET_4_0 || MOBILE
26
27 using System;
28 using System.Threading;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Runtime.Serialization;
32
33 namespace System.Collections.Concurrent
34 {
35         
36         [System.Diagnostics.DebuggerDisplay ("Count = {Count}")]
37         [System.Diagnostics.DebuggerTypeProxy (typeof (CollectionDebuggerView<>))]
38         public class ConcurrentStack<T> : IProducerConsumerCollection<T>, IEnumerable<T>,
39                                           ICollection, IEnumerable
40         {
41                 class Node
42                 {
43                         public T Value = default (T);
44                         public Node Next = null;
45                 }
46                 
47                 Node head = null;
48                 
49                 int count;
50
51                 class NodeObjectPool : ObjectPool<Node> {
52                         protected override Node Creator ()
53                         {
54                                 return new Node ();
55                         }
56                 }
57                 static readonly NodeObjectPool pool = new NodeObjectPool ();
58
59                 static Node ZeroOut (Node node)
60                 {
61                         node.Value = default(T);
62                         node.Next = null;
63                         return node;
64                 }
65                 
66                 public ConcurrentStack ()
67                 {
68                 }
69                 
70                 public ConcurrentStack (IEnumerable<T> collection)
71                 {
72                         foreach (T item in collection) 
73                                 Push (item);
74                 }
75                 
76                 bool IProducerConsumerCollection<T>.TryAdd (T elem)
77                 {
78                         Push (elem);
79                         return true;
80                 }
81                 
82                 public void Push (T item)
83                 {
84                         Node temp = pool.Take ();
85                         temp.Value = item;
86                         do {
87                           temp.Next = head;
88                         } while (Interlocked.CompareExchange (ref head, temp, temp.Next) != temp.Next);
89                         
90                         Interlocked.Increment (ref count);
91                 }
92
93                 public void PushRange (T[] items)
94                 {
95                         PushRange (items, 0, items.Length);
96                 }
97                 
98                 public void PushRange (T[] items, int startIndex, int count)
99                 {
100                         Node insert = null;
101                         Node first = null;
102                         
103                         for (int i = startIndex; i < count; i++) {
104                                 Node temp = pool.Take ();
105                                 temp.Value = items[i];
106                                 temp.Next = insert;
107                                 insert = temp;
108                                 
109                                 if (first == null)
110                                         first = temp;
111                         }
112                         
113                         do {
114                                 first.Next = head;
115                         } while (Interlocked.CompareExchange (ref head, insert, first.Next) != first.Next);
116                         
117                         Interlocked.Add (ref count, count);
118                 }
119                 
120                 public bool TryPop (out T result)
121                 {
122                         Node temp;
123                         do {
124                                 temp = head;
125                                 // The stak is empty
126                                 if (temp == null) {
127                                         result = default (T);
128                                         return false;
129                                 }
130                         } while (Interlocked.CompareExchange (ref head, temp.Next, temp) != temp);
131                         
132                         Interlocked.Decrement (ref count);
133                         
134                         result = temp.Value;
135                         pool.Release (ZeroOut (temp));
136
137                         return true;
138                 }
139
140                 public int TryPopRange (T[] items)
141                 {
142                         if (items == null)
143                                 throw new ArgumentNullException ("items");
144                         return TryPopRange (items, 0, items.Length);
145                 }
146
147                 public int TryPopRange (T[] items, int startIndex, int count)
148                 {
149                         if (items == null)
150                                 throw new ArgumentNullException ("items");
151                         if (startIndex < 0 || startIndex >= items.Length)
152                                 throw new ArgumentOutOfRangeException ("startIndex");
153                         if (count < 0)
154                                 throw new ArgumentOutOfRangeException ("count");
155                         if (startIndex + count > items.Length)
156                                 throw new ArgumentException ("startIndex + count is greater than the length of items.");
157
158                         Node temp;
159                         Node end;
160                         
161                         do {
162                                 temp = head;
163                                 if (temp == null)
164                                         return -1;
165                                 end = temp;
166                                 for (int j = 0; j < count; j++) {
167                                         end = end.Next;
168                                         if (end == null)
169                                                 break;
170                                 }
171                         } while (Interlocked.CompareExchange (ref head, end, temp) != temp);
172                         
173                         int i;
174                         for (i = startIndex; i < startIndex + count && temp != null; i++) {
175                                 items[i] = temp.Value;
176                                 end = temp;
177                                 temp = temp.Next;
178                                 pool.Release (ZeroOut (end));
179                         }
180                         Interlocked.Add (ref this.count, -(i - startIndex));
181                         
182                         return i - startIndex;
183                 }
184                 
185                 public bool TryPeek (out T result)
186                 {
187                         Node myHead = head;
188                         if (myHead == null) {
189                                 result = default (T);
190                                 return false;
191                         }
192                         result = myHead.Value;
193                         return true;
194                 }
195                 
196                 public void Clear ()
197                 {
198                         // This is not satisfactory
199                         count = 0;
200                         head = null;
201                 }
202                 
203                 IEnumerator IEnumerable.GetEnumerator ()
204                 {
205                         return (IEnumerator)InternalGetEnumerator ();
206                 }
207                 
208                 public IEnumerator<T> GetEnumerator ()
209                 {
210                         return InternalGetEnumerator ();
211                 }
212
213                 IEnumerator<T> InternalGetEnumerator ()
214                 {
215                         Node my_head = head;
216                         if (my_head == null) {
217                                 yield break;
218                         } else {
219                                 do {
220                                         yield return my_head.Value;
221                                 } while ((my_head = my_head.Next) != null);
222                         }
223                 }
224                 
225                 void ICollection.CopyTo (Array array, int index)
226                 {
227                         if (array == null)
228                                 throw new ArgumentNullException ("array");
229                         if (array.Rank > 1)
230                                 throw new ArgumentException ("The array can't be multidimensional");
231                         if (array.GetLowerBound (0) != 0)
232                                 throw new ArgumentException ("The array needs to be 0-based");
233
234                         T[] dest = array as T[];
235                         if (dest == null)
236                                 throw new ArgumentException ("The array cannot be cast to the collection element type", "array");
237                         CopyTo (dest, index);
238                 }
239                 
240                 public void CopyTo (T[] array, int index)
241                 {
242                         if (array == null)
243                                 throw new ArgumentNullException ("array");
244                         if (index < 0)
245                                 throw new ArgumentOutOfRangeException ("index");
246                         if (index >= array.Length)
247                                 throw new ArgumentException ("index is equals or greather than array length", "index");
248
249                         IEnumerator<T> e = InternalGetEnumerator ();
250                         int i = index;
251                         while (e.MoveNext ()) {
252                                 if (i == array.Length - index)
253                                         throw new ArgumentException ("The number of elememts in the collection exceeds the capacity of array", "array");
254                                 array[i++] = e.Current;
255                         }
256                 }
257                 
258                 bool ICollection.IsSynchronized {
259                         get { return true; }
260                 }
261                 
262                 bool IProducerConsumerCollection<T>.TryTake (out T item)
263                 {
264                         return TryPop (out item);
265                 }
266                 
267                 object syncRoot = new object ();
268                 object ICollection.SyncRoot {
269                         get { return syncRoot; }
270                 }
271                 
272                 public T[] ToArray ()
273                 {
274                         return new List<T> (this).ToArray ();
275                 }
276                 
277                 public int Count {
278                         get {
279                                 return count;
280                         }
281                 }
282                 
283                 public bool IsEmpty {
284                         get {
285                                 return count == 0;
286                         }
287                 }
288         }
289 }
290 #endif