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
30 using System.Collections.Generic;
31 using System.Threading;
33 #if INSIDE_MONO_PARALLEL
34 namespace Mono.Threading.Tasks
36 namespace System.Threading.Tasks
39 #if INSIDE_MONO_PARALLEL
42 class CyclicDeque<T> : IConcurrentDeque<T>
44 const int BaseSize = 11;
49 CircularArray<T> array = new CircularArray<T> (BaseSize);
51 public void PushBottom (T obj)
56 // Take care of growing
57 var size = b - top - upperBound;
60 a = a.Grow (b, upperBound);
64 // Register the new value
65 a.segment[b % a.size] = obj;
66 Interlocked.Increment (ref bottom);
69 public PopResult PopBottom (out T obj)
73 int b = Interlocked.Decrement (ref bottom);
80 Interlocked.Add (ref bottom, t - b);
81 return PopResult.Empty;
84 obj = a.segment[b % a.size];
86 return PopResult.Succeed;
87 Interlocked.Add (ref bottom, t + 1 - b);
89 if (Interlocked.CompareExchange (ref top, t + 1, t) != t)
90 return PopResult.Empty;
92 return PopResult.Succeed;
95 public bool PeekBottom (out T obj)
99 int b = Interlocked.Decrement (ref bottom);
107 obj = a.segment[b % a.size];
111 public PopResult PopTop (out T obj)
119 return PopResult.Empty;
121 if (Interlocked.CompareExchange (ref top, t + 1, t) != t)
122 return PopResult.Abort;
125 obj = a.segment[t % a.size];
127 return PopResult.Succeed;
130 internal bool PeekTop (out T obj)
141 obj = a.segment[t % a.size];
146 public IEnumerable<T> GetEnumerable ()
149 return a.GetEnumerable (bottom, ref top);
152 public bool IsEmpty {
161 internal class CircularArray<T>
163 readonly int baseSize;
164 public readonly int size;
165 public readonly T[] segment;
167 public CircularArray (int baseSize)
169 this.baseSize = baseSize;
170 this.size = 1 << baseSize;
171 this.segment = new T[size];
180 public T this[int index] {
182 return segment[index % size];
185 segment[index % size] = value;
189 public CircularArray<T> Grow (int bottom, int top)
191 var grow = new CircularArray<T> (baseSize + 1);
193 for (int i = top; i < bottom; i++) {
194 grow.segment[i] = segment[i % size];
200 public IEnumerable<T> GetEnumerable (int bottom, ref int top)
202 int instantTop = top;
203 T[] slice = new T[bottom - instantTop];
205 for (int i = instantTop; i < bottom; i++)
206 slice[++destIndex] = segment[i % size];
208 return RealGetEnumerable (slice, bottom, top, instantTop);
211 IEnumerable<T> RealGetEnumerable (T[] slice, int bottom, int realTop, int initialTop)
213 int destIndex = (int)(realTop - initialTop - 1);
214 for (int i = realTop; i < bottom; ++i)
215 yield return slice[++destIndex];