< prev index next >

src/java.base/share/classes/jdk/internal/misc/Unsafe.java

Print this page
rev 52850 : imported patch method-var-handles


 157      * The first two parameters are interpreted exactly as with
 158      * {@link #getInt(Object, long)} to refer to a specific
 159      * Java variable (field or array element).  The given value
 160      * is stored into that variable.
 161      * <p>
 162      * The variable must be of the same type as the method
 163      * parameter {@code x}.
 164      *
 165      * @param o Java heap object in which the variable resides, if any, else
 166      *        null
 167      * @param offset indication of where the variable resides in a Java heap
 168      *        object, if any, else a memory address locating the variable
 169      *        statically
 170      * @param x the value to store into the indicated Java variable
 171      * @throws RuntimeException No defined exceptions are thrown, not even
 172      *         {@link NullPointerException}
 173      */
 174     @HotSpotIntrinsicCandidate
 175     public native void putInt(Object o, long offset, int x);
 176 










 177     public native boolean isFlattenedArray(Class<?> arrayClass);
 178 
 179     /**
 180      * Fetches a reference value from a given Java variable.



 181      * @see #getInt(Object, long)
 182      */
 183     @HotSpotIntrinsicCandidate
 184     public native Object getObject(Object o, long offset);
 185 
 186     public native Object getValue(Object base, long offset, Class<?> valueType);
 187 
 188     /**
 189      * Stores a reference value into a given Java variable.


 190      * <p>
 191      * Unless the reference {@code x} being stored is either null
 192      * or matches the field type, the results are undefined.
 193      * If the reference {@code o} is non-null, card marks or
 194      * other store barriers for that object (if the VM requires them)
 195      * are updated.
 196      * @see #putInt(Object, long, int)
 197      */
 198     @HotSpotIntrinsicCandidate
 199     public native void putObject(Object o, long offset, Object x);
 200 
 201     public native void putValue(Object base, long offset, Class<?> valueType, Object value);





































 202 
 203     /** @see #getInt(Object, long) */
 204     @HotSpotIntrinsicCandidate
 205     public native boolean getBoolean(Object o, long offset);
 206 
 207     /** @see #putInt(Object, long, int) */
 208     @HotSpotIntrinsicCandidate
 209     public native void    putBoolean(Object o, long offset, boolean x);
 210 
 211     /** @see #getInt(Object, long) */
 212     @HotSpotIntrinsicCandidate
 213     public native byte    getByte(Object o, long offset);
 214 
 215     /** @see #putInt(Object, long, int) */
 216     @HotSpotIntrinsicCandidate
 217     public native void    putByte(Object o, long offset, byte x);
 218 
 219     /** @see #getInt(Object, long) */
 220     @HotSpotIntrinsicCandidate
 221     public native short   getShort(Object o, long offset);


1291     }
1292 
1293     /** Throws the exception without telling the verifier. */
1294     public native void throwException(Throwable ee);
1295 
1296     /**
1297      * Atomically updates Java variable to {@code x} if it is currently
1298      * holding {@code expected}.
1299      *
1300      * <p>This operation has memory semantics of a {@code volatile} read
1301      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1302      *
1303      * @return {@code true} if successful
1304      */
1305     @HotSpotIntrinsicCandidate
1306     public final native boolean compareAndSetObject(Object o, long offset,
1307                                                     Object expected,
1308                                                     Object x);
1309 
1310     @ForceInline
1311     public final boolean compareAndSetValue(Object o, long offset,
1312                                             Class<?> valueType,
1313                                             Object expected,
1314                                             Object x) {
1315         synchronized (valueLock) {
1316             Object witness = getValue(o, offset, valueType);
1317             if (witness.equals(expected)) {
1318                 putValue(o, offset, valueType, x);
1319                 return true;
1320             }
1321             else {
1322                 return false;
1323             }
1324         }
1325     }
1326 
1327     @HotSpotIntrinsicCandidate
1328     public final native Object compareAndExchangeObject(Object o, long offset,
1329                                                         Object expected,
1330                                                         Object x);
1331 
1332     @ForceInline
1333     public final Object compareAndExchangeValue(Object o, long offset,
1334                                                 Class<?> valueType,
1335                                                 Object expected,
1336                                                 Object x) {
1337         synchronized (valueLock) {
1338             Object witness = getValue(o, offset, valueType);
1339             if (witness.equals(expected)) {
1340                 putValue(o, offset, valueType, x);
1341             }
1342             return witness;
1343         }
1344     }
1345 
1346     @HotSpotIntrinsicCandidate
1347     public final Object compareAndExchangeObjectAcquire(Object o, long offset,
1348                                                                Object expected,
1349                                                                Object x) {
1350         return compareAndExchangeObject(o, offset, expected, x);
1351     }
1352 
1353     @ForceInline
1354     public final Object compareAndExchangeValueAcquire(Object o, long offset,
1355                                                        Class<?> valueType,
1356                                                        Object expected,
1357                                                        Object x) {
1358         return compareAndExchangeValue(o, offset, valueType, expected, x);
1359     }
1360 
1361     @HotSpotIntrinsicCandidate
1362     public final Object compareAndExchangeObjectRelease(Object o, long offset,
1363                                                                Object expected,
1364                                                                Object x) {
1365         return compareAndExchangeObject(o, offset, expected, x);
1366     }
1367 
1368     @ForceInline
1369     public final Object compareAndExchangeValueRelease(Object o, long offset,
1370                                                        Class<?> valueType,
1371                                                        Object expected,
1372                                                        Object x) {
1373         return compareAndExchangeValue(o, offset, valueType, expected, x);
1374     }
1375 
1376     @HotSpotIntrinsicCandidate
1377     public final boolean weakCompareAndSetObjectPlain(Object o, long offset,
1378                                                       Object expected,
1379                                                       Object x) {
1380         return compareAndSetObject(o, offset, expected, x);
1381     }
1382 
1383     @ForceInline
1384     public final boolean weakCompareAndSetValuePlain(Object o, long offset,
1385                                                      Class<?> valueType,
1386                                                      Object expected,
1387                                                      Object x) {
1388         return compareAndSetValue(o, offset, valueType, expected, x);
1389     }
1390 
1391     @HotSpotIntrinsicCandidate
1392     public final boolean weakCompareAndSetObjectAcquire(Object o, long offset,
1393                                                         Object expected,
1394                                                         Object x) {
1395         return compareAndSetObject(o, offset, expected, x);
1396     }
1397 
1398     @ForceInline
1399     public final boolean weakCompareAndSetValueAcquire(Object o, long offset,
1400                                                        Class<?> valueType,
1401                                                        Object expected,
1402                                                        Object x) {
1403         return compareAndSetValue(o, offset, valueType, expected, x);
1404     }
1405 
1406     @HotSpotIntrinsicCandidate
1407     public final boolean weakCompareAndSetObjectRelease(Object o, long offset,
1408                                                         Object expected,
1409                                                         Object x) {
1410         return compareAndSetObject(o, offset, expected, x);
1411     }
1412 
1413     @ForceInline
1414     public final boolean weakCompareAndSetValueRelease(Object o, long offset,
1415                                                        Class<?> valueType,
1416                                                        Object expected,
1417                                                        Object x) {
1418         return compareAndSetValue(o, offset, valueType, expected, x);
1419     }
1420 
1421     @HotSpotIntrinsicCandidate
1422     public final boolean weakCompareAndSetObject(Object o, long offset,
1423                                                  Object expected,
1424                                                  Object x) {
1425         return compareAndSetObject(o, offset, expected, x);
1426     }
1427 
1428     @ForceInline
1429     public final boolean weakCompareAndSetValue(Object o, long offset,
1430                                                 Class<?> valueType,
1431                                                 Object expected,
1432                                                 Object x) {
1433         return compareAndSetValue(o, offset, valueType, expected, x);
1434     }
1435 
1436     /**
1437      * Atomically updates Java variable to {@code x} if it is currently
1438      * holding {@code expected}.
1439      *
1440      * <p>This operation has memory semantics of a {@code volatile} read
1441      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1442      *
1443      * @return {@code true} if successful
1444      */
1445     @HotSpotIntrinsicCandidate
1446     public final native boolean compareAndSetInt(Object o, long offset,
1447                                                  int expected,
1448                                                  int x);
1449 
1450     @HotSpotIntrinsicCandidate
1451     public final native int compareAndExchangeInt(Object o, long offset,
1452                                                   int expected,


2038     public final boolean weakCompareAndSetLong(Object o, long offset,
2039                                                long expected,
2040                                                long x) {
2041         return compareAndSetLong(o, offset, expected, x);
2042     }
2043 
2044     /**
2045      * Fetches a reference value from a given Java variable, with volatile
2046      * load semantics. Otherwise identical to {@link #getObject(Object, long)}
2047      */
2048     @HotSpotIntrinsicCandidate
2049     public native Object getObjectVolatile(Object o, long offset);
2050 
2051     /**
2052      * Global lock for atomic and volatile strength access to any value of
2053      * a value type.  This is a temporary workaround until better localized
2054      * atomic access mechanisms are supported for value types.
2055      */
2056     private static final Object valueLock = new Object();
2057 
2058     public final Object getValueVolatile(Object base, long offset, Class<?> valueType) {
2059         synchronized (valueLock) {
2060             return getValue(base, offset, valueType);
2061         }
2062     }
2063 
2064     /**
2065      * Stores a reference value into a given Java variable, with
2066      * volatile store semantics. Otherwise identical to {@link #putObject(Object, long, Object)}
2067      */
2068     @HotSpotIntrinsicCandidate
2069     public native void    putObjectVolatile(Object o, long offset, Object x);
2070 
2071     public final void putValueVolatile(Object o, long offset, Class<?> valueType, Object x) {
2072         synchronized (valueLock) {
2073             putValue(o, offset, valueType, x);
2074         }
2075     }
2076 
2077     /** Volatile version of {@link #getInt(Object, long)}  */
2078     @HotSpotIntrinsicCandidate
2079     public native int     getIntVolatile(Object o, long offset);
2080 
2081     /** Volatile version of {@link #putInt(Object, long, int)}  */
2082     @HotSpotIntrinsicCandidate
2083     public native void    putIntVolatile(Object o, long offset, int x);
2084 
2085     /** Volatile version of {@link #getBoolean(Object, long)}  */
2086     @HotSpotIntrinsicCandidate
2087     public native boolean getBooleanVolatile(Object o, long offset);
2088 
2089     /** Volatile version of {@link #putBoolean(Object, long, boolean)}  */
2090     @HotSpotIntrinsicCandidate
2091     public native void    putBooleanVolatile(Object o, long offset, boolean x);


2129     /** Volatile version of {@link #putFloat(Object, long, float)}  */
2130     @HotSpotIntrinsicCandidate
2131     public native void    putFloatVolatile(Object o, long offset, float x);
2132 
2133     /** Volatile version of {@link #getDouble(Object, long)}  */
2134     @HotSpotIntrinsicCandidate
2135     public native double  getDoubleVolatile(Object o, long offset);
2136 
2137     /** Volatile version of {@link #putDouble(Object, long, double)}  */
2138     @HotSpotIntrinsicCandidate
2139     public native void    putDoubleVolatile(Object o, long offset, double x);
2140 
2141 
2142 
2143     /** Acquire version of {@link #getObjectVolatile(Object, long)} */
2144     @HotSpotIntrinsicCandidate
2145     public final Object getObjectAcquire(Object o, long offset) {
2146         return getObjectVolatile(o, offset);
2147     }
2148 
2149     public final Object getValueAcquire(Object base, long offset, Class<?> valueType) {
2150         return getValueVolatile(base, offset, valueType);
2151     }
2152 
2153     /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2154     @HotSpotIntrinsicCandidate
2155     public final boolean getBooleanAcquire(Object o, long offset) {
2156         return getBooleanVolatile(o, offset);
2157     }
2158 
2159     /** Acquire version of {@link #getByteVolatile(Object, long)} */
2160     @HotSpotIntrinsicCandidate
2161     public final byte getByteAcquire(Object o, long offset) {
2162         return getByteVolatile(o, offset);
2163     }
2164 
2165     /** Acquire version of {@link #getShortVolatile(Object, long)} */
2166     @HotSpotIntrinsicCandidate
2167     public final short getShortAcquire(Object o, long offset) {
2168         return getShortVolatile(o, offset);
2169     }


2197     public final double getDoubleAcquire(Object o, long offset) {
2198         return getDoubleVolatile(o, offset);
2199     }
2200 
2201     /*
2202       * Versions of {@link #putObjectVolatile(Object, long, Object)}
2203       * that do not guarantee immediate visibility of the store to
2204       * other threads. This method is generally only useful if the
2205       * underlying field is a Java volatile (or if an array cell, one
2206       * that is otherwise only accessed using volatile accesses).
2207       *
2208       * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2209       */
2210 
2211     /** Release version of {@link #putObjectVolatile(Object, long, Object)} */
2212     @HotSpotIntrinsicCandidate
2213     public final void putObjectRelease(Object o, long offset, Object x) {
2214         putObjectVolatile(o, offset, x);
2215     }
2216 
2217     public final void putValueRelease(Object o, long offset, Class<?> valueType, Object x) {
2218         putValueVolatile(o, offset, valueType, x);
2219     }
2220 
2221     /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2222     @HotSpotIntrinsicCandidate
2223     public final void putBooleanRelease(Object o, long offset, boolean x) {
2224         putBooleanVolatile(o, offset, x);
2225     }
2226 
2227     /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2228     @HotSpotIntrinsicCandidate
2229     public final void putByteRelease(Object o, long offset, byte x) {
2230         putByteVolatile(o, offset, x);
2231     }
2232 
2233     /** Release version of {@link #putShortVolatile(Object, long, short)} */
2234     @HotSpotIntrinsicCandidate
2235     public final void putShortRelease(Object o, long offset, short x) {
2236         putShortVolatile(o, offset, x);
2237     }


2257     /** Release version of {@link #putLongVolatile(Object, long, long)} */
2258     @HotSpotIntrinsicCandidate
2259     public final void putLongRelease(Object o, long offset, long x) {
2260         putLongVolatile(o, offset, x);
2261     }
2262 
2263     /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2264     @HotSpotIntrinsicCandidate
2265     public final void putDoubleRelease(Object o, long offset, double x) {
2266         putDoubleVolatile(o, offset, x);
2267     }
2268 
2269     // ------------------------------ Opaque --------------------------------------
2270 
2271     /** Opaque version of {@link #getObjectVolatile(Object, long)} */
2272     @HotSpotIntrinsicCandidate
2273     public final Object getObjectOpaque(Object o, long offset) {
2274         return getObjectVolatile(o, offset);
2275     }
2276 
2277     public final Object getValueOpaque(Object base, long offset, Class<?> valueType) {
2278         return getValueVolatile(base, offset, valueType);
2279     }
2280 
2281     /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2282     @HotSpotIntrinsicCandidate
2283     public final boolean getBooleanOpaque(Object o, long offset) {
2284         return getBooleanVolatile(o, offset);
2285     }
2286 
2287     /** Opaque version of {@link #getByteVolatile(Object, long)} */
2288     @HotSpotIntrinsicCandidate
2289     public final byte getByteOpaque(Object o, long offset) {
2290         return getByteVolatile(o, offset);
2291     }
2292 
2293     /** Opaque version of {@link #getShortVolatile(Object, long)} */
2294     @HotSpotIntrinsicCandidate
2295     public final short getShortOpaque(Object o, long offset) {
2296         return getShortVolatile(o, offset);
2297     }


2315     }
2316 
2317     /** Opaque version of {@link #getLongVolatile(Object, long)} */
2318     @HotSpotIntrinsicCandidate
2319     public final long getLongOpaque(Object o, long offset) {
2320         return getLongVolatile(o, offset);
2321     }
2322 
2323     /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2324     @HotSpotIntrinsicCandidate
2325     public final double getDoubleOpaque(Object o, long offset) {
2326         return getDoubleVolatile(o, offset);
2327     }
2328 
2329     /** Opaque version of {@link #putObjectVolatile(Object, long, Object)} */
2330     @HotSpotIntrinsicCandidate
2331     public final void putObjectOpaque(Object o, long offset, Object x) {
2332         putObjectVolatile(o, offset, x);
2333     }
2334 
2335     public final void putValueOpaque(Object o, long offset, Class<?> valueType, Object x) {
2336         putValueVolatile(o, offset, valueType, x);
2337     }
2338 
2339     /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2340     @HotSpotIntrinsicCandidate
2341     public final void putBooleanOpaque(Object o, long offset, boolean x) {
2342         putBooleanVolatile(o, offset, x);
2343     }
2344 
2345     /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2346     @HotSpotIntrinsicCandidate
2347     public final void putByteOpaque(Object o, long offset, byte x) {
2348         putByteVolatile(o, offset, x);
2349     }
2350 
2351     /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2352     @HotSpotIntrinsicCandidate
2353     public final void putShortOpaque(Object o, long offset, short x) {
2354         putShortVolatile(o, offset, x);
2355     }


2753     /**
2754      * Atomically exchanges the given reference value with the current
2755      * reference value of a field or array element within the given
2756      * object {@code o} at the given {@code offset}.
2757      *
2758      * @param o object/array to update the field/element in
2759      * @param offset field/element offset
2760      * @param newValue new value
2761      * @return the previous value
2762      * @since 1.8
2763      */
2764     @HotSpotIntrinsicCandidate
2765     public final Object getAndSetObject(Object o, long offset, Object newValue) {
2766         Object v;
2767         do {
2768             v = getObjectVolatile(o, offset);
2769         } while (!weakCompareAndSetObject(o, offset, v, newValue));
2770         return v;
2771     }
2772 
2773     public final Object getAndSetValue(Object o, long offset, Class<?> valueType, Object newValue) {

2774         synchronized (valueLock) {
2775             Object oldValue = getValue(o, offset, valueType);
2776             putValue(o, offset, valueType, newValue);
2777             return oldValue;
2778         }
2779     }
2780 
2781     @ForceInline
2782     public final Object getAndSetObjectRelease(Object o, long offset, Object newValue) {
2783         Object v;
2784         do {
2785             v = getObject(o, offset);
2786         } while (!weakCompareAndSetObjectRelease(o, offset, v, newValue));
2787         return v;
2788     }
2789 
2790     @ForceInline
2791     public final Object getAndSetValueRelease(Object o, long offset, Class<?> valueType, Object newValue) {
2792         return getAndSetValue(o, offset, valueType, newValue);
2793     }
2794 
2795     @ForceInline
2796     public final Object getAndSetObjectAcquire(Object o, long offset, Object newValue) {
2797         Object v;
2798         do {
2799             v = getObjectAcquire(o, offset);
2800         } while (!weakCompareAndSetObjectAcquire(o, offset, v, newValue));
2801         return v;
2802     }
2803 
2804     @ForceInline
2805     public final Object getAndSetValueAcquire(Object o, long offset, Class<?> valueType, Object newValue) {
2806         return getAndSetValue(o, offset, valueType, newValue);
2807     }
2808 
2809     @HotSpotIntrinsicCandidate
2810     public final byte getAndSetByte(Object o, long offset, byte newValue) {
2811         byte v;
2812         do {
2813             v = getByteVolatile(o, offset);
2814         } while (!weakCompareAndSetByte(o, offset, v, newValue));
2815         return v;
2816     }
2817 
2818     @ForceInline
2819     public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
2820         byte v;
2821         do {
2822             v = getByte(o, offset);
2823         } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
2824         return v;
2825     }




 157      * The first two parameters are interpreted exactly as with
 158      * {@link #getInt(Object, long)} to refer to a specific
 159      * Java variable (field or array element).  The given value
 160      * is stored into that variable.
 161      * <p>
 162      * The variable must be of the same type as the method
 163      * parameter {@code x}.
 164      *
 165      * @param o Java heap object in which the variable resides, if any, else
 166      *        null
 167      * @param offset indication of where the variable resides in a Java heap
 168      *        object, if any, else a memory address locating the variable
 169      *        statically
 170      * @param x the value to store into the indicated Java variable
 171      * @throws RuntimeException No defined exceptions are thrown, not even
 172      *         {@link NullPointerException}
 173      */
 174     @HotSpotIntrinsicCandidate
 175     public native void putInt(Object o, long offset, int x);
 176 
 177     /**
 178      * Returns true if the given class is a regular value type.
 179      */
 180     public boolean isValueType(Class<?> c) {
 181         return c.isValue() && c == c.asValueType();
 182     }
 183 
 184     /**
 185      * Returns true if the given class is a flattened array.
 186      */
 187     public native boolean isFlattenedArray(Class<?> arrayClass);
 188 
 189     /**
 190      * Fetches a reference value from a given Java variable.
 191      * This method can return a reference to either an object or value
 192      * or a null reference.
 193      *
 194      * @see #getInt(Object, long)
 195      */
 196     @HotSpotIntrinsicCandidate
 197     public native Object getObject(Object o, long offset);
 198 


 199     /**
 200      * Stores a reference value into a given Java variable.
 201      * This method can store a reference to either an object or value
 202      * or a null reference.
 203      * <p>
 204      * Unless the reference {@code x} being stored is either null
 205      * or matches the field type, the results are undefined.
 206      * If the reference {@code o} is non-null, card marks or
 207      * other store barriers for that object (if the VM requires them)
 208      * are updated.
 209      * @see #putInt(Object, long, int)
 210      */
 211     @HotSpotIntrinsicCandidate
 212     public native void putObject(Object o, long offset, Object x);
 213 
 214     /**
 215      * Fetches a value of type {@code <V>} from a given Java variable.
 216      * More specifically, fetches a field or array element within the given
 217      * {@code o} object at the given offset, or (if {@code o} is null)
 218      * from the memory address whose numerical value is the given offset.
 219      *
 220      * @param o Java heap object in which the variable resides, if any, else
 221      *        null
 222      * @param offset indication of where the variable resides in a Java heap
 223      *        object, if any, else a memory address locating the variable
 224      *        statically
 225      * @param vc value class
 226      * @param <V> the type of a value
 227      * @return the value fetched from the indicated Java variable
 228      * @throws RuntimeException No defined exceptions are thrown, not even
 229      *         {@link NullPointerException}
 230      */
 231     public native <V> V getValue(Object o, long offset, Class<?> vc);
 232 
 233     /**
 234      * Stores the given value into a given Java variable.
 235      *
 236      * Unless the reference {@code o} being stored is either null
 237      * or matches the field type and not in a value container,
 238      * the results are undefined.
 239      *
 240      * @param o Java heap object in which the variable resides, if any, else
 241      *        null
 242      * @param offset indication of where the variable resides in a Java heap
 243      *        object, if any, else a memory address locating the variable
 244      *        statically
 245      * @param vc value class
 246      * @param v the value to store into the indicated Java variable
 247      * @param <V> the type of a value
 248      * @throws RuntimeException No defined exceptions are thrown, not even
 249      *         {@link NullPointerException}
 250      */
 251     public native <V> void putValue(Object o, long offset, Class<?> vc, V v);
 252 
 253     /** @see #getInt(Object, long) */
 254     @HotSpotIntrinsicCandidate
 255     public native boolean getBoolean(Object o, long offset);
 256 
 257     /** @see #putInt(Object, long, int) */
 258     @HotSpotIntrinsicCandidate
 259     public native void    putBoolean(Object o, long offset, boolean x);
 260 
 261     /** @see #getInt(Object, long) */
 262     @HotSpotIntrinsicCandidate
 263     public native byte    getByte(Object o, long offset);
 264 
 265     /** @see #putInt(Object, long, int) */
 266     @HotSpotIntrinsicCandidate
 267     public native void    putByte(Object o, long offset, byte x);
 268 
 269     /** @see #getInt(Object, long) */
 270     @HotSpotIntrinsicCandidate
 271     public native short   getShort(Object o, long offset);


1341     }
1342 
1343     /** Throws the exception without telling the verifier. */
1344     public native void throwException(Throwable ee);
1345 
1346     /**
1347      * Atomically updates Java variable to {@code x} if it is currently
1348      * holding {@code expected}.
1349      *
1350      * <p>This operation has memory semantics of a {@code volatile} read
1351      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1352      *
1353      * @return {@code true} if successful
1354      */
1355     @HotSpotIntrinsicCandidate
1356     public final native boolean compareAndSetObject(Object o, long offset,
1357                                                     Object expected,
1358                                                     Object x);
1359 
1360     @ForceInline
1361     public final <V> boolean compareAndSetValue(Object o, long offset,
1362                                                 Class<?> valueType,
1363                                                 V expected,
1364                                                 V x) {
1365         synchronized (valueLock) {
1366             Object witness = getValue(o, offset, valueType);
1367             if (witness.equals(expected)) {
1368                 putValue(o, offset, valueType, x);
1369                 return true;
1370             }
1371             else {
1372                 return false;
1373             }
1374         }
1375     }
1376 
1377     @HotSpotIntrinsicCandidate
1378     public final native Object compareAndExchangeObject(Object o, long offset,
1379                                                         Object expected,
1380                                                         Object x);

1381     @ForceInline
1382     public final <V> Object compareAndExchangeValue(Object o, long offset,
1383                                                     Class<?> valueType,
1384                                                     V expected,
1385                                                     V x) {
1386         synchronized (valueLock) {
1387             Object witness = getValue(o, offset, valueType);
1388             if (witness.equals(expected)) {
1389                 putValue(o, offset, valueType, x);
1390             }
1391             return witness;
1392         }
1393     }
1394 
1395     @HotSpotIntrinsicCandidate
1396     public final Object compareAndExchangeObjectAcquire(Object o, long offset,
1397                                                         Object expected,
1398                                                         Object x) {
1399         return compareAndExchangeObject(o, offset, expected, x);
1400     }
1401 
1402     @ForceInline
1403     public final <V> Object compareAndExchangeValueAcquire(Object o, long offset,
1404                                                            Class<?> valueType,
1405                                                            V expected,
1406                                                            V x) {
1407         return compareAndExchangeValue(o, offset, valueType, expected, x);
1408     }
1409 
1410     @HotSpotIntrinsicCandidate
1411     public final Object compareAndExchangeObjectRelease(Object o, long offset,
1412                                                         Object expected,
1413                                                         Object x) {
1414         return compareAndExchangeObject(o, offset, expected, x);
1415     }
1416 
1417     @ForceInline
1418     public final <V> Object compareAndExchangeValueRelease(Object o, long offset,
1419                                                            Class<?> valueType,
1420                                                            V expected,
1421                                                            V x) {
1422         return compareAndExchangeValue(o, offset, valueType, expected, x);
1423     }
1424 
1425     @HotSpotIntrinsicCandidate
1426     public final boolean weakCompareAndSetObjectPlain(Object o, long offset,
1427                                                       Object expected,
1428                                                       Object x) {
1429         return compareAndSetObject(o, offset, expected, x);
1430     }
1431 
1432     @ForceInline
1433     public final <V> boolean weakCompareAndSetValuePlain(Object o, long offset,
1434                                                          Class<?> valueType,
1435                                                          V expected,
1436                                                          V x) {
1437         return compareAndSetValue(o, offset, valueType, expected, x);
1438     }
1439 
1440     @HotSpotIntrinsicCandidate
1441     public final boolean weakCompareAndSetObjectAcquire(Object o, long offset,
1442                                                         Object expected,
1443                                                         Object x) {
1444         return compareAndSetObject(o, offset, expected, x);
1445     }
1446 
1447     @ForceInline
1448     public final <V> boolean weakCompareAndSetValueAcquire(Object o, long offset,
1449                                                            Class<?> valueType,
1450                                                            V expected,
1451                                                            V x) {
1452         return compareAndSetValue(o, offset, valueType, expected, x);
1453     }
1454 
1455     @HotSpotIntrinsicCandidate
1456     public final boolean weakCompareAndSetObjectRelease(Object o, long offset,
1457                                                         Object expected,
1458                                                         Object x) {
1459         return compareAndSetObject(o, offset, expected, x);
1460     }
1461 
1462     @ForceInline
1463     public final <V> boolean weakCompareAndSetValueRelease(Object o, long offset,
1464                                                            Class<?> valueType,
1465                                                            V expected,
1466                                                            V x) {
1467         return compareAndSetValue(o, offset, valueType, expected, x);
1468     }
1469 
1470     @HotSpotIntrinsicCandidate
1471     public final boolean weakCompareAndSetObject(Object o, long offset,
1472                                                  Object expected,
1473                                                  Object x) {
1474         return compareAndSetObject(o, offset, expected, x);
1475     }
1476 
1477     @ForceInline
1478     public final <V> boolean weakCompareAndSetValue(Object o, long offset,
1479                                                     Class<?> valueType,
1480                                                     V expected,
1481                                                     V x) {
1482         return compareAndSetValue(o, offset, valueType, expected, x);
1483     }
1484 
1485     /**
1486      * Atomically updates Java variable to {@code x} if it is currently
1487      * holding {@code expected}.
1488      *
1489      * <p>This operation has memory semantics of a {@code volatile} read
1490      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1491      *
1492      * @return {@code true} if successful
1493      */
1494     @HotSpotIntrinsicCandidate
1495     public final native boolean compareAndSetInt(Object o, long offset,
1496                                                  int expected,
1497                                                  int x);
1498 
1499     @HotSpotIntrinsicCandidate
1500     public final native int compareAndExchangeInt(Object o, long offset,
1501                                                   int expected,


2087     public final boolean weakCompareAndSetLong(Object o, long offset,
2088                                                long expected,
2089                                                long x) {
2090         return compareAndSetLong(o, offset, expected, x);
2091     }
2092 
2093     /**
2094      * Fetches a reference value from a given Java variable, with volatile
2095      * load semantics. Otherwise identical to {@link #getObject(Object, long)}
2096      */
2097     @HotSpotIntrinsicCandidate
2098     public native Object getObjectVolatile(Object o, long offset);
2099 
2100     /**
2101      * Global lock for atomic and volatile strength access to any value of
2102      * a value type.  This is a temporary workaround until better localized
2103      * atomic access mechanisms are supported for value types.
2104      */
2105     private static final Object valueLock = new Object();
2106 
2107     public final <V> Object getValueVolatile(Object base, long offset, Class<?> valueType) {
2108         synchronized (valueLock) {
2109             return getValue(base, offset, valueType);
2110         }
2111     }
2112 
2113     /**
2114      * Stores a reference value into a given Java variable, with
2115      * volatile store semantics. Otherwise identical to {@link #putObject(Object, long, Object)}
2116      */
2117     @HotSpotIntrinsicCandidate
2118     public native void    putObjectVolatile(Object o, long offset, Object x);
2119 
2120     public final <V> void putValueVolatile(Object o, long offset, Class<?> valueType, V x) {
2121         synchronized (valueLock) {
2122             putValue(o, offset, valueType, x);
2123         }
2124     }
2125 
2126     /** Volatile version of {@link #getInt(Object, long)}  */
2127     @HotSpotIntrinsicCandidate
2128     public native int     getIntVolatile(Object o, long offset);
2129 
2130     /** Volatile version of {@link #putInt(Object, long, int)}  */
2131     @HotSpotIntrinsicCandidate
2132     public native void    putIntVolatile(Object o, long offset, int x);
2133 
2134     /** Volatile version of {@link #getBoolean(Object, long)}  */
2135     @HotSpotIntrinsicCandidate
2136     public native boolean getBooleanVolatile(Object o, long offset);
2137 
2138     /** Volatile version of {@link #putBoolean(Object, long, boolean)}  */
2139     @HotSpotIntrinsicCandidate
2140     public native void    putBooleanVolatile(Object o, long offset, boolean x);


2178     /** Volatile version of {@link #putFloat(Object, long, float)}  */
2179     @HotSpotIntrinsicCandidate
2180     public native void    putFloatVolatile(Object o, long offset, float x);
2181 
2182     /** Volatile version of {@link #getDouble(Object, long)}  */
2183     @HotSpotIntrinsicCandidate
2184     public native double  getDoubleVolatile(Object o, long offset);
2185 
2186     /** Volatile version of {@link #putDouble(Object, long, double)}  */
2187     @HotSpotIntrinsicCandidate
2188     public native void    putDoubleVolatile(Object o, long offset, double x);
2189 
2190 
2191 
2192     /** Acquire version of {@link #getObjectVolatile(Object, long)} */
2193     @HotSpotIntrinsicCandidate
2194     public final Object getObjectAcquire(Object o, long offset) {
2195         return getObjectVolatile(o, offset);
2196     }
2197 
2198     public final <V> Object getValueAcquire(Object base, long offset, Class<?> valueType) {
2199         return getValueVolatile(base, offset, valueType);
2200     }
2201 
2202     /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2203     @HotSpotIntrinsicCandidate
2204     public final boolean getBooleanAcquire(Object o, long offset) {
2205         return getBooleanVolatile(o, offset);
2206     }
2207 
2208     /** Acquire version of {@link #getByteVolatile(Object, long)} */
2209     @HotSpotIntrinsicCandidate
2210     public final byte getByteAcquire(Object o, long offset) {
2211         return getByteVolatile(o, offset);
2212     }
2213 
2214     /** Acquire version of {@link #getShortVolatile(Object, long)} */
2215     @HotSpotIntrinsicCandidate
2216     public final short getShortAcquire(Object o, long offset) {
2217         return getShortVolatile(o, offset);
2218     }


2246     public final double getDoubleAcquire(Object o, long offset) {
2247         return getDoubleVolatile(o, offset);
2248     }
2249 
2250     /*
2251       * Versions of {@link #putObjectVolatile(Object, long, Object)}
2252       * that do not guarantee immediate visibility of the store to
2253       * other threads. This method is generally only useful if the
2254       * underlying field is a Java volatile (or if an array cell, one
2255       * that is otherwise only accessed using volatile accesses).
2256       *
2257       * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2258       */
2259 
2260     /** Release version of {@link #putObjectVolatile(Object, long, Object)} */
2261     @HotSpotIntrinsicCandidate
2262     public final void putObjectRelease(Object o, long offset, Object x) {
2263         putObjectVolatile(o, offset, x);
2264     }
2265 
2266     public final <V> void putValueRelease(Object o, long offset, Class<?> valueType, V x) {
2267         putValueVolatile(o, offset, valueType, x);
2268     }
2269 
2270     /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2271     @HotSpotIntrinsicCandidate
2272     public final void putBooleanRelease(Object o, long offset, boolean x) {
2273         putBooleanVolatile(o, offset, x);
2274     }
2275 
2276     /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2277     @HotSpotIntrinsicCandidate
2278     public final void putByteRelease(Object o, long offset, byte x) {
2279         putByteVolatile(o, offset, x);
2280     }
2281 
2282     /** Release version of {@link #putShortVolatile(Object, long, short)} */
2283     @HotSpotIntrinsicCandidate
2284     public final void putShortRelease(Object o, long offset, short x) {
2285         putShortVolatile(o, offset, x);
2286     }


2306     /** Release version of {@link #putLongVolatile(Object, long, long)} */
2307     @HotSpotIntrinsicCandidate
2308     public final void putLongRelease(Object o, long offset, long x) {
2309         putLongVolatile(o, offset, x);
2310     }
2311 
2312     /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2313     @HotSpotIntrinsicCandidate
2314     public final void putDoubleRelease(Object o, long offset, double x) {
2315         putDoubleVolatile(o, offset, x);
2316     }
2317 
2318     // ------------------------------ Opaque --------------------------------------
2319 
2320     /** Opaque version of {@link #getObjectVolatile(Object, long)} */
2321     @HotSpotIntrinsicCandidate
2322     public final Object getObjectOpaque(Object o, long offset) {
2323         return getObjectVolatile(o, offset);
2324     }
2325 
2326     public final <V> Object getValueOpaque(Object base, long offset, Class<?> valueType) {
2327         return getValueVolatile(base, offset, valueType);
2328     }
2329 
2330     /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2331     @HotSpotIntrinsicCandidate
2332     public final boolean getBooleanOpaque(Object o, long offset) {
2333         return getBooleanVolatile(o, offset);
2334     }
2335 
2336     /** Opaque version of {@link #getByteVolatile(Object, long)} */
2337     @HotSpotIntrinsicCandidate
2338     public final byte getByteOpaque(Object o, long offset) {
2339         return getByteVolatile(o, offset);
2340     }
2341 
2342     /** Opaque version of {@link #getShortVolatile(Object, long)} */
2343     @HotSpotIntrinsicCandidate
2344     public final short getShortOpaque(Object o, long offset) {
2345         return getShortVolatile(o, offset);
2346     }


2364     }
2365 
2366     /** Opaque version of {@link #getLongVolatile(Object, long)} */
2367     @HotSpotIntrinsicCandidate
2368     public final long getLongOpaque(Object o, long offset) {
2369         return getLongVolatile(o, offset);
2370     }
2371 
2372     /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2373     @HotSpotIntrinsicCandidate
2374     public final double getDoubleOpaque(Object o, long offset) {
2375         return getDoubleVolatile(o, offset);
2376     }
2377 
2378     /** Opaque version of {@link #putObjectVolatile(Object, long, Object)} */
2379     @HotSpotIntrinsicCandidate
2380     public final void putObjectOpaque(Object o, long offset, Object x) {
2381         putObjectVolatile(o, offset, x);
2382     }
2383 
2384     public final <V> void putValueOpaque(Object o, long offset, Class<?> valueType, V x) {
2385         putValueVolatile(o, offset, valueType, x);
2386     }
2387 
2388     /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2389     @HotSpotIntrinsicCandidate
2390     public final void putBooleanOpaque(Object o, long offset, boolean x) {
2391         putBooleanVolatile(o, offset, x);
2392     }
2393 
2394     /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2395     @HotSpotIntrinsicCandidate
2396     public final void putByteOpaque(Object o, long offset, byte x) {
2397         putByteVolatile(o, offset, x);
2398     }
2399 
2400     /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2401     @HotSpotIntrinsicCandidate
2402     public final void putShortOpaque(Object o, long offset, short x) {
2403         putShortVolatile(o, offset, x);
2404     }


2802     /**
2803      * Atomically exchanges the given reference value with the current
2804      * reference value of a field or array element within the given
2805      * object {@code o} at the given {@code offset}.
2806      *
2807      * @param o object/array to update the field/element in
2808      * @param offset field/element offset
2809      * @param newValue new value
2810      * @return the previous value
2811      * @since 1.8
2812      */
2813     @HotSpotIntrinsicCandidate
2814     public final Object getAndSetObject(Object o, long offset, Object newValue) {
2815         Object v;
2816         do {
2817             v = getObjectVolatile(o, offset);
2818         } while (!weakCompareAndSetObject(o, offset, v, newValue));
2819         return v;
2820     }
2821 
2822     @SuppressWarnings("unchecked")
2823     public final <V> Object getAndSetValue(Object o, long offset, Class<?> valueType, V newValue) {
2824         synchronized (valueLock) {
2825             Object oldValue = getValue(o, offset, valueType);
2826             putValue(o, offset, valueType, newValue);
2827             return oldValue;
2828         }
2829     }
2830 
2831     @ForceInline
2832     public final Object getAndSetObjectRelease(Object o, long offset, Object newValue) {
2833         Object v;
2834         do {
2835             v = getObject(o, offset);
2836         } while (!weakCompareAndSetObjectRelease(o, offset, v, newValue));
2837         return v;
2838     }
2839 
2840     @ForceInline
2841     public final <V> Object getAndSetValueRelease(Object o, long offset, Class<?> valueType, V newValue) {
2842         return getAndSetValue(o, offset, valueType, newValue);
2843     }
2844 
2845     @ForceInline
2846     public final Object getAndSetObjectAcquire(Object o, long offset, Object newValue) {
2847         Object v;
2848         do {
2849             v = getObjectAcquire(o, offset);
2850         } while (!weakCompareAndSetObjectAcquire(o, offset, v, newValue));
2851         return v;
2852     }
2853 
2854     @ForceInline
2855     public final <V> Object getAndSetValueAcquire(Object o, long offset, Class<?> valueType, V newValue) {
2856         return getAndSetValue(o, offset, valueType, newValue);
2857     }
2858 
2859     @HotSpotIntrinsicCandidate
2860     public final byte getAndSetByte(Object o, long offset, byte newValue) {
2861         byte v;
2862         do {
2863             v = getByteVolatile(o, offset);
2864         } while (!weakCompareAndSetByte(o, offset, v, newValue));
2865         return v;
2866     }
2867 
2868     @ForceInline
2869     public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
2870         byte v;
2871         do {
2872             v = getByte(o, offset);
2873         } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
2874         return v;
2875     }


< prev index next >