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)
53 /* Read is implemented as a simple load operation on 64bits
54 * so no need to make the distinction ourselves
56 long b = Interlocked.Read (ref bottom);
59 // Take care of growing
60 if (b - upperBound >= a.Size - 1) {
61 upperBound = Interlocked.Read (ref top);
62 a = a.Grow (b, upperBound);
66 // Register the new value
67 a.segment[b % a.size] = obj;
68 Interlocked.Increment (ref bottom);
71 public PopResult PopBottom (out T obj)
75 long b = Interlocked.Decrement (ref bottom);
77 long t = Interlocked.Read (ref top);
82 Interlocked.Add (ref bottom, t - b);
83 return PopResult.Empty;
86 obj = a.segment[b % a.size];
88 return PopResult.Succeed;
89 Interlocked.Add (ref bottom, t + 1 - b);
91 if (Interlocked.CompareExchange (ref top, t + 1, t) != t)
92 return PopResult.Empty;
94 return PopResult.Succeed;
97 public PopResult PopTop (out T obj)
101 long t = Interlocked.Read (ref top);
102 long b = Interlocked.Read (ref bottom);
105 return PopResult.Empty;
107 if (Interlocked.CompareExchange (ref top, t + 1, t) != t)
108 return PopResult.Abort;
111 obj = a.segment[t % a.size];
113 return PopResult.Succeed;
116 public IEnumerable<T> GetEnumerable ()
119 return a.GetEnumerable (bottom, ref top);
123 internal class CircularArray<T>
125 readonly int baseSize;
126 public readonly int size;
127 public readonly T[] segment;
129 public CircularArray (int baseSize)
131 this.baseSize = baseSize;
132 this.size = 1 << baseSize;
133 this.segment = new T[size];
142 public T this[long index] {
144 return segment[index % size];
147 segment[index % size] = value;
151 public CircularArray<T> Grow (long bottom, long top)
153 var grow = new CircularArray<T> (baseSize + 1);
155 for (long i = top; i < bottom; i++) {
156 grow.segment[i] = segment[i % size];
162 public IEnumerable<T> GetEnumerable (long bottom, ref long top)
164 long instantTop = top;
165 T[] slice = new T[bottom - instantTop];
167 for (long i = instantTop; i < bottom; i++)
168 slice[++destIndex] = segment[i % size];
170 return RealGetEnumerable (slice, bottom, top, instantTop);
173 IEnumerable<T> RealGetEnumerable (T[] slice, long bottom, long realTop, long initialTop)
175 int destIndex = (int)(realTop - initialTop - 1);
176 for (long i = realTop; i < bottom; ++i)
177 yield return slice[++destIndex];