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);
1192 }
1193
1194
1195 /** The value of {@code arrayBaseOffset(boolean[].class)} */
1196 public static final long ARRAY_BOOLEAN_BASE_OFFSET
1197 = theUnsafe.arrayBaseOffset(boolean[].class);
1198
1199 /** The value of {@code arrayBaseOffset(byte[].class)} */
1200 public static final long ARRAY_BYTE_BASE_OFFSET
1201 = theUnsafe.arrayBaseOffset(byte[].class);
1202
1203 /** The value of {@code arrayBaseOffset(short[].class)} */
1204 public static final long ARRAY_SHORT_BASE_OFFSET
1205 = theUnsafe.arrayBaseOffset(short[].class);
1206
1207 /** The value of {@code arrayBaseOffset(char[].class)} */
1208 public static final long ARRAY_CHAR_BASE_OFFSET
1209 = theUnsafe.arrayBaseOffset(char[].class);
1210
1211 /** The value of {@code arrayBaseOffset(int[].class)} */
1212 public static final long ARRAY_INT_BASE_OFFSET
1213 = theUnsafe.arrayBaseOffset(int[].class);
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(Object[] array) {
238 if (array == null) {
239 throw new NullPointerException();
240 }
241 return arrayLayout0(array);
242 }
243
244 private native int arrayLayout0(Object[] array);
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 * Fetches a reference value from a given Java variable.
268 * This method can return a reference to either an object or value
269 * or a null reference.
270 *
271 * @see #getInt(Object, long)
272 */
273 @IntrinsicCandidate
274 public native Object getReference(Object o, long offset);
275
276 /**
277 * Stores a reference value into a given Java variable.
278 * This method can store a reference to either an object or value
279 * or a null reference.
280 * <p>
281 * Unless the reference {@code x} being stored is either null
282 * or matches the field type, the results are undefined.
283 * If the reference {@code o} is non-null, card marks or
284 * other store barriers for that object (if the VM requires them)
285 * are updated.
286 * @see #putInt(Object, long, int)
287 */
288 @IntrinsicCandidate
289 public native void putReference(Object o, long offset, Object x);
290
291 /**
292 * Fetches a value of type {@code <V>} from a given Java variable.
293 * More specifically, fetches a field or array element within the given
294 * {@code o} object at the given offset, or (if {@code o} is null)
295 * from the memory address whose numerical value is the given offset.
296 *
297 * @param o Java heap object in which the variable resides, if any, else
298 * null
299 * @param offset indication of where the variable resides in a Java heap
300 * object, if any, else a memory address locating the variable
301 * statically
302 * @param valueType value type
303 * @param <V> the type of a value
304 * @return the value fetched from the indicated Java variable
305 * @throws RuntimeException No defined exceptions are thrown, not even
306 * {@link NullPointerException}
307 */
308 @IntrinsicCandidate
309 public native <V> V getValue(Object o, long offset, Class<?> valueType);
310
311 /**
312 * Fetches a value of type {@code <V>} from a given Java variable.
313 * More specifically, fetches a field or array element within the given
314 * {@code o} object at the given offset, or (if {@code o} is null)
315 * from the memory address whose numerical value is the given offset.
316 *
317 * @param o Java heap object in which the variable resides, if any, else
318 * null
319 * @param offset indication of where the variable resides in a Java heap
320 * object, if any, else a memory address locating the variable
321 * statically
322 * @param layoutKind opaque value used by the VM to know the layout
323 * the field or array element. This value must be retrieved with
324 * {@link #fieldLayout} or {@link #arrayLayout}.
325 * @param valueType value type
326 * @param <V> the type of a value
327 * @return the value fetched from the indicated Java variable
328 * @throws RuntimeException No defined exceptions are thrown, not even
329 * {@link NullPointerException}
330 */
331 @IntrinsicCandidate
332 public native <V> V getFlatValue(Object o, long offset, int layoutKind, Class<?> valueType);
333
334
335 /**
336 * Stores the given value into a given Java variable.
337 *
338 * Unless the reference {@code o} being stored is either null
339 * or matches the field type, the results are undefined.
340 *
341 * @param o Java heap object in which the variable resides, if any, else
342 * null
343 * @param offset indication of where the variable resides in a Java heap
344 * object, if any, else a memory address locating the variable
345 * statically
346 * @param valueType value type
347 * @param v the value to store into the indicated Java variable
348 * @param <V> the type of a value
349 * @throws RuntimeException No defined exceptions are thrown, not even
350 * {@link NullPointerException}
351 */
352 @IntrinsicCandidate
353 public native <V> void putValue(Object o, long offset, Class<?> valueType, V v);
354
355 /**
356 * Stores the given value into a given Java variable.
357 *
358 * Unless the reference {@code o} being stored is either null
359 * or matches the field type, the results are undefined.
360 *
361 * @param o Java heap object in which the variable resides, if any, else
362 * null
363 * @param offset indication of where the variable resides in a Java heap
364 * object, if any, else a memory address locating the variable
365 * statically
366 * @param layoutKind opaque value used by the VM to know the layout
367 * the field or array element. This value must be retrieved with
368 * {@link #fieldLayout} or {@link #arrayLayout}.
369 * @param valueType value type
370 * @param v the value to store into the indicated Java variable
371 * @param <V> the type of a value
372 * @throws RuntimeException No defined exceptions are thrown, not even
373 * {@link NullPointerException}
374 */
375 @IntrinsicCandidate
376 public native <V> void putFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, V v);
377
378 /**
379 * Returns an object instance with a private buffered value whose layout
380 * and contents is exactly the given value instance. The return object
381 * is in the larval state that can be updated using the unsafe put operation.
382 *
383 * @param value a value instance
384 * @param <V> the type of the given value instance
385 */
386 @IntrinsicCandidate
387 public native <V> V makePrivateBuffer(V value);
388
389 /**
390 * Exits the larval state and returns a value instance.
391 *
392 * @param value a value instance
393 * @param <V> the type of the given value instance
394 */
395 @IntrinsicCandidate
396 public native <V> V finishPrivateBuffer(V value);
397
398 /**
399 * Returns the header size of the given value type.
400 *
401 * @param valueType value type
402 * @return the header size of the value type
403 */
404 public native <V> long valueHeaderSize(Class<V> valueType);
405
406 /** @see #getInt(Object, long) */
407 @IntrinsicCandidate
408 public native boolean getBoolean(Object o, long offset);
409
410 /** @see #putInt(Object, long, int) */
411 @IntrinsicCandidate
412 public native void putBoolean(Object o, long offset, boolean x);
413
414 /** @see #getInt(Object, long) */
415 @IntrinsicCandidate
416 public native byte getByte(Object o, long offset);
417
418 /** @see #putInt(Object, long, int) */
419 @IntrinsicCandidate
420 public native void putByte(Object o, long offset, byte x);
421
422 /** @see #getInt(Object, long) */
423 @IntrinsicCandidate
424 public native short getShort(Object o, long offset);
425
1353 }
1354
1355 /**
1356 * Ensures the given class has been initialized (see JVMS-5.5 for details).
1357 * This is often needed in conjunction with obtaining the static field base
1358 * of a class.
1359 *
1360 * The call returns when either class {@code c} is fully initialized or
1361 * class {@code c} is being initialized and the call is performed from
1362 * the initializing thread. In the latter case a subsequent call to
1363 * {@link #shouldBeInitialized} will return {@code true}.
1364 */
1365 public void ensureClassInitialized(Class<?> c) {
1366 if (c == null) {
1367 throw new NullPointerException();
1368 }
1369
1370 ensureClassInitialized0(c);
1371 }
1372
1373 /**
1374 * The reading or writing of strict static fields may require
1375 * special processing. Notify the VM that such an event is about
1376 * to happen. The VM may respond by throwing an exception, in the
1377 * case of a read of an uninitialized field. If the VM allows the
1378 * method to return normally, no further calls are needed, with
1379 * the same arguments.
1380 */
1381 public void notifyStrictStaticAccess(Class<?> c, long staticFieldOffset, boolean writing) {
1382 if (c == null) {
1383 throw new NullPointerException();
1384 }
1385 notifyStrictStaticAccess0(c, staticFieldOffset, writing);
1386 }
1387
1388 /**
1389 * Reports the offset of the first element in the storage allocation of a
1390 * given array class. If {@link #arrayIndexScale} returns a non-zero value
1391 * for the same class, you may use that scale factor, together with this
1392 * base offset, to form new offsets to access elements of arrays of the
1393 * given class.
1394 * <p>
1395 * The return value is in the range of a {@code int}. The return type is
1396 * {@code long} to emphasize that long arithmetic should always be used
1397 * for offset calculations to avoid overflows.
1398 *
1399 * @see #getInt(Object, long)
1400 * @see #putInt(Object, long, int)
1401 */
1402 public long arrayBaseOffset(Class<?> arrayClass) {
1403 if (arrayClass == null) {
1404 throw new NullPointerException();
1405 }
1406
1407 return arrayBaseOffset0(arrayClass);
1408 }
1409
1410 public long arrayBaseOffset(Object[] array) {
1411 if (array == null) {
1412 throw new NullPointerException();
1413 }
1414
1415 return arrayBaseOffset1(array);
1416 }
1417
1418 /** The value of {@code arrayBaseOffset(boolean[].class)} */
1419 public static final long ARRAY_BOOLEAN_BASE_OFFSET
1420 = theUnsafe.arrayBaseOffset(boolean[].class);
1421
1422 /** The value of {@code arrayBaseOffset(byte[].class)} */
1423 public static final long ARRAY_BYTE_BASE_OFFSET
1424 = theUnsafe.arrayBaseOffset(byte[].class);
1425
1426 /** The value of {@code arrayBaseOffset(short[].class)} */
1427 public static final long ARRAY_SHORT_BASE_OFFSET
1428 = theUnsafe.arrayBaseOffset(short[].class);
1429
1430 /** The value of {@code arrayBaseOffset(char[].class)} */
1431 public static final long ARRAY_CHAR_BASE_OFFSET
1432 = theUnsafe.arrayBaseOffset(char[].class);
1433
1434 /** The value of {@code arrayBaseOffset(int[].class)} */
1435 public static final long ARRAY_INT_BASE_OFFSET
1436 = theUnsafe.arrayBaseOffset(int[].class);
1456 * allocation of a given array class. However, arrays of "narrow" types
1457 * will generally not work properly with accessors like {@link
1458 * #getByte(Object, long)}, so the scale factor for such classes is reported
1459 * as zero.
1460 * <p>
1461 * The computation of the actual memory offset should always use {@code
1462 * long} arithmetic to avoid overflows.
1463 *
1464 * @see #arrayBaseOffset
1465 * @see #getInt(Object, long)
1466 * @see #putInt(Object, long, int)
1467 */
1468 public int arrayIndexScale(Class<?> arrayClass) {
1469 if (arrayClass == null) {
1470 throw new NullPointerException();
1471 }
1472
1473 return arrayIndexScale0(arrayClass);
1474 }
1475
1476 public int arrayIndexScale(Object[] array) {
1477 if (array == null) {
1478 throw new NullPointerException();
1479 }
1480
1481 return arrayIndexScale1(array);
1482 }
1483
1484 /**
1485 * Return the size of the object in the heap.
1486 * @param o an object
1487 * @return the objects's size
1488 * @since Valhalla
1489 */
1490 public long getObjectSize(Object o) {
1491 if (o == null)
1492 throw new NullPointerException();
1493 return getObjectSize0(o);
1494 }
1495
1496 /** The value of {@code arrayIndexScale(boolean[].class)} */
1497 public static final int ARRAY_BOOLEAN_INDEX_SCALE
1498 = theUnsafe.arrayIndexScale(boolean[].class);
1499
1500 /** The value of {@code arrayIndexScale(byte[].class)} */
1501 public static final int ARRAY_BYTE_INDEX_SCALE
1502 = theUnsafe.arrayIndexScale(byte[].class);
1503
1504 /** The value of {@code arrayIndexScale(short[].class)} */
1505 public static final int ARRAY_SHORT_INDEX_SCALE
1506 = theUnsafe.arrayIndexScale(short[].class);
1507
1508 /** The value of {@code arrayIndexScale(char[].class)} */
1509 public static final int ARRAY_CHAR_INDEX_SCALE
1510 = theUnsafe.arrayIndexScale(char[].class);
1511
1512 /** The value of {@code arrayIndexScale(int[].class)} */
1513 public static final int ARRAY_INT_INDEX_SCALE
1514 = theUnsafe.arrayIndexScale(int[].class);
1653 return null;
1654 }
1655
1656 /** Throws the exception without telling the verifier. */
1657 public native void throwException(Throwable ee);
1658
1659 /**
1660 * Atomically updates Java variable to {@code x} if it is currently
1661 * holding {@code expected}.
1662 *
1663 * <p>This operation has memory semantics of a {@code volatile} read
1664 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1665 *
1666 * @return {@code true} if successful
1667 */
1668 @IntrinsicCandidate
1669 public final native boolean compareAndSetReference(Object o, long offset,
1670 Object expected,
1671 Object x);
1672
1673 private final boolean isValueObject(Object o) {
1674 return o != null && o.getClass().isValue();
1675 }
1676
1677 /*
1678 * For value type, CAS should do substitutability test as opposed
1679 * to two pointers comparison.
1680 */
1681 @ForceInline
1682 public final <V> boolean compareAndSetReference(Object o, long offset,
1683 Class<?> type,
1684 V expected,
1685 V x) {
1686 if (type.isValue() || isValueObject(expected)) {
1687 while (true) {
1688 Object witness = getReferenceVolatile(o, offset);
1689 if (witness != expected) {
1690 return false;
1691 }
1692 if (compareAndSetReference(o, offset, witness, x)) {
1693 return true;
1694 }
1695 }
1696 } else {
1697 return compareAndSetReference(o, offset, expected, x);
1698 }
1699 }
1700
1701 @ForceInline
1702 public final <V> boolean compareAndSetFlatValue(Object o, long offset,
1703 int layout,
1704 Class<?> valueType,
1705 V expected,
1706 V x) {
1707 while (true) {
1708 Object witness = getFlatValueVolatile(o, offset, layout, valueType);
1709 if (witness != expected) {
1710 return false;
1711 }
1712 if (compareAndSetFlatValueAsBytes(o, offset, layout, valueType, witness, x)) {
1713 return true;
1714 }
1715 }
1716 }
1717
1718 @IntrinsicCandidate
1719 public final native Object compareAndExchangeReference(Object o, long offset,
1720 Object expected,
1721 Object x);
1722
1723 @ForceInline
1724 public final <V> Object compareAndExchangeReference(Object o, long offset,
1725 Class<?> valueType,
1726 V expected,
1727 V x) {
1728 if (valueType.isValue() || isValueObject(expected)) {
1729 while (true) {
1730 Object witness = getReferenceVolatile(o, offset);
1731 if (witness != expected) {
1732 return witness;
1733 }
1734 if (compareAndSetReference(o, offset, witness, x)) {
1735 return witness;
1736 }
1737 }
1738 } else {
1739 return compareAndExchangeReference(o, offset, expected, x);
1740 }
1741 }
1742
1743 @ForceInline
1744 public final <V> Object compareAndExchangeFlatValue(Object o, long offset,
1745 int layout,
1746 Class<?> valueType,
1747 V expected,
1748 V x) {
1749 while (true) {
1750 Object witness = getFlatValueVolatile(o, offset, layout, valueType);
1751 if (witness != expected) {
1752 return witness;
1753 }
1754 if (compareAndSetFlatValueAsBytes(o, offset, layout, valueType, witness, x)) {
1755 return witness;
1756 }
1757 }
1758 }
1759
1760 @IntrinsicCandidate
1761 public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1762 Object expected,
1763 Object x) {
1764 return compareAndExchangeReference(o, offset, expected, x);
1765 }
1766
1767 public final <V> Object compareAndExchangeReferenceAcquire(Object o, long offset,
1768 Class<?> valueType,
1769 V expected,
1770 V x) {
1771 return compareAndExchangeReference(o, offset, valueType, expected, x);
1772 }
1773
1774 @ForceInline
1775 public final <V> Object compareAndExchangeFlatValueAcquire(Object o, long offset,
1776 int layout,
1777 Class<?> valueType,
1778 V expected,
1779 V x) {
1780 return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1781 }
1782
1783 @IntrinsicCandidate
1784 public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1785 Object expected,
1786 Object x) {
1787 return compareAndExchangeReference(o, offset, expected, x);
1788 }
1789
1790 public final <V> Object compareAndExchangeReferenceRelease(Object o, long offset,
1791 Class<?> valueType,
1792 V expected,
1793 V x) {
1794 return compareAndExchangeReference(o, offset, valueType, expected, x);
1795 }
1796
1797 @ForceInline
1798 public final <V> Object compareAndExchangeFlatValueRelease(Object o, long offset,
1799 int layout,
1800 Class<?> valueType,
1801 V expected,
1802 V x) {
1803 return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1804 }
1805
1806 @IntrinsicCandidate
1807 public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1808 Object expected,
1809 Object x) {
1810 return compareAndSetReference(o, offset, expected, x);
1811 }
1812
1813 public final <V> boolean weakCompareAndSetReferencePlain(Object o, long offset,
1814 Class<?> valueType,
1815 V expected,
1816 V x) {
1817 if (valueType.isValue() || isValueObject(expected)) {
1818 return compareAndSetReference(o, offset, valueType, expected, x);
1819 } else {
1820 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1821 }
1822 }
1823
1824 @ForceInline
1825 public final <V> boolean weakCompareAndSetFlatValuePlain(Object o, long offset,
1826 int layout,
1827 Class<?> valueType,
1828 V expected,
1829 V x) {
1830 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1831 }
1832
1833 @IntrinsicCandidate
1834 public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1835 Object expected,
1836 Object x) {
1837 return compareAndSetReference(o, offset, expected, x);
1838 }
1839
1840 public final <V> boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1841 Class<?> valueType,
1842 V expected,
1843 V x) {
1844 if (valueType.isValue() || isValueObject(expected)) {
1845 return compareAndSetReference(o, offset, valueType, expected, x);
1846 } else {
1847 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1848 }
1849 }
1850
1851 @ForceInline
1852 public final <V> boolean weakCompareAndSetFlatValueAcquire(Object o, long offset,
1853 int layout,
1854 Class<?> valueType,
1855 V expected,
1856 V x) {
1857 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1858 }
1859
1860 @IntrinsicCandidate
1861 public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1862 Object expected,
1863 Object x) {
1864 return compareAndSetReference(o, offset, expected, x);
1865 }
1866
1867 public final <V> boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1868 Class<?> valueType,
1869 V expected,
1870 V x) {
1871 if (valueType.isValue() || isValueObject(expected)) {
1872 return compareAndSetReference(o, offset, valueType, expected, x);
1873 } else {
1874 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1875 }
1876 }
1877
1878 @ForceInline
1879 public final <V> boolean weakCompareAndSetFlatValueRelease(Object o, long offset,
1880 int layout,
1881 Class<?> valueType,
1882 V expected,
1883 V x) {
1884 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1885 }
1886
1887 @IntrinsicCandidate
1888 public final boolean weakCompareAndSetReference(Object o, long offset,
1889 Object expected,
1890 Object x) {
1891 return compareAndSetReference(o, offset, expected, x);
1892 }
1893
1894 public final <V> boolean weakCompareAndSetReference(Object o, long offset,
1895 Class<?> valueType,
1896 V expected,
1897 V x) {
1898 if (valueType.isValue() || isValueObject(expected)) {
1899 return compareAndSetReference(o, offset, valueType, expected, x);
1900 } else {
1901 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1902 }
1903 }
1904
1905 @ForceInline
1906 public final <V> boolean weakCompareAndSetFlatValue(Object o, long offset,
1907 int layout,
1908 Class<?> valueType,
1909 V expected,
1910 V x) {
1911 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1912 }
1913
1914 /**
1915 * Atomically updates Java variable to {@code x} if it is currently
1916 * holding {@code expected}.
1917 *
1918 * <p>This operation has memory semantics of a {@code volatile} read
1919 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1920 *
1921 * @return {@code true} if successful
1922 */
1923 @IntrinsicCandidate
1924 public final native boolean compareAndSetInt(Object o, long offset,
1925 int expected,
1926 int x);
1927
1928 @IntrinsicCandidate
1929 public final native int compareAndExchangeInt(Object o, long offset,
1930 int expected,
1931 int x);
1932
1933 @IntrinsicCandidate
2509 public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2510 long expected,
2511 long x) {
2512 return compareAndSetLong(o, offset, expected, x);
2513 }
2514
2515 @IntrinsicCandidate
2516 public final boolean weakCompareAndSetLong(Object o, long offset,
2517 long expected,
2518 long x) {
2519 return compareAndSetLong(o, offset, expected, x);
2520 }
2521
2522 /**
2523 * Fetches a reference value from a given Java variable, with volatile
2524 * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2525 */
2526 @IntrinsicCandidate
2527 public native Object getReferenceVolatile(Object o, long offset);
2528
2529 @ForceInline
2530 public final <V> Object getFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType) {
2531 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2532 Object res = getFlatValue(o, offset, layout, valueType);
2533 fullFence();
2534 return res;
2535 }
2536
2537 /**
2538 * Stores a reference value into a given Java variable, with
2539 * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2540 */
2541 @IntrinsicCandidate
2542 public native void putReferenceVolatile(Object o, long offset, Object x);
2543
2544 @ForceInline
2545 public final <V> void putFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType, V x) {
2546 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2547 putFlatValueRelease(o, offset, layout, valueType, x);
2548 fullFence();
2549 }
2550
2551 /** Volatile version of {@link #getInt(Object, long)} */
2552 @IntrinsicCandidate
2553 public native int getIntVolatile(Object o, long offset);
2554
2555 /** Volatile version of {@link #putInt(Object, long, int)} */
2556 @IntrinsicCandidate
2557 public native void putIntVolatile(Object o, long offset, int x);
2558
2559 /** Volatile version of {@link #getBoolean(Object, long)} */
2560 @IntrinsicCandidate
2561 public native boolean getBooleanVolatile(Object o, long offset);
2562
2563 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */
2564 @IntrinsicCandidate
2565 public native void putBooleanVolatile(Object o, long offset, boolean x);
2566
2567 /** Volatile version of {@link #getByte(Object, long)} */
2568 @IntrinsicCandidate
2569 public native byte getByteVolatile(Object o, long offset);
2570
2603 /** Volatile version of {@link #putFloat(Object, long, float)} */
2604 @IntrinsicCandidate
2605 public native void putFloatVolatile(Object o, long offset, float x);
2606
2607 /** Volatile version of {@link #getDouble(Object, long)} */
2608 @IntrinsicCandidate
2609 public native double getDoubleVolatile(Object o, long offset);
2610
2611 /** Volatile version of {@link #putDouble(Object, long, double)} */
2612 @IntrinsicCandidate
2613 public native void putDoubleVolatile(Object o, long offset, double x);
2614
2615
2616
2617 /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2618 @IntrinsicCandidate
2619 public final Object getReferenceAcquire(Object o, long offset) {
2620 return getReferenceVolatile(o, offset);
2621 }
2622
2623 @ForceInline
2624 public final <V> Object getFlatValueAcquire(Object o, long offset, int layout, Class<?> valueType) {
2625 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2626 Object res = getFlatValue(o, offset, layout, valueType);
2627 loadFence();
2628 return res;
2629 }
2630
2631 /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2632 @IntrinsicCandidate
2633 public final boolean getBooleanAcquire(Object o, long offset) {
2634 return getBooleanVolatile(o, offset);
2635 }
2636
2637 /** Acquire version of {@link #getByteVolatile(Object, long)} */
2638 @IntrinsicCandidate
2639 public final byte getByteAcquire(Object o, long offset) {
2640 return getByteVolatile(o, offset);
2641 }
2642
2643 /** Acquire version of {@link #getShortVolatile(Object, long)} */
2644 @IntrinsicCandidate
2645 public final short getShortAcquire(Object o, long offset) {
2646 return getShortVolatile(o, offset);
2647 }
2648
2649 /** Acquire version of {@link #getCharVolatile(Object, long)} */
2650 @IntrinsicCandidate
2675 public final double getDoubleAcquire(Object o, long offset) {
2676 return getDoubleVolatile(o, offset);
2677 }
2678
2679 /*
2680 * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2681 * that do not guarantee immediate visibility of the store to
2682 * other threads. This method is generally only useful if the
2683 * underlying field is a Java volatile (or if an array cell, one
2684 * that is otherwise only accessed using volatile accesses).
2685 *
2686 * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2687 */
2688
2689 /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2690 @IntrinsicCandidate
2691 public final void putReferenceRelease(Object o, long offset, Object x) {
2692 putReferenceVolatile(o, offset, x);
2693 }
2694
2695 @ForceInline
2696 public final <V> void putFlatValueRelease(Object o, long offset, int layout, Class<?> valueType, V x) {
2697 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2698 storeFence();
2699 putFlatValue(o, offset, layout, valueType, x);
2700 }
2701
2702 /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2703 @IntrinsicCandidate
2704 public final void putBooleanRelease(Object o, long offset, boolean x) {
2705 putBooleanVolatile(o, offset, x);
2706 }
2707
2708 /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2709 @IntrinsicCandidate
2710 public final void putByteRelease(Object o, long offset, byte x) {
2711 putByteVolatile(o, offset, x);
2712 }
2713
2714 /** Release version of {@link #putShortVolatile(Object, long, short)} */
2715 @IntrinsicCandidate
2716 public final void putShortRelease(Object o, long offset, short x) {
2717 putShortVolatile(o, offset, x);
2718 }
2719
2720 /** Release version of {@link #putCharVolatile(Object, long, char)} */
2721 @IntrinsicCandidate
2738 /** Release version of {@link #putLongVolatile(Object, long, long)} */
2739 @IntrinsicCandidate
2740 public final void putLongRelease(Object o, long offset, long x) {
2741 putLongVolatile(o, offset, x);
2742 }
2743
2744 /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2745 @IntrinsicCandidate
2746 public final void putDoubleRelease(Object o, long offset, double x) {
2747 putDoubleVolatile(o, offset, x);
2748 }
2749
2750 // ------------------------------ Opaque --------------------------------------
2751
2752 /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2753 @IntrinsicCandidate
2754 public final Object getReferenceOpaque(Object o, long offset) {
2755 return getReferenceVolatile(o, offset);
2756 }
2757
2758 @ForceInline
2759 public final <V> Object getFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType) {
2760 // this is stronger than opaque semantics
2761 return getFlatValueAcquire(o, offset, layout, valueType);
2762 }
2763
2764 /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2765 @IntrinsicCandidate
2766 public final boolean getBooleanOpaque(Object o, long offset) {
2767 return getBooleanVolatile(o, offset);
2768 }
2769
2770 /** Opaque version of {@link #getByteVolatile(Object, long)} */
2771 @IntrinsicCandidate
2772 public final byte getByteOpaque(Object o, long offset) {
2773 return getByteVolatile(o, offset);
2774 }
2775
2776 /** Opaque version of {@link #getShortVolatile(Object, long)} */
2777 @IntrinsicCandidate
2778 public final short getShortOpaque(Object o, long offset) {
2779 return getShortVolatile(o, offset);
2780 }
2781
2782 /** Opaque version of {@link #getCharVolatile(Object, long)} */
2783 @IntrinsicCandidate
2798 }
2799
2800 /** Opaque version of {@link #getLongVolatile(Object, long)} */
2801 @IntrinsicCandidate
2802 public final long getLongOpaque(Object o, long offset) {
2803 return getLongVolatile(o, offset);
2804 }
2805
2806 /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2807 @IntrinsicCandidate
2808 public final double getDoubleOpaque(Object o, long offset) {
2809 return getDoubleVolatile(o, offset);
2810 }
2811
2812 /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2813 @IntrinsicCandidate
2814 public final void putReferenceOpaque(Object o, long offset, Object x) {
2815 putReferenceVolatile(o, offset, x);
2816 }
2817
2818 @ForceInline
2819 public final <V> void putFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType, V x) {
2820 // this is stronger than opaque semantics
2821 putFlatValueRelease(o, offset, layout, valueType, x);
2822 }
2823
2824 /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2825 @IntrinsicCandidate
2826 public final void putBooleanOpaque(Object o, long offset, boolean x) {
2827 putBooleanVolatile(o, offset, x);
2828 }
2829
2830 /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2831 @IntrinsicCandidate
2832 public final void putByteOpaque(Object o, long offset, byte x) {
2833 putByteVolatile(o, offset, x);
2834 }
2835
2836 /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2837 @IntrinsicCandidate
2838 public final void putShortOpaque(Object o, long offset, short x) {
2839 putShortVolatile(o, offset, x);
2840 }
2841
2842 /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2843 @IntrinsicCandidate
2852 }
2853
2854 /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2855 @IntrinsicCandidate
2856 public final void putFloatOpaque(Object o, long offset, float x) {
2857 putFloatVolatile(o, offset, x);
2858 }
2859
2860 /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2861 @IntrinsicCandidate
2862 public final void putLongOpaque(Object o, long offset, long x) {
2863 putLongVolatile(o, offset, x);
2864 }
2865
2866 /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2867 @IntrinsicCandidate
2868 public final void putDoubleOpaque(Object o, long offset, double x) {
2869 putDoubleVolatile(o, offset, x);
2870 }
2871
2872 @ForceInline
2873 private boolean compareAndSetFlatValueAsBytes(Object o, long offset, int layout, Class<?> valueType, Object expected, Object x) {
2874 // We turn the payload of an atomic value into a numeric value (of suitable type)
2875 // by storing the value into an array element (of matching layout) and by reading
2876 // back the array element as an integral value. After which we can implement the CAS
2877 // as a plain numeric CAS. Note: this only works if the payload contains no oops
2878 // (see VarHandles::isAtomicFlat).
2879 Object[] expectedArray = newSpecialArray(valueType, 1, layout);
2880 Object xArray = newSpecialArray(valueType, 1, layout);
2881 long base = arrayBaseOffset(expectedArray);
2882 int scale = arrayIndexScale(expectedArray);
2883 putFlatValue(expectedArray, base, layout, valueType, expected);
2884 putFlatValue(xArray, base, layout, valueType, x);
2885 switch (scale) {
2886 case 1: {
2887 byte expectedByte = getByte(expectedArray, base);
2888 byte xByte = getByte(xArray, base);
2889 return compareAndSetByte(o, offset, expectedByte, xByte);
2890 }
2891 case 2: {
2892 short expectedShort = getShort(expectedArray, base);
2893 short xShort = getShort(xArray, base);
2894 return compareAndSetShort(o, offset, expectedShort, xShort);
2895 }
2896 case 4: {
2897 int expectedInt = getInt(expectedArray, base);
2898 int xInt = getInt(xArray, base);
2899 return compareAndSetInt(o, offset, expectedInt, xInt);
2900 }
2901 case 8: {
2902 long expectedLong = getLong(expectedArray, base);
2903 long xLong = getLong(xArray, base);
2904 return compareAndSetLong(o, offset, expectedLong, xLong);
2905 }
2906 default: {
2907 throw new UnsupportedOperationException();
2908 }
2909 }
2910 }
2911
2912 /**
2913 * Unblocks the given thread blocked on {@code park}, or, if it is
2914 * not blocked, causes the subsequent call to {@code park} not to
2915 * block. Note: this operation is "unsafe" solely because the
2916 * caller must somehow ensure that the thread has not been
2917 * destroyed. Nothing special is usually required to ensure this
2918 * when called from Java (in which there will ordinarily be a live
2919 * reference to the thread) but this is not nearly-automatically
2920 * so when calling from native code.
2921 *
2922 * @param thread the thread to unpark.
2923 */
2924 @IntrinsicCandidate
2925 public native void unpark(Object thread);
2926
2927 /**
2928 * Blocks current thread, returning when a balancing
2929 * {@code unpark} occurs, or a balancing {@code unpark} has
2930 * already occurred, or the thread is interrupted, or, if not
2931 * absolute and time is not zero, the given time nanoseconds have
3278 /**
3279 * Atomically exchanges the given reference value with the current
3280 * reference value of a field or array element within the given
3281 * object {@code o} at the given {@code offset}.
3282 *
3283 * @param o object/array to update the field/element in
3284 * @param offset field/element offset
3285 * @param newValue new value
3286 * @return the previous value
3287 * @since 1.8
3288 */
3289 @IntrinsicCandidate
3290 public final Object getAndSetReference(Object o, long offset, Object newValue) {
3291 Object v;
3292 do {
3293 v = getReferenceVolatile(o, offset);
3294 } while (!weakCompareAndSetReference(o, offset, v, newValue));
3295 return v;
3296 }
3297
3298 @ForceInline
3299 public final Object getAndSetReference(Object o, long offset, Class<?> valueType, Object newValue) {
3300 Object v;
3301 do {
3302 v = getReferenceVolatile(o, offset);
3303 } while (!compareAndSetReference(o, offset, valueType, v, newValue));
3304 return v;
3305 }
3306
3307 @ForceInline
3308 public Object getAndSetFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, Object newValue) {
3309 Object v;
3310 do {
3311 v = getFlatValueVolatile(o, offset, layoutKind, valueType);
3312 } while (!compareAndSetFlatValue(o, offset, layoutKind, valueType, v, newValue));
3313 return v;
3314 }
3315
3316 @ForceInline
3317 public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
3318 Object v;
3319 do {
3320 v = getReference(o, offset);
3321 } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
3322 return v;
3323 }
3324
3325 @ForceInline
3326 public final Object getAndSetReferenceRelease(Object o, long offset, Class<?> valueType, Object newValue) {
3327 return getAndSetReference(o, offset, valueType, newValue);
3328 }
3329
3330 @ForceInline
3331 public Object getAndSetFlatValueRelease(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3332 return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3333 }
3334
3335 @ForceInline
3336 public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
3337 Object v;
3338 do {
3339 v = getReferenceAcquire(o, offset);
3340 } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
3341 return v;
3342 }
3343
3344 @ForceInline
3345 public final Object getAndSetReferenceAcquire(Object o, long offset, Class<?> valueType, Object newValue) {
3346 return getAndSetReference(o, offset, valueType, newValue);
3347 }
3348
3349 @ForceInline
3350 public Object getAndSetFlatValueAcquire(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3351 return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3352 }
3353
3354 @IntrinsicCandidate
3355 public final byte getAndSetByte(Object o, long offset, byte newValue) {
3356 byte v;
3357 do {
3358 v = getByteVolatile(o, offset);
3359 } while (!weakCompareAndSetByte(o, offset, v, newValue));
3360 return v;
3361 }
3362
3363 @ForceInline
3364 public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
3365 byte v;
3366 do {
3367 v = getByte(o, offset);
3368 } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
3369 return v;
3370 }
3371
3372 @ForceInline
3373 public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {
4389 private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n) ; }
4390 private static int convEndian(boolean big, int n) { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n) ; }
4391 private static long convEndian(boolean big, long n) { return big == BIG_ENDIAN ? n : Long.reverseBytes(n) ; }
4392
4393
4394
4395 private native long allocateMemory0(long bytes);
4396 private native long reallocateMemory0(long address, long bytes);
4397 private native void freeMemory0(long address);
4398 @IntrinsicCandidate
4399 private native void setMemory0(Object o, long offset, long bytes, byte value);
4400 @IntrinsicCandidate
4401 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4402 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
4403 private native long objectFieldOffset0(Field f);
4404 private native long objectFieldOffset1(Class<?> c, String name);
4405 private native long staticFieldOffset0(Field f);
4406 private native Object staticFieldBase0(Field f);
4407 private native boolean shouldBeInitialized0(Class<?> c);
4408 private native void ensureClassInitialized0(Class<?> c);
4409 private native void notifyStrictStaticAccess0(Class<?> c, long staticFieldOffset, boolean writing);
4410 private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetic
4411 private native int arrayBaseOffset1(Object[] array);
4412 private native int arrayIndexScale0(Class<?> arrayClass);
4413 private native int arrayIndexScale1(Object[] array);
4414 private native long getObjectSize0(Object o);
4415 private native int getLoadAverage0(double[] loadavg, int nelems);
4416
4417
4418 /**
4419 * Invokes the given direct byte buffer's cleaner, if any.
4420 *
4421 * @param directBuffer a direct byte buffer
4422 * @throws NullPointerException if {@code directBuffer} is null
4423 * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
4424 * or is a {@link java.nio.Buffer#slice slice}, or is a
4425 * {@link java.nio.Buffer#duplicate duplicate}
4426 */
4427 public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
4428 if (!directBuffer.isDirect())
4429 throw new IllegalArgumentException("buffer is non-direct");
4430
4431 DirectBuffer db = (DirectBuffer) directBuffer;
4432 if (db.attachment() != null)
4433 throw new IllegalArgumentException("duplicate or slice");
4434
|