| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
/* |
| 4 |
* WARNING: Auto-generated file (7/18/2012 4:47:38 PM) |
| 5 |
* |
| 6 |
* Stripped down code based on ndp\clr\src\BCL\System\Collections\Concurrent\ConcurrentQueue.cs |
| 7 |
*/ |
| 8 |
|
| 9 |
#if NO_CDS_COLLECTIONS |
| 10 |
|
| 11 |
#pragma warning disable 0420 |
| 12 |
|
| 13 |
using System; |
| 14 |
using System.Collections.Generic; |
| 15 |
using System.Diagnostics.Contracts; |
| 16 |
using System.Threading; |
| 17 |
|
| 18 |
namespace System.Collections.Concurrent |
| 19 |
{ |
| 20 |
internal class ConcurrentQueue<T> |
| 21 |
{ |
| 22 |
private volatile Segment m_head; |
| 23 |
private volatile Segment m_tail; |
| 24 |
|
| 25 |
private const int SEGMENT_SIZE = 32; |
| 26 |
|
| 27 |
public ConcurrentQueue() |
| 28 |
{ |
| 29 |
m_head = m_tail = new Segment(0, this); |
| 30 |
} |
| 31 |
|
| 32 |
public bool IsEmpty |
| 33 |
{ |
| 34 |
get |
| 35 |
{ |
| 36 |
Segment head = m_head; |
| 37 |
if (!head.IsEmpty) |
| 38 |
//fast route 1: |
| 39 |
//if current head is not empty, then queue is not empty |
| 40 |
return false; |
| 41 |
else if (head.Next == null) |
| 42 |
//fast route 2: |
| 43 |
//if current head is empty and it's the last segment |
| 44 |
//then queue is empty |
| 45 |
return true; |
| 46 |
else |
| 47 |
//slow route: |
| 48 |
//current head is empty and it is NOT the last segment, |
| 49 |
//it means another thread is growing new segment |
| 50 |
{ |
| 51 |
SpinWait spin = new SpinWait(); |
| 52 |
while (head.IsEmpty) |
| 53 |
{ |
| 54 |
if (head.Next == null) |
| 55 |
return true; |
| 56 |
|
| 57 |
spin.SpinOnce(); |
| 58 |
head = m_head; |
| 59 |
} |
| 60 |
return false; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public void Enqueue(T item) |
| 66 |
{ |
| 67 |
SpinWait spin = new SpinWait(); |
| 68 |
while (true) |
| 69 |
{ |
| 70 |
Segment tail = m_tail; |
| 71 |
if (tail.TryAppend(item)) |
| 72 |
return; |
| 73 |
spin.SpinOnce(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public bool TryDequeue(out T result) |
| 78 |
{ |
| 79 |
while (!IsEmpty) |
| 80 |
{ |
| 81 |
Segment head = m_head; |
| 82 |
if (head.TryRemove(out result)) |
| 83 |
return true; |
| 84 |
//since method IsEmpty spins, we don't need to spin in the while loop |
| 85 |
} |
| 86 |
result = default(T); |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
private class Segment |
| 91 |
{ |
| 92 |
//we define two volatile arrays: m_array and m_state. Note that the accesses to the array items |
| 93 |
//do not get volatile treatment. But we don't need to worry about loading adjacent elements or |
| 94 |
//store/load on adjacent elements would suffer reordering. |
| 95 |
// - Two stores: these are at risk, but CLRv2 memory model guarantees store-release hence we are safe. |
| 96 |
// - Two loads: because one item from two volatile arrays are accessed, the loads of the array references |
| 97 |
// are sufficient to prevent reordering of the loads of the elements. |
| 98 |
internal volatile T[] m_array; |
| 99 |
|
| 100 |
// For each entry in m_array, the corresponding entry in m_state indicates whether this position contains |
| 101 |
// a valid value. m_state is initially all false. |
| 102 |
internal volatile VolatileBool[] m_state; |
| 103 |
|
| 104 |
//pointer to the next segment. null if the current segment is the last segment |
| 105 |
private volatile Segment m_next; |
| 106 |
|
| 107 |
//We use this zero based index to track how many segments have been created for the queue, and |
| 108 |
//to compute how many active segments are there currently. |
| 109 |
// * The number of currently active segments is : m_tail.m_index - m_head.m_index + 1; |
| 110 |
// * m_index is incremented with every Segment.Grow operation. We use Int64 type, and we can safely |
| 111 |
// assume that it never overflows. To overflow, we need to do 2^63 increments, even at a rate of 4 |
| 112 |
// billion (2^32) increments per second, it takes 2^31 seconds, which is about 64 years. |
| 113 |
internal readonly long m_index; |
| 114 |
|
| 115 |
//indices of where the first and last valid values |
| 116 |
// - m_low points to the position of the next element to pop from this segment, range [0, infinity) |
| 117 |
// m_low >= SEGMENT_SIZE implies the segment is disposable |
| 118 |
// - m_high points to the position of the latest pushed element, range [-1, infinity) |
| 119 |
// m_high == -1 implies the segment is new and empty |
| 120 |
// m_high >= SEGMENT_SIZE-1 means this segment is ready to grow. |
| 121 |
// and the thread who sets m_high to SEGMENT_SIZE-1 is responsible to grow the segment |
| 122 |
// - Math.Min(m_low, SEGMENT_SIZE) > Math.Min(m_high, SEGMENT_SIZE-1) implies segment is empty |
| 123 |
// - initially m_low =0 and m_high=-1; |
| 124 |
private volatile int m_low; |
| 125 |
private volatile int m_high; |
| 126 |
|
| 127 |
private volatile ConcurrentQueue<T> m_source; |
| 128 |
|
| 129 |
internal Segment(long index, ConcurrentQueue<T> source) |
| 130 |
{ |
| 131 |
m_array = new T[SEGMENT_SIZE]; |
| 132 |
m_state = new VolatileBool[SEGMENT_SIZE]; //all initialized to false |
| 133 |
m_high = -1; |
| 134 |
Contract.Assert(index >= 0); |
| 135 |
m_index = index; |
| 136 |
m_source = source; |
| 137 |
} |
| 138 |
|
| 139 |
internal Segment Next |
| 140 |
{ |
| 141 |
get { return m_next; } |
| 142 |
} |
| 143 |
|
| 144 |
internal bool IsEmpty |
| 145 |
{ |
| 146 |
get { return (Low > High); } |
| 147 |
} |
| 148 |
|
| 149 |
internal void UnsafeAdd(T value) |
| 150 |
{ |
| 151 |
Contract.Assert(m_high < SEGMENT_SIZE - 1); |
| 152 |
m_high++; |
| 153 |
m_array[m_high] = value; |
| 154 |
m_state[m_high].m_value = true; |
| 155 |
} |
| 156 |
|
| 157 |
internal Segment UnsafeGrow() |
| 158 |
{ |
| 159 |
Contract.Assert(m_high >= SEGMENT_SIZE - 1); |
| 160 |
Segment newSegment = new Segment(m_index + 1, m_source); //m_index is Int64, we don't need to worry about overflow |
| 161 |
m_next = newSegment; |
| 162 |
return newSegment; |
| 163 |
} |
| 164 |
|
| 165 |
internal void Grow() |
| 166 |
{ |
| 167 |
//no CAS is needed, since there is no contention (other threads are blocked, busy waiting) |
| 168 |
Segment newSegment = new Segment(m_index + 1, m_source); //m_index is Int64, we don't need to worry about overflow |
| 169 |
m_next = newSegment; |
| 170 |
Contract.Assert(m_source.m_tail == this); |
| 171 |
m_source.m_tail = m_next; |
| 172 |
} |
| 173 |
|
| 174 |
internal bool TryAppend(T value) |
| 175 |
{ |
| 176 |
//quickly check if m_high is already over the boundary, if so, bail out |
| 177 |
if (m_high >= SEGMENT_SIZE - 1) |
| 178 |
{ |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
//Now we will use a CAS to increment m_high, and store the result in newhigh. |
| 183 |
//Depending on how many free spots left in this segment and how many threads are doing this Increment |
| 184 |
//at this time, the returning "newhigh" can be |
| 185 |
// 1) < SEGMENT_SIZE - 1 : we took a spot in this segment, and not the last one, just insert the value |
| 186 |
// 2) == SEGMENT_SIZE - 1 : we took the last spot, insert the value AND grow the segment |
| 187 |
// 3) > SEGMENT_SIZE - 1 : we failed to reserve a spot in this segment, we return false to |
| 188 |
// Queue.Enqueue method, telling it to try again in the next segment. |
| 189 |
|
| 190 |
int newhigh = SEGMENT_SIZE; //initial value set to be over the boundary |
| 191 |
|
| 192 |
//We need do Interlocked.Increment and value/state update in a finally block to ensure that they run |
| 193 |
//without interuption. This is to prevent anything from happening between them, and another dequeue |
| 194 |
//thread maybe spinning forever to wait for m_state[] to be true; |
| 195 |
try |
| 196 |
{ } |
| 197 |
finally |
| 198 |
{ |
| 199 |
newhigh = Interlocked.Increment(ref m_high); |
| 200 |
if (newhigh <= SEGMENT_SIZE - 1) |
| 201 |
{ |
| 202 |
m_array[newhigh] = value; |
| 203 |
m_state[newhigh].m_value = true; |
| 204 |
} |
| 205 |
|
| 206 |
//if this thread takes up the last slot in the segment, then this thread is responsible |
| 207 |
//to grow a new segment. Calling Grow must be in the finally block too for reliability reason: |
| 208 |
//if thread abort during Grow, other threads will be left busy spinning forever. |
| 209 |
if (newhigh == SEGMENT_SIZE - 1) |
| 210 |
{ |
| 211 |
Grow(); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
//if newhigh <= SEGMENT_SIZE-1, it means the current thread successfully takes up a spot |
| 216 |
return newhigh <= SEGMENT_SIZE - 1; |
| 217 |
} |
| 218 |
|
| 219 |
internal bool TryRemove(out T result) |
| 220 |
{ |
| 221 |
SpinWait spin = new SpinWait(); |
| 222 |
int lowLocal = Low, highLocal = High; |
| 223 |
while (lowLocal <= highLocal) |
| 224 |
{ |
| 225 |
//try to update m_low |
| 226 |
if (Interlocked.CompareExchange(ref m_low, lowLocal + 1, lowLocal) == lowLocal) |
| 227 |
{ |
| 228 |
//if the specified value is not available (this spot is taken by a push operation, |
| 229 |
// but the value is not written into yet), then spin |
| 230 |
SpinWait spinLocal = new SpinWait(); |
| 231 |
while (!m_state[lowLocal].m_value) |
| 232 |
{ |
| 233 |
spinLocal.SpinOnce(); |
| 234 |
} |
| 235 |
result = m_array[lowLocal]; |
| 236 |
m_array[lowLocal] = default(T); //release the reference to the object. |
| 237 |
|
| 238 |
//if the current thread sets m_low to SEGMENT_SIZE, which means the current segment becomes |
| 239 |
//disposable, then this thread is responsible to dispose this segment, and reset m_head |
| 240 |
if (lowLocal + 1 >= SEGMENT_SIZE) |
| 241 |
{ |
| 242 |
// Invariant: we only dispose the current m_head, not any other segment |
| 243 |
// In usual situation, disposing a segment is simply seting m_head to m_head.m_next |
| 244 |
// But there is one special case, where m_head and m_tail points to the same and ONLY |
| 245 |
//segment of the queue: Another thread A is doing Enqueue and finds that it needs to grow, |
| 246 |
//while the *current* thread is doing *this* Dequeue operation, and finds that it needs to |
| 247 |
//dispose the current (and ONLY) segment. Then we need to wait till thread A finishes its |
| 248 |
//Grow operation, this is the reason of having the following while loop |
| 249 |
spinLocal = new SpinWait(); |
| 250 |
while (m_next == null) |
| 251 |
{ |
| 252 |
spinLocal.SpinOnce(); |
| 253 |
} |
| 254 |
Contract.Assert(m_source.m_head == this); |
| 255 |
m_source.m_head = m_next; |
| 256 |
} |
| 257 |
return true; |
| 258 |
} |
| 259 |
else |
| 260 |
{ |
| 261 |
//CAS failed due to contention: spin briefly and retry |
| 262 |
spin.SpinOnce(); |
| 263 |
lowLocal = Low; highLocal = High; |
| 264 |
} |
| 265 |
}//end of while |
| 266 |
result = default(T); |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
internal bool TryPeek(out T result) |
| 271 |
{ |
| 272 |
result = default(T); |
| 273 |
int lowLocal = Low; |
| 274 |
if (lowLocal > High) |
| 275 |
return false; |
| 276 |
SpinWait spin = new SpinWait(); |
| 277 |
while (!m_state[lowLocal].m_value) |
| 278 |
{ |
| 279 |
spin.SpinOnce(); |
| 280 |
} |
| 281 |
result = m_array[lowLocal]; |
| 282 |
return true; |
| 283 |
} |
| 284 |
|
| 285 |
internal int Low |
| 286 |
{ |
| 287 |
get |
| 288 |
{ |
| 289 |
return Math.Min(m_low, SEGMENT_SIZE); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
internal int High |
| 294 |
{ |
| 295 |
get |
| 296 |
{ |
| 297 |
//if m_high > SEGMENT_SIZE, it means it's out of range, we should return |
| 298 |
//SEGMENT_SIZE-1 as the logical position |
| 299 |
return Math.Min(m_high, SEGMENT_SIZE - 1); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
} |
| 304 |
}//end of class Segment |
| 305 |
|
| 306 |
struct VolatileBool |
| 307 |
{ |
| 308 |
public VolatileBool(bool value) |
| 309 |
{ |
| 310 |
m_value = value; |
| 311 |
} |
| 312 |
public volatile bool m_value; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
#endif |