-
Notifications
You must be signed in to change notification settings - Fork 554
/
MpmcUnboundedXaddArrayQueue.java
454 lines (423 loc) · 15.1 KB
/
MpmcUnboundedXaddArrayQueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jctools.queues;
/**
* An MPMC array queue which starts at <i>initialCapacity</i> and grows unbounded in linked chunks.<br>
* Differently from {@link MpmcArrayQueue} it is designed to provide a better scaling when more
* producers are concurrently offering.
*
* @author https://github.com/franz1981
*/
public class MpmcUnboundedXaddArrayQueue<E> extends MpUnboundedXaddArrayQueue<MpmcUnboundedXaddChunk<E>, E>
{
/**
* @param chunkSize The buffer size to be used in each chunk of this queue
* @param maxPooledChunks The maximum number of reused chunks kept around to avoid allocation, chunks are pre-allocated
*/
public MpmcUnboundedXaddArrayQueue(int chunkSize, int maxPooledChunks)
{
super(chunkSize, maxPooledChunks);
}
public MpmcUnboundedXaddArrayQueue(int chunkSize)
{
this(chunkSize, 2);
}
@Override
final MpmcUnboundedXaddChunk<E> newChunk(long index, MpmcUnboundedXaddChunk<E> prev, int chunkSize, boolean pooled)
{
return new MpmcUnboundedXaddChunk(index, prev, chunkSize, pooled);
}
@Override
public boolean offer(E e)
{
if (null == e)
{
throw new NullPointerException();
}
final int chunkMask = this.chunkMask;
final int chunkShift = this.chunkShift;
final long pIndex = getAndIncrementProducerIndex();
final int piChunkOffset = (int) (pIndex & chunkMask);
final long piChunkIndex = pIndex >> chunkShift;
MpmcUnboundedXaddChunk<E> pChunk = lvProducerChunk();
if (pChunk.lvIndex() != piChunkIndex)
{
// Other producers may have advanced the producer chunk as we claimed a slot in a prev chunk, or we may have
// now stepped into a brand new chunk which needs appending.
pChunk = producerChunkForIndex(pChunk, piChunkIndex);
}
final boolean isPooled = pChunk.isPooled();
if (isPooled)
{
// wait any previous consumer to finish its job
pChunk.spinForElement(piChunkOffset, true);
}
pChunk.soElement(piChunkOffset, e);
if (isPooled)
{
pChunk.soSequence(piChunkOffset, piChunkIndex);
}
return true;
}
@Override
public E poll()
{
final int chunkMask = this.chunkMask;
final int chunkShift = this.chunkShift;
long cIndex;
MpmcUnboundedXaddChunk<E> cChunk;
int ciChunkOffset;
boolean isFirstElementOfNextChunk;
boolean pooled = false;
E e = null;
MpmcUnboundedXaddChunk<E> next = null;
long pIndex = -1; // start with bogus value, hope we don't need it
long ciChunkIndex;
while (true)
{
cIndex = this.lvConsumerIndex();
// chunk is in sync with the index, and is safe to mutate after CAS of index (because we pre-verify it
// matched the indicate ciChunkIndex)
cChunk = this.lvConsumerChunk();
ciChunkOffset = (int) (cIndex & chunkMask);
ciChunkIndex = cIndex >> chunkShift;
final long ccChunkIndex = cChunk.lvIndex();
isFirstElementOfNextChunk = ciChunkOffset == 0 && cIndex != 0;
if (isFirstElementOfNextChunk) {
if (ciChunkIndex - ccChunkIndex != 1)
{
continue;
}
next = cChunk.lvNext();
// next could have been modified by another racing consumer, but:
// - if null: it still needs to check q empty + casConsumerIndex
// - if !null: it will fail on casConsumerIndex
if (next == null)
{
if (cIndex >= pIndex && // test against cached pIndex
cIndex == (pIndex = lvProducerIndex())) // update pIndex if we must
{
// strict empty check, this ensures [Queue.poll() == null iff isEmpty()]
return null;
}
// we will go ahead with the CAS and have the winning consumer spin for the next buffer
}
// not empty: can attempt the cas (and transition to next chunk if successful)
if (casConsumerIndex(cIndex, cIndex + 1))
{
break;
}
continue;
}
if (ccChunkIndex > ciChunkIndex)
{
//stale view of the world
continue;
}
// mid chunk elements
assert !isFirstElementOfNextChunk && ccChunkIndex <= ciChunkIndex;
pooled = cChunk.isPooled();
if (ccChunkIndex == ciChunkIndex)
{
if (pooled)
{
// Pooled chunks need a stronger guarantee than just element null checking in case of a stale view
// on a reused entry where a racing consumer has grabbed the slot but not yet null-ed it out and a
// producer has not yet set it to the new value.
final long sequence = cChunk.lvSequence(ciChunkOffset);
if (sequence == ciChunkIndex)
{
if (casConsumerIndex(cIndex, cIndex + 1))
{
break;
}
continue;
}
if (sequence > ciChunkIndex)
{
//stale view of the world
continue;
}
// sequence < ciChunkIndex: element yet to be set?
}
else
{
e = cChunk.lvElement(ciChunkOffset);
if (e != null)
{
if (casConsumerIndex(cIndex, cIndex + 1))
{
break;
}
continue;
}
// e == null: element yet to be set?
}
}
// ccChunkIndex < ciChunkIndex || e == null || sequence < ciChunkIndex:
if (cIndex >= pIndex && // test against cached pIndex
cIndex == (pIndex = lvProducerIndex())) // update pIndex if we must
{
// strict empty check, this ensures [Queue.poll() == null iff isEmpty()]
return null;
}
}
// if we are the isFirstElementOfNextChunk we need to get the consumer chunk
if (isFirstElementOfNextChunk)
{
e = linkNextConsumerChunkAndPoll(cChunk, next, ciChunkIndex);
}
else
{
if (pooled)
{
e = cChunk.lvElement(ciChunkOffset);
}
assert !cChunk.isPooled() || (cChunk.isPooled() && cChunk.lvSequence(ciChunkOffset) == ciChunkIndex);
cChunk.soElement(ciChunkOffset, null);
}
return e;
}
private E linkNextConsumerChunkAndPoll(
MpmcUnboundedXaddChunk<E> cChunk,
MpmcUnboundedXaddChunk<E> next,
long expectedChunkIndex)
{
while (next == null)
{
next = cChunk.lvNext();
}
// we can freely spin awaiting producer, because we are the only one in charge to
// rotate the consumer buffer and use next
final E e = next.spinForElement(0, false);
final boolean pooled = next.isPooled();
if (pooled)
{
next.spinForSequence(0, expectedChunkIndex);
}
next.soElement(0, null);
moveToNextConsumerChunk(cChunk, next);
return e;
}
@Override
public E peek()
{
final int chunkMask = this.chunkMask;
final int chunkShift = this.chunkShift;
long cIndex;
E e;
do
{
e = null;
cIndex = this.lvConsumerIndex();
MpmcUnboundedXaddChunk<E> cChunk = this.lvConsumerChunk();
final int ciChunkOffset = (int) (cIndex & chunkMask);
final long ciChunkIndex = cIndex >> chunkShift;
final boolean firstElementOfNewChunk = ciChunkOffset == 0 && cIndex != 0;
if (firstElementOfNewChunk)
{
final long expectedChunkIndex = ciChunkIndex - 1;
if (expectedChunkIndex != cChunk.lvIndex())
{
continue;
}
final MpmcUnboundedXaddChunk<E> next = cChunk.lvNext();
if (next == null)
{
continue;
}
cChunk = next;
}
if (cChunk.isPooled())
{
if (cChunk.lvSequence(ciChunkOffset) != ciChunkIndex)
{
continue;
}
} else {
if (cChunk.lvIndex() != ciChunkIndex)
{
continue;
}
}
e = cChunk.lvElement(ciChunkOffset);
}
while (e == null && cIndex != lvProducerIndex());
return e;
}
@Override
public E relaxedPoll()
{
final int chunkMask = this.chunkMask;
final int chunkShift = this.chunkShift;
final long cIndex = this.lvConsumerIndex();
final MpmcUnboundedXaddChunk<E> cChunk = this.lvConsumerChunk();
final int ciChunkOffset = (int) (cIndex & chunkMask);
final long ciChunkIndex = cIndex >> chunkShift;
final boolean firstElementOfNewChunk = ciChunkOffset == 0 && cIndex != 0;
if (firstElementOfNewChunk)
{
final long expectedChunkIndex = ciChunkIndex - 1;
final MpmcUnboundedXaddChunk<E> next;
final long ccChunkIndex = cChunk.lvIndex();
if (expectedChunkIndex != ccChunkIndex || (next = cChunk.lvNext()) == null)
{
return null;
}
E e = null;
final boolean pooled = next.isPooled();
if (pooled)
{
if (next.lvSequence(ciChunkOffset) != ciChunkIndex)
{
return null;
}
}
else
{
e = next.lvElement(ciChunkOffset);
if (e == null)
{
return null;
}
}
if (!casConsumerIndex(cIndex, cIndex + 1))
{
return null;
}
if (pooled)
{
e = next.lvElement(ciChunkOffset);
}
assert e != null;
next.soElement(0, null);
moveToNextConsumerChunk(cChunk, next);
return e;
}
else
{
final boolean pooled = cChunk.isPooled();
E e = null;
if (pooled)
{
final long sequence = cChunk.lvSequence(ciChunkOffset);
if (sequence != ciChunkIndex)
{
return null;
}
}
else
{
final long ccChunkIndex = cChunk.lvIndex();
if (ccChunkIndex != ciChunkIndex || (e = cChunk.lvElement(ciChunkOffset)) == null)
{
return null;
}
}
if (!casConsumerIndex(cIndex, cIndex + 1))
{
return null;
}
if (pooled)
{
e = cChunk.lvElement(ciChunkOffset);
assert e != null;
}
assert !pooled || (pooled && cChunk.lvSequence(ciChunkOffset) == ciChunkIndex);
cChunk.soElement(ciChunkOffset, null);
return e;
}
}
@Override
public E relaxedPeek()
{
final int chunkMask = this.chunkMask;
final int chunkShift = this.chunkShift;
final long cIndex = this.lvConsumerIndex();
final int ciChunkOffset = (int) (cIndex & chunkMask);
final long ciChunkIndex = cIndex >> chunkShift;
MpmcUnboundedXaddChunk<E> consumerBuffer = this.lvConsumerChunk();
final int chunkSize = chunkMask + 1;
final boolean firstElementOfNewChunk = ciChunkOffset == 0 && cIndex >= chunkSize;
if (firstElementOfNewChunk)
{
final long expectedChunkIndex = ciChunkIndex - 1;
if (expectedChunkIndex != consumerBuffer.lvIndex())
{
return null;
}
final MpmcUnboundedXaddChunk<E> next = consumerBuffer.lvNext();
if (next == null)
{
return null;
}
consumerBuffer = next;
}
if (consumerBuffer.isPooled())
{
if (consumerBuffer.lvSequence(ciChunkOffset) != ciChunkIndex)
{
return null;
}
}
else
{
if (consumerBuffer.lvIndex() != ciChunkIndex)
{
return null;
}
}
return consumerBuffer.lvElement(ciChunkOffset);
}
@Override
public int fill(Supplier<E> s, int limit)
{
if (null == s)
throw new IllegalArgumentException("supplier is null");
if (limit < 0)
throw new IllegalArgumentException("limit is negative:" + limit);
if (limit == 0)
return 0;
final int chunkShift = this.chunkShift;
final int chunkMask = this.chunkMask;
long producerSeq = getAndAddProducerIndex(limit);
MpmcUnboundedXaddChunk<E> producerBuffer = null;
for (int i = 0; i < limit; i++)
{
final int pOffset = (int) (producerSeq & chunkMask);
long chunkIndex = producerSeq >> chunkShift;
if (producerBuffer == null || producerBuffer.lvIndex() != chunkIndex)
{
producerBuffer = producerChunkForIndex(producerBuffer, chunkIndex);
if (producerBuffer.isPooled())
{
chunkIndex = producerBuffer.lvIndex();
}
}
if (producerBuffer.isPooled())
{
while (producerBuffer.lvElement(pOffset) != null)
{
}
}
producerBuffer.soElement(pOffset, s.get());
if (producerBuffer.isPooled())
{
producerBuffer.soSequence(pOffset, chunkIndex);
}
producerSeq++;
}
return limit;
}
}