FTR: JVM Lang Summit presentation
Paul Sandoz
paul.sandoz at oracle.com
Tue Sep 2 16:59:44 UTC 2014
On Aug 8, 2014, at 11:34 PM, Remi Forax <forax at univ-mlv.fr> wrote:
> Something along :
>
> private static MethodHandle compareAndSet(Lookup lookup, Class<?> declaringClass, String fieldName, Class<?> fieldType)
> throws NoSuchFieldException, IllegalAccessException {
> MethodHandle getter = lookup.findGetter(declaringClass, fieldName, fieldType);
> MethodHandleInfo methodHandleInfo = lookup.revealDirect(getter);
> Field field = methodHandleInfo.reflectAs(Field.class, lookup);
> long offset = UNSAFE.objectFieldOffset(field);
> MethodHandle mh = MethodHandles.insertArguments(COMPARE_AND_SWAP_OBJECT, 1, offset);
> return mh.asType(MethodType.methodType(boolean.class, declaringClass, fieldType, fieldType));
> }
>
> public class VarHandle<T, U> {
> private final @Stable MethodHandle compareAndSet;
>
> VarHandle(MethodHandle compareAndSet) {
> this.compareAndSet = compareAndSet;
> }
>
> public boolean compareAndSet(T object, U expected, U value) {
> try {
> return (boolean)compareAndSet.invoke(object, expected, value);
> } catch (Throwable e) {
> if (e instanceof RuntimeException) throw (RuntimeException)e;
> if (e instanceof Error) throw (Error)e;
> throw new AssertionError(e);
> }
> }
> }
>
(FWIW i initially modified the DHM code to expose out MethodHandles to CAS etc along similar lines to existing DHMs and LFs corresponding to get/putfield.)
Performance-wise, as of now, the above is likely to be slower or on a par with AtomicReferenceFieldUpdater or reflection (if there was a CAS op on Field). Perhaps it will eventually be possible to achieve that with the above, but from what John says it sounds like much investigation is required.
Any replacement of Unsafe in j.u.concurrent classes has to be almost, if not, as performant as that of direct Unsafe usage. It's proving quite useful to have something that works along similar fundamental lines (w.r.t. method invocation) that already gets quite close to Unsafe performance and from which we can evaluate and improve certain aspects.
Paul.
More information about the valhalla-dev
mailing list