-
Notifications
You must be signed in to change notification settings - Fork 554
/
MpscBlockingConsumerArrayQueue.java
858 lines (751 loc) · 27.4 KB
/
MpscBlockingConsumerArrayQueue.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
/*
* 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;
import java.util.AbstractQueue;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import org.jctools.queues.IndexedQueueSizeUtil.IndexedQueue;
import org.jctools.util.Pow2;
import org.jctools.util.RangeUtil;
import static org.jctools.queues.LinkedArrayQueueUtil.modifiedCalcCircularRefElementOffset;
import static org.jctools.util.UnsafeAccess.UNSAFE;
import static org.jctools.util.UnsafeAccess.fieldOffset;
import static org.jctools.util.UnsafeRefArrayAccess.*;
@SuppressWarnings("unused")
abstract class MpscBlockingConsumerArrayQueuePad1<E> extends AbstractQueue<E> implements IndexedQueue
{
byte b000,b001,b002,b003,b004,b005,b006,b007;// 8b
byte b010,b011,b012,b013,b014,b015,b016,b017;// 16b
byte b020,b021,b022,b023,b024,b025,b026,b027;// 24b
byte b030,b031,b032,b033,b034,b035,b036,b037;// 32b
byte b040,b041,b042,b043,b044,b045,b046,b047;// 40b
byte b050,b051,b052,b053,b054,b055,b056,b057;// 48b
byte b060,b061,b062,b063,b064,b065,b066,b067;// 56b
byte b070,b071,b072,b073,b074,b075,b076,b077;// 64b
byte b100,b101,b102,b103,b104,b105,b106,b107;// 72b
byte b110,b111,b112,b113,b114,b115,b116,b117;// 80b
byte b120,b121,b122,b123,b124,b125,b126,b127;// 88b
byte b130,b131,b132,b133,b134,b135,b136,b137;// 96b
byte b140,b141,b142,b143,b144,b145,b146,b147;//104b
byte b150,b151,b152,b153,b154,b155,b156,b157;//112b
byte b160,b161,b162,b163,b164,b165,b166,b167;//120b
byte b170,b171,b172,b173,b174,b175,b176,b177;//128b
}
// $gen:ordered-fields
abstract class MpscBlockingConsumerArrayQueueColdProducerFields<E> extends MpscBlockingConsumerArrayQueuePad1<E>
{
private final static long P_LIMIT_OFFSET = fieldOffset(MpscBlockingConsumerArrayQueueColdProducerFields.class,"producerLimit");
private volatile long producerLimit;
protected final long producerMask;
protected final E[] producerBuffer;
MpscBlockingConsumerArrayQueueColdProducerFields(long producerMask, E[] producerBuffer)
{
this.producerMask = producerMask;
this.producerBuffer = producerBuffer;
}
final long lvProducerLimit()
{
return producerLimit;
}
final boolean casProducerLimit(long expect, long newValue)
{
return UNSAFE.compareAndSwapLong(this, P_LIMIT_OFFSET, expect, newValue);
}
final void soProducerLimit(long newValue)
{
UNSAFE.putOrderedLong(this, P_LIMIT_OFFSET, newValue);
}
}
@SuppressWarnings("unused")
abstract class MpscBlockingConsumerArrayQueuePad2<E> extends MpscBlockingConsumerArrayQueueColdProducerFields<E>
{
byte b000,b001,b002,b003,b004,b005,b006,b007;// 8b
byte b010,b011,b012,b013,b014,b015,b016,b017;// 16b
byte b020,b021,b022,b023,b024,b025,b026,b027;// 24b
byte b030,b031,b032,b033,b034,b035,b036,b037;// 32b
byte b040,b041,b042,b043,b044,b045,b046,b047;// 40b
byte b050,b051,b052,b053,b054,b055,b056,b057;// 48b
byte b060,b061,b062,b063,b064,b065,b066,b067;// 56b
// byte b070,b071,b072,b073,b074,b075,b076,b077;// 64b
MpscBlockingConsumerArrayQueuePad2(long mask, E[] buffer)
{
super(mask, buffer);
}
}
// $gen:ordered-fields
abstract class MpscBlockingConsumerArrayQueueProducerFields<E> extends MpscBlockingConsumerArrayQueuePad2<E>
{
private final static long P_INDEX_OFFSET = fieldOffset(MpscBlockingConsumerArrayQueueProducerFields.class, "producerIndex");
private volatile long producerIndex;
MpscBlockingConsumerArrayQueueProducerFields(long mask, E[] buffer)
{
super(mask, buffer);
}
@Override
public final long lvProducerIndex()
{
return producerIndex;
}
final void soProducerIndex(long newValue)
{
UNSAFE.putOrderedLong(this, P_INDEX_OFFSET, newValue);
}
final boolean casProducerIndex(long expect, long newValue)
{
return UNSAFE.compareAndSwapLong(this, P_INDEX_OFFSET, expect, newValue);
}
}
@SuppressWarnings("unused")
abstract class MpscBlockingConsumerArrayQueuePad3<E> extends MpscBlockingConsumerArrayQueueProducerFields<E>
{
byte b000,b001,b002,b003,b004,b005,b006,b007;// 8b
byte b010,b011,b012,b013,b014,b015,b016,b017;// 16b
byte b020,b021,b022,b023,b024,b025,b026,b027;// 24b
byte b030,b031,b032,b033,b034,b035,b036,b037;// 32b
byte b040,b041,b042,b043,b044,b045,b046,b047;// 40b
byte b050,b051,b052,b053,b054,b055,b056,b057;// 48b
byte b060,b061,b062,b063,b064,b065,b066,b067;// 56b
byte b070,b071,b072,b073,b074,b075,b076,b077;// 64b
byte b100,b101,b102,b103,b104,b105,b106,b107;// 72b
byte b110,b111,b112,b113,b114,b115,b116,b117;// 80b
byte b120,b121,b122,b123,b124,b125,b126,b127;// 88b
byte b130,b131,b132,b133,b134,b135,b136,b137;// 96b
byte b140,b141,b142,b143,b144,b145,b146,b147;//104b
byte b150,b151,b152,b153,b154,b155,b156,b157;//112b
byte b160,b161,b162,b163,b164,b165,b166,b167;//120b
byte b170,b171,b172,b173,b174,b175,b176,b177;//128b
MpscBlockingConsumerArrayQueuePad3(long mask, E[] buffer)
{
super(mask, buffer);
}
}
// $gen:ordered-fields
abstract class MpscBlockingConsumerArrayQueueConsumerFields<E> extends MpscBlockingConsumerArrayQueuePad3<E>
{
private final static long C_INDEX_OFFSET = fieldOffset(MpscBlockingConsumerArrayQueueConsumerFields.class,"consumerIndex");
private final static long BLOCKED_OFFSET = fieldOffset(MpscBlockingConsumerArrayQueueConsumerFields.class,"blocked");
private volatile long consumerIndex;
protected final long consumerMask;
private volatile Thread blocked;
protected final E[] consumerBuffer;
MpscBlockingConsumerArrayQueueConsumerFields(long mask, E[] buffer)
{
super(mask, buffer);
consumerMask = mask;
consumerBuffer = buffer;
}
@Override
public final long lvConsumerIndex()
{
return consumerIndex;
}
final long lpConsumerIndex()
{
return UNSAFE.getLong(this, C_INDEX_OFFSET);
}
final void soConsumerIndex(long newValue)
{
UNSAFE.putOrderedLong(this, C_INDEX_OFFSET, newValue);
}
final Thread lvBlocked()
{
return blocked;
}
final void soBlocked(Thread thread)
{
UNSAFE.putOrderedObject(this, BLOCKED_OFFSET, thread);
}
}
/**
* This is a partial implementation of the {@link java.util.concurrent.BlockingQueue} on the consumer side only on top
* of the mechanics described in {@link BaseMpscLinkedArrayQueue}, but with the reservation bit used for blocking rather
* than resizing in this instance.
*/
@SuppressWarnings("unused")
public class MpscBlockingConsumerArrayQueue<E> extends MpscBlockingConsumerArrayQueueConsumerFields<E>
implements MessagePassingQueue<E>, QueueProgressIndicators, BlockingQueue<E>
{
byte b000,b001,b002,b003,b004,b005,b006,b007;// 8b
byte b010,b011,b012,b013,b014,b015,b016,b017;// 16b
byte b020,b021,b022,b023,b024,b025,b026,b027;// 24b
byte b030,b031,b032,b033,b034,b035,b036,b037;// 32b
byte b040,b041,b042,b043,b044,b045,b046,b047;// 40b
byte b050,b051,b052,b053,b054,b055,b056,b057;// 48b
byte b060,b061,b062,b063,b064,b065,b066,b067;// 56b
byte b070,b071,b072,b073,b074,b075,b076,b077;// 64b
byte b100,b101,b102,b103,b104,b105,b106,b107;// 72b
byte b110,b111,b112,b113,b114,b115,b116,b117;// 80b
byte b120,b121,b122,b123,b124,b125,b126,b127;// 88b
byte b130,b131,b132,b133,b134,b135,b136,b137;// 96b
byte b140,b141,b142,b143,b144,b145,b146,b147;//104b
byte b150,b151,b152,b153,b154,b155,b156,b157;//112b
byte b160,b161,b162,b163,b164,b165,b166,b167;//120b
byte b170,b171,b172,b173,b174,b175,b176,b177;//128b
public MpscBlockingConsumerArrayQueue(final int capacity)
{
// leave lower bit of mask clear
super((Pow2.roundToPowerOfTwo(capacity) - 1) << 1, (E[]) allocateRefArray(Pow2.roundToPowerOfTwo(capacity)));
RangeUtil.checkGreaterThanOrEqual(capacity, 1, "capacity");
soProducerLimit((Pow2.roundToPowerOfTwo(capacity) - 1) << 1); // we know it's all empty to start with
}
@Override
public final Iterator<E> iterator()
{
throw new UnsupportedOperationException();
}
@Override
public final int size()
{
// NOTE: because indices are on even numbers we cannot use the size util.
/*
* It is possible for a thread to be interrupted or reschedule between the read of the producer and
* consumer indices, therefore protection is required to ensure size is within valid range. In the
* event of concurrent polls/offers to this method the size is OVER estimated as we read consumer
* index BEFORE the producer index.
*/
long after = lvConsumerIndex();
long size;
while (true)
{
final long before = after;
final long currentProducerIndex = lvProducerIndex();
after = lvConsumerIndex();
if (before == after)
{
size = ((currentProducerIndex - after) >> 1);
break;
}
}
// Long overflow is impossible, so size is always positive. Integer overflow is possible for the unbounded
// indexed queues.
if (size > Integer.MAX_VALUE)
{
return Integer.MAX_VALUE;
}
else
{
return (int) size;
}
}
@Override
public final boolean isEmpty()
{
// Order matters!
// Loading consumer before producer allows for producer increments after consumer index is read.
// This ensures this method is conservative in it's estimate. Note that as this is an MPMC there is
// nothing we can do to make this an exact method.
return ((this.lvConsumerIndex()/2) == (this.lvProducerIndex()/2));
}
@Override
public String toString()
{
return this.getClass().getName();
}
/**
* {@link #offer} if {@link #size()} is less than threshold.
*
* @param e the object to offer onto the queue, not null
* @param threshold the maximum allowable size
* @return true if the offer is successful, false if queue size exceeds threshold
* @since 3.0.1
*/
public boolean offerIfBelowThreshold(final E e, int threshold)
{
if (null == e)
{
throw new NullPointerException();
}
final long mask = this.producerMask;
final long capacity = mask + 2;
threshold = threshold << 1;
final E[] buffer = this.producerBuffer;
long pIndex;
while (true)
{
pIndex = lvProducerIndex();
// lower bit is indicative of blocked consumer
if ((pIndex & 1) == 1)
{
if (offerAndWakeup(buffer, mask, pIndex, e)) {
return true;
}
continue;
}
// pIndex is even (lower bit is 0) -> actual index is (pIndex >> 1), consumer is awake
final long producerLimit = lvProducerLimit();
// Use producer limit to save a read of the more rapidly mutated consumer index.
// Assumption: queue is usually empty or near empty
// available is also << 1
final long available = producerLimit - pIndex;
// sizeEstimate <= size
final long sizeEstimate = capacity - available;
if (sizeEstimate >= threshold ||
// producerLimit check allows for threshold >= capacity
producerLimit <= pIndex)
{
if (!recalculateProducerLimit(pIndex, producerLimit, lvConsumerIndex(), capacity, threshold))
{
return false;
}
}
// Claim the index
if (casProducerIndex(pIndex, pIndex + 2))
{
break;
}
}
final long offset = modifiedCalcCircularRefElementOffset(pIndex, mask);
// INDEX visible before ELEMENT
soRefElement(buffer, offset, e); // release element e
return true;
}
@Override
public boolean offer(final E e)
{
if (null == e)
{
throw new NullPointerException();
}
final long mask = this.producerMask;
final E[] buffer = this.producerBuffer;
long pIndex;
while (true)
{
pIndex = lvProducerIndex();
// lower bit is indicative of blocked consumer
if ((pIndex & 1) == 1)
{
if (offerAndWakeup(buffer, mask, pIndex, e))
return true;
continue;
}
// pIndex is even (lower bit is 0) -> actual index is (pIndex >> 1), consumer is awake
final long producerLimit = lvProducerLimit();
// Use producer limit to save a read of the more rapidly mutated consumer index.
// Assumption: queue is usually empty or near empty
if (producerLimit <= pIndex)
{
if (!recalculateProducerLimit(mask, pIndex, producerLimit))
{
return false;
}
}
// Claim the index
if (casProducerIndex(pIndex, pIndex + 2))
{
break;
}
}
final long offset = modifiedCalcCircularRefElementOffset(pIndex, mask);
// INDEX visible before ELEMENT
soRefElement(buffer, offset, e); // release element e
return true;
}
@Override
public void put(E e) throws InterruptedException
{
if (!offer(e))
throw new UnsupportedOperationException();
}
@Override
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
{
if (offer(e))
return true;
throw new UnsupportedOperationException();
}
private boolean offerAndWakeup(E[] buffer, long mask, long pIndex, E e)
{
final long offset = modifiedCalcCircularRefElementOffset(pIndex, mask);
final Thread consumerThread = lvBlocked();
// We could see a null here through a race with the consumer not yet storing the reference. Just retry.
if (consumerThread == null)
{
return false;
}
// Claim the slot and the responsibility of unparking
if(!casProducerIndex(pIndex, pIndex + 1))
{
return false;
}
soRefElement(buffer, offset, e);
LockSupport.unpark(consumerThread);
return true;
}
private boolean recalculateProducerLimit(long mask, long pIndex, long producerLimit)
{
return recalculateProducerLimit(pIndex, producerLimit, lvConsumerIndex(), mask + 2, mask + 2);
}
private boolean recalculateProducerLimit(long pIndex, long producerLimit, long cIndex, long bufferCapacity, long threshold)
{
// try to update the limit with our new found knowledge on cIndex
if (cIndex + bufferCapacity > pIndex)
{
casProducerLimit(producerLimit, cIndex + bufferCapacity);
}
// full and cannot grow, or hit threshold
long size = pIndex - cIndex;
return size < threshold && size < bufferCapacity;
}
private void wakeupConsumer()
{
Thread consumerThread;
do
{
consumerThread = lvBlocked();
} while (consumerThread == null);
LockSupport.unpark(consumerThread);
}
public E blockedPeek() throws InterruptedException
{
final E[] buffer = consumerBuffer;
final long mask = consumerMask;
final long cIndex = lpConsumerIndex();
final long offset = modifiedCalcCircularRefElementOffset(cIndex, mask);
final E e = lvRefElement(buffer, offset);
if (e != null) {
return e;
}
return parkUntilNext(buffer, cIndex, offset, Long.MAX_VALUE, true);
}
/**
* {@inheritDoc}
* <p>
* This implementation is correct for single consumer thread use only.
*/
public E take() throws InterruptedException
{
final E[] buffer = consumerBuffer;
final long mask = consumerMask;
final long cIndex = lpConsumerIndex();
final long offset = modifiedCalcCircularRefElementOffset(cIndex, mask);
E e = lvRefElement(buffer, offset);
if (e == null)
{
return parkUntilNext(buffer, cIndex, offset, Long.MAX_VALUE, false);
}
soRefElement(buffer, offset, null); // release element null
soConsumerIndex(cIndex + 2); // release cIndex
return e;
}
public E blockedPeek(long timeout, TimeUnit unit) throws InterruptedException
{
final E[] buffer = consumerBuffer;
final long mask = consumerMask;
final long cIndex = lpConsumerIndex();
final long offset = modifiedCalcCircularRefElementOffset(cIndex, mask);
final E e = lvRefElement(buffer, offset);
if (e != null) {
return e;
}
long timeoutNs = unit.toNanos(timeout);
if (timeoutNs <= 0)
{
return null;
}
return parkUntilNext(buffer, cIndex, offset, timeoutNs, true);
}
/**
* {@inheritDoc}
* <p>
* This implementation is correct for single consumer thread use only.
*/
public E poll(long timeout, TimeUnit unit) throws InterruptedException
{
final E[] buffer = consumerBuffer;
final long mask = consumerMask;
final long cIndex = lpConsumerIndex();
final long offset = modifiedCalcCircularRefElementOffset(cIndex, mask);
E e = lvRefElement(buffer, offset);
if (e == null)
{
long timeoutNs = unit.toNanos(timeout);
if (timeoutNs <= 0)
{
return null;
}
return parkUntilNext(buffer, cIndex, offset, timeoutNs, false);
}
soRefElement(buffer, offset, null); // release element null
soConsumerIndex(cIndex + 2); // release cIndex
return e;
}
private E parkUntilNext(E[] buffer, long cIndex, long offset, long timeoutNs, boolean peek) throws InterruptedException {
E e;
final long pIndex = lvProducerIndex();
if (cIndex == pIndex && // queue is empty
casProducerIndex(pIndex, pIndex + 1)) // we announce ourselves as parked
{
// producers only try a wakeup when both the index and the blocked thread are visible, otherwise they spin
soBlocked(Thread.currentThread());
// ignore deadline when it's forever
final long deadlineNs = timeoutNs == Long.MAX_VALUE ? 0 : System.nanoTime() + timeoutNs;
try
{
while (true)
{
LockSupport.parkNanos(this, timeoutNs);
if (Thread.interrupted())
{
casProducerIndex(pIndex + 1, pIndex);
throw new InterruptedException();
}
if ((lvProducerIndex() & 1) == 0) {
break;
}
// ignore deadline when it's forever
timeoutNs = timeoutNs == Long.MAX_VALUE ? Long.MAX_VALUE : deadlineNs - System.nanoTime();
if (timeoutNs <= 0)
{
if (casProducerIndex(pIndex + 1, pIndex))
{
// ran out of time and the producer has not moved the index
return null;
}
break; // just in the nick of time
}
}
}
finally
{
soBlocked(null);
}
}
// producer index is visible before element, so if we wake up between the index moving and the element
// store we could see a null.
e = spinWaitForElement(buffer, offset);
if (peek) {
return e;
}
soRefElement(buffer, offset, null); // release element null
soConsumerIndex(cIndex + 2); // release cIndex
return e;
}
@Override
public int remainingCapacity()
{
return capacity() - size();
}
@Override
public int drainTo(Collection<? super E> c)
{
throw new UnsupportedOperationException();
}
@Override
public int drainTo(Collection<? super E> c, int maxElements)
{
throw new UnsupportedOperationException();
}
/**
* {@inheritDoc}
* <p>
* This implementation is correct for single consumer thread use only.
*/
@Override
public E poll()
{
final E[] buffer = consumerBuffer;
final long mask = consumerMask;
final long index = lpConsumerIndex();
final long offset = modifiedCalcCircularRefElementOffset(index, mask);
E e = lvRefElement(buffer, offset);
if (e == null)
{
// consumer can't see the odd producer index
if (index != lvProducerIndex())
{
// poll() == null iff queue is empty, null element is not strong enough indicator, so we must
// check the producer index. If the queue is indeed not empty we spin until element is
// visible.
e = spinWaitForElement(buffer, offset);
}
else
{
return null;
}
}
soRefElement(buffer, offset, null); // release element null
soConsumerIndex(index + 2); // release cIndex
return e;
}
private static <E> E spinWaitForElement(E[] buffer, long offset)
{
E e;
do
{
e = lvRefElement(buffer, offset);
}
while (e == null);
return e;
}
/**
* {@inheritDoc}
* <p>
* This implementation is correct for single consumer thread use only.
*/
@Override
public E peek()
{
final E[] buffer = consumerBuffer;
final long mask = consumerMask;
final long index = lpConsumerIndex();
final long offset = modifiedCalcCircularRefElementOffset(index, mask);
E e = lvRefElement(buffer, offset);
if (e == null && index != lvProducerIndex())
{
// peek() == null iff queue is empty, null element is not strong enough indicator, so we must
// check the producer index. If the queue is indeed not empty we spin until element is visible.
e = spinWaitForElement(buffer, offset);
}
return e;
}
@Override
public long currentProducerIndex()
{
return lvProducerIndex() / 2;
}
@Override
public long currentConsumerIndex()
{
return lvConsumerIndex() / 2;
}
@Override
public int capacity()
{
return (int) ((consumerMask + 2) >> 1);
}
@Override
public boolean relaxedOffer(E e)
{
return offer(e);
}
@Override
public E relaxedPoll()
{
final E[] buffer = consumerBuffer;
final long index = lpConsumerIndex();
final long mask = consumerMask;
final long offset = modifiedCalcCircularRefElementOffset(index, mask);
E e = lvRefElement(buffer, offset);
if (e == null)
{
return null;
}
soRefElement(buffer, offset, null);
soConsumerIndex(index + 2);
return e;
}
@Override
public E relaxedPeek()
{
final E[] buffer = consumerBuffer;
final long index = lpConsumerIndex();
final long mask = consumerMask;
final long offset = modifiedCalcCircularRefElementOffset(index, mask);
return lvRefElement(buffer, offset);
}
@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 long mask = this.producerMask;
long pIndex;
int claimedSlots;
boolean wakeup = false;
long batchIndex = 0;
final long shiftedBatchSize = 2L * limit;
while (true)
{
pIndex = lvProducerIndex();
long producerLimit = lvProducerLimit();
// lower bit is indicative of blocked consumer
if ((pIndex & 1) == 1)
{
if(!casProducerIndex(pIndex, pIndex + 1))
{
continue;
}
// We've claimed pIndex, now we need to wake up consumer and set the element
wakeup = true;
batchIndex = pIndex + 1;
pIndex = pIndex - 1;
break;
}
// pIndex is even (lower bit is 0) -> actual index is (pIndex >> 1), consumer is awake
// we want 'limit' slots, but will settle for whatever is visible to 'producerLimit'
batchIndex = Math.min(producerLimit, pIndex + shiftedBatchSize); // -> producerLimit >= batchIndex
// Use producer limit to save a read of the more rapidly mutated consumer index.
// Assumption: queue is usually empty or near empty
if (pIndex >= producerLimit)
{
if (!recalculateProducerLimit(mask, pIndex, producerLimit))
{
return 0;
}
batchIndex = Math.min(lvProducerLimit(), pIndex + shiftedBatchSize);
}
// Claim the index
if (casProducerIndex(pIndex, batchIndex))
{
break;
}
}
claimedSlots = (int) ((batchIndex - pIndex) / 2);
final E[] buffer = this.producerBuffer;
// first element offset might be a wakeup, so peeled from loop
for (int i = 0; i < claimedSlots; i++)
{
long offset = modifiedCalcCircularRefElementOffset(pIndex + 2L * i, mask);
soRefElement(buffer, offset, s.get());
}
if (wakeup)
{
wakeupConsumer();
}
return claimedSlots;
}
@Override
public int fill(Supplier<E> s)
{
return MessagePassingQueueUtil.fillBounded(this, s);
}
@Override
public void fill(Supplier<E> s, WaitStrategy wait, ExitCondition exit)
{
MessagePassingQueueUtil.fill(this, s, wait, exit);
}
@Override
public int drain(Consumer<E> c)
{
return drain(c, capacity());
}
@Override
public int drain(final Consumer<E> c, final int limit)
{
return MessagePassingQueueUtil.drain(this, c, limit);
}
@Override
public void drain(Consumer<E> c, WaitStrategy w, ExitCondition exit)
{
MessagePassingQueueUtil.drain(this, c, w, exit);
}
}