Remove excessive shortcut key matching in ToolStrip
[mono.git] / mcs / class / corlib / System.Collections.Concurrent / ConcurrentQueue.cs
1 // ConcurrentQueue.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
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 ConcurrentQueue<T> : IProducerConsumerCollection<T>, IEnumerable<T>, ICollection,
39                                           IEnumerable
40         {
41                 class Node
42                 {
43                         public T Value;
44                         public Node Next;
45                 }
46                 
47                 Node head = new Node ();
48                 Node tail;
49                 int count;
50
51                 public ConcurrentQueue ()
52                 {
53                         tail = head;
54                 }
55                 
56                 public ConcurrentQueue (IEnumerable<T> collection): this()
57                 {
58                         foreach (T item in collection)
59                                 Enqueue (item);
60                 }
61                 
62                 public void Enqueue (T item)
63                 {
64                         Node node = new Node ();
65                         node.Value = item;
66                         
67                         Node oldTail = null;
68                         Node oldNext = null;
69                         
70                         bool update = false;
71                         while (!update) {
72                                 oldTail = tail;
73                                 oldNext = oldTail.Next;
74                                 
75                                 // Did tail was already updated ?
76                                 if (tail == oldTail) {
77                                         if (oldNext == null) {
78                                                 // The place is for us
79                                                 update = Interlocked.CompareExchange (ref tail.Next, node, null) == null;
80                                         } else {
81                                                 // another Thread already used the place so give him a hand by putting tail where it should be
82                                                 Interlocked.CompareExchange (ref tail, oldNext, oldTail);
83                                         }
84                                 }
85                         }
86                         // At this point we added correctly our node, now we have to update tail. If it fails then it will be done by another thread
87                         Interlocked.CompareExchange (ref tail, node, oldTail);
88                         Interlocked.Increment (ref count);
89                 }
90                 
91                 bool IProducerConsumerCollection<T>.TryAdd (T item)
92                 {
93                         Enqueue (item);
94                         return true;
95                 }
96
97                 public bool TryDequeue (out T result)
98                 {
99                         result = default (T);
100                         Node oldNext = null;
101                         bool advanced = false;
102
103                         while (!advanced) {
104                                 Node oldHead = head;
105                                 Node oldTail = tail;
106                                 oldNext = oldHead.Next;
107                                 
108                                 if (oldHead == head) {
109                                         // Empty case ?
110                                         if (oldHead == oldTail) {
111                                                 // This should be false then
112                                                 if (oldNext != null) {
113                                                         // If not then the linked list is mal formed, update tail
114                                                         Interlocked.CompareExchange (ref tail, oldNext, oldTail);
115                                                         continue;
116                                                 }
117                                                 result = default (T);
118                                                 return false;
119                                         } else {
120                                                 result = oldNext.Value;
121                                                 advanced = Interlocked.CompareExchange (ref head, oldNext, oldHead) == oldHead;
122                                         }
123                                 }
124                         }
125
126                         oldNext.Value = default (T);
127
128                         Interlocked.Decrement (ref count);
129
130                         return true;
131                 }
132                 
133                 public bool TryPeek (out T result)
134                 {
135                         result = default (T);
136                         bool update = true;
137                         
138                         while (update)
139                         {
140                                 Node oldHead = head;
141                                 Node oldNext = oldHead.Next;
142
143                                 if (oldNext == null) {
144                                         result = default (T);
145                                         return false;
146                                 }
147
148                                 result = oldNext.Value;
149                                 
150                                 //check if head has been updated
151                                 update = head != oldHead;
152                         }
153                         return true;
154                 }
155                 
156                 internal void Clear ()
157                 {
158                         count = 0;
159                         tail = head = new Node ();
160                 }
161                 
162                 IEnumerator IEnumerable.GetEnumerator ()
163                 {
164                         return (IEnumerator)InternalGetEnumerator ();
165                 }
166                 
167                 public IEnumerator<T> GetEnumerator ()
168                 {
169                         return InternalGetEnumerator ();
170                 }
171                 
172                 IEnumerator<T> InternalGetEnumerator ()
173                 {
174                         Node my_head = head;
175                         while ((my_head = my_head.Next) != null) {
176                                 yield return my_head.Value;
177                         }
178                 }
179                 
180                 void ICollection.CopyTo (Array array, int index)
181                 {
182                         if (array == null)
183                                 throw new ArgumentNullException ("array");
184                         if (array.Rank > 1)
185                                 throw new ArgumentException ("The array can't be multidimensional");
186                         if (array.GetLowerBound (0) != 0)
187                                 throw new ArgumentException ("The array needs to be 0-based");
188
189                         T[] dest = array as T[];
190                         if (dest == null)
191                                 throw new ArgumentException ("The array cannot be cast to the collection element type", "array");
192                         CopyTo (dest, index);
193                 }
194                 
195                 public void CopyTo (T[] array, int index)
196                 {
197                         if (array == null)
198                                 throw new ArgumentNullException ("array");
199                         if (index < 0)
200                                 throw new ArgumentOutOfRangeException ("index");
201                         if (index >= array.Length)
202                                 throw new ArgumentException ("index is equals or greather than array length", "index");
203
204                         IEnumerator<T> e = InternalGetEnumerator ();
205                         int i = index;
206                         while (e.MoveNext ()) {
207                                 if (i == array.Length - index)
208                                         throw new ArgumentException ("The number of elememts in the collection exceeds the capacity of array", "array");
209                                 array[i++] = e.Current;
210                         }
211                 }
212                 
213                 public T[] ToArray ()
214                 {
215                         return new List<T> (this).ToArray ();
216                 }
217                 
218                 bool ICollection.IsSynchronized {
219                         get { return true; }
220                 }
221
222                 bool IProducerConsumerCollection<T>.TryTake (out T item)
223                 {
224                         return TryDequeue (out item);
225                 }
226                 
227                 object syncRoot = new object();
228                 object ICollection.SyncRoot {
229                         get { return syncRoot; }
230                 }
231                 
232                 public int Count {
233                         get {
234                                 return count;
235                         }
236                 }
237                 
238                 public bool IsEmpty {
239                         get {
240                                 return count == 0;
241                         }
242                 }
243         }
244 }
245 #endif