Few 4.0 profile corcompare changes
[mono.git] / mcs / class / corlib / System.Threading.Tasks / CyclicDeque.cs
1 // 
2 // CyclicDeque.cs
3 //  
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // 
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
8 // 
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:
15 // 
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 // 
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
25 // THE SOFTWARE.
26
27 #if NET_4_0 || MOBILE
28
29 using System;
30 using System.Collections.Generic;
31 using System.Threading;
32
33 #if INSIDE_MONO_PARALLEL
34 namespace Mono.Threading.Tasks
35 #else
36 namespace System.Threading.Tasks
37 #endif
38 {
39 #if INSIDE_MONO_PARALLEL
40         public
41 #endif
42         enum PopResult  {
43                 Succeed,
44                 Empty,
45                 Abort
46         }
47
48 #if INSIDE_MONO_PARALLEL
49         public
50 #endif
51         interface IConcurrentDeque<T>
52         {
53                 void PushBottom (T obj);
54                 PopResult PopBottom (out T obj);
55                 PopResult PopTop (out T obj);
56                 IEnumerable<T> GetEnumerable ();
57         }
58
59 #if INSIDE_MONO_PARALLEL
60         public
61 #endif
62         class CyclicDeque<T> : IConcurrentDeque<T>
63         {
64                 const int BaseSize = 11;
65                 
66                 long bottom;
67                 long top;
68                 long upperBound;
69                 CircularArray<T> array = new CircularArray<T> (BaseSize);
70                 
71                 public void PushBottom (T obj)
72                 {
73                         /* Read is implemented as a simple load operation on 64bits
74                          * so no need to make the distinction ourselves
75                          */
76                         long b = Interlocked.Read (ref bottom);
77                         var a = array;
78                         
79                         // Take care of growing
80                         if (b - upperBound >= a.Size - 1) {
81                                 upperBound = Interlocked.Read (ref top);
82                                 a = a.Grow (b, upperBound);
83                                 array = a;
84                         }
85                         
86                         // Register the new value
87                         a.segment[b % a.size] = obj;
88                         Interlocked.Increment (ref bottom);
89                 }
90                 
91                 public PopResult PopBottom (out T obj)
92                 {
93                         obj = default (T);
94                         
95                         long b = Interlocked.Decrement (ref bottom);
96                         var a = array;
97                         long t = Interlocked.Read (ref top);
98                         long size = b - t;
99                         
100                         if (size < 0) {
101                                 // Set bottom to t
102                                 Interlocked.Add (ref bottom, t - b);
103                                 return PopResult.Empty;
104                         }
105                         
106                         obj = a.segment[b % a.size];
107                         if (size > 0)
108                                 return PopResult.Succeed;
109                         Interlocked.Add (ref bottom, t + 1 - b);
110                         
111                         if (Interlocked.CompareExchange (ref top, t + 1, t) != t)
112                                 return PopResult.Empty;
113                         
114                         return PopResult.Succeed;
115                 }
116                 
117                 public PopResult PopTop (out T obj)
118                 {
119                         obj = default (T);
120                         
121                         long t = Interlocked.Read (ref top);
122                         long b = Interlocked.Read (ref bottom);
123                         
124                         if (b - t <= 0)
125                                 return PopResult.Empty;
126                         
127                         if (Interlocked.CompareExchange (ref top, t + 1, t) != t)
128                                 return PopResult.Abort;
129                         
130                         var a = array;
131                         obj = a.segment[t % a.size];
132                         
133                         return PopResult.Succeed;
134                 }
135                 
136                 public IEnumerable<T> GetEnumerable ()
137                 {
138                         var a = array;
139                         return a.GetEnumerable (bottom, ref top);
140                 }
141         }
142         
143         internal class CircularArray<T>
144         {
145                 readonly int baseSize;
146                 public readonly int size;
147                 public readonly T[] segment;
148                 
149                 public CircularArray (int baseSize)
150                 {
151                         this.baseSize = baseSize;
152                         this.size = 1 << baseSize;
153                         this.segment = new T[size];
154                 }
155                 
156                 public long Size {
157                         get {
158                                 return size;
159                         }
160                 }
161                 
162                 public T this[long index] {
163                         get {
164                                 return segment[index % size];
165                         }
166                         set {
167                                 segment[index % size] = value;
168                         }
169                 }
170                 
171                 public CircularArray<T> Grow (long bottom, long top)
172                 {
173                         var grow = new CircularArray<T> (baseSize + 1);
174                         
175                         for (long i = top; i < bottom; i++) {
176                                 grow.segment[i] = segment[i % size];
177                         }
178                         
179                         return grow;
180                 }
181                 
182                 public IEnumerable<T> GetEnumerable (long bottom, ref long top)
183                 {
184                         long instantTop = top;
185                         T[] slice = new T[bottom - instantTop];
186                         int destIndex = -1;
187                         for (long i = instantTop; i < bottom; i++)
188                                 slice[++destIndex] = segment[i % size];
189
190                         return RealGetEnumerable (slice, bottom, top, instantTop);
191                 }
192
193                 IEnumerable<T> RealGetEnumerable (T[] slice, long bottom, long realTop, long initialTop)
194                 {
195                         int destIndex = (int)(realTop - initialTop - 1);
196                         for (long i = realTop; i < bottom; ++i)
197                                 yield return slice[++destIndex];
198                 }
199         }
200 }
201 #endif