A Multi-Producer-Multi-Consumer queue based on a
ConcurrentCircularArrayQueue
. This implies that any and all
threads may call the offer/poll/peek methods and correctness is maintained.
This implementation follows patterns documented on the package level for False Sharing protection.
The algorithm for offer/poll is an adaptation of the one put forward by D. Vyukov (See
here). The original algorithm
uses an array of structs which should offer nice locality properties but is sadly not possible in Java (waiting on
Value Types or similar). The alternative explored here utilizes 2 arrays, one for each field of the struct. There is
a further alternative in the experimental project which uses iteration phase markers to achieve the same algo and is
closer structurally to the original, but sadly does not perform as well as this implementation.
Tradeoffs to keep in mind:
- Padding for false sharing: counter fields and queue fields are all padded as well as either side of both arrays.
We are trading memory to avoid false sharing(active and passive).
- 2 arrays instead of one: The algorithm requires an extra array of longs matching the size of the elements array.
This is doubling/tripling the memory allocated for the buffer.
- Power of 2 capacity: Actual elements buffer (and sequence buffer) is the closest power of 2 larger or
equal to the requested capacity.