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
1152 }
1153
1154 /**
1155 * Ensures the given class has been initialized (see JVMS-5.5 for details).
1156 * This is often needed in conjunction with obtaining the static field base
1157 * of a class.
1158 *
1159 * The call returns when either class {@code c} is fully initialized or
1160 * class {@code c} is being initialized and the call is performed from
1161 * the initializing thread. In the latter case a subsequent call to
1162 * {@link #shouldBeInitialized} will return {@code true}.
1163 */
1164 public void ensureClassInitialized(Class<?> c) {
1165 if (c == null) {
1166 throw new NullPointerException();
1167 }
1168
1169 ensureClassInitialized0(c);
1170 }
1171
1172 /**
1173 * Reports the offset of the first element in the storage allocation of a
1174 * given array class. If {@link #arrayIndexScale} returns a non-zero value
1175 * for the same class, you may use that scale factor, together with this
1176 * base offset, to form new offsets to access elements of arrays of the
1177 * given class.
1178 * <p>
1179 * The return value is in the range of a {@code int}. The return type is
1180 * {@code long} to emphasize that long arithmetic should always be used
1181 * for offset calculations to avoid overflows.
1182 *
1183 * @see #getInt(Object, long)
1184 * @see #putInt(Object, long, int)
1185 */
1186 public long arrayBaseOffset(Class<?> arrayClass) {
1187 if (arrayClass == null) {
1188 throw new NullPointerException();
1189 }
1190
1191 return arrayBaseOffset0(arrayClass);
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) {
3833 private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n) ; }
3834 private static int convEndian(boolean big, int n) { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n) ; }
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
1357 }
1358
1359 /**
1360 * Ensures the given class has been initialized (see JVMS-5.5 for details).
1361 * This is often needed in conjunction with obtaining the static field base
1362 * of a class.
1363 *
1364 * The call returns when either class {@code c} is fully initialized or
1365 * class {@code c} is being initialized and the call is performed from
1366 * the initializing thread. In the latter case a subsequent call to
1367 * {@link #shouldBeInitialized} will return {@code true}.
1368 */
1369 public void ensureClassInitialized(Class<?> c) {
1370 if (c == null) {
1371 throw new NullPointerException();
1372 }
1373
1374 ensureClassInitialized0(c);
1375 }
1376
1377 /**
1378 * The reading or writing of strict static fields may require
1379 * special processing. Notify the VM that such an event is about
1380 * to happen. The VM may respond by throwing an exception, in the
1381 * case of a read of an uninitialized field. If the VM allows the
1382 * method to return normally, no further calls are needed, with
1383 * the same arguments.
1384 */
1385 public void notifyStrictStaticAccess(Class<?> c, long staticFieldOffset, boolean writing) {
1386 if (c == null) {
1387 throw new NullPointerException();
1388 }
1389 notifyStrictStaticAccess0(c, staticFieldOffset, writing);
1390 }
1391
1392 /**
1393 * Reports the offset of the first element in the storage allocation of a
1394 * given array class. If {@link #arrayIndexScale} returns a non-zero value
1395 * for the same class, you may use that scale factor, together with this
1396 * base offset, to form new offsets to access elements of arrays of the
1397 * given class.
1398 * <p>
1399 * The return value is in the range of a {@code int}. The return type is
1400 * {@code long} to emphasize that long arithmetic should always be used
1401 * for offset calculations to avoid overflows.
1402 *
1403 * @see #getInt(Object, long)
1404 * @see #putInt(Object, long, int)
1405 */
1406 public long arrayBaseOffset(Class<?> arrayClass) {
1407 if (arrayClass == null) {
1408 throw new NullPointerException();
1409 }
1410
1411 return arrayBaseOffset0(arrayClass);
1453 * allocation of a given array class. However, arrays of "narrow" types
1454 * will generally not work properly with accessors like {@link
1455 * #getByte(Object, long)}, so the scale factor for such classes is reported
1456 * as zero.
1457 * <p>
1458 * The computation of the actual memory offset should always use {@code
1459 * long} arithmetic to avoid overflows.
1460 *
1461 * @see #arrayBaseOffset
1462 * @see #getInt(Object, long)
1463 * @see #putInt(Object, long, int)
1464 */
1465 public int arrayIndexScale(Class<?> arrayClass) {
1466 if (arrayClass == null) {
1467 throw new NullPointerException();
1468 }
1469
1470 return arrayIndexScale0(arrayClass);
1471 }
1472
1473 /**
1474 * Return the size of the object in the heap.
1475 * @param o an object
1476 * @return the objects's size
1477 * @since Valhalla
1478 */
1479 public long getObjectSize(Object o) {
1480 if (o == null)
1481 throw new NullPointerException();
1482 return getObjectSize0(o);
1483 }
1484
1485 /** The value of {@code arrayIndexScale(boolean[].class)} */
1486 public static final int ARRAY_BOOLEAN_INDEX_SCALE
1487 = theUnsafe.arrayIndexScale(boolean[].class);
1488
1489 /** The value of {@code arrayIndexScale(byte[].class)} */
1490 public static final int ARRAY_BYTE_INDEX_SCALE
1491 = theUnsafe.arrayIndexScale(byte[].class);
1492
1493 /** The value of {@code arrayIndexScale(short[].class)} */
1494 public static final int ARRAY_SHORT_INDEX_SCALE
1495 = theUnsafe.arrayIndexScale(short[].class);
1496
1497 /** The value of {@code arrayIndexScale(char[].class)} */
1498 public static final int ARRAY_CHAR_INDEX_SCALE
1499 = theUnsafe.arrayIndexScale(char[].class);
1500
1501 /** The value of {@code arrayIndexScale(int[].class)} */
1502 public static final int ARRAY_INT_INDEX_SCALE
1503 = theUnsafe.arrayIndexScale(int[].class);
1642 return null;
1643 }
1644
1645 /** Throws the exception without telling the verifier. */
1646 public native void throwException(Throwable ee);
1647
1648 /**
1649 * Atomically updates Java variable to {@code x} if it is currently
1650 * holding {@code expected}.
1651 *
1652 * <p>This operation has memory semantics of a {@code volatile} read
1653 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1654 *
1655 * @return {@code true} if successful
1656 */
1657 @IntrinsicCandidate
1658 public final native boolean compareAndSetReference(Object o, long offset,
1659 Object expected,
1660 Object x);
1661
1662 private final boolean isValueObject(Object o) {
1663 return o != null && o.getClass().isValue();
1664 }
1665
1666 /*
1667 * For value type, CAS should do substitutability test as opposed
1668 * to two pointers comparison.
1669 */
1670 @ForceInline
1671 public final <V> boolean compareAndSetReference(Object o, long offset,
1672 Class<?> type,
1673 V expected,
1674 V x) {
1675 if (type.isValue() || isValueObject(expected)) {
1676 while (true) {
1677 Object witness = getReferenceVolatile(o, offset);
1678 if (witness != expected) {
1679 return false;
1680 }
1681 if (compareAndSetReference(o, offset, witness, x)) {
1682 return true;
1683 }
1684 }
1685 } else {
1686 return compareAndSetReference(o, offset, expected, x);
1687 }
1688 }
1689
1690 @ForceInline
1691 public final <V> boolean compareAndSetFlatValue(Object o, long offset,
1692 int layout,
1693 Class<?> valueType,
1694 V expected,
1695 V x) {
1696 while (true) {
1697 Object witness = getFlatValueVolatile(o, offset, layout, valueType);
1698 if (witness != expected) {
1699 return false;
1700 }
1701 if (compareAndSetFlatValueAsBytes(o, offset, layout, valueType, witness, x)) {
1702 return true;
1703 }
1704 }
1705 }
1706
1707 @IntrinsicCandidate
1708 public final native Object compareAndExchangeReference(Object o, long offset,
1709 Object expected,
1710 Object x);
1711
1712 @ForceInline
1713 public final <V> Object compareAndExchangeReference(Object o, long offset,
1714 Class<?> valueType,
1715 V expected,
1716 V x) {
1717 if (valueType.isValue() || isValueObject(expected)) {
1718 while (true) {
1719 Object witness = getReferenceVolatile(o, offset);
1720 if (witness != expected) {
1721 return witness;
1722 }
1723 if (compareAndSetReference(o, offset, witness, x)) {
1724 return witness;
1725 }
1726 }
1727 } else {
1728 return compareAndExchangeReference(o, offset, expected, x);
1729 }
1730 }
1731
1732 @ForceInline
1733 public final <V> Object compareAndExchangeFlatValue(Object o, long offset,
1734 int layout,
1735 Class<?> valueType,
1736 V expected,
1737 V x) {
1738 while (true) {
1739 Object witness = getFlatValueVolatile(o, offset, layout, valueType);
1740 if (witness != expected) {
1741 return witness;
1742 }
1743 if (compareAndSetFlatValueAsBytes(o, offset, layout, valueType, witness, x)) {
1744 return witness;
1745 }
1746 }
1747 }
1748
1749 @IntrinsicCandidate
1750 public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1751 Object expected,
1752 Object x) {
1753 return compareAndExchangeReference(o, offset, expected, x);
1754 }
1755
1756 public final <V> Object compareAndExchangeReferenceAcquire(Object o, long offset,
1757 Class<?> valueType,
1758 V expected,
1759 V x) {
1760 return compareAndExchangeReference(o, offset, valueType, expected, x);
1761 }
1762
1763 @ForceInline
1764 public final <V> Object compareAndExchangeFlatValueAcquire(Object o, long offset,
1765 int layout,
1766 Class<?> valueType,
1767 V expected,
1768 V x) {
1769 return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1770 }
1771
1772 @IntrinsicCandidate
1773 public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1774 Object expected,
1775 Object x) {
1776 return compareAndExchangeReference(o, offset, expected, x);
1777 }
1778
1779 public final <V> Object compareAndExchangeReferenceRelease(Object o, long offset,
1780 Class<?> valueType,
1781 V expected,
1782 V x) {
1783 return compareAndExchangeReference(o, offset, valueType, expected, x);
1784 }
1785
1786 @ForceInline
1787 public final <V> Object compareAndExchangeFlatValueRelease(Object o, long offset,
1788 int layout,
1789 Class<?> valueType,
1790 V expected,
1791 V x) {
1792 return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1793 }
1794
1795 @IntrinsicCandidate
1796 public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1797 Object expected,
1798 Object x) {
1799 return compareAndSetReference(o, offset, expected, x);
1800 }
1801
1802 public final <V> boolean weakCompareAndSetReferencePlain(Object o, long offset,
1803 Class<?> valueType,
1804 V expected,
1805 V x) {
1806 if (valueType.isValue() || isValueObject(expected)) {
1807 return compareAndSetReference(o, offset, valueType, expected, x);
1808 } else {
1809 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1810 }
1811 }
1812
1813 @ForceInline
1814 public final <V> boolean weakCompareAndSetFlatValuePlain(Object o, long offset,
1815 int layout,
1816 Class<?> valueType,
1817 V expected,
1818 V x) {
1819 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1820 }
1821
1822 @IntrinsicCandidate
1823 public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1824 Object expected,
1825 Object x) {
1826 return compareAndSetReference(o, offset, expected, x);
1827 }
1828
1829 public final <V> boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1830 Class<?> valueType,
1831 V expected,
1832 V x) {
1833 if (valueType.isValue() || isValueObject(expected)) {
1834 return compareAndSetReference(o, offset, valueType, expected, x);
1835 } else {
1836 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1837 }
1838 }
1839
1840 @ForceInline
1841 public final <V> boolean weakCompareAndSetFlatValueAcquire(Object o, long offset,
1842 int layout,
1843 Class<?> valueType,
1844 V expected,
1845 V x) {
1846 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1847 }
1848
1849 @IntrinsicCandidate
1850 public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1851 Object expected,
1852 Object x) {
1853 return compareAndSetReference(o, offset, expected, x);
1854 }
1855
1856 public final <V> boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1857 Class<?> valueType,
1858 V expected,
1859 V x) {
1860 if (valueType.isValue() || isValueObject(expected)) {
1861 return compareAndSetReference(o, offset, valueType, expected, x);
1862 } else {
1863 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1864 }
1865 }
1866
1867 @ForceInline
1868 public final <V> boolean weakCompareAndSetFlatValueRelease(Object o, long offset,
1869 int layout,
1870 Class<?> valueType,
1871 V expected,
1872 V x) {
1873 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1874 }
1875
1876 @IntrinsicCandidate
1877 public final boolean weakCompareAndSetReference(Object o, long offset,
1878 Object expected,
1879 Object x) {
1880 return compareAndSetReference(o, offset, expected, x);
1881 }
1882
1883 public final <V> boolean weakCompareAndSetReference(Object o, long offset,
1884 Class<?> valueType,
1885 V expected,
1886 V x) {
1887 if (valueType.isValue() || isValueObject(expected)) {
1888 return compareAndSetReference(o, offset, valueType, expected, x);
1889 } else {
1890 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1891 }
1892 }
1893
1894 @ForceInline
1895 public final <V> boolean weakCompareAndSetFlatValue(Object o, long offset,
1896 int layout,
1897 Class<?> valueType,
1898 V expected,
1899 V x) {
1900 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1901 }
1902
1903 /**
1904 * Atomically updates Java variable to {@code x} if it is currently
1905 * holding {@code expected}.
1906 *
1907 * <p>This operation has memory semantics of a {@code volatile} read
1908 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1909 *
1910 * @return {@code true} if successful
1911 */
1912 @IntrinsicCandidate
1913 public final native boolean compareAndSetInt(Object o, long offset,
1914 int expected,
1915 int x);
1916
1917 @IntrinsicCandidate
1918 public final native int compareAndExchangeInt(Object o, long offset,
1919 int expected,
1920 int x);
1921
1922 @IntrinsicCandidate
2498 public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2499 long expected,
2500 long x) {
2501 return compareAndSetLong(o, offset, expected, x);
2502 }
2503
2504 @IntrinsicCandidate
2505 public final boolean weakCompareAndSetLong(Object o, long offset,
2506 long expected,
2507 long x) {
2508 return compareAndSetLong(o, offset, expected, x);
2509 }
2510
2511 /**
2512 * Fetches a reference value from a given Java variable, with volatile
2513 * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2514 */
2515 @IntrinsicCandidate
2516 public native Object getReferenceVolatile(Object o, long offset);
2517
2518 @ForceInline
2519 public final <V> Object getFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType) {
2520 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2521 Object res = getFlatValue(o, offset, layout, valueType);
2522 fullFence();
2523 return res;
2524 }
2525
2526 /**
2527 * Stores a reference value into a given Java variable, with
2528 * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2529 */
2530 @IntrinsicCandidate
2531 public native void putReferenceVolatile(Object o, long offset, Object x);
2532
2533 @ForceInline
2534 public final <V> void putFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType, V x) {
2535 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2536 putFlatValueRelease(o, offset, layout, valueType, x);
2537 fullFence();
2538 }
2539
2540 /** Volatile version of {@link #getInt(Object, long)} */
2541 @IntrinsicCandidate
2542 public native int getIntVolatile(Object o, long offset);
2543
2544 /** Volatile version of {@link #putInt(Object, long, int)} */
2545 @IntrinsicCandidate
2546 public native void putIntVolatile(Object o, long offset, int x);
2547
2548 /** Volatile version of {@link #getBoolean(Object, long)} */
2549 @IntrinsicCandidate
2550 public native boolean getBooleanVolatile(Object o, long offset);
2551
2552 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */
2553 @IntrinsicCandidate
2554 public native void putBooleanVolatile(Object o, long offset, boolean x);
2555
2556 /** Volatile version of {@link #getByte(Object, long)} */
2557 @IntrinsicCandidate
2558 public native byte getByteVolatile(Object o, long offset);
2559
2592 /** Volatile version of {@link #putFloat(Object, long, float)} */
2593 @IntrinsicCandidate
2594 public native void putFloatVolatile(Object o, long offset, float x);
2595
2596 /** Volatile version of {@link #getDouble(Object, long)} */
2597 @IntrinsicCandidate
2598 public native double getDoubleVolatile(Object o, long offset);
2599
2600 /** Volatile version of {@link #putDouble(Object, long, double)} */
2601 @IntrinsicCandidate
2602 public native void putDoubleVolatile(Object o, long offset, double x);
2603
2604
2605
2606 /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2607 @IntrinsicCandidate
2608 public final Object getReferenceAcquire(Object o, long offset) {
2609 return getReferenceVolatile(o, offset);
2610 }
2611
2612 @ForceInline
2613 public final <V> Object getFlatValueAcquire(Object o, long offset, int layout, Class<?> valueType) {
2614 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2615 Object res = getFlatValue(o, offset, layout, valueType);
2616 loadFence();
2617 return res;
2618 }
2619
2620 /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2621 @IntrinsicCandidate
2622 public final boolean getBooleanAcquire(Object o, long offset) {
2623 return getBooleanVolatile(o, offset);
2624 }
2625
2626 /** Acquire version of {@link #getByteVolatile(Object, long)} */
2627 @IntrinsicCandidate
2628 public final byte getByteAcquire(Object o, long offset) {
2629 return getByteVolatile(o, offset);
2630 }
2631
2632 /** Acquire version of {@link #getShortVolatile(Object, long)} */
2633 @IntrinsicCandidate
2634 public final short getShortAcquire(Object o, long offset) {
2635 return getShortVolatile(o, offset);
2636 }
2637
2638 /** Acquire version of {@link #getCharVolatile(Object, long)} */
2639 @IntrinsicCandidate
2664 public final double getDoubleAcquire(Object o, long offset) {
2665 return getDoubleVolatile(o, offset);
2666 }
2667
2668 /*
2669 * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2670 * that do not guarantee immediate visibility of the store to
2671 * other threads. This method is generally only useful if the
2672 * underlying field is a Java volatile (or if an array cell, one
2673 * that is otherwise only accessed using volatile accesses).
2674 *
2675 * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2676 */
2677
2678 /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2679 @IntrinsicCandidate
2680 public final void putReferenceRelease(Object o, long offset, Object x) {
2681 putReferenceVolatile(o, offset, x);
2682 }
2683
2684 @ForceInline
2685 public final <V> void putFlatValueRelease(Object o, long offset, int layout, Class<?> valueType, V x) {
2686 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2687 storeFence();
2688 putFlatValue(o, offset, layout, valueType, x);
2689 }
2690
2691 /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2692 @IntrinsicCandidate
2693 public final void putBooleanRelease(Object o, long offset, boolean x) {
2694 putBooleanVolatile(o, offset, x);
2695 }
2696
2697 /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2698 @IntrinsicCandidate
2699 public final void putByteRelease(Object o, long offset, byte x) {
2700 putByteVolatile(o, offset, x);
2701 }
2702
2703 /** Release version of {@link #putShortVolatile(Object, long, short)} */
2704 @IntrinsicCandidate
2705 public final void putShortRelease(Object o, long offset, short x) {
2706 putShortVolatile(o, offset, x);
2707 }
2708
2709 /** Release version of {@link #putCharVolatile(Object, long, char)} */
2710 @IntrinsicCandidate
2727 /** Release version of {@link #putLongVolatile(Object, long, long)} */
2728 @IntrinsicCandidate
2729 public final void putLongRelease(Object o, long offset, long x) {
2730 putLongVolatile(o, offset, x);
2731 }
2732
2733 /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2734 @IntrinsicCandidate
2735 public final void putDoubleRelease(Object o, long offset, double x) {
2736 putDoubleVolatile(o, offset, x);
2737 }
2738
2739 // ------------------------------ Opaque --------------------------------------
2740
2741 /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2742 @IntrinsicCandidate
2743 public final Object getReferenceOpaque(Object o, long offset) {
2744 return getReferenceVolatile(o, offset);
2745 }
2746
2747 @ForceInline
2748 public final <V> Object getFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType) {
2749 // this is stronger than opaque semantics
2750 return getFlatValueAcquire(o, offset, layout, valueType);
2751 }
2752
2753 /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2754 @IntrinsicCandidate
2755 public final boolean getBooleanOpaque(Object o, long offset) {
2756 return getBooleanVolatile(o, offset);
2757 }
2758
2759 /** Opaque version of {@link #getByteVolatile(Object, long)} */
2760 @IntrinsicCandidate
2761 public final byte getByteOpaque(Object o, long offset) {
2762 return getByteVolatile(o, offset);
2763 }
2764
2765 /** Opaque version of {@link #getShortVolatile(Object, long)} */
2766 @IntrinsicCandidate
2767 public final short getShortOpaque(Object o, long offset) {
2768 return getShortVolatile(o, offset);
2769 }
2770
2771 /** Opaque version of {@link #getCharVolatile(Object, long)} */
2772 @IntrinsicCandidate
2787 }
2788
2789 /** Opaque version of {@link #getLongVolatile(Object, long)} */
2790 @IntrinsicCandidate
2791 public final long getLongOpaque(Object o, long offset) {
2792 return getLongVolatile(o, offset);
2793 }
2794
2795 /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2796 @IntrinsicCandidate
2797 public final double getDoubleOpaque(Object o, long offset) {
2798 return getDoubleVolatile(o, offset);
2799 }
2800
2801 /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2802 @IntrinsicCandidate
2803 public final void putReferenceOpaque(Object o, long offset, Object x) {
2804 putReferenceVolatile(o, offset, x);
2805 }
2806
2807 @ForceInline
2808 public final <V> void putFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType, V x) {
2809 // this is stronger than opaque semantics
2810 putFlatValueRelease(o, offset, layout, valueType, x);
2811 }
2812
2813 /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2814 @IntrinsicCandidate
2815 public final void putBooleanOpaque(Object o, long offset, boolean x) {
2816 putBooleanVolatile(o, offset, x);
2817 }
2818
2819 /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2820 @IntrinsicCandidate
2821 public final void putByteOpaque(Object o, long offset, byte x) {
2822 putByteVolatile(o, offset, x);
2823 }
2824
2825 /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2826 @IntrinsicCandidate
2827 public final void putShortOpaque(Object o, long offset, short x) {
2828 putShortVolatile(o, offset, x);
2829 }
2830
2831 /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2832 @IntrinsicCandidate
2841 }
2842
2843 /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2844 @IntrinsicCandidate
2845 public final void putFloatOpaque(Object o, long offset, float x) {
2846 putFloatVolatile(o, offset, x);
2847 }
2848
2849 /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2850 @IntrinsicCandidate
2851 public final void putLongOpaque(Object o, long offset, long x) {
2852 putLongVolatile(o, offset, x);
2853 }
2854
2855 /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2856 @IntrinsicCandidate
2857 public final void putDoubleOpaque(Object o, long offset, double x) {
2858 putDoubleVolatile(o, offset, x);
2859 }
2860
2861 @ForceInline
2862 private boolean compareAndSetFlatValueAsBytes(Object o, long offset, int layout, Class<?> valueType, Object expected, Object x) {
2863 // We turn the payload of an atomic value into a numeric value (of suitable type)
2864 // by storing the value into an array element (of matching layout) and by reading
2865 // back the array element as an integral value. After which we can implement the CAS
2866 // as a plain numeric CAS. Note: this only works if the payload contains no oops
2867 // (see VarHandles::isAtomicFlat).
2868 Object expectedArray = newSpecialArray(valueType, 1, layout);
2869 Object xArray = newSpecialArray(valueType, 1, layout);
2870 long base = arrayBaseOffset(expectedArray.getClass());
2871 int scale = arrayIndexScale(expectedArray.getClass());
2872 putFlatValue(expectedArray, base, layout, valueType, expected);
2873 putFlatValue(xArray, base, layout, valueType, x);
2874 switch (scale) {
2875 case 1: {
2876 byte expectedByte = getByte(expectedArray, base);
2877 byte xByte = getByte(xArray, base);
2878 return compareAndSetByte(o, offset, expectedByte, xByte);
2879 }
2880 case 2: {
2881 short expectedShort = getShort(expectedArray, base);
2882 short xShort = getShort(xArray, base);
2883 return compareAndSetShort(o, offset, expectedShort, xShort);
2884 }
2885 case 4: {
2886 int expectedInt = getInt(expectedArray, base);
2887 int xInt = getInt(xArray, base);
2888 return compareAndSetInt(o, offset, expectedInt, xInt);
2889 }
2890 case 8: {
2891 long expectedLong = getLong(expectedArray, base);
2892 long xLong = getLong(xArray, base);
2893 return compareAndSetLong(o, offset, expectedLong, xLong);
2894 }
2895 default: {
2896 throw new UnsupportedOperationException();
2897 }
2898 }
2899 }
2900
2901 /**
2902 * Unblocks the given thread blocked on {@code park}, or, if it is
2903 * not blocked, causes the subsequent call to {@code park} not to
2904 * block. Note: this operation is "unsafe" solely because the
2905 * caller must somehow ensure that the thread has not been
2906 * destroyed. Nothing special is usually required to ensure this
2907 * when called from Java (in which there will ordinarily be a live
2908 * reference to the thread) but this is not nearly-automatically
2909 * so when calling from native code.
2910 *
2911 * @param thread the thread to unpark.
2912 */
2913 @IntrinsicCandidate
2914 public native void unpark(Object thread);
2915
2916 /**
2917 * Blocks current thread, returning when a balancing
2918 * {@code unpark} occurs, or a balancing {@code unpark} has
2919 * already occurred, or the thread is interrupted, or, if not
2920 * absolute and time is not zero, the given time nanoseconds have
3267 /**
3268 * Atomically exchanges the given reference value with the current
3269 * reference value of a field or array element within the given
3270 * object {@code o} at the given {@code offset}.
3271 *
3272 * @param o object/array to update the field/element in
3273 * @param offset field/element offset
3274 * @param newValue new value
3275 * @return the previous value
3276 * @since 1.8
3277 */
3278 @IntrinsicCandidate
3279 public final Object getAndSetReference(Object o, long offset, Object newValue) {
3280 Object v;
3281 do {
3282 v = getReferenceVolatile(o, offset);
3283 } while (!weakCompareAndSetReference(o, offset, v, newValue));
3284 return v;
3285 }
3286
3287 @ForceInline
3288 public final Object getAndSetReference(Object o, long offset, Class<?> valueType, Object newValue) {
3289 Object v;
3290 do {
3291 v = getReferenceVolatile(o, offset);
3292 } while (!compareAndSetReference(o, offset, valueType, v, newValue));
3293 return v;
3294 }
3295
3296 @ForceInline
3297 public Object getAndSetFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, Object newValue) {
3298 Object v;
3299 do {
3300 v = getFlatValueVolatile(o, offset, layoutKind, valueType);
3301 } while (!compareAndSetFlatValue(o, offset, layoutKind, valueType, v, newValue));
3302 return v;
3303 }
3304
3305 @ForceInline
3306 public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
3307 Object v;
3308 do {
3309 v = getReference(o, offset);
3310 } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
3311 return v;
3312 }
3313
3314 @ForceInline
3315 public final Object getAndSetReferenceRelease(Object o, long offset, Class<?> valueType, Object newValue) {
3316 return getAndSetReference(o, offset, valueType, newValue);
3317 }
3318
3319 @ForceInline
3320 public Object getAndSetFlatValueRelease(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3321 return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3322 }
3323
3324 @ForceInline
3325 public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
3326 Object v;
3327 do {
3328 v = getReferenceAcquire(o, offset);
3329 } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
3330 return v;
3331 }
3332
3333 @ForceInline
3334 public final Object getAndSetReferenceAcquire(Object o, long offset, Class<?> valueType, Object newValue) {
3335 return getAndSetReference(o, offset, valueType, newValue);
3336 }
3337
3338 @ForceInline
3339 public Object getAndSetFlatValueAcquire(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3340 return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3341 }
3342
3343 @IntrinsicCandidate
3344 public final byte getAndSetByte(Object o, long offset, byte newValue) {
3345 byte v;
3346 do {
3347 v = getByteVolatile(o, offset);
3348 } while (!weakCompareAndSetByte(o, offset, v, newValue));
3349 return v;
3350 }
3351
3352 @ForceInline
3353 public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
3354 byte v;
3355 do {
3356 v = getByte(o, offset);
3357 } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
3358 return v;
3359 }
3360
3361 @ForceInline
3362 public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {
4378 private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n) ; }
4379 private static int convEndian(boolean big, int n) { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n) ; }
4380 private static long convEndian(boolean big, long n) { return big == BIG_ENDIAN ? n : Long.reverseBytes(n) ; }
4381
4382
4383
4384 private native long allocateMemory0(long bytes);
4385 private native long reallocateMemory0(long address, long bytes);
4386 private native void freeMemory0(long address);
4387 @IntrinsicCandidate
4388 private native void setMemory0(Object o, long offset, long bytes, byte value);
4389 @IntrinsicCandidate
4390 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4391 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
4392 private native long objectFieldOffset0(Field f);
4393 private native long objectFieldOffset1(Class<?> c, String name);
4394 private native long staticFieldOffset0(Field f);
4395 private native Object staticFieldBase0(Field f);
4396 private native boolean shouldBeInitialized0(Class<?> c);
4397 private native void ensureClassInitialized0(Class<?> c);
4398 private native void notifyStrictStaticAccess0(Class<?> c, long staticFieldOffset, boolean writing);
4399 private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetic
4400 private native int arrayIndexScale0(Class<?> arrayClass);
4401 private native long getObjectSize0(Object o);
4402 private native int getLoadAverage0(double[] loadavg, int nelems);
4403
4404
4405 /**
4406 * Invokes the given direct byte buffer's cleaner, if any.
4407 *
4408 * @param directBuffer a direct byte buffer
4409 * @throws NullPointerException if {@code directBuffer} is null
4410 * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
4411 * or is a {@link java.nio.Buffer#slice slice}, or is a
4412 * {@link java.nio.Buffer#duplicate duplicate}
4413 */
4414 public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
4415 if (!directBuffer.isDirect())
4416 throw new IllegalArgumentException("buffer is non-direct");
4417
4418 DirectBuffer db = (DirectBuffer) directBuffer;
4419 if (db.attachment() != null)
4420 throw new IllegalArgumentException("duplicate or slice");
4421
|