1 #if NET_4_0 || BOOTSTRAP_NET_4_0
6 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 using System.Collections.Generic;
30 using System.Threading;
32 namespace System.Threading.Tasks
34 internal enum PopResult {
40 internal interface IDequeOperations<T>
42 void PushBottom (T obj);
43 PopResult PopBottom (out T obj);
44 PopResult PopTop (out T obj);
47 internal interface IValueReader
49 long Read (ref long value);
52 internal class CyclicDeque<T> : IDequeOperations<T>
54 readonly IValueReader reader;
55 const int BaseSize = 11;
57 class Reader32 : IValueReader
59 public long Read (ref long value)
61 return Interlocked.Read (ref value);
65 class Reader64 : IValueReader
67 public long Read (ref long value)
75 // We do the distinction between 32bits and 64bits
77 reader = new Reader32 ();
79 reader = new Reader64 ();
85 CircularArray<T> array = new CircularArray<T> (BaseSize);
87 public void PushBottom (T obj)
89 long b = reader.Read (ref bottom);
92 // Take care of growing
93 if (b - upperBound >= a.Size - 1) {
94 upperBound = reader.Read (ref top);
95 a = a.Grow (b, upperBound);
96 Interlocked.Exchange (ref array, a);
99 // Register the new value
101 Interlocked.Increment (ref bottom);
104 public PopResult PopBottom (out T obj)
108 long b = Interlocked.Decrement (ref bottom);
110 long t = reader.Read (ref top);
115 Interlocked.Add (ref bottom, t - b);
116 return PopResult.Empty;
121 return PopResult.Succeed;
122 Interlocked.Add (ref bottom, t + 1 - b);
124 if (Interlocked.CompareExchange (ref top, t + 1, t) != t)
125 return PopResult.Empty;
127 return PopResult.Succeed;
130 public PopResult PopTop (out T obj)
134 long t = reader.Read (ref top);
135 long b = reader.Read (ref bottom);
138 return PopResult.Empty;
140 if (Interlocked.CompareExchange (ref top, t + 1, t) != t)
141 return PopResult.Abort;
146 return PopResult.Succeed;
149 public IEnumerable<T> GetEnumerable ()
152 return a.GetEnumerable ();
156 internal class CircularArray<T>
158 readonly int baseSize;
160 readonly T[] segment;
162 public CircularArray (int baseSize)
164 this.baseSize = baseSize;
165 this.size = 1 << baseSize;
166 this.segment = new T[size];
175 public T this[long index] {
177 return segment[index % Size];
180 segment[index % Size] = value;
184 public CircularArray<T> Grow (long bottom, long top)
186 var grow = new CircularArray<T> (baseSize + 1);
188 for (long i = top; i < bottom; i++) {
195 public IEnumerable<T> GetEnumerable ()
197 return ((IEnumerable<T>)segment);