[concurrency-interest] AtomicReferenceFieldUpdater vs Unsafe

Rodrigo Kumpera kumpera at gmail.com
Thu Nov 17 12:25:03 EST 2011

On Thu, Nov 17, 2011 at 7:37 AM, Doug Lea <dl at cs.oswego.edu> wrote:

> On 11/17/11 06:10, √iktor Ҡlang wrote:
>> Right now it feels like if you're doing high-perf concurrency in this
>> sense you
>> have to choose between:
>> A) Cache-trashing (and extra memory usage) because you went with
>> AtomicLong/Ref
>> whatnot
>> B) CPU overhead caused by Atomic*Updaters and if you do any inheritance
>> you'll
>> get even more checks each call (and extra mem usage due to padding).
>> C) Sacrifice cross-platform because you had to drop down to Unsafe to get
>> the
>> correct machinecode emitted.
> Yes.
> The main underlying problem is a fundamental one: Java bytecodes
> support only two field read/write operations (getField/putField),
> plus an implicit mode argument for volatiles. However, there is no
> casField, or special r/w modes like releaseField. One level up,
> there is no syntax for "lvalue" operations on fields. So the
> only options are to directly invoke these lvalue operations
> via Unsafe, which require that you know the address of the field,
> or to use Updaters, that don't require the address, but must
> perform type checks and security checks that you have access to the
> field.
> No one knows of any good alternatives. We tried for Java7
> to reduce need for either option by proposing Fences that
> would at least emulate the different memory modes, but that's
> also problematic enough to not be worth adopting.
The CLR has a general enough solution for all those problems for almost a
All you need is pass by reference and reified generics and all it will take
is trivial
JIT support for some method intrinsics. For example, in C# you'd do:

class Foo { int a; string b; }

Foo f = ...
Interlocked.CompareExchange (ref f.a, 1, 0);
Interlocked.CompareExchange (ref f.b, "test", null);

It works fine and enable it to work correctly across fields, locals,
captured locals and array elements.

Given Java 9 will have reified generics, one could wish for pass by
reference on java 10 and the solution
to this issue will be trivial.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20111117/c14688ab/attachment.html>

More information about the Concurrency-interest mailing list