jdk/src/java.base/share/classes/java/lang/invoke/X-VarHandleByteArrayView.java.template
changeset 45518 4a116dd82fb5
parent 40734 48879ea67e2a
equal deleted inserted replaced
45440:d952dcd38dba 45518:4a116dd82fb5
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   184 #if[CAS]
   184 #if[CAS]
   185 
   185 
   186         @ForceInline
   186         @ForceInline
   187         static boolean compareAndSet(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) {
   187         static boolean compareAndSet(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) {
   188             byte[] ba = (byte[]) oba;
   188             byte[] ba = (byte[]) oba;
   189             return UNSAFE.compareAndSwap$RawType$(
   189             return UNSAFE.compareAndSet$RawType$(
   190                     ba,
   190                     ba,
   191                     address(ba, index(ba, index)),
   191                     address(ba, index(ba, index)),
   192                     convEndian(handle.be, expected), convEndian(handle.be, value));
   192                     convEndian(handle.be, expected), convEndian(handle.be, value));
   193         }
   193         }
   194 
   194 
   195         @ForceInline
   195         @ForceInline
   196         static $type$ compareAndExchange(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) {
   196         static $type$ compareAndExchange(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) {
   197             byte[] ba = (byte[]) oba;
   197             byte[] ba = (byte[]) oba;
   198             return convEndian(handle.be,
   198             return convEndian(handle.be,
   199                               UNSAFE.compareAndExchange$RawType$Volatile(
   199                               UNSAFE.compareAndExchange$RawType$(
   200                                       ba,
   200                                       ba,
   201                                       address(ba, index(ba, index)),
   201                                       address(ba, index(ba, index)),
   202                                       convEndian(handle.be, expected), convEndian(handle.be, value)));
   202                                       convEndian(handle.be, expected), convEndian(handle.be, value)));
   203         }
   203         }
   204 
   204 
   223         }
   223         }
   224 
   224 
   225         @ForceInline
   225         @ForceInline
   226         static boolean weakCompareAndSetPlain(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) {
   226         static boolean weakCompareAndSetPlain(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) {
   227             byte[] ba = (byte[]) oba;
   227             byte[] ba = (byte[]) oba;
   228             return UNSAFE.weakCompareAndSwap$RawType$(
   228             return UNSAFE.weakCompareAndSet$RawType$Plain(
   229                     ba,
   229                     ba,
   230                     address(ba, index(ba, index)),
   230                     address(ba, index(ba, index)),
   231                     convEndian(handle.be, expected), convEndian(handle.be, value));
   231                     convEndian(handle.be, expected), convEndian(handle.be, value));
   232         }
   232         }
   233 
   233 
   234         @ForceInline
   234         @ForceInline
   235         static boolean weakCompareAndSet(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) {
   235         static boolean weakCompareAndSet(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) {
   236             byte[] ba = (byte[]) oba;
   236             byte[] ba = (byte[]) oba;
   237             return UNSAFE.weakCompareAndSwap$RawType$Volatile(
   237             return UNSAFE.weakCompareAndSet$RawType$(
   238                     ba,
   238                     ba,
   239                     address(ba, index(ba, index)),
   239                     address(ba, index(ba, index)),
   240                     convEndian(handle.be, expected), convEndian(handle.be, value));
   240                     convEndian(handle.be, expected), convEndian(handle.be, value));
   241         }
   241         }
   242 
   242 
   243         @ForceInline
   243         @ForceInline
   244         static boolean weakCompareAndSetAcquire(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) {
   244         static boolean weakCompareAndSetAcquire(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) {
   245             byte[] ba = (byte[]) oba;
   245             byte[] ba = (byte[]) oba;
   246             return UNSAFE.weakCompareAndSwap$RawType$Acquire(
   246             return UNSAFE.weakCompareAndSet$RawType$Acquire(
   247                     ba,
   247                     ba,
   248                     address(ba, index(ba, index)),
   248                     address(ba, index(ba, index)),
   249                     convEndian(handle.be, expected), convEndian(handle.be, value));
   249                     convEndian(handle.be, expected), convEndian(handle.be, value));
   250         }
   250         }
   251 
   251 
   252         @ForceInline
   252         @ForceInline
   253         static boolean weakCompareAndSetRelease(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) {
   253         static boolean weakCompareAndSetRelease(ArrayHandle handle, Object oba, int index, $type$ expected, $type$ value) {
   254             byte[] ba = (byte[]) oba;
   254             byte[] ba = (byte[]) oba;
   255             return UNSAFE.weakCompareAndSwap$RawType$Release(
   255             return UNSAFE.weakCompareAndSet$RawType$Release(
   256                     ba,
   256                     ba,
   257                     address(ba, index(ba, index)),
   257                     address(ba, index(ba, index)),
   258                     convEndian(handle.be, expected), convEndian(handle.be, value));
   258                     convEndian(handle.be, expected), convEndian(handle.be, value));
   259         }
   259         }
   260 
   260 
   334             $type$ nativeExpectedValue, expectedValue;
   334             $type$ nativeExpectedValue, expectedValue;
   335             long offset = address(ba, index(ba, index));
   335             long offset = address(ba, index(ba, index));
   336             do {
   336             do {
   337                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(ba, offset);
   337                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(ba, offset);
   338                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   338                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   339             } while (!UNSAFE.weakCompareAndSwap$RawType$Volatile(ba, offset,
   339             } while (!UNSAFE.weakCompareAndSet$RawType$(ba, offset,
   340                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue + delta)));
   340                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue + delta)));
   341             return expectedValue;
   341             return expectedValue;
   342         }
   342         }
   343 #end[AtomicAdd]
   343 #end[AtomicAdd]
   344 #if[Bitwise]
   344 #if[Bitwise]
   387             $type$ nativeExpectedValue, expectedValue;
   387             $type$ nativeExpectedValue, expectedValue;
   388             long offset = address(ba, index(ba, index));
   388             long offset = address(ba, index(ba, index));
   389             do {
   389             do {
   390                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(ba, offset);
   390                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(ba, offset);
   391                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   391                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   392             } while (!UNSAFE.weakCompareAndSwap$RawType$Volatile(ba, offset,
   392             } while (!UNSAFE.weakCompareAndSet$RawType$(ba, offset,
   393                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue | value)));
   393                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue | value)));
   394             return expectedValue;
   394             return expectedValue;
   395         }
   395         }
   396 
   396 
   397         @ForceInline
   397         @ForceInline
   438             $type$ nativeExpectedValue, expectedValue;
   438             $type$ nativeExpectedValue, expectedValue;
   439             long offset = address(ba, index(ba, index));
   439             long offset = address(ba, index(ba, index));
   440             do {
   440             do {
   441                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(ba, offset);
   441                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(ba, offset);
   442                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   442                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   443             } while (!UNSAFE.weakCompareAndSwap$RawType$Volatile(ba, offset,
   443             } while (!UNSAFE.weakCompareAndSet$RawType$(ba, offset,
   444                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue & value)));
   444                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue & value)));
   445             return expectedValue;
   445             return expectedValue;
   446         }
   446         }
   447 
   447 
   448         @ForceInline
   448         @ForceInline
   489             $type$ nativeExpectedValue, expectedValue;
   489             $type$ nativeExpectedValue, expectedValue;
   490             long offset = address(ba, index(ba, index));
   490             long offset = address(ba, index(ba, index));
   491             do {
   491             do {
   492                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(ba, offset);
   492                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(ba, offset);
   493                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   493                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   494             } while (!UNSAFE.weakCompareAndSwap$RawType$Volatile(ba, offset,
   494             } while (!UNSAFE.weakCompareAndSet$RawType$(ba, offset,
   495                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue ^ value)));
   495                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue ^ value)));
   496             return expectedValue;
   496             return expectedValue;
   497         }
   497         }
   498 #end[Bitwise]
   498 #end[Bitwise]
   499 
   499 
   623 #if[CAS]
   623 #if[CAS]
   624 
   624 
   625         @ForceInline
   625         @ForceInline
   626         static boolean compareAndSet(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) {
   626         static boolean compareAndSet(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) {
   627             ByteBuffer bb = (ByteBuffer) obb;
   627             ByteBuffer bb = (ByteBuffer) obb;
   628             return UNSAFE.compareAndSwap$RawType$(
   628             return UNSAFE.compareAndSet$RawType$(
   629                     UNSAFE.getObject(bb, BYTE_BUFFER_HB),
   629                     UNSAFE.getObject(bb, BYTE_BUFFER_HB),
   630                     address(bb, indexRO(bb, index)),
   630                     address(bb, indexRO(bb, index)),
   631                     convEndian(handle.be, expected), convEndian(handle.be, value));
   631                     convEndian(handle.be, expected), convEndian(handle.be, value));
   632         }
   632         }
   633 
   633 
   634         @ForceInline
   634         @ForceInline
   635         static $type$ compareAndExchange(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) {
   635         static $type$ compareAndExchange(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) {
   636             ByteBuffer bb = (ByteBuffer) obb;
   636             ByteBuffer bb = (ByteBuffer) obb;
   637             return convEndian(handle.be,
   637             return convEndian(handle.be,
   638                               UNSAFE.compareAndExchange$RawType$Volatile(
   638                               UNSAFE.compareAndExchange$RawType$(
   639                                       UNSAFE.getObject(bb, BYTE_BUFFER_HB),
   639                                       UNSAFE.getObject(bb, BYTE_BUFFER_HB),
   640                                       address(bb, indexRO(bb, index)),
   640                                       address(bb, indexRO(bb, index)),
   641                                       convEndian(handle.be, expected), convEndian(handle.be, value)));
   641                                       convEndian(handle.be, expected), convEndian(handle.be, value)));
   642         }
   642         }
   643 
   643 
   662         }
   662         }
   663 
   663 
   664         @ForceInline
   664         @ForceInline
   665         static boolean weakCompareAndSetPlain(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) {
   665         static boolean weakCompareAndSetPlain(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) {
   666             ByteBuffer bb = (ByteBuffer) obb;
   666             ByteBuffer bb = (ByteBuffer) obb;
   667             return UNSAFE.weakCompareAndSwap$RawType$(
   667             return UNSAFE.weakCompareAndSet$RawType$Plain(
   668                     UNSAFE.getObject(bb, BYTE_BUFFER_HB),
   668                     UNSAFE.getObject(bb, BYTE_BUFFER_HB),
   669                     address(bb, indexRO(bb, index)),
   669                     address(bb, indexRO(bb, index)),
   670                     convEndian(handle.be, expected), convEndian(handle.be, value));
   670                     convEndian(handle.be, expected), convEndian(handle.be, value));
   671         }
   671         }
   672 
   672 
   673         @ForceInline
   673         @ForceInline
   674         static boolean weakCompareAndSet(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) {
   674         static boolean weakCompareAndSet(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) {
   675             ByteBuffer bb = (ByteBuffer) obb;
   675             ByteBuffer bb = (ByteBuffer) obb;
   676             return UNSAFE.weakCompareAndSwap$RawType$Volatile(
   676             return UNSAFE.weakCompareAndSet$RawType$(
   677                     UNSAFE.getObject(bb, BYTE_BUFFER_HB),
   677                     UNSAFE.getObject(bb, BYTE_BUFFER_HB),
   678                     address(bb, indexRO(bb, index)),
   678                     address(bb, indexRO(bb, index)),
   679                     convEndian(handle.be, expected), convEndian(handle.be, value));
   679                     convEndian(handle.be, expected), convEndian(handle.be, value));
   680         }
   680         }
   681 
   681 
   682         @ForceInline
   682         @ForceInline
   683         static boolean weakCompareAndSetAcquire(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) {
   683         static boolean weakCompareAndSetAcquire(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) {
   684             ByteBuffer bb = (ByteBuffer) obb;
   684             ByteBuffer bb = (ByteBuffer) obb;
   685             return UNSAFE.weakCompareAndSwap$RawType$Acquire(
   685             return UNSAFE.weakCompareAndSet$RawType$Acquire(
   686                     UNSAFE.getObject(bb, BYTE_BUFFER_HB),
   686                     UNSAFE.getObject(bb, BYTE_BUFFER_HB),
   687                     address(bb, indexRO(bb, index)),
   687                     address(bb, indexRO(bb, index)),
   688                     convEndian(handle.be, expected), convEndian(handle.be, value));
   688                     convEndian(handle.be, expected), convEndian(handle.be, value));
   689         }
   689         }
   690 
   690 
   691         @ForceInline
   691         @ForceInline
   692         static boolean weakCompareAndSetRelease(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) {
   692         static boolean weakCompareAndSetRelease(ByteBufferHandle handle, Object obb, int index, $type$ expected, $type$ value) {
   693             ByteBuffer bb = (ByteBuffer) obb;
   693             ByteBuffer bb = (ByteBuffer) obb;
   694             return UNSAFE.weakCompareAndSwap$RawType$Release(
   694             return UNSAFE.weakCompareAndSet$RawType$Release(
   695                     UNSAFE.getObject(bb, BYTE_BUFFER_HB),
   695                     UNSAFE.getObject(bb, BYTE_BUFFER_HB),
   696                     address(bb, indexRO(bb, index)),
   696                     address(bb, indexRO(bb, index)),
   697                     convEndian(handle.be, expected), convEndian(handle.be, value));
   697                     convEndian(handle.be, expected), convEndian(handle.be, value));
   698         }
   698         }
   699 
   699 
   774             Object base = UNSAFE.getObject(bb, BYTE_BUFFER_HB);
   774             Object base = UNSAFE.getObject(bb, BYTE_BUFFER_HB);
   775             long offset = address(bb, indexRO(bb, index));
   775             long offset = address(bb, indexRO(bb, index));
   776             do {
   776             do {
   777                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(base, offset);
   777                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(base, offset);
   778                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   778                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   779             } while (!UNSAFE.weakCompareAndSwap$RawType$Volatile(base, offset,
   779             } while (!UNSAFE.weakCompareAndSet$RawType$(base, offset,
   780                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue + delta)));
   780                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue + delta)));
   781             return expectedValue;
   781             return expectedValue;
   782         }
   782         }
   783 #end[AtomicAdd]
   783 #end[AtomicAdd]
   784 #if[Bitwise]
   784 #if[Bitwise]
   828             Object base = UNSAFE.getObject(bb, BYTE_BUFFER_HB);
   828             Object base = UNSAFE.getObject(bb, BYTE_BUFFER_HB);
   829             long offset = address(bb, indexRO(bb, index));
   829             long offset = address(bb, indexRO(bb, index));
   830             do {
   830             do {
   831                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(base, offset);
   831                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(base, offset);
   832                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   832                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   833             } while (!UNSAFE.weakCompareAndSwap$RawType$Volatile(base, offset,
   833             } while (!UNSAFE.weakCompareAndSet$RawType$(base, offset,
   834                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue | value)));
   834                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue | value)));
   835             return expectedValue;
   835             return expectedValue;
   836         }
   836         }
   837 
   837 
   838         @ForceInline
   838         @ForceInline
   880             Object base = UNSAFE.getObject(bb, BYTE_BUFFER_HB);
   880             Object base = UNSAFE.getObject(bb, BYTE_BUFFER_HB);
   881             long offset = address(bb, indexRO(bb, index));
   881             long offset = address(bb, indexRO(bb, index));
   882             do {
   882             do {
   883                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(base, offset);
   883                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(base, offset);
   884                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   884                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   885             } while (!UNSAFE.weakCompareAndSwap$RawType$Volatile(base, offset,
   885             } while (!UNSAFE.weakCompareAndSet$RawType$(base, offset,
   886                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue & value)));
   886                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue & value)));
   887             return expectedValue;
   887             return expectedValue;
   888         }
   888         }
   889         
   889 
   890         
   890 
   891         @ForceInline
   891         @ForceInline
   892         static $type$ getAndBitwiseXor(ByteBufferHandle handle, Object obb, int index, $type$ value) {
   892         static $type$ getAndBitwiseXor(ByteBufferHandle handle, Object obb, int index, $type$ value) {
   893             ByteBuffer bb = (ByteBuffer) obb;
   893             ByteBuffer bb = (ByteBuffer) obb;
   894             if (handle.be == BE) {
   894             if (handle.be == BE) {
   895                 return UNSAFE.getAndBitwiseXor$RawType$(
   895                 return UNSAFE.getAndBitwiseXor$RawType$(
   933             Object base = UNSAFE.getObject(bb, BYTE_BUFFER_HB);
   933             Object base = UNSAFE.getObject(bb, BYTE_BUFFER_HB);
   934             long offset = address(bb, indexRO(bb, index));
   934             long offset = address(bb, indexRO(bb, index));
   935             do {
   935             do {
   936                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(base, offset);
   936                 nativeExpectedValue = UNSAFE.get$RawType$Volatile(base, offset);
   937                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   937                 expectedValue = $RawBoxType$.reverseBytes(nativeExpectedValue);
   938             } while (!UNSAFE.weakCompareAndSwap$RawType$Volatile(base, offset,
   938             } while (!UNSAFE.weakCompareAndSet$RawType$(base, offset,
   939                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue ^ value)));
   939                     nativeExpectedValue, $RawBoxType$.reverseBytes(expectedValue ^ value)));
   940             return expectedValue;
   940             return expectedValue;
   941         }
   941         }
   942 #end[Bitwise]
   942 #end[Bitwise]
   943 
   943