9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.internal.misc;
27
28 import jdk.internal.ref.Cleaner;
29 import jdk.internal.vm.annotation.ForceInline;
30 import jdk.internal.vm.annotation.IntrinsicCandidate;
31 import sun.nio.ch.DirectBuffer;
32
33 import java.lang.reflect.Field;
34 import java.security.ProtectionDomain;
35
36 import static jdk.internal.misc.UnsafeConstants.*;
37
38 /**
39 * A collection of methods for performing low-level, unsafe operations.
40 * Although the class and all methods are public, use of this class is
41 * limited because only trusted code can obtain instances of it.
42 *
43 * <em>Note:</em> It is the responsibility of the caller to make sure
44 * arguments are checked before methods of this class are
45 * called. While some rudimentary checks are performed on the input,
46 * the checks are best effort and when performance is an overriding
47 * priority, as when methods of this class are optimized by the
48 * runtime compiler, some or all checks (if any) may be elided. Hence,
165 * The first two parameters are interpreted exactly as with
166 * {@link #getInt(Object, long)} to refer to a specific
167 * Java variable (field or array element). The given value
168 * is stored into that variable.
169 * <p>
170 * The variable must be of the same type as the method
171 * parameter {@code x}.
172 *
173 * @param o Java heap object in which the variable resides, if any, else
174 * null
175 * @param offset indication of where the variable resides in a Java heap
176 * object, if any, else a memory address locating the variable
177 * statically
178 * @param x the value to store into the indicated Java variable
179 * @throws RuntimeException No defined exceptions are thrown, not even
180 * {@link NullPointerException}
181 */
182 @IntrinsicCandidate
183 public native void putInt(Object o, long offset, int x);
184
185 /**
186 * Fetches a reference value from a given Java variable.
187 * @see #getInt(Object, long)
188 */
189 @IntrinsicCandidate
190 public native Object getReference(Object o, long offset);
191
192 /**
193 * Stores a reference value into a given Java variable.
194 * <p>
195 * Unless the reference {@code x} being stored is either null
196 * or matches the field type, the results are undefined.
197 * If the reference {@code o} is non-null, card marks or
198 * other store barriers for that object (if the VM requires them)
199 * are updated.
200 * @see #putInt(Object, long, int)
201 */
202 @IntrinsicCandidate
203 public native void putReference(Object o, long offset, Object x);
204
205 /** @see #getInt(Object, long) */
206 @IntrinsicCandidate
207 public native boolean getBoolean(Object o, long offset);
208
209 /** @see #putInt(Object, long, int) */
210 @IntrinsicCandidate
211 public native void putBoolean(Object o, long offset, boolean x);
212
213 /** @see #getInt(Object, long) */
214 @IntrinsicCandidate
215 public native byte getByte(Object o, long offset);
216
217 /** @see #putInt(Object, long, int) */
218 @IntrinsicCandidate
219 public native void putByte(Object o, long offset, byte x);
220
221 /** @see #getInt(Object, long) */
222 @IntrinsicCandidate
223 public native short getShort(Object o, long offset);
224
1233 * allocation of a given array class. However, arrays of "narrow" types
1234 * will generally not work properly with accessors like {@link
1235 * #getByte(Object, long)}, so the scale factor for such classes is reported
1236 * as zero.
1237 * <p>
1238 * The computation of the actual memory offset should always use {@code
1239 * long} arithmetic to avoid overflows.
1240 *
1241 * @see #arrayBaseOffset
1242 * @see #getInt(Object, long)
1243 * @see #putInt(Object, long, int)
1244 */
1245 public int arrayIndexScale(Class<?> arrayClass) {
1246 if (arrayClass == null) {
1247 throw new NullPointerException();
1248 }
1249
1250 return arrayIndexScale0(arrayClass);
1251 }
1252
1253
1254 /** The value of {@code arrayIndexScale(boolean[].class)} */
1255 public static final int ARRAY_BOOLEAN_INDEX_SCALE
1256 = theUnsafe.arrayIndexScale(boolean[].class);
1257
1258 /** The value of {@code arrayIndexScale(byte[].class)} */
1259 public static final int ARRAY_BYTE_INDEX_SCALE
1260 = theUnsafe.arrayIndexScale(byte[].class);
1261
1262 /** The value of {@code arrayIndexScale(short[].class)} */
1263 public static final int ARRAY_SHORT_INDEX_SCALE
1264 = theUnsafe.arrayIndexScale(short[].class);
1265
1266 /** The value of {@code arrayIndexScale(char[].class)} */
1267 public static final int ARRAY_CHAR_INDEX_SCALE
1268 = theUnsafe.arrayIndexScale(char[].class);
1269
1270 /** The value of {@code arrayIndexScale(int[].class)} */
1271 public static final int ARRAY_INT_INDEX_SCALE
1272 = theUnsafe.arrayIndexScale(int[].class);
1411 return null;
1412 }
1413
1414 /** Throws the exception without telling the verifier. */
1415 public native void throwException(Throwable ee);
1416
1417 /**
1418 * Atomically updates Java variable to {@code x} if it is currently
1419 * holding {@code expected}.
1420 *
1421 * <p>This operation has memory semantics of a {@code volatile} read
1422 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1423 *
1424 * @return {@code true} if successful
1425 */
1426 @IntrinsicCandidate
1427 public final native boolean compareAndSetReference(Object o, long offset,
1428 Object expected,
1429 Object x);
1430
1431 @IntrinsicCandidate
1432 public final native Object compareAndExchangeReference(Object o, long offset,
1433 Object expected,
1434 Object x);
1435
1436 @IntrinsicCandidate
1437 public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1438 Object expected,
1439 Object x) {
1440 return compareAndExchangeReference(o, offset, expected, x);
1441 }
1442
1443 @IntrinsicCandidate
1444 public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1445 Object expected,
1446 Object x) {
1447 return compareAndExchangeReference(o, offset, expected, x);
1448 }
1449
1450 @IntrinsicCandidate
1451 public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1452 Object expected,
1453 Object x) {
1454 return compareAndSetReference(o, offset, expected, x);
1455 }
1456
1457 @IntrinsicCandidate
1458 public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1459 Object expected,
1460 Object x) {
1461 return compareAndSetReference(o, offset, expected, x);
1462 }
1463
1464 @IntrinsicCandidate
1465 public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1466 Object expected,
1467 Object x) {
1468 return compareAndSetReference(o, offset, expected, x);
1469 }
1470
1471 @IntrinsicCandidate
1472 public final boolean weakCompareAndSetReference(Object o, long offset,
1473 Object expected,
1474 Object x) {
1475 return compareAndSetReference(o, offset, expected, x);
1476 }
1477
1478 /**
1479 * Atomically updates Java variable to {@code x} if it is currently
1480 * holding {@code expected}.
1481 *
1482 * <p>This operation has memory semantics of a {@code volatile} read
1483 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1484 *
1485 * @return {@code true} if successful
1486 */
1487 @IntrinsicCandidate
1488 public final native boolean compareAndSetInt(Object o, long offset,
1489 int expected,
1490 int x);
1491
1492 @IntrinsicCandidate
1493 public final native int compareAndExchangeInt(Object o, long offset,
1494 int expected,
1495 int x);
1496
1497 @IntrinsicCandidate
2073 public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2074 long expected,
2075 long x) {
2076 return compareAndSetLong(o, offset, expected, x);
2077 }
2078
2079 @IntrinsicCandidate
2080 public final boolean weakCompareAndSetLong(Object o, long offset,
2081 long expected,
2082 long x) {
2083 return compareAndSetLong(o, offset, expected, x);
2084 }
2085
2086 /**
2087 * Fetches a reference value from a given Java variable, with volatile
2088 * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2089 */
2090 @IntrinsicCandidate
2091 public native Object getReferenceVolatile(Object o, long offset);
2092
2093 /**
2094 * Stores a reference value into a given Java variable, with
2095 * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2096 */
2097 @IntrinsicCandidate
2098 public native void putReferenceVolatile(Object o, long offset, Object x);
2099
2100 /** Volatile version of {@link #getInt(Object, long)} */
2101 @IntrinsicCandidate
2102 public native int getIntVolatile(Object o, long offset);
2103
2104 /** Volatile version of {@link #putInt(Object, long, int)} */
2105 @IntrinsicCandidate
2106 public native void putIntVolatile(Object o, long offset, int x);
2107
2108 /** Volatile version of {@link #getBoolean(Object, long)} */
2109 @IntrinsicCandidate
2110 public native boolean getBooleanVolatile(Object o, long offset);
2111
2112 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */
2113 @IntrinsicCandidate
2114 public native void putBooleanVolatile(Object o, long offset, boolean x);
2115
2116 /** Volatile version of {@link #getByte(Object, long)} */
2117 @IntrinsicCandidate
2118 public native byte getByteVolatile(Object o, long offset);
2119
2152 /** Volatile version of {@link #putFloat(Object, long, float)} */
2153 @IntrinsicCandidate
2154 public native void putFloatVolatile(Object o, long offset, float x);
2155
2156 /** Volatile version of {@link #getDouble(Object, long)} */
2157 @IntrinsicCandidate
2158 public native double getDoubleVolatile(Object o, long offset);
2159
2160 /** Volatile version of {@link #putDouble(Object, long, double)} */
2161 @IntrinsicCandidate
2162 public native void putDoubleVolatile(Object o, long offset, double x);
2163
2164
2165
2166 /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2167 @IntrinsicCandidate
2168 public final Object getReferenceAcquire(Object o, long offset) {
2169 return getReferenceVolatile(o, offset);
2170 }
2171
2172 /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2173 @IntrinsicCandidate
2174 public final boolean getBooleanAcquire(Object o, long offset) {
2175 return getBooleanVolatile(o, offset);
2176 }
2177
2178 /** Acquire version of {@link #getByteVolatile(Object, long)} */
2179 @IntrinsicCandidate
2180 public final byte getByteAcquire(Object o, long offset) {
2181 return getByteVolatile(o, offset);
2182 }
2183
2184 /** Acquire version of {@link #getShortVolatile(Object, long)} */
2185 @IntrinsicCandidate
2186 public final short getShortAcquire(Object o, long offset) {
2187 return getShortVolatile(o, offset);
2188 }
2189
2190 /** Acquire version of {@link #getCharVolatile(Object, long)} */
2191 @IntrinsicCandidate
2216 public final double getDoubleAcquire(Object o, long offset) {
2217 return getDoubleVolatile(o, offset);
2218 }
2219
2220 /*
2221 * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2222 * that do not guarantee immediate visibility of the store to
2223 * other threads. This method is generally only useful if the
2224 * underlying field is a Java volatile (or if an array cell, one
2225 * that is otherwise only accessed using volatile accesses).
2226 *
2227 * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2228 */
2229
2230 /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2231 @IntrinsicCandidate
2232 public final void putReferenceRelease(Object o, long offset, Object x) {
2233 putReferenceVolatile(o, offset, x);
2234 }
2235
2236 /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2237 @IntrinsicCandidate
2238 public final void putBooleanRelease(Object o, long offset, boolean x) {
2239 putBooleanVolatile(o, offset, x);
2240 }
2241
2242 /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2243 @IntrinsicCandidate
2244 public final void putByteRelease(Object o, long offset, byte x) {
2245 putByteVolatile(o, offset, x);
2246 }
2247
2248 /** Release version of {@link #putShortVolatile(Object, long, short)} */
2249 @IntrinsicCandidate
2250 public final void putShortRelease(Object o, long offset, short x) {
2251 putShortVolatile(o, offset, x);
2252 }
2253
2254 /** Release version of {@link #putCharVolatile(Object, long, char)} */
2255 @IntrinsicCandidate
2272 /** Release version of {@link #putLongVolatile(Object, long, long)} */
2273 @IntrinsicCandidate
2274 public final void putLongRelease(Object o, long offset, long x) {
2275 putLongVolatile(o, offset, x);
2276 }
2277
2278 /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2279 @IntrinsicCandidate
2280 public final void putDoubleRelease(Object o, long offset, double x) {
2281 putDoubleVolatile(o, offset, x);
2282 }
2283
2284 // ------------------------------ Opaque --------------------------------------
2285
2286 /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2287 @IntrinsicCandidate
2288 public final Object getReferenceOpaque(Object o, long offset) {
2289 return getReferenceVolatile(o, offset);
2290 }
2291
2292 /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2293 @IntrinsicCandidate
2294 public final boolean getBooleanOpaque(Object o, long offset) {
2295 return getBooleanVolatile(o, offset);
2296 }
2297
2298 /** Opaque version of {@link #getByteVolatile(Object, long)} */
2299 @IntrinsicCandidate
2300 public final byte getByteOpaque(Object o, long offset) {
2301 return getByteVolatile(o, offset);
2302 }
2303
2304 /** Opaque version of {@link #getShortVolatile(Object, long)} */
2305 @IntrinsicCandidate
2306 public final short getShortOpaque(Object o, long offset) {
2307 return getShortVolatile(o, offset);
2308 }
2309
2310 /** Opaque version of {@link #getCharVolatile(Object, long)} */
2311 @IntrinsicCandidate
2326 }
2327
2328 /** Opaque version of {@link #getLongVolatile(Object, long)} */
2329 @IntrinsicCandidate
2330 public final long getLongOpaque(Object o, long offset) {
2331 return getLongVolatile(o, offset);
2332 }
2333
2334 /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2335 @IntrinsicCandidate
2336 public final double getDoubleOpaque(Object o, long offset) {
2337 return getDoubleVolatile(o, offset);
2338 }
2339
2340 /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2341 @IntrinsicCandidate
2342 public final void putReferenceOpaque(Object o, long offset, Object x) {
2343 putReferenceVolatile(o, offset, x);
2344 }
2345
2346 /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2347 @IntrinsicCandidate
2348 public final void putBooleanOpaque(Object o, long offset, boolean x) {
2349 putBooleanVolatile(o, offset, x);
2350 }
2351
2352 /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2353 @IntrinsicCandidate
2354 public final void putByteOpaque(Object o, long offset, byte x) {
2355 putByteVolatile(o, offset, x);
2356 }
2357
2358 /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2359 @IntrinsicCandidate
2360 public final void putShortOpaque(Object o, long offset, short x) {
2361 putShortVolatile(o, offset, x);
2362 }
2363
2364 /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2365 @IntrinsicCandidate
2374 }
2375
2376 /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2377 @IntrinsicCandidate
2378 public final void putFloatOpaque(Object o, long offset, float x) {
2379 putFloatVolatile(o, offset, x);
2380 }
2381
2382 /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2383 @IntrinsicCandidate
2384 public final void putLongOpaque(Object o, long offset, long x) {
2385 putLongVolatile(o, offset, x);
2386 }
2387
2388 /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2389 @IntrinsicCandidate
2390 public final void putDoubleOpaque(Object o, long offset, double x) {
2391 putDoubleVolatile(o, offset, x);
2392 }
2393
2394 /**
2395 * Unblocks the given thread blocked on {@code park}, or, if it is
2396 * not blocked, causes the subsequent call to {@code park} not to
2397 * block. Note: this operation is "unsafe" solely because the
2398 * caller must somehow ensure that the thread has not been
2399 * destroyed. Nothing special is usually required to ensure this
2400 * when called from Java (in which there will ordinarily be a live
2401 * reference to the thread) but this is not nearly-automatically
2402 * so when calling from native code.
2403 *
2404 * @param thread the thread to unpark.
2405 */
2406 @IntrinsicCandidate
2407 public native void unpark(Object thread);
2408
2409 /**
2410 * Blocks current thread, returning when a balancing
2411 * {@code unpark} occurs, or a balancing {@code unpark} has
2412 * already occurred, or the thread is interrupted, or, if not
2413 * absolute and time is not zero, the given time nanoseconds have
2760 /**
2761 * Atomically exchanges the given reference value with the current
2762 * reference value of a field or array element within the given
2763 * object {@code o} at the given {@code offset}.
2764 *
2765 * @param o object/array to update the field/element in
2766 * @param offset field/element offset
2767 * @param newValue new value
2768 * @return the previous value
2769 * @since 1.8
2770 */
2771 @IntrinsicCandidate
2772 public final Object getAndSetReference(Object o, long offset, Object newValue) {
2773 Object v;
2774 do {
2775 v = getReferenceVolatile(o, offset);
2776 } while (!weakCompareAndSetReference(o, offset, v, newValue));
2777 return v;
2778 }
2779
2780 @ForceInline
2781 public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
2782 Object v;
2783 do {
2784 v = getReference(o, offset);
2785 } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
2786 return v;
2787 }
2788
2789 @ForceInline
2790 public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
2791 Object v;
2792 do {
2793 v = getReferenceAcquire(o, offset);
2794 } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
2795 return v;
2796 }
2797
2798 @IntrinsicCandidate
2799 public final byte getAndSetByte(Object o, long offset, byte newValue) {
2800 byte v;
2801 do {
2802 v = getByteVolatile(o, offset);
2803 } while (!weakCompareAndSetByte(o, offset, v, newValue));
2804 return v;
2805 }
2806
2807 @ForceInline
2808 public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
2809 byte v;
2810 do {
2811 v = getByte(o, offset);
2812 } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
2813 return v;
2814 }
2815
2816 @ForceInline
2817 public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {
3835 private static long convEndian(boolean big, long n) { return big == BIG_ENDIAN ? n : Long.reverseBytes(n) ; }
3836
3837
3838
3839 private native long allocateMemory0(long bytes);
3840 private native long reallocateMemory0(long address, long bytes);
3841 private native void freeMemory0(long address);
3842 @IntrinsicCandidate
3843 private native void setMemory0(Object o, long offset, long bytes, byte value);
3844 @IntrinsicCandidate
3845 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
3846 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
3847 private native long objectFieldOffset0(Field f);
3848 private native long objectFieldOffset1(Class<?> c, String name);
3849 private native long staticFieldOffset0(Field f);
3850 private native Object staticFieldBase0(Field f);
3851 private native boolean shouldBeInitialized0(Class<?> c);
3852 private native void ensureClassInitialized0(Class<?> c);
3853 private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetic
3854 private native int arrayIndexScale0(Class<?> arrayClass);
3855 private native int getLoadAverage0(double[] loadavg, int nelems);
3856
3857
3858 /**
3859 * Invokes the given direct byte buffer's cleaner, if any.
3860 *
3861 * @param directBuffer a direct byte buffer
3862 * @throws NullPointerException if {@code directBuffer} is null
3863 * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
3864 * or is a {@link java.nio.Buffer#slice slice}, or is a
3865 * {@link java.nio.Buffer#duplicate duplicate}
3866 */
3867 public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
3868 if (!directBuffer.isDirect())
3869 throw new IllegalArgumentException("buffer is non-direct");
3870
3871 DirectBuffer db = (DirectBuffer) directBuffer;
3872 if (db.attachment() != null)
3873 throw new IllegalArgumentException("duplicate or slice");
3874
|
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.internal.misc;
27
28 import jdk.internal.ref.Cleaner;
29 import jdk.internal.value.ValueClass;
30 import jdk.internal.vm.annotation.ForceInline;
31 import jdk.internal.vm.annotation.IntrinsicCandidate;
32 import sun.nio.ch.DirectBuffer;
33
34 import java.lang.reflect.Field;
35 import java.security.ProtectionDomain;
36
37 import static jdk.internal.misc.UnsafeConstants.*;
38
39 /**
40 * A collection of methods for performing low-level, unsafe operations.
41 * Although the class and all methods are public, use of this class is
42 * limited because only trusted code can obtain instances of it.
43 *
44 * <em>Note:</em> It is the responsibility of the caller to make sure
45 * arguments are checked before methods of this class are
46 * called. While some rudimentary checks are performed on the input,
47 * the checks are best effort and when performance is an overriding
48 * priority, as when methods of this class are optimized by the
49 * runtime compiler, some or all checks (if any) may be elided. Hence,
166 * The first two parameters are interpreted exactly as with
167 * {@link #getInt(Object, long)} to refer to a specific
168 * Java variable (field or array element). The given value
169 * is stored into that variable.
170 * <p>
171 * The variable must be of the same type as the method
172 * parameter {@code x}.
173 *
174 * @param o Java heap object in which the variable resides, if any, else
175 * null
176 * @param offset indication of where the variable resides in a Java heap
177 * object, if any, else a memory address locating the variable
178 * statically
179 * @param x the value to store into the indicated Java variable
180 * @throws RuntimeException No defined exceptions are thrown, not even
181 * {@link NullPointerException}
182 */
183 @IntrinsicCandidate
184 public native void putInt(Object o, long offset, int x);
185
186
187 /**
188 * Returns true if the given field is flattened.
189 */
190 public boolean isFlatField(Field f) {
191 if (f == null) {
192 throw new NullPointerException();
193 }
194 return isFlatField0(f);
195 }
196
197 private native boolean isFlatField0(Object o);
198
199 /* Returns true if the given field has a null marker
200 * <p>
201 * Nullable flat fields are stored in a flattened representation
202 * and have an associated null marker to indicate if the the field value is
203 * null or the one stored with the flat representation
204 */
205
206 public boolean hasNullMarker(Field f) {
207 if (f == null) {
208 throw new NullPointerException();
209 }
210 return hasNullMarker0(f);
211 }
212
213 private native boolean hasNullMarker0(Object o);
214
215 /* Returns the offset of the null marker of the field,
216 * or -1 if the field doesn't have a null marker
217 */
218
219 public int nullMarkerOffset(Field f) {
220 if (f == null) {
221 throw new NullPointerException();
222 }
223 return nullMarkerOffset0(f);
224 }
225
226 private native int nullMarkerOffset0(Object o);
227
228 public static final int NON_FLAT_LAYOUT = 0;
229
230 /* Reports the kind of layout used for an element in the storage
231 * allocation of the given array. Do not expect to perform any logic
232 * or layout control with this value, it is just an opaque token
233 * used for performance reasons.
234 *
235 * A layout of 0 indicates this array is not flat.
236 */
237 public int arrayLayout(Class<?> arrayClass) {
238 if (arrayClass == null) {
239 throw new NullPointerException();
240 }
241 return arrayLayout0(arrayClass);
242 }
243
244 private native int arrayLayout0(Object o);
245
246
247 /* Reports the kind of layout used for a given field in the storage
248 * allocation of its class. Do not expect to perform any logic
249 * or layout control with this value, it is just an opaque token
250 * used for performance reasons.
251 *
252 * A layout of 0 indicates this field is not flat.
253 */
254 public int fieldLayout(Field f) {
255 if (f == null) {
256 throw new NullPointerException();
257 }
258 return fieldLayout0(f);
259 }
260
261 private native int fieldLayout0(Object o);
262
263 public native Object[] newSpecialArray(Class<?> componentType,
264 int length, int layoutKind);
265
266 /**
267 * Returns true if the given class is a flattened array.
268 */
269 @IntrinsicCandidate
270 public native boolean isFlatArray(Class<?> arrayClass);
271
272 /**
273 * Fetches a reference value from a given Java variable.
274 * This method can return a reference to either an object or value
275 * or a null reference.
276 *
277 * @see #getInt(Object, long)
278 */
279 @IntrinsicCandidate
280 public native Object getReference(Object o, long offset);
281
282 /**
283 * Stores a reference value into a given Java variable.
284 * This method can store a reference to either an object or value
285 * or a null reference.
286 * <p>
287 * Unless the reference {@code x} being stored is either null
288 * or matches the field type, the results are undefined.
289 * If the reference {@code o} is non-null, card marks or
290 * other store barriers for that object (if the VM requires them)
291 * are updated.
292 * @see #putInt(Object, long, int)
293 */
294 @IntrinsicCandidate
295 public native void putReference(Object o, long offset, Object x);
296
297 /**
298 * Fetches a value of type {@code <V>} from a given Java variable.
299 * More specifically, fetches a field or array element within the given
300 * {@code o} object at the given offset, or (if {@code o} is null)
301 * from the memory address whose numerical value is the given offset.
302 *
303 * @param o Java heap object in which the variable resides, if any, else
304 * null
305 * @param offset indication of where the variable resides in a Java heap
306 * object, if any, else a memory address locating the variable
307 * statically
308 * @param valueType value type
309 * @param <V> the type of a value
310 * @return the value fetched from the indicated Java variable
311 * @throws RuntimeException No defined exceptions are thrown, not even
312 * {@link NullPointerException}
313 */
314 @IntrinsicCandidate
315 public native <V> V getValue(Object o, long offset, Class<?> valueType);
316
317 /**
318 * Fetches a value of type {@code <V>} from a given Java variable.
319 * More specifically, fetches a field or array element within the given
320 * {@code o} object at the given offset, or (if {@code o} is null)
321 * from the memory address whose numerical value is the given offset.
322 *
323 * @param o Java heap object in which the variable resides, if any, else
324 * null
325 * @param offset indication of where the variable resides in a Java heap
326 * object, if any, else a memory address locating the variable
327 * statically
328 * @param layoutKind opaque value used by the VM to know the layout
329 * the field or array element. This value must be retrieved with
330 * {@link #fieldLayout} or {@link #arrayLayout}.
331 * @param valueType value type
332 * @param <V> the type of a value
333 * @return the value fetched from the indicated Java variable
334 * @throws RuntimeException No defined exceptions are thrown, not even
335 * {@link NullPointerException}
336 */
337 public native <V> V getFlatValue(Object o, long offset, int layoutKind, Class<?> valueType);
338
339
340 /**
341 * Stores the given value into a given Java variable.
342 *
343 * Unless the reference {@code o} being stored is either null
344 * or matches the field type, the results are undefined.
345 *
346 * @param o Java heap object in which the variable resides, if any, else
347 * null
348 * @param offset indication of where the variable resides in a Java heap
349 * object, if any, else a memory address locating the variable
350 * statically
351 * @param valueType value type
352 * @param v the value to store into the indicated Java variable
353 * @param <V> the type of a value
354 * @throws RuntimeException No defined exceptions are thrown, not even
355 * {@link NullPointerException}
356 */
357 @IntrinsicCandidate
358 public native <V> void putValue(Object o, long offset, Class<?> valueType, V v);
359
360 /**
361 * Stores the given value into a given Java variable.
362 *
363 * Unless the reference {@code o} being stored is either null
364 * or matches the field type, the results are undefined.
365 *
366 * @param o Java heap object in which the variable resides, if any, else
367 * null
368 * @param offset indication of where the variable resides in a Java heap
369 * object, if any, else a memory address locating the variable
370 * statically
371 * @param layoutKind opaque value used by the VM to know the layout
372 * the field or array element. This value must be retrieved with
373 * {@link #fieldLayout} or {@link #arrayLayout}.
374 * @param valueType value type
375 * @param v the value to store into the indicated Java variable
376 * @param <V> the type of a value
377 * @throws RuntimeException No defined exceptions are thrown, not even
378 * {@link NullPointerException}
379 */
380 public native <V> void putFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, V v);
381
382 /**
383 * Returns an object instance with a private buffered value whose layout
384 * and contents is exactly the given value instance. The return object
385 * is in the larval state that can be updated using the unsafe put operation.
386 *
387 * @param value a value instance
388 * @param <V> the type of the given value instance
389 */
390 @IntrinsicCandidate
391 public native <V> V makePrivateBuffer(V value);
392
393 /**
394 * Exits the larval state and returns a value instance.
395 *
396 * @param value a value instance
397 * @param <V> the type of the given value instance
398 */
399 @IntrinsicCandidate
400 public native <V> V finishPrivateBuffer(V value);
401
402 /**
403 * Returns the header size of the given value type.
404 *
405 * @param valueType value type
406 * @return the header size of the value type
407 */
408 public native <V> long valueHeaderSize(Class<V> valueType);
409
410 /** @see #getInt(Object, long) */
411 @IntrinsicCandidate
412 public native boolean getBoolean(Object o, long offset);
413
414 /** @see #putInt(Object, long, int) */
415 @IntrinsicCandidate
416 public native void putBoolean(Object o, long offset, boolean x);
417
418 /** @see #getInt(Object, long) */
419 @IntrinsicCandidate
420 public native byte getByte(Object o, long offset);
421
422 /** @see #putInt(Object, long, int) */
423 @IntrinsicCandidate
424 public native void putByte(Object o, long offset, byte x);
425
426 /** @see #getInt(Object, long) */
427 @IntrinsicCandidate
428 public native short getShort(Object o, long offset);
429
1438 * allocation of a given array class. However, arrays of "narrow" types
1439 * will generally not work properly with accessors like {@link
1440 * #getByte(Object, long)}, so the scale factor for such classes is reported
1441 * as zero.
1442 * <p>
1443 * The computation of the actual memory offset should always use {@code
1444 * long} arithmetic to avoid overflows.
1445 *
1446 * @see #arrayBaseOffset
1447 * @see #getInt(Object, long)
1448 * @see #putInt(Object, long, int)
1449 */
1450 public int arrayIndexScale(Class<?> arrayClass) {
1451 if (arrayClass == null) {
1452 throw new NullPointerException();
1453 }
1454
1455 return arrayIndexScale0(arrayClass);
1456 }
1457
1458 /**
1459 * Return the size of the object in the heap.
1460 * @param o an object
1461 * @return the objects's size
1462 * @since Valhalla
1463 */
1464 public long getObjectSize(Object o) {
1465 if (o == null)
1466 throw new NullPointerException();
1467 return getObjectSize0(o);
1468 }
1469
1470 /** The value of {@code arrayIndexScale(boolean[].class)} */
1471 public static final int ARRAY_BOOLEAN_INDEX_SCALE
1472 = theUnsafe.arrayIndexScale(boolean[].class);
1473
1474 /** The value of {@code arrayIndexScale(byte[].class)} */
1475 public static final int ARRAY_BYTE_INDEX_SCALE
1476 = theUnsafe.arrayIndexScale(byte[].class);
1477
1478 /** The value of {@code arrayIndexScale(short[].class)} */
1479 public static final int ARRAY_SHORT_INDEX_SCALE
1480 = theUnsafe.arrayIndexScale(short[].class);
1481
1482 /** The value of {@code arrayIndexScale(char[].class)} */
1483 public static final int ARRAY_CHAR_INDEX_SCALE
1484 = theUnsafe.arrayIndexScale(char[].class);
1485
1486 /** The value of {@code arrayIndexScale(int[].class)} */
1487 public static final int ARRAY_INT_INDEX_SCALE
1488 = theUnsafe.arrayIndexScale(int[].class);
1627 return null;
1628 }
1629
1630 /** Throws the exception without telling the verifier. */
1631 public native void throwException(Throwable ee);
1632
1633 /**
1634 * Atomically updates Java variable to {@code x} if it is currently
1635 * holding {@code expected}.
1636 *
1637 * <p>This operation has memory semantics of a {@code volatile} read
1638 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1639 *
1640 * @return {@code true} if successful
1641 */
1642 @IntrinsicCandidate
1643 public final native boolean compareAndSetReference(Object o, long offset,
1644 Object expected,
1645 Object x);
1646
1647 private final boolean isValueObject(Object o) {
1648 return o != null && o.getClass().isValue();
1649 }
1650
1651 /*
1652 * For value type, CAS should do substitutability test as opposed
1653 * to two pointers comparison.
1654 */
1655 @ForceInline
1656 public final <V> boolean compareAndSetReference(Object o, long offset,
1657 Class<?> type,
1658 V expected,
1659 V x) {
1660 if (type.isValue() || isValueObject(expected)) {
1661 while (true) {
1662 Object witness = getReferenceVolatile(o, offset);
1663 if (witness != expected) {
1664 return false;
1665 }
1666 if (compareAndSetReference(o, offset, witness, x)) {
1667 return true;
1668 }
1669 }
1670 } else {
1671 return compareAndSetReference(o, offset, expected, x);
1672 }
1673 }
1674
1675 @ForceInline
1676 public final <V> boolean compareAndSetFlatValue(Object o, long offset,
1677 int layout,
1678 Class<?> valueType,
1679 V expected,
1680 V x) {
1681 while (true) {
1682 Object witness = getFlatValueVolatile(o, offset, layout, valueType);
1683 if (witness != expected) {
1684 return false;
1685 }
1686 if (compareAndSetFlatValueAsBytes(o, offset, layout, valueType, witness, x)) {
1687 return true;
1688 }
1689 }
1690 }
1691
1692 @IntrinsicCandidate
1693 public final native Object compareAndExchangeReference(Object o, long offset,
1694 Object expected,
1695 Object x);
1696
1697 @ForceInline
1698 public final <V> Object compareAndExchangeReference(Object o, long offset,
1699 Class<?> valueType,
1700 V expected,
1701 V x) {
1702 if (valueType.isValue() || isValueObject(expected)) {
1703 while (true) {
1704 Object witness = getReferenceVolatile(o, offset);
1705 if (witness != expected) {
1706 return witness;
1707 }
1708 if (compareAndSetReference(o, offset, witness, x)) {
1709 return witness;
1710 }
1711 }
1712 } else {
1713 return compareAndExchangeReference(o, offset, expected, x);
1714 }
1715 }
1716
1717 @ForceInline
1718 public final <V> Object compareAndExchangeFlatValue(Object o, long offset,
1719 int layout,
1720 Class<?> valueType,
1721 V expected,
1722 V x) {
1723 while (true) {
1724 Object witness = getFlatValueVolatile(o, offset, layout, valueType);
1725 if (witness != expected) {
1726 return witness;
1727 }
1728 if (compareAndSetFlatValueAsBytes(o, offset, layout, valueType, witness, x)) {
1729 return witness;
1730 }
1731 }
1732 }
1733
1734 @IntrinsicCandidate
1735 public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1736 Object expected,
1737 Object x) {
1738 return compareAndExchangeReference(o, offset, expected, x);
1739 }
1740
1741 public final <V> Object compareAndExchangeReferenceAcquire(Object o, long offset,
1742 Class<?> valueType,
1743 V expected,
1744 V x) {
1745 return compareAndExchangeReference(o, offset, valueType, expected, x);
1746 }
1747
1748 @ForceInline
1749 public final <V> Object compareAndExchangeFlatValueAcquire(Object o, long offset,
1750 int layout,
1751 Class<?> valueType,
1752 V expected,
1753 V x) {
1754 return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1755 }
1756
1757 @IntrinsicCandidate
1758 public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1759 Object expected,
1760 Object x) {
1761 return compareAndExchangeReference(o, offset, expected, x);
1762 }
1763
1764 public final <V> Object compareAndExchangeReferenceRelease(Object o, long offset,
1765 Class<?> valueType,
1766 V expected,
1767 V x) {
1768 return compareAndExchangeReference(o, offset, valueType, expected, x);
1769 }
1770
1771 @ForceInline
1772 public final <V> Object compareAndExchangeFlatValueRelease(Object o, long offset,
1773 int layout,
1774 Class<?> valueType,
1775 V expected,
1776 V x) {
1777 return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1778 }
1779
1780 @IntrinsicCandidate
1781 public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1782 Object expected,
1783 Object x) {
1784 return compareAndSetReference(o, offset, expected, x);
1785 }
1786
1787 public final <V> boolean weakCompareAndSetReferencePlain(Object o, long offset,
1788 Class<?> valueType,
1789 V expected,
1790 V x) {
1791 if (valueType.isValue() || isValueObject(expected)) {
1792 return compareAndSetReference(o, offset, valueType, expected, x);
1793 } else {
1794 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1795 }
1796 }
1797
1798 @ForceInline
1799 public final <V> boolean weakCompareAndSetFlatValuePlain(Object o, long offset,
1800 int layout,
1801 Class<?> valueType,
1802 V expected,
1803 V x) {
1804 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1805 }
1806
1807 @IntrinsicCandidate
1808 public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1809 Object expected,
1810 Object x) {
1811 return compareAndSetReference(o, offset, expected, x);
1812 }
1813
1814 public final <V> boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1815 Class<?> valueType,
1816 V expected,
1817 V x) {
1818 if (valueType.isValue() || isValueObject(expected)) {
1819 return compareAndSetReference(o, offset, valueType, expected, x);
1820 } else {
1821 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1822 }
1823 }
1824
1825 @ForceInline
1826 public final <V> boolean weakCompareAndSetFlatValueAcquire(Object o, long offset,
1827 int layout,
1828 Class<?> valueType,
1829 V expected,
1830 V x) {
1831 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1832 }
1833
1834 @IntrinsicCandidate
1835 public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1836 Object expected,
1837 Object x) {
1838 return compareAndSetReference(o, offset, expected, x);
1839 }
1840
1841 public final <V> boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1842 Class<?> valueType,
1843 V expected,
1844 V x) {
1845 if (valueType.isValue() || isValueObject(expected)) {
1846 return compareAndSetReference(o, offset, valueType, expected, x);
1847 } else {
1848 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1849 }
1850 }
1851
1852 @ForceInline
1853 public final <V> boolean weakCompareAndSetFlatValueRelease(Object o, long offset,
1854 int layout,
1855 Class<?> valueType,
1856 V expected,
1857 V x) {
1858 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1859 }
1860
1861 @IntrinsicCandidate
1862 public final boolean weakCompareAndSetReference(Object o, long offset,
1863 Object expected,
1864 Object x) {
1865 return compareAndSetReference(o, offset, expected, x);
1866 }
1867
1868 public final <V> boolean weakCompareAndSetReference(Object o, long offset,
1869 Class<?> valueType,
1870 V expected,
1871 V x) {
1872 if (valueType.isValue() || isValueObject(expected)) {
1873 return compareAndSetReference(o, offset, valueType, expected, x);
1874 } else {
1875 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1876 }
1877 }
1878
1879 @ForceInline
1880 public final <V> boolean weakCompareAndSetFlatValue(Object o, long offset,
1881 int layout,
1882 Class<?> valueType,
1883 V expected,
1884 V x) {
1885 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1886 }
1887
1888 /**
1889 * Atomically updates Java variable to {@code x} if it is currently
1890 * holding {@code expected}.
1891 *
1892 * <p>This operation has memory semantics of a {@code volatile} read
1893 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1894 *
1895 * @return {@code true} if successful
1896 */
1897 @IntrinsicCandidate
1898 public final native boolean compareAndSetInt(Object o, long offset,
1899 int expected,
1900 int x);
1901
1902 @IntrinsicCandidate
1903 public final native int compareAndExchangeInt(Object o, long offset,
1904 int expected,
1905 int x);
1906
1907 @IntrinsicCandidate
2483 public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2484 long expected,
2485 long x) {
2486 return compareAndSetLong(o, offset, expected, x);
2487 }
2488
2489 @IntrinsicCandidate
2490 public final boolean weakCompareAndSetLong(Object o, long offset,
2491 long expected,
2492 long x) {
2493 return compareAndSetLong(o, offset, expected, x);
2494 }
2495
2496 /**
2497 * Fetches a reference value from a given Java variable, with volatile
2498 * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2499 */
2500 @IntrinsicCandidate
2501 public native Object getReferenceVolatile(Object o, long offset);
2502
2503 @ForceInline
2504 public final <V> Object getFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType) {
2505 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2506 Object res = getFlatValue(o, offset, layout, valueType);
2507 fullFence();
2508 return res;
2509 }
2510
2511 /**
2512 * Stores a reference value into a given Java variable, with
2513 * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2514 */
2515 @IntrinsicCandidate
2516 public native void putReferenceVolatile(Object o, long offset, Object x);
2517
2518 @ForceInline
2519 public final <V> void putFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType, V x) {
2520 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2521 putFlatValueRelease(o, offset, layout, valueType, x);
2522 fullFence();
2523 }
2524
2525 /** Volatile version of {@link #getInt(Object, long)} */
2526 @IntrinsicCandidate
2527 public native int getIntVolatile(Object o, long offset);
2528
2529 /** Volatile version of {@link #putInt(Object, long, int)} */
2530 @IntrinsicCandidate
2531 public native void putIntVolatile(Object o, long offset, int x);
2532
2533 /** Volatile version of {@link #getBoolean(Object, long)} */
2534 @IntrinsicCandidate
2535 public native boolean getBooleanVolatile(Object o, long offset);
2536
2537 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */
2538 @IntrinsicCandidate
2539 public native void putBooleanVolatile(Object o, long offset, boolean x);
2540
2541 /** Volatile version of {@link #getByte(Object, long)} */
2542 @IntrinsicCandidate
2543 public native byte getByteVolatile(Object o, long offset);
2544
2577 /** Volatile version of {@link #putFloat(Object, long, float)} */
2578 @IntrinsicCandidate
2579 public native void putFloatVolatile(Object o, long offset, float x);
2580
2581 /** Volatile version of {@link #getDouble(Object, long)} */
2582 @IntrinsicCandidate
2583 public native double getDoubleVolatile(Object o, long offset);
2584
2585 /** Volatile version of {@link #putDouble(Object, long, double)} */
2586 @IntrinsicCandidate
2587 public native void putDoubleVolatile(Object o, long offset, double x);
2588
2589
2590
2591 /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2592 @IntrinsicCandidate
2593 public final Object getReferenceAcquire(Object o, long offset) {
2594 return getReferenceVolatile(o, offset);
2595 }
2596
2597 @ForceInline
2598 public final <V> Object getFlatValueAcquire(Object o, long offset, int layout, Class<?> valueType) {
2599 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2600 Object res = getFlatValue(o, offset, layout, valueType);
2601 loadFence();
2602 return res;
2603 }
2604
2605 /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2606 @IntrinsicCandidate
2607 public final boolean getBooleanAcquire(Object o, long offset) {
2608 return getBooleanVolatile(o, offset);
2609 }
2610
2611 /** Acquire version of {@link #getByteVolatile(Object, long)} */
2612 @IntrinsicCandidate
2613 public final byte getByteAcquire(Object o, long offset) {
2614 return getByteVolatile(o, offset);
2615 }
2616
2617 /** Acquire version of {@link #getShortVolatile(Object, long)} */
2618 @IntrinsicCandidate
2619 public final short getShortAcquire(Object o, long offset) {
2620 return getShortVolatile(o, offset);
2621 }
2622
2623 /** Acquire version of {@link #getCharVolatile(Object, long)} */
2624 @IntrinsicCandidate
2649 public final double getDoubleAcquire(Object o, long offset) {
2650 return getDoubleVolatile(o, offset);
2651 }
2652
2653 /*
2654 * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2655 * that do not guarantee immediate visibility of the store to
2656 * other threads. This method is generally only useful if the
2657 * underlying field is a Java volatile (or if an array cell, one
2658 * that is otherwise only accessed using volatile accesses).
2659 *
2660 * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2661 */
2662
2663 /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2664 @IntrinsicCandidate
2665 public final void putReferenceRelease(Object o, long offset, Object x) {
2666 putReferenceVolatile(o, offset, x);
2667 }
2668
2669 @ForceInline
2670 public final <V> void putFlatValueRelease(Object o, long offset, int layout, Class<?> valueType, V x) {
2671 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2672 storeFence();
2673 putFlatValue(o, offset, layout, valueType, x);
2674 }
2675
2676 /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2677 @IntrinsicCandidate
2678 public final void putBooleanRelease(Object o, long offset, boolean x) {
2679 putBooleanVolatile(o, offset, x);
2680 }
2681
2682 /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2683 @IntrinsicCandidate
2684 public final void putByteRelease(Object o, long offset, byte x) {
2685 putByteVolatile(o, offset, x);
2686 }
2687
2688 /** Release version of {@link #putShortVolatile(Object, long, short)} */
2689 @IntrinsicCandidate
2690 public final void putShortRelease(Object o, long offset, short x) {
2691 putShortVolatile(o, offset, x);
2692 }
2693
2694 /** Release version of {@link #putCharVolatile(Object, long, char)} */
2695 @IntrinsicCandidate
2712 /** Release version of {@link #putLongVolatile(Object, long, long)} */
2713 @IntrinsicCandidate
2714 public final void putLongRelease(Object o, long offset, long x) {
2715 putLongVolatile(o, offset, x);
2716 }
2717
2718 /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2719 @IntrinsicCandidate
2720 public final void putDoubleRelease(Object o, long offset, double x) {
2721 putDoubleVolatile(o, offset, x);
2722 }
2723
2724 // ------------------------------ Opaque --------------------------------------
2725
2726 /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2727 @IntrinsicCandidate
2728 public final Object getReferenceOpaque(Object o, long offset) {
2729 return getReferenceVolatile(o, offset);
2730 }
2731
2732 @ForceInline
2733 public final <V> Object getFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType) {
2734 // this is stronger than opaque semantics
2735 return getFlatValueAcquire(o, offset, layout, valueType);
2736 }
2737
2738 /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2739 @IntrinsicCandidate
2740 public final boolean getBooleanOpaque(Object o, long offset) {
2741 return getBooleanVolatile(o, offset);
2742 }
2743
2744 /** Opaque version of {@link #getByteVolatile(Object, long)} */
2745 @IntrinsicCandidate
2746 public final byte getByteOpaque(Object o, long offset) {
2747 return getByteVolatile(o, offset);
2748 }
2749
2750 /** Opaque version of {@link #getShortVolatile(Object, long)} */
2751 @IntrinsicCandidate
2752 public final short getShortOpaque(Object o, long offset) {
2753 return getShortVolatile(o, offset);
2754 }
2755
2756 /** Opaque version of {@link #getCharVolatile(Object, long)} */
2757 @IntrinsicCandidate
2772 }
2773
2774 /** Opaque version of {@link #getLongVolatile(Object, long)} */
2775 @IntrinsicCandidate
2776 public final long getLongOpaque(Object o, long offset) {
2777 return getLongVolatile(o, offset);
2778 }
2779
2780 /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2781 @IntrinsicCandidate
2782 public final double getDoubleOpaque(Object o, long offset) {
2783 return getDoubleVolatile(o, offset);
2784 }
2785
2786 /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2787 @IntrinsicCandidate
2788 public final void putReferenceOpaque(Object o, long offset, Object x) {
2789 putReferenceVolatile(o, offset, x);
2790 }
2791
2792 @ForceInline
2793 public final <V> void putFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType, V x) {
2794 // this is stronger than opaque semantics
2795 putFlatValueRelease(o, offset, layout, valueType, x);
2796 }
2797
2798 /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2799 @IntrinsicCandidate
2800 public final void putBooleanOpaque(Object o, long offset, boolean x) {
2801 putBooleanVolatile(o, offset, x);
2802 }
2803
2804 /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2805 @IntrinsicCandidate
2806 public final void putByteOpaque(Object o, long offset, byte x) {
2807 putByteVolatile(o, offset, x);
2808 }
2809
2810 /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2811 @IntrinsicCandidate
2812 public final void putShortOpaque(Object o, long offset, short x) {
2813 putShortVolatile(o, offset, x);
2814 }
2815
2816 /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2817 @IntrinsicCandidate
2826 }
2827
2828 /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2829 @IntrinsicCandidate
2830 public final void putFloatOpaque(Object o, long offset, float x) {
2831 putFloatVolatile(o, offset, x);
2832 }
2833
2834 /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2835 @IntrinsicCandidate
2836 public final void putLongOpaque(Object o, long offset, long x) {
2837 putLongVolatile(o, offset, x);
2838 }
2839
2840 /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2841 @IntrinsicCandidate
2842 public final void putDoubleOpaque(Object o, long offset, double x) {
2843 putDoubleVolatile(o, offset, x);
2844 }
2845
2846 @ForceInline
2847 private boolean compareAndSetFlatValueAsBytes(Object o, long offset, int layout, Class<?> valueType, Object expected, Object x) {
2848 // We turn the payload of an atomic value into a numeric value (of suitable type)
2849 // by storing the value into an array element (of matching layout) and by reading
2850 // back the array element as an integral value. After which we can implement the CAS
2851 // as a plain numeric CAS. Note: this only works if the payload contains no oops
2852 // (see VarHandles::isAtomicFlat).
2853 Object expectedArray = newSpecialArray(valueType, 1, layout);
2854 Object xArray = newSpecialArray(valueType, 1, layout);
2855 long base = arrayBaseOffset(expectedArray.getClass());
2856 int scale = arrayIndexScale(expectedArray.getClass());
2857 putFlatValue(expectedArray, base, layout, valueType, expected);
2858 putFlatValue(xArray, base, layout, valueType, x);
2859 switch (scale) {
2860 case 1: {
2861 byte expectedByte = getByte(expectedArray, base);
2862 byte xByte = getByte(xArray, base);
2863 return compareAndSetByte(o, offset, expectedByte, xByte);
2864 }
2865 case 2: {
2866 short expectedShort = getShort(expectedArray, base);
2867 short xShort = getShort(xArray, base);
2868 return compareAndSetShort(o, offset, expectedShort, xShort);
2869 }
2870 case 4: {
2871 int expectedInt = getInt(expectedArray, base);
2872 int xInt = getInt(xArray, base);
2873 return compareAndSetInt(o, offset, expectedInt, xInt);
2874 }
2875 case 8: {
2876 long expectedLong = getLong(expectedArray, base);
2877 long xLong = getLong(xArray, base);
2878 return compareAndSetLong(o, offset, expectedLong, xLong);
2879 }
2880 default: {
2881 throw new UnsupportedOperationException();
2882 }
2883 }
2884 }
2885
2886 /**
2887 * Unblocks the given thread blocked on {@code park}, or, if it is
2888 * not blocked, causes the subsequent call to {@code park} not to
2889 * block. Note: this operation is "unsafe" solely because the
2890 * caller must somehow ensure that the thread has not been
2891 * destroyed. Nothing special is usually required to ensure this
2892 * when called from Java (in which there will ordinarily be a live
2893 * reference to the thread) but this is not nearly-automatically
2894 * so when calling from native code.
2895 *
2896 * @param thread the thread to unpark.
2897 */
2898 @IntrinsicCandidate
2899 public native void unpark(Object thread);
2900
2901 /**
2902 * Blocks current thread, returning when a balancing
2903 * {@code unpark} occurs, or a balancing {@code unpark} has
2904 * already occurred, or the thread is interrupted, or, if not
2905 * absolute and time is not zero, the given time nanoseconds have
3252 /**
3253 * Atomically exchanges the given reference value with the current
3254 * reference value of a field or array element within the given
3255 * object {@code o} at the given {@code offset}.
3256 *
3257 * @param o object/array to update the field/element in
3258 * @param offset field/element offset
3259 * @param newValue new value
3260 * @return the previous value
3261 * @since 1.8
3262 */
3263 @IntrinsicCandidate
3264 public final Object getAndSetReference(Object o, long offset, Object newValue) {
3265 Object v;
3266 do {
3267 v = getReferenceVolatile(o, offset);
3268 } while (!weakCompareAndSetReference(o, offset, v, newValue));
3269 return v;
3270 }
3271
3272 @ForceInline
3273 public final Object getAndSetReference(Object o, long offset, Class<?> valueType, Object newValue) {
3274 Object v;
3275 do {
3276 v = getReferenceVolatile(o, offset);
3277 } while (!compareAndSetReference(o, offset, valueType, v, newValue));
3278 return v;
3279 }
3280
3281 @ForceInline
3282 public Object getAndSetFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, Object newValue) {
3283 Object v;
3284 do {
3285 v = getFlatValueVolatile(o, offset, layoutKind, valueType);
3286 } while (!compareAndSetFlatValue(o, offset, layoutKind, valueType, v, newValue));
3287 return v;
3288 }
3289
3290 @ForceInline
3291 public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
3292 Object v;
3293 do {
3294 v = getReference(o, offset);
3295 } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
3296 return v;
3297 }
3298
3299 @ForceInline
3300 public final Object getAndSetReferenceRelease(Object o, long offset, Class<?> valueType, Object newValue) {
3301 return getAndSetReference(o, offset, valueType, newValue);
3302 }
3303
3304 @ForceInline
3305 public Object getAndSetFlatValueRelease(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3306 return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3307 }
3308
3309 @ForceInline
3310 public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
3311 Object v;
3312 do {
3313 v = getReferenceAcquire(o, offset);
3314 } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
3315 return v;
3316 }
3317
3318 @ForceInline
3319 public final Object getAndSetReferenceAcquire(Object o, long offset, Class<?> valueType, Object newValue) {
3320 return getAndSetReference(o, offset, valueType, newValue);
3321 }
3322
3323 @ForceInline
3324 public Object getAndSetFlatValueAcquire(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3325 return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3326 }
3327
3328 @IntrinsicCandidate
3329 public final byte getAndSetByte(Object o, long offset, byte newValue) {
3330 byte v;
3331 do {
3332 v = getByteVolatile(o, offset);
3333 } while (!weakCompareAndSetByte(o, offset, v, newValue));
3334 return v;
3335 }
3336
3337 @ForceInline
3338 public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
3339 byte v;
3340 do {
3341 v = getByte(o, offset);
3342 } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
3343 return v;
3344 }
3345
3346 @ForceInline
3347 public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {
4365 private static long convEndian(boolean big, long n) { return big == BIG_ENDIAN ? n : Long.reverseBytes(n) ; }
4366
4367
4368
4369 private native long allocateMemory0(long bytes);
4370 private native long reallocateMemory0(long address, long bytes);
4371 private native void freeMemory0(long address);
4372 @IntrinsicCandidate
4373 private native void setMemory0(Object o, long offset, long bytes, byte value);
4374 @IntrinsicCandidate
4375 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4376 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
4377 private native long objectFieldOffset0(Field f);
4378 private native long objectFieldOffset1(Class<?> c, String name);
4379 private native long staticFieldOffset0(Field f);
4380 private native Object staticFieldBase0(Field f);
4381 private native boolean shouldBeInitialized0(Class<?> c);
4382 private native void ensureClassInitialized0(Class<?> c);
4383 private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetic
4384 private native int arrayIndexScale0(Class<?> arrayClass);
4385 private native long getObjectSize0(Object o);
4386 private native int getLoadAverage0(double[] loadavg, int nelems);
4387
4388
4389 /**
4390 * Invokes the given direct byte buffer's cleaner, if any.
4391 *
4392 * @param directBuffer a direct byte buffer
4393 * @throws NullPointerException if {@code directBuffer} is null
4394 * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
4395 * or is a {@link java.nio.Buffer#slice slice}, or is a
4396 * {@link java.nio.Buffer#duplicate duplicate}
4397 */
4398 public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
4399 if (!directBuffer.isDirect())
4400 throw new IllegalArgumentException("buffer is non-direct");
4401
4402 DirectBuffer db = (DirectBuffer) directBuffer;
4403 if (db.attachment() != null)
4404 throw new IllegalArgumentException("duplicate or slice");
4405
|