Primitive Queue<any T> considerations

MacGregor, Duncan (GE Energy Management) duncan.macgregor at
Wed Nov 18 18:53:11 UTC 2015

On 18/11/2015, 16:22, "valhalla-dev on behalf of Dávid Karnok"
<valhalla-dev-bounces at on behalf of akarnokd at>

>Let's assume I have a value type named cell:
>valuetype Cell<any T> {
>   public T value;
>   public int mark;
>and a power-of-2 array of cells:
>Cell<any T>[] array;
>having Cell with final fields is useless here because then I have a
>constant array and not a queue.

Maybe I¹m not following you here but the fact fields on a Cell are final
does not stop you from replacing one Cell in array with a new Cell, just
as you can replace individual ints in an array of ints.

So you offset method would be more like

offset = producerIndex & (array.length - 1)
if (array[offset].mark != 0) {
    return false;
array[offset] = Cell(value, 1);

return true;

And that operation on the array will be guaranteed to be atomic (though
probably needs a compare and swap in case another thread is trying to do
the same thing).


More information about the valhalla-dev mailing list