5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
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
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Diagnostics;
32 using System.Runtime.InteropServices;
34 using System.Threading;
35 using System.Threading.Tasks;
37 namespace System.Collections.Concurrent
40 [DebuggerDisplay ("Count={Count}")]
41 [DebuggerTypeProxy (typeof (CollectionDebuggerView<>))]
42 public class ConcurrentBag<T> : IProducerConsumerCollection<T>, IEnumerable<T>, IEnumerable
44 const int multiplier = 2;
45 int size = Environment.ProcessorCount + 1;
48 CyclicDeque<T>[] container;
50 object syncLock = new object ();
52 public ConcurrentBag ()
54 container = new CyclicDeque<T>[size];
55 for (int i = 0; i < container.Length; i++)
56 container[i] = new CyclicDeque<T> ();
59 public ConcurrentBag (IEnumerable<T> enumerable) : this ()
61 foreach (T item in enumerable)
65 public bool TryAdd (T item)
72 public void Add (T item)
74 Interlocked.Increment (ref count);
77 CyclicDeque<T> bag = GetBag ();
78 bag.PushBottom (item);
81 public bool TryTake (out T item)
84 CyclicDeque<T> bag = GetBag ();
86 if (bag == null || bag.PopBottom (out item) != PopResult.Succeed) {
87 for (int i = 0; i < container.Length; i++) {
88 if (container[i].PopTop (out item) == PopResult.Succeed) {
89 Interlocked.Decrement (ref count);
94 Interlocked.Decrement (ref count);
107 public bool IsEmpty {
113 object System.Collections.ICollection.SyncRoot {
119 bool System.Collections.ICollection.IsSynchronized {
125 IEnumerator IEnumerable.GetEnumerator ()
127 return GetEnumeratorInternal ();
130 IEnumerator<T> IEnumerable<T>.GetEnumerator ()
132 return GetEnumeratorInternal ();
135 IEnumerator<T> GetEnumeratorInternal ()
137 for (int i = 0; i < size; i++) {
138 CyclicDeque<T> bag = container[i];
139 foreach (T item in bag.GetEnumerable ()) {
145 void System.Collections.ICollection.CopyTo (Array array, int index)
147 T[] a = array as T[];
154 public void CopyTo (T[] array, int index)
157 if (array.Length < c + index)
158 throw new InvalidOperationException ("Array is not big enough");
160 CopyTo (array, index, c);
163 void CopyTo (T[] array, int index, int num)
167 foreach (T item in this) {
175 public T[] ToArray ()
187 return Thread.CurrentThread.ManagedThreadId - 1;
190 void GrowIfNecessary ()
192 int index = GetIndex ();
193 int currentSize = size;
195 while (index > currentSize - 1) {
201 CyclicDeque<T> GetBag ()
205 return i < container.Length ? container[i] : null;
208 void Grow (int referenceSize)
211 if (referenceSize != size)
214 CyclicDeque<T>[] slice = new CyclicDeque<T>[size * multiplier];
216 for (i = 0; i < container.Length; i++)
217 slice[i] = container[i];
218 for (; i < slice.Length; i++)
219 slice[i] = new CyclicDeque<T> ();