[concurrency-interest] CAS using a MethodHandle

Charles Oliver Nutter headius at headius.com
Mon Dec 19 18:58:40 EST 2011


On Mon, Dec 19, 2011 at 4:14 PM, Roman Elizarov <elizarov at devexperts.com> wrote:
> Is it possible to implement Atomic*FieldUpdater on top of the MethodHandle to Unsafe?
> That is, place all checks in Atomic*FieldUpdater factory method, keep method handle(s) in final fields of updater and trivially implement CAS and other updater methods via method handle(s)? Can we thus get the best of two worlds -- type-safe and backwards-compatible API and better performance? // I assume, that HotSpot will be able to inline it all the way through, will not it?

It wouldn't necessarily inline, since the code inside
AtomicReferenceFieldUpdater would be polymorphic (i.e. a different
MethodHandle for each updater, but all called through the same path).

If it were possible to emit an invokedynamic into the Java code,
though, you could have an inlinable path:

invokedynamic -> MH for CAS -> Unsafe.CAS

Another option would be adding a method to AtomicReferenceFieldUpdater
that returns a MethodHandle for some of those opeerations...something
like

MethodHandle handle = ARFU.getCompareAndSwapHandle();
...
boolean result = (boolean)handle.invokeExact(this, expected, value);

I believe that would inline all the way through to Unsafe.

- Charlie



More information about the Concurrency-interest mailing list