Primitive Queue<any T> considerations
Vitaly Davidovich
vitalyd at gmail.com
Wed Nov 18 20:29:45 UTC 2015
Please no! :)
What if I have 6 fields? 7 fields? 15 fields?
On Wed, Nov 18, 2015 at 3:16 PM, Peter Levart <peter.levart at gmail.com>
wrote:
>
>
> On 11/18/2015 07:55 PM, Vitaly Davidovich wrote:
>
> As discussed upthread, how would that operation be atomic for arbitrarily
> sized value types?
>
>
> Not atomic, but for SPSC queue discussed here and for the price of
> doubling the writes, this problem could be solved like:
>
> array[offset] = Cell(value, 0);
> UNSAFE.storeFence();
> array[offset] = Cell(value, 1);
> producerIndex++;
>
>
> Maybe JIT could even detect such pattern and try to optimize it doing
> something equivalent but not expressible:
>
> array[offset].value = value;
> UNSAFE.storeFence();
> array[offset].mark = 1;
> producerIndex++;
>
>
>
> Regards, Peter
>
>
> On Wed, Nov 18, 2015 at 1:53 PM, MacGregor, Duncan (GE Energy Management) <duncan.macgregor at ge.com> wrote:
>
>
> On 18/11/2015, 16:22, "valhalla-dev on behalf of Dávid Karnok"<valhalla-dev-bounces at openjdk.java.net on behalf of akarnokd at gmail.com> <valhalla-dev-bounces at openjdk.java.netonbehalfofakarnokd@gmail.com>
> wrote:
>
>
> 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);
> producerIndex++;
>
> 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).
>
> Duncan.
>
>
>
>
>
More information about the valhalla-dev
mailing list