1 /* 2 * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 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, 49 * the caller must not rely on the checks and corresponding 50 * exceptions! 51 * 52 * @author John R. Rose 53 * @see #getUnsafe 54 */ 55 56 public final class Unsafe { 57 58 private static native void registerNatives(); 59 static { 60 registerNatives(); 61 } 62 63 private Unsafe() {} 64 65 private static final Unsafe theUnsafe = new Unsafe(); 66 67 /** 68 * Provides the caller with the capability of performing unsafe 69 * operations. 70 * 71 * <p>The returned {@code Unsafe} object should be carefully guarded 72 * by the caller, since it can be used to read and write data at arbitrary 73 * memory addresses. It must never be passed to untrusted code. 74 * 75 * <p>Most methods in this class are very low-level, and correspond to a 76 * small number of hardware instructions (on typical machines). Compilers 77 * are encouraged to optimize these methods accordingly. 78 * 79 * <p>Here is a suggested idiom for using unsafe operations: 80 * 81 * <pre> {@code 82 * class MyTrustedClass { 83 * private static final Unsafe unsafe = Unsafe.getUnsafe(); 84 * ... 85 * private long myCountAddress = ...; 86 * public int getCount() { return unsafe.getByte(myCountAddress); } 87 * }}</pre> 88 * 89 * (It may assist compilers to make the local variable {@code final}.) 90 */ 91 public static Unsafe getUnsafe() { 92 return theUnsafe; 93 } 94 95 /// peek and poke operations 96 /// (compilers should optimize these to memory ops) 97 98 // These work on object fields in the Java heap. 99 // They will not work on elements of packed arrays. 100 101 /** 102 * Fetches a value from a given Java variable. 103 * More specifically, fetches a field or array element within the given 104 * object {@code o} at the given offset, or (if {@code o} is null) 105 * from the memory address whose numerical value is the given offset. 106 * <p> 107 * The results are undefined unless one of the following cases is true: 108 * <ul> 109 * <li>The offset was obtained from {@link #objectFieldOffset} on 110 * the {@link java.lang.reflect.Field} of some Java field and the object 111 * referred to by {@code o} is of a class compatible with that 112 * field's class. 113 * 114 * <li>The offset and object reference {@code o} (either null or 115 * non-null) were both obtained via {@link #staticFieldOffset} 116 * and {@link #staticFieldBase} (respectively) from the 117 * reflective {@link Field} representation of some Java field. 118 * 119 * <li>The object referred to by {@code o} is an array, and the offset 120 * is an integer of the form {@code B+N*S}, where {@code N} is 121 * a valid index into the array, and {@code B} and {@code S} are 122 * the values obtained by {@link #arrayBaseOffset} and {@link 123 * #arrayIndexScale} (respectively) from the array's class. The value 124 * referred to is the {@code N}<em>th</em> element of the array. 125 * 126 * </ul> 127 * <p> 128 * If one of the above cases is true, the call references a specific Java 129 * variable (field or array element). However, the results are undefined 130 * if that variable is not in fact of the type returned by this method. 131 * <p> 132 * This method refers to a variable by means of two parameters, and so 133 * it provides (in effect) a <em>double-register</em> addressing mode 134 * for Java variables. When the object reference is null, this method 135 * uses its offset as an absolute address. This is similar in operation 136 * to methods such as {@link #getInt(long)}, which provide (in effect) a 137 * <em>single-register</em> addressing mode for non-Java variables. 138 * However, because Java variables may have a different layout in memory 139 * from non-Java variables, programmers should not assume that these 140 * two addressing modes are ever equivalent. Also, programmers should 141 * remember that offsets from the double-register addressing mode cannot 142 * be portably confused with longs used in the single-register addressing 143 * mode. 144 * 145 * @param o Java heap object in which the variable resides, if any, else 146 * null 147 * @param offset indication of where the variable resides in a Java heap 148 * object, if any, else a memory address locating the variable 149 * statically 150 * @return the value fetched from the indicated Java variable 151 * @throws RuntimeException No defined exceptions are thrown, not even 152 * {@link NullPointerException} 153 */ 154 @IntrinsicCandidate 155 public native int getInt(Object o, long offset); 156 157 /** 158 * Stores a value into a given Java variable. 159 * <p> 160 * The first two parameters are interpreted exactly as with 161 * {@link #getInt(Object, long)} to refer to a specific 162 * Java variable (field or array element). The given value 163 * is stored into that variable. 164 * <p> 165 * The variable must be of the same type as the method 166 * parameter {@code x}. 167 * 168 * @param o Java heap object in which the variable resides, if any, else 169 * null 170 * @param offset indication of where the variable resides in a Java heap 171 * object, if any, else a memory address locating the variable 172 * statically 173 * @param x the value to store into the indicated Java variable 174 * @throws RuntimeException No defined exceptions are thrown, not even 175 * {@link NullPointerException} 176 */ 177 @IntrinsicCandidate 178 public native void putInt(Object o, long offset, int x); 179 180 /** 181 * Fetches a reference value from a given Java variable. 182 * @see #getInt(Object, long) 183 */ 184 @IntrinsicCandidate 185 public native Object getReference(Object o, long offset); 186 187 /** 188 * Stores a reference value into a given Java variable. 189 * <p> 190 * Unless the reference {@code x} being stored is either null 191 * or matches the field type, the results are undefined. 192 * If the reference {@code o} is non-null, card marks or 193 * other store barriers for that object (if the VM requires them) 194 * are updated. 195 * @see #putInt(Object, long, int) 196 */ 197 @IntrinsicCandidate 198 public native void putReference(Object o, long offset, Object x); 199 200 /** @see #getInt(Object, long) */ 201 @IntrinsicCandidate 202 public native boolean getBoolean(Object o, long offset); 203 204 /** @see #putInt(Object, long, int) */ 205 @IntrinsicCandidate 206 public native void putBoolean(Object o, long offset, boolean x); 207 208 /** @see #getInt(Object, long) */ 209 @IntrinsicCandidate 210 public native byte getByte(Object o, long offset); 211 212 /** @see #putInt(Object, long, int) */ 213 @IntrinsicCandidate 214 public native void putByte(Object o, long offset, byte x); 215 216 /** @see #getInt(Object, long) */ 217 @IntrinsicCandidate 218 public native short getShort(Object o, long offset); 219 220 /** @see #putInt(Object, long, int) */ 221 @IntrinsicCandidate 222 public native void putShort(Object o, long offset, short x); 223 224 /** @see #getInt(Object, long) */ 225 @IntrinsicCandidate 226 public native char getChar(Object o, long offset); 227 228 /** @see #putInt(Object, long, int) */ 229 @IntrinsicCandidate 230 public native void putChar(Object o, long offset, char x); 231 232 /** @see #getInt(Object, long) */ 233 @IntrinsicCandidate 234 public native long getLong(Object o, long offset); 235 236 /** @see #putInt(Object, long, int) */ 237 @IntrinsicCandidate 238 public native void putLong(Object o, long offset, long x); 239 240 /** @see #getInt(Object, long) */ 241 @IntrinsicCandidate 242 public native float getFloat(Object o, long offset); 243 244 /** @see #putInt(Object, long, int) */ 245 @IntrinsicCandidate 246 public native void putFloat(Object o, long offset, float x); 247 248 /** @see #getInt(Object, long) */ 249 @IntrinsicCandidate 250 public native double getDouble(Object o, long offset); 251 252 /** @see #putInt(Object, long, int) */ 253 @IntrinsicCandidate 254 public native void putDouble(Object o, long offset, double x); 255 256 /** 257 * Fetches a native pointer from a given memory address. If the address is 258 * zero, or does not point into a block obtained from {@link 259 * #allocateMemory}, the results are undefined. 260 * 261 * <p>If the native pointer is less than 64 bits wide, it is extended as 262 * an unsigned number to a Java long. The pointer may be indexed by any 263 * given byte offset, simply by adding that offset (as a simple integer) to 264 * the long representing the pointer. The number of bytes actually read 265 * from the target address may be determined by consulting {@link 266 * #addressSize}. 267 * 268 * @see #allocateMemory 269 * @see #getInt(Object, long) 270 */ 271 @ForceInline 272 public long getAddress(Object o, long offset) { 273 if (ADDRESS_SIZE == 4) { 274 return Integer.toUnsignedLong(getInt(o, offset)); 275 } else { 276 return getLong(o, offset); 277 } 278 } 279 280 /** 281 * Stores a native pointer into a given memory address. If the address is 282 * zero, or does not point into a block obtained from {@link 283 * #allocateMemory}, the results are undefined. 284 * 285 * <p>The number of bytes actually written at the target address may be 286 * determined by consulting {@link #addressSize}. 287 * 288 * @see #allocateMemory 289 * @see #putInt(Object, long, int) 290 */ 291 @ForceInline 292 public void putAddress(Object o, long offset, long x) { 293 if (ADDRESS_SIZE == 4) { 294 putInt(o, offset, (int)x); 295 } else { 296 putLong(o, offset, x); 297 } 298 } 299 300 // These read VM internal data. 301 302 /** 303 * Fetches an uncompressed reference value from a given native variable 304 * ignoring the VM's compressed references mode. 305 * 306 * @param address a memory address locating the variable 307 * @return the value fetched from the indicated native variable 308 */ 309 public native Object getUncompressedObject(long address); 310 311 // These work on values in the C heap. 312 313 /** 314 * Fetches a value from a given memory address. If the address is zero, or 315 * does not point into a block obtained from {@link #allocateMemory}, the 316 * results are undefined. 317 * 318 * @see #allocateMemory 319 */ 320 @ForceInline 321 public byte getByte(long address) { 322 return getByte(null, address); 323 } 324 325 /** 326 * Stores a value into a given memory address. If the address is zero, or 327 * does not point into a block obtained from {@link #allocateMemory}, the 328 * results are undefined. 329 * 330 * @see #getByte(long) 331 */ 332 @ForceInline 333 public void putByte(long address, byte x) { 334 putByte(null, address, x); 335 } 336 337 /** @see #getByte(long) */ 338 @ForceInline 339 public short getShort(long address) { 340 return getShort(null, address); 341 } 342 343 /** @see #putByte(long, byte) */ 344 @ForceInline 345 public void putShort(long address, short x) { 346 putShort(null, address, x); 347 } 348 349 /** @see #getByte(long) */ 350 @ForceInline 351 public char getChar(long address) { 352 return getChar(null, address); 353 } 354 355 /** @see #putByte(long, byte) */ 356 @ForceInline 357 public void putChar(long address, char x) { 358 putChar(null, address, x); 359 } 360 361 /** @see #getByte(long) */ 362 @ForceInline 363 public int getInt(long address) { 364 return getInt(null, address); 365 } 366 367 /** @see #putByte(long, byte) */ 368 @ForceInline 369 public void putInt(long address, int x) { 370 putInt(null, address, x); 371 } 372 373 /** @see #getByte(long) */ 374 @ForceInline 375 public long getLong(long address) { 376 return getLong(null, address); 377 } 378 379 /** @see #putByte(long, byte) */ 380 @ForceInline 381 public void putLong(long address, long x) { 382 putLong(null, address, x); 383 } 384 385 /** @see #getByte(long) */ 386 @ForceInline 387 public float getFloat(long address) { 388 return getFloat(null, address); 389 } 390 391 /** @see #putByte(long, byte) */ 392 @ForceInline 393 public void putFloat(long address, float x) { 394 putFloat(null, address, x); 395 } 396 397 /** @see #getByte(long) */ 398 @ForceInline 399 public double getDouble(long address) { 400 return getDouble(null, address); 401 } 402 403 /** @see #putByte(long, byte) */ 404 @ForceInline 405 public void putDouble(long address, double x) { 406 putDouble(null, address, x); 407 } 408 409 /** @see #getAddress(Object, long) */ 410 @ForceInline 411 public long getAddress(long address) { 412 return getAddress(null, address); 413 } 414 415 /** @see #putAddress(Object, long, long) */ 416 @ForceInline 417 public void putAddress(long address, long x) { 418 putAddress(null, address, x); 419 } 420 421 422 423 /// helper methods for validating various types of objects/values 424 425 /** 426 * Create an exception reflecting that some of the input was invalid 427 * 428 * <em>Note:</em> It is the responsibility of the caller to make 429 * sure arguments are checked before the methods are called. While 430 * some rudimentary checks are performed on the input, the checks 431 * are best effort and when performance is an overriding priority, 432 * as when methods of this class are optimized by the runtime 433 * compiler, some or all checks (if any) may be elided. Hence, the 434 * caller must not rely on the checks and corresponding 435 * exceptions! 436 * 437 * @return an exception object 438 */ 439 private RuntimeException invalidInput() { 440 return new IllegalArgumentException(); 441 } 442 443 /** 444 * Check if a value is 32-bit clean (32 MSB are all zero) 445 * 446 * @param value the 64-bit value to check 447 * 448 * @return true if the value is 32-bit clean 449 */ 450 private boolean is32BitClean(long value) { 451 return value >>> 32 == 0; 452 } 453 454 /** 455 * Check the validity of a size (the equivalent of a size_t) 456 * 457 * @throws RuntimeException if the size is invalid 458 * (<em>Note:</em> after optimization, invalid inputs may 459 * go undetected, which will lead to unpredictable 460 * behavior) 461 */ 462 private void checkSize(long size) { 463 if (ADDRESS_SIZE == 4) { 464 // Note: this will also check for negative sizes 465 if (!is32BitClean(size)) { 466 throw invalidInput(); 467 } 468 } else if (size < 0) { 469 throw invalidInput(); 470 } 471 } 472 473 /** 474 * Check the validity of a native address (the equivalent of void*) 475 * 476 * @throws RuntimeException if the address is invalid 477 * (<em>Note:</em> after optimization, invalid inputs may 478 * go undetected, which will lead to unpredictable 479 * behavior) 480 */ 481 private void checkNativeAddress(long address) { 482 if (ADDRESS_SIZE == 4) { 483 // Accept both zero and sign extended pointers. A valid 484 // pointer will, after the +1 below, either have produced 485 // the value 0x0 or 0x1. Masking off the low bit allows 486 // for testing against 0. 487 if ((((address >> 32) + 1) & ~1) != 0) { 488 throw invalidInput(); 489 } 490 } 491 } 492 493 /** 494 * Check the validity of an offset, relative to a base object 495 * 496 * @param o the base object 497 * @param offset the offset to check 498 * 499 * @throws RuntimeException if the size is invalid 500 * (<em>Note:</em> after optimization, invalid inputs may 501 * go undetected, which will lead to unpredictable 502 * behavior) 503 */ 504 private void checkOffset(Object o, long offset) { 505 if (ADDRESS_SIZE == 4) { 506 // Note: this will also check for negative offsets 507 if (!is32BitClean(offset)) { 508 throw invalidInput(); 509 } 510 } else if (offset < 0) { 511 throw invalidInput(); 512 } 513 } 514 515 /** 516 * Check the validity of a double-register pointer 517 * 518 * Note: This code deliberately does *not* check for NPE for (at 519 * least) three reasons: 520 * 521 * 1) NPE is not just NULL/0 - there is a range of values all 522 * resulting in an NPE, which is not trivial to check for 523 * 524 * 2) It is the responsibility of the callers of Unsafe methods 525 * to verify the input, so throwing an exception here is not really 526 * useful - passing in a NULL pointer is a critical error and the 527 * must not expect an exception to be thrown anyway. 528 * 529 * 3) the actual operations will detect NULL pointers anyway by 530 * means of traps and signals (like SIGSEGV). 531 * 532 * @param o Java heap object, or null 533 * @param offset indication of where the variable resides in a Java heap 534 * object, if any, else a memory address locating the variable 535 * statically 536 * 537 * @throws RuntimeException if the pointer is invalid 538 * (<em>Note:</em> after optimization, invalid inputs may 539 * go undetected, which will lead to unpredictable 540 * behavior) 541 */ 542 private void checkPointer(Object o, long offset) { 543 if (o == null) { 544 checkNativeAddress(offset); 545 } else { 546 checkOffset(o, offset); 547 } 548 } 549 550 /** 551 * Check if a type is a primitive array type 552 * 553 * @param c the type to check 554 * 555 * @return true if the type is a primitive array type 556 */ 557 private void checkPrimitiveArray(Class<?> c) { 558 Class<?> componentType = c.getComponentType(); 559 if (componentType == null || !componentType.isPrimitive()) { 560 throw invalidInput(); 561 } 562 } 563 564 /** 565 * Check that a pointer is a valid primitive array type pointer 566 * 567 * Note: pointers off-heap are considered to be primitive arrays 568 * 569 * @throws RuntimeException if the pointer is invalid 570 * (<em>Note:</em> after optimization, invalid inputs may 571 * go undetected, which will lead to unpredictable 572 * behavior) 573 */ 574 private void checkPrimitivePointer(Object o, long offset) { 575 checkPointer(o, offset); 576 577 if (o != null) { 578 // If on heap, it must be a primitive array 579 checkPrimitiveArray(o.getClass()); 580 } 581 } 582 583 584 /// wrappers for malloc, realloc, free: 585 586 /** 587 * Round up allocation size to a multiple of HeapWordSize. 588 */ 589 private long alignToHeapWordSize(long bytes) { 590 if (bytes >= 0) { 591 return (bytes + ADDRESS_SIZE - 1) & ~(ADDRESS_SIZE - 1); 592 } else { 593 throw invalidInput(); 594 } 595 } 596 597 /** 598 * Allocates a new block of native memory, of the given size in bytes. The 599 * contents of the memory are uninitialized; they will generally be 600 * garbage. The resulting native pointer will never be zero, and will be 601 * aligned for all value types. Dispose of this memory by calling {@link 602 * #freeMemory}, or resize it with {@link #reallocateMemory}. 603 * 604 * <em>Note:</em> It is the responsibility of the caller to make 605 * sure arguments are checked before the methods are called. While 606 * some rudimentary checks are performed on the input, the checks 607 * are best effort and when performance is an overriding priority, 608 * as when methods of this class are optimized by the runtime 609 * compiler, some or all checks (if any) may be elided. Hence, the 610 * caller must not rely on the checks and corresponding 611 * exceptions! 612 * 613 * @throws RuntimeException if the size is negative or too large 614 * for the native size_t type 615 * 616 * @throws OutOfMemoryError if the allocation is refused by the system 617 * 618 * @see #getByte(long) 619 * @see #putByte(long, byte) 620 */ 621 public long allocateMemory(long bytes) { 622 bytes = alignToHeapWordSize(bytes); 623 624 allocateMemoryChecks(bytes); 625 626 if (bytes == 0) { 627 return 0; 628 } 629 630 long p = allocateMemory0(bytes); 631 if (p == 0) { 632 throw new OutOfMemoryError("Unable to allocate " + bytes + " bytes"); 633 } 634 635 return p; 636 } 637 638 /** 639 * Validate the arguments to allocateMemory 640 * 641 * @throws RuntimeException if the arguments are invalid 642 * (<em>Note:</em> after optimization, invalid inputs may 643 * go undetected, which will lead to unpredictable 644 * behavior) 645 */ 646 private void allocateMemoryChecks(long bytes) { 647 checkSize(bytes); 648 } 649 650 /** 651 * Resizes a new block of native memory, to the given size in bytes. The 652 * contents of the new block past the size of the old block are 653 * uninitialized; they will generally be garbage. The resulting native 654 * pointer will be zero if and only if the requested size is zero. The 655 * resulting native pointer will be aligned for all value types. Dispose 656 * of this memory by calling {@link #freeMemory}, or resize it with {@link 657 * #reallocateMemory}. The address passed to this method may be null, in 658 * which case an allocation will be performed. 659 * 660 * <em>Note:</em> It is the responsibility of the caller to make 661 * sure arguments are checked before the methods are called. While 662 * some rudimentary checks are performed on the input, the checks 663 * are best effort and when performance is an overriding priority, 664 * as when methods of this class are optimized by the runtime 665 * compiler, some or all checks (if any) may be elided. Hence, the 666 * caller must not rely on the checks and corresponding 667 * exceptions! 668 * 669 * @throws RuntimeException if the size is negative or too large 670 * for the native size_t type 671 * 672 * @throws OutOfMemoryError if the allocation is refused by the system 673 * 674 * @see #allocateMemory 675 */ 676 public long reallocateMemory(long address, long bytes) { 677 bytes = alignToHeapWordSize(bytes); 678 679 reallocateMemoryChecks(address, bytes); 680 681 if (bytes == 0) { 682 freeMemory(address); 683 return 0; 684 } 685 686 long p = (address == 0) ? allocateMemory0(bytes) : reallocateMemory0(address, bytes); 687 if (p == 0) { 688 throw new OutOfMemoryError("Unable to allocate " + bytes + " bytes"); 689 } 690 691 return p; 692 } 693 694 /** 695 * Validate the arguments to reallocateMemory 696 * 697 * @throws RuntimeException if the arguments are invalid 698 * (<em>Note:</em> after optimization, invalid inputs may 699 * go undetected, which will lead to unpredictable 700 * behavior) 701 */ 702 private void reallocateMemoryChecks(long address, long bytes) { 703 checkPointer(null, address); 704 checkSize(bytes); 705 } 706 707 /** 708 * Sets all bytes in a given block of memory to a fixed value 709 * (usually zero). 710 * 711 * <p>This method determines a block's base address by means of two parameters, 712 * and so it provides (in effect) a <em>double-register</em> addressing mode, 713 * as discussed in {@link #getInt(Object,long)}. When the object reference is null, 714 * the offset supplies an absolute base address. 715 * 716 * <p>The stores are in coherent (atomic) units of a size determined 717 * by the address and length parameters. If the effective address and 718 * length are all even modulo 8, the stores take place in 'long' units. 719 * If the effective address and length are (resp.) even modulo 4 or 2, 720 * the stores take place in units of 'int' or 'short'. 721 * 722 * <em>Note:</em> It is the responsibility of the caller to make 723 * sure arguments are checked before the methods are called. While 724 * some rudimentary checks are performed on the input, the checks 725 * are best effort and when performance is an overriding priority, 726 * as when methods of this class are optimized by the runtime 727 * compiler, some or all checks (if any) may be elided. Hence, the 728 * caller must not rely on the checks and corresponding 729 * exceptions! 730 * 731 * @throws RuntimeException if any of the arguments is invalid 732 * 733 * @since 1.7 734 */ 735 public void setMemory(Object o, long offset, long bytes, byte value) { 736 setMemoryChecks(o, offset, bytes, value); 737 738 if (bytes == 0) { 739 return; 740 } 741 742 setMemory0(o, offset, bytes, value); 743 } 744 745 /** 746 * Sets all bytes in a given block of memory to a fixed value 747 * (usually zero). This provides a <em>single-register</em> addressing mode, 748 * as discussed in {@link #getInt(Object,long)}. 749 * 750 * <p>Equivalent to {@code setMemory(null, address, bytes, value)}. 751 */ 752 public void setMemory(long address, long bytes, byte value) { 753 setMemory(null, address, bytes, value); 754 } 755 756 /** 757 * Validate the arguments to setMemory 758 * 759 * @throws RuntimeException if the arguments are invalid 760 * (<em>Note:</em> after optimization, invalid inputs may 761 * go undetected, which will lead to unpredictable 762 * behavior) 763 */ 764 private void setMemoryChecks(Object o, long offset, long bytes, byte value) { 765 checkPrimitivePointer(o, offset); 766 checkSize(bytes); 767 } 768 769 /** 770 * Sets all bytes in a given block of memory to a copy of another 771 * block. 772 * 773 * <p>This method determines each block's base address by means of two parameters, 774 * and so it provides (in effect) a <em>double-register</em> addressing mode, 775 * as discussed in {@link #getInt(Object,long)}. When the object reference is null, 776 * the offset supplies an absolute base address. 777 * 778 * <p>The transfers are in coherent (atomic) units of a size determined 779 * by the address and length parameters. If the effective addresses and 780 * length are all even modulo 8, the transfer takes place in 'long' units. 781 * If the effective addresses and length are (resp.) even modulo 4 or 2, 782 * the transfer takes place in units of 'int' or 'short'. 783 * 784 * <em>Note:</em> It is the responsibility of the caller to make 785 * sure arguments are checked before the methods are called. While 786 * some rudimentary checks are performed on the input, the checks 787 * are best effort and when performance is an overriding priority, 788 * as when methods of this class are optimized by the runtime 789 * compiler, some or all checks (if any) may be elided. Hence, the 790 * caller must not rely on the checks and corresponding 791 * exceptions! 792 * 793 * @throws RuntimeException if any of the arguments is invalid 794 * 795 * @since 1.7 796 */ 797 public void copyMemory(Object srcBase, long srcOffset, 798 Object destBase, long destOffset, 799 long bytes) { 800 copyMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes); 801 802 if (bytes == 0) { 803 return; 804 } 805 806 copyMemory0(srcBase, srcOffset, destBase, destOffset, bytes); 807 } 808 809 /** 810 * Sets all bytes in a given block of memory to a copy of another 811 * block. This provides a <em>single-register</em> addressing mode, 812 * as discussed in {@link #getInt(Object,long)}. 813 * 814 * Equivalent to {@code copyMemory(null, srcAddress, null, destAddress, bytes)}. 815 */ 816 public void copyMemory(long srcAddress, long destAddress, long bytes) { 817 copyMemory(null, srcAddress, null, destAddress, bytes); 818 } 819 820 /** 821 * Validate the arguments to copyMemory 822 * 823 * @throws RuntimeException if any of the arguments is invalid 824 * (<em>Note:</em> after optimization, invalid inputs may 825 * go undetected, which will lead to unpredictable 826 * behavior) 827 */ 828 private void copyMemoryChecks(Object srcBase, long srcOffset, 829 Object destBase, long destOffset, 830 long bytes) { 831 checkSize(bytes); 832 checkPrimitivePointer(srcBase, srcOffset); 833 checkPrimitivePointer(destBase, destOffset); 834 } 835 836 /** 837 * Copies all elements from one block of memory to another block, 838 * *unconditionally* byte swapping the elements on the fly. 839 * 840 * <p>This method determines each block's base address by means of two parameters, 841 * and so it provides (in effect) a <em>double-register</em> addressing mode, 842 * as discussed in {@link #getInt(Object,long)}. When the object reference is null, 843 * the offset supplies an absolute base address. 844 * 845 * <em>Note:</em> It is the responsibility of the caller to make 846 * sure arguments are checked before the methods are called. While 847 * some rudimentary checks are performed on the input, the checks 848 * are best effort and when performance is an overriding priority, 849 * as when methods of this class are optimized by the runtime 850 * compiler, some or all checks (if any) may be elided. Hence, the 851 * caller must not rely on the checks and corresponding 852 * exceptions! 853 * 854 * @throws RuntimeException if any of the arguments is invalid 855 * 856 * @since 9 857 */ 858 public void copySwapMemory(Object srcBase, long srcOffset, 859 Object destBase, long destOffset, 860 long bytes, long elemSize) { 861 copySwapMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes, elemSize); 862 863 if (bytes == 0) { 864 return; 865 } 866 867 copySwapMemory0(srcBase, srcOffset, destBase, destOffset, bytes, elemSize); 868 } 869 870 private void copySwapMemoryChecks(Object srcBase, long srcOffset, 871 Object destBase, long destOffset, 872 long bytes, long elemSize) { 873 checkSize(bytes); 874 875 if (elemSize != 2 && elemSize != 4 && elemSize != 8) { 876 throw invalidInput(); 877 } 878 if (bytes % elemSize != 0) { 879 throw invalidInput(); 880 } 881 882 checkPrimitivePointer(srcBase, srcOffset); 883 checkPrimitivePointer(destBase, destOffset); 884 } 885 886 /** 887 * Copies all elements from one block of memory to another block, byte swapping the 888 * elements on the fly. 889 * 890 * This provides a <em>single-register</em> addressing mode, as 891 * discussed in {@link #getInt(Object,long)}. 892 * 893 * Equivalent to {@code copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize)}. 894 */ 895 public void copySwapMemory(long srcAddress, long destAddress, long bytes, long elemSize) { 896 copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize); 897 } 898 899 /** 900 * Disposes of a block of native memory, as obtained from {@link 901 * #allocateMemory} or {@link #reallocateMemory}. The address passed to 902 * this method may be null, in which case no action is taken. 903 * 904 * <em>Note:</em> It is the responsibility of the caller to make 905 * sure arguments are checked before the methods are called. While 906 * some rudimentary checks are performed on the input, the checks 907 * are best effort and when performance is an overriding priority, 908 * as when methods of this class are optimized by the runtime 909 * compiler, some or all checks (if any) may be elided. Hence, the 910 * caller must not rely on the checks and corresponding 911 * exceptions! 912 * 913 * @throws RuntimeException if any of the arguments is invalid 914 * 915 * @see #allocateMemory 916 */ 917 public void freeMemory(long address) { 918 freeMemoryChecks(address); 919 920 if (address == 0) { 921 return; 922 } 923 924 freeMemory0(address); 925 } 926 927 /** 928 * Validate the arguments to freeMemory 929 * 930 * @throws RuntimeException if the arguments are invalid 931 * (<em>Note:</em> after optimization, invalid inputs may 932 * go undetected, which will lead to unpredictable 933 * behavior) 934 */ 935 private void freeMemoryChecks(long address) { 936 checkPointer(null, address); 937 } 938 939 /** 940 * Ensure writeback of a specified virtual memory address range 941 * from cache to physical memory. All bytes in the address range 942 * are guaranteed to have been written back to physical memory on 943 * return from this call i.e. subsequently executed store 944 * instructions are guaranteed not to be visible before the 945 * writeback is completed. 946 * 947 * @param address 948 * the lowest byte address that must be guaranteed written 949 * back to memory. bytes at lower addresses may also be 950 * written back. 951 * 952 * @param length 953 * the length in bytes of the region starting at address 954 * that must be guaranteed written back to memory. 955 * 956 * @throws RuntimeException if memory writeback is not supported 957 * on the current hardware of if the arguments are invalid. 958 * (<em>Note:</em> after optimization, invalid inputs may 959 * go undetected, which will lead to unpredictable 960 * behavior) 961 * 962 * @since 14 963 */ 964 965 public void writebackMemory(long address, long length) { 966 checkWritebackEnabled(); 967 checkWritebackMemory(address, length); 968 969 // perform any required pre-writeback barrier 970 writebackPreSync0(); 971 972 // write back one cache line at a time 973 long line = dataCacheLineAlignDown(address); 974 long end = address + length; 975 while (line < end) { 976 writeback0(line); 977 line += dataCacheLineFlushSize(); 978 } 979 980 // perform any required post-writeback barrier 981 writebackPostSync0(); 982 } 983 984 /** 985 * Validate the arguments to writebackMemory 986 * 987 * @throws RuntimeException if the arguments are invalid 988 * (<em>Note:</em> after optimization, invalid inputs may 989 * go undetected, which will lead to unpredictable 990 * behavior) 991 */ 992 private void checkWritebackMemory(long address, long length) { 993 checkNativeAddress(address); 994 checkSize(length); 995 } 996 997 /** 998 * Validate that the current hardware supports memory writeback. 999 * (<em>Note:</em> this is a belt and braces check. Clients are 1000 * expected to test whether writeback is enabled by calling 1001 * ({@link isWritebackEnabled #isWritebackEnabled} and avoid 1002 * calling method {@link writeback #writeback} if it is disabled). 1003 * 1004 * 1005 * @throws RuntimeException if memory writeback is not supported 1006 */ 1007 private void checkWritebackEnabled() { 1008 if (!isWritebackEnabled()) { 1009 throw new RuntimeException("writebackMemory not enabled!"); 1010 } 1011 } 1012 1013 /** 1014 * force writeback of an individual cache line. 1015 * 1016 * @param address 1017 * the start address of the cache line to be written back 1018 */ 1019 @IntrinsicCandidate 1020 private native void writeback0(long address); 1021 1022 /** 1023 * Serialize writeback operations relative to preceding memory writes. 1024 */ 1025 @IntrinsicCandidate 1026 private native void writebackPreSync0(); 1027 1028 /** 1029 * Serialize writeback operations relative to following memory writes. 1030 */ 1031 @IntrinsicCandidate 1032 private native void writebackPostSync0(); 1033 1034 /// random queries 1035 1036 /** 1037 * This constant differs from all results that will ever be returned from 1038 * {@link #staticFieldOffset}, {@link #objectFieldOffset}, 1039 * or {@link #arrayBaseOffset}. 1040 */ 1041 public static final int INVALID_FIELD_OFFSET = -1; 1042 1043 /** 1044 * Reports the location of a given field in the storage allocation of its 1045 * class. Do not expect to perform any sort of arithmetic on this offset; 1046 * it is just a cookie which is passed to the unsafe heap memory accessors. 1047 * 1048 * <p>Any given field will always have the same offset and base, and no 1049 * two distinct fields of the same class will ever have the same offset 1050 * and base. 1051 * 1052 * <p>As of 1.4.1, offsets for fields are represented as long values, 1053 * although the Sun JVM does not use the most significant 32 bits. 1054 * However, JVM implementations which store static fields at absolute 1055 * addresses can use long offsets and null base pointers to express 1056 * the field locations in a form usable by {@link #getInt(Object,long)}. 1057 * Therefore, code which will be ported to such JVMs on 64-bit platforms 1058 * must preserve all bits of static field offsets. 1059 * @see #getInt(Object, long) 1060 */ 1061 public long objectFieldOffset(Field f) { 1062 if (f == null) { 1063 throw new NullPointerException(); 1064 } 1065 1066 return objectFieldOffset0(f); 1067 } 1068 1069 /** 1070 * Reports the location of the field with a given name in the storage 1071 * allocation of its class. 1072 * 1073 * @throws NullPointerException if any parameter is {@code null}. 1074 * @throws InternalError if there is no field named {@code name} declared 1075 * in class {@code c}, i.e., if {@code c.getDeclaredField(name)} 1076 * would throw {@code java.lang.NoSuchFieldException}. 1077 * 1078 * @see #objectFieldOffset(Field) 1079 */ 1080 public long objectFieldOffset(Class<?> c, String name) { 1081 if (c == null || name == null) { 1082 throw new NullPointerException(); 1083 } 1084 1085 return objectFieldOffset1(c, name); 1086 } 1087 1088 /** 1089 * Reports the location of a given static field, in conjunction with {@link 1090 * #staticFieldBase}. 1091 * <p>Do not expect to perform any sort of arithmetic on this offset; 1092 * it is just a cookie which is passed to the unsafe heap memory accessors. 1093 * 1094 * <p>Any given field will always have the same offset, and no two distinct 1095 * fields of the same class will ever have the same offset. 1096 * 1097 * <p>As of 1.4.1, offsets for fields are represented as long values, 1098 * although the Sun JVM does not use the most significant 32 bits. 1099 * It is hard to imagine a JVM technology which needs more than 1100 * a few bits to encode an offset within a non-array object, 1101 * However, for consistency with other methods in this class, 1102 * this method reports its result as a long value. 1103 * @see #getInt(Object, long) 1104 */ 1105 public long staticFieldOffset(Field f) { 1106 if (f == null) { 1107 throw new NullPointerException(); 1108 } 1109 1110 return staticFieldOffset0(f); 1111 } 1112 1113 /** 1114 * Reports the location of a given static field, in conjunction with {@link 1115 * #staticFieldOffset}. 1116 * <p>Fetch the base "Object", if any, with which static fields of the 1117 * given class can be accessed via methods like {@link #getInt(Object, 1118 * long)}. This value may be null. This value may refer to an object 1119 * which is a "cookie", not guaranteed to be a real Object, and it should 1120 * not be used in any way except as argument to the get and put routines in 1121 * this class. 1122 */ 1123 public Object staticFieldBase(Field f) { 1124 if (f == null) { 1125 throw new NullPointerException(); 1126 } 1127 1128 return staticFieldBase0(f); 1129 } 1130 1131 /** 1132 * Detects if the given class may need to be initialized. This is often 1133 * needed in conjunction with obtaining the static field base of a 1134 * class. 1135 * @return false only if a call to {@code ensureClassInitialized} would have no effect 1136 */ 1137 public boolean shouldBeInitialized(Class<?> c) { 1138 if (c == null) { 1139 throw new NullPointerException(); 1140 } 1141 1142 return shouldBeInitialized0(c); 1143 } 1144 1145 /** 1146 * Ensures the given class has been initialized (see JVMS-5.5 for details). 1147 * This is often needed in conjunction with obtaining the static field base 1148 * of a class. 1149 * 1150 * The call returns when either class {@code c} is fully initialized or 1151 * class {@code c} is being initialized and the call is performed from 1152 * the initializing thread. In the latter case a subsequent call to 1153 * {@link #shouldBeInitialized} will return {@code true}. 1154 */ 1155 public void ensureClassInitialized(Class<?> c) { 1156 if (c == null) { 1157 throw new NullPointerException(); 1158 } 1159 1160 ensureClassInitialized0(c); 1161 } 1162 1163 /** 1164 * Reports the offset of the first element in the storage allocation of a 1165 * given array class. If {@link #arrayIndexScale} returns a non-zero value 1166 * for the same class, you may use that scale factor, together with this 1167 * base offset, to form new offsets to access elements of arrays of the 1168 * given class. 1169 * 1170 * @see #getInt(Object, long) 1171 * @see #putInt(Object, long, int) 1172 */ 1173 public int arrayBaseOffset(Class<?> arrayClass) { 1174 if (arrayClass == null) { 1175 throw new NullPointerException(); 1176 } 1177 1178 return arrayBaseOffset0(arrayClass); 1179 } 1180 1181 1182 /** The value of {@code arrayBaseOffset(boolean[].class)} */ 1183 public static final int ARRAY_BOOLEAN_BASE_OFFSET 1184 = theUnsafe.arrayBaseOffset(boolean[].class); 1185 1186 /** The value of {@code arrayBaseOffset(byte[].class)} */ 1187 public static final int ARRAY_BYTE_BASE_OFFSET 1188 = theUnsafe.arrayBaseOffset(byte[].class); 1189 1190 /** The value of {@code arrayBaseOffset(short[].class)} */ 1191 public static final int ARRAY_SHORT_BASE_OFFSET 1192 = theUnsafe.arrayBaseOffset(short[].class); 1193 1194 /** The value of {@code arrayBaseOffset(char[].class)} */ 1195 public static final int ARRAY_CHAR_BASE_OFFSET 1196 = theUnsafe.arrayBaseOffset(char[].class); 1197 1198 /** The value of {@code arrayBaseOffset(int[].class)} */ 1199 public static final int ARRAY_INT_BASE_OFFSET 1200 = theUnsafe.arrayBaseOffset(int[].class); 1201 1202 /** The value of {@code arrayBaseOffset(long[].class)} */ 1203 public static final int ARRAY_LONG_BASE_OFFSET 1204 = theUnsafe.arrayBaseOffset(long[].class); 1205 1206 /** The value of {@code arrayBaseOffset(float[].class)} */ 1207 public static final int ARRAY_FLOAT_BASE_OFFSET 1208 = theUnsafe.arrayBaseOffset(float[].class); 1209 1210 /** The value of {@code arrayBaseOffset(double[].class)} */ 1211 public static final int ARRAY_DOUBLE_BASE_OFFSET 1212 = theUnsafe.arrayBaseOffset(double[].class); 1213 1214 /** The value of {@code arrayBaseOffset(Object[].class)} */ 1215 public static final int ARRAY_OBJECT_BASE_OFFSET 1216 = theUnsafe.arrayBaseOffset(Object[].class); 1217 1218 /** 1219 * Reports the scale factor for addressing elements in the storage 1220 * allocation of a given array class. However, arrays of "narrow" types 1221 * will generally not work properly with accessors like {@link 1222 * #getByte(Object, long)}, so the scale factor for such classes is reported 1223 * as zero. 1224 * 1225 * @see #arrayBaseOffset 1226 * @see #getInt(Object, long) 1227 * @see #putInt(Object, long, int) 1228 */ 1229 public int arrayIndexScale(Class<?> arrayClass) { 1230 if (arrayClass == null) { 1231 throw new NullPointerException(); 1232 } 1233 1234 return arrayIndexScale0(arrayClass); 1235 } 1236 1237 1238 /** The value of {@code arrayIndexScale(boolean[].class)} */ 1239 public static final int ARRAY_BOOLEAN_INDEX_SCALE 1240 = theUnsafe.arrayIndexScale(boolean[].class); 1241 1242 /** The value of {@code arrayIndexScale(byte[].class)} */ 1243 public static final int ARRAY_BYTE_INDEX_SCALE 1244 = theUnsafe.arrayIndexScale(byte[].class); 1245 1246 /** The value of {@code arrayIndexScale(short[].class)} */ 1247 public static final int ARRAY_SHORT_INDEX_SCALE 1248 = theUnsafe.arrayIndexScale(short[].class); 1249 1250 /** The value of {@code arrayIndexScale(char[].class)} */ 1251 public static final int ARRAY_CHAR_INDEX_SCALE 1252 = theUnsafe.arrayIndexScale(char[].class); 1253 1254 /** The value of {@code arrayIndexScale(int[].class)} */ 1255 public static final int ARRAY_INT_INDEX_SCALE 1256 = theUnsafe.arrayIndexScale(int[].class); 1257 1258 /** The value of {@code arrayIndexScale(long[].class)} */ 1259 public static final int ARRAY_LONG_INDEX_SCALE 1260 = theUnsafe.arrayIndexScale(long[].class); 1261 1262 /** The value of {@code arrayIndexScale(float[].class)} */ 1263 public static final int ARRAY_FLOAT_INDEX_SCALE 1264 = theUnsafe.arrayIndexScale(float[].class); 1265 1266 /** The value of {@code arrayIndexScale(double[].class)} */ 1267 public static final int ARRAY_DOUBLE_INDEX_SCALE 1268 = theUnsafe.arrayIndexScale(double[].class); 1269 1270 /** The value of {@code arrayIndexScale(Object[].class)} */ 1271 public static final int ARRAY_OBJECT_INDEX_SCALE 1272 = theUnsafe.arrayIndexScale(Object[].class); 1273 1274 /** 1275 * Reports the size in bytes of a native pointer, as stored via {@link 1276 * #putAddress}. This value will be either 4 or 8. Note that the sizes of 1277 * other primitive types (as stored in native memory blocks) is determined 1278 * fully by their information content. 1279 */ 1280 public int addressSize() { 1281 return ADDRESS_SIZE; 1282 } 1283 1284 /** The value of {@code addressSize()} */ 1285 public static final int ADDRESS_SIZE = ADDRESS_SIZE0; 1286 1287 /** 1288 * Reports the size in bytes of a native memory page (whatever that is). 1289 * This value will always be a power of two. 1290 */ 1291 public int pageSize() { return PAGE_SIZE; } 1292 1293 /** 1294 * Reports the size in bytes of a data cache line written back by 1295 * the hardware cache line flush operation available to the JVM or 1296 * 0 if data cache line flushing is not enabled. 1297 */ 1298 public int dataCacheLineFlushSize() { return DATA_CACHE_LINE_FLUSH_SIZE; } 1299 1300 /** 1301 * Rounds down address to a data cache line boundary as 1302 * determined by {@link #dataCacheLineFlushSize} 1303 * @return the rounded down address 1304 */ 1305 public long dataCacheLineAlignDown(long address) { 1306 return (address & ~(DATA_CACHE_LINE_FLUSH_SIZE - 1)); 1307 } 1308 1309 /** 1310 * Returns true if data cache line writeback 1311 */ 1312 public static boolean isWritebackEnabled() { return DATA_CACHE_LINE_FLUSH_SIZE != 0; } 1313 1314 /// random trusted operations from JNI: 1315 1316 /** 1317 * Tells the VM to define a class, without security checks. By default, the 1318 * class loader and protection domain come from the caller's class. 1319 */ 1320 public Class<?> defineClass(String name, byte[] b, int off, int len, 1321 ClassLoader loader, 1322 ProtectionDomain protectionDomain) { 1323 if (b == null) { 1324 throw new NullPointerException(); 1325 } 1326 if (len < 0) { 1327 throw new ArrayIndexOutOfBoundsException(); 1328 } 1329 1330 return defineClass0(name, b, off, len, loader, protectionDomain); 1331 } 1332 1333 public native Class<?> defineClass0(String name, byte[] b, int off, int len, 1334 ClassLoader loader, 1335 ProtectionDomain protectionDomain); 1336 1337 /** 1338 * Allocates an instance but does not run any constructor. 1339 * Initializes the class if it has not yet been. 1340 */ 1341 @IntrinsicCandidate 1342 public native Object allocateInstance(Class<?> cls) 1343 throws InstantiationException; 1344 1345 /** 1346 * Allocates an array of a given type, but does not do zeroing. 1347 * <p> 1348 * This method should only be used in the very rare cases where a high-performance code 1349 * overwrites the destination array completely, and compilers cannot assist in zeroing elimination. 1350 * In an overwhelming majority of cases, a normal Java allocation should be used instead. 1351 * <p> 1352 * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents 1353 * before allowing untrusted code, or code in other threads, to observe the reference 1354 * to the newly allocated array. In addition, the publication of the array reference must be 1355 * safe according to the Java Memory Model requirements. 1356 * <p> 1357 * The safest approach to deal with an uninitialized array is to keep the reference to it in local 1358 * variable at least until the initialization is complete, and then publish it <b>once</b>, either 1359 * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor, 1360 * or issuing a {@link #storeFence} before publishing the reference. 1361 * <p> 1362 * @implnote This method can only allocate primitive arrays, to avoid garbage reference 1363 * elements that could break heap integrity. 1364 * 1365 * @param componentType array component type to allocate 1366 * @param length array size to allocate 1367 * @throws IllegalArgumentException if component type is null, or not a primitive class; 1368 * or the length is negative 1369 */ 1370 public Object allocateUninitializedArray(Class<?> componentType, int length) { 1371 if (componentType == null) { 1372 throw new IllegalArgumentException("Component type is null"); 1373 } 1374 if (!componentType.isPrimitive()) { 1375 throw new IllegalArgumentException("Component type is not primitive"); 1376 } 1377 if (length < 0) { 1378 throw new IllegalArgumentException("Negative length"); 1379 } 1380 return allocateUninitializedArray0(componentType, length); 1381 } 1382 1383 @IntrinsicCandidate 1384 private Object allocateUninitializedArray0(Class<?> componentType, int length) { 1385 // These fallbacks provide zeroed arrays, but intrinsic is not required to 1386 // return the zeroed arrays. 1387 if (componentType == byte.class) return new byte[length]; 1388 if (componentType == boolean.class) return new boolean[length]; 1389 if (componentType == short.class) return new short[length]; 1390 if (componentType == char.class) return new char[length]; 1391 if (componentType == int.class) return new int[length]; 1392 if (componentType == float.class) return new float[length]; 1393 if (componentType == long.class) return new long[length]; 1394 if (componentType == double.class) return new double[length]; 1395 return null; 1396 } 1397 1398 /** Throws the exception without telling the verifier. */ 1399 public native void throwException(Throwable ee); 1400 1401 /** 1402 * Atomically updates Java variable to {@code x} if it is currently 1403 * holding {@code expected}. 1404 * 1405 * <p>This operation has memory semantics of a {@code volatile} read 1406 * and write. Corresponds to C11 atomic_compare_exchange_strong. 1407 * 1408 * @return {@code true} if successful 1409 */ 1410 @IntrinsicCandidate 1411 public final native boolean compareAndSetReference(Object o, long offset, 1412 Object expected, 1413 Object x); 1414 1415 @IntrinsicCandidate 1416 public final native Object compareAndExchangeReference(Object o, long offset, 1417 Object expected, 1418 Object x); 1419 1420 @IntrinsicCandidate 1421 public final Object compareAndExchangeReferenceAcquire(Object o, long offset, 1422 Object expected, 1423 Object x) { 1424 return compareAndExchangeReference(o, offset, expected, x); 1425 } 1426 1427 @IntrinsicCandidate 1428 public final Object compareAndExchangeReferenceRelease(Object o, long offset, 1429 Object expected, 1430 Object x) { 1431 return compareAndExchangeReference(o, offset, expected, x); 1432 } 1433 1434 @IntrinsicCandidate 1435 public final boolean weakCompareAndSetReferencePlain(Object o, long offset, 1436 Object expected, 1437 Object x) { 1438 return compareAndSetReference(o, offset, expected, x); 1439 } 1440 1441 @IntrinsicCandidate 1442 public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset, 1443 Object expected, 1444 Object x) { 1445 return compareAndSetReference(o, offset, expected, x); 1446 } 1447 1448 @IntrinsicCandidate 1449 public final boolean weakCompareAndSetReferenceRelease(Object o, long offset, 1450 Object expected, 1451 Object x) { 1452 return compareAndSetReference(o, offset, expected, x); 1453 } 1454 1455 @IntrinsicCandidate 1456 public final boolean weakCompareAndSetReference(Object o, long offset, 1457 Object expected, 1458 Object x) { 1459 return compareAndSetReference(o, offset, expected, x); 1460 } 1461 1462 /** 1463 * Atomically updates Java variable to {@code x} if it is currently 1464 * holding {@code expected}. 1465 * 1466 * <p>This operation has memory semantics of a {@code volatile} read 1467 * and write. Corresponds to C11 atomic_compare_exchange_strong. 1468 * 1469 * @return {@code true} if successful 1470 */ 1471 @IntrinsicCandidate 1472 public final native boolean compareAndSetInt(Object o, long offset, 1473 int expected, 1474 int x); 1475 1476 @IntrinsicCandidate 1477 public final native int compareAndExchangeInt(Object o, long offset, 1478 int expected, 1479 int x); 1480 1481 @IntrinsicCandidate 1482 public final int compareAndExchangeIntAcquire(Object o, long offset, 1483 int expected, 1484 int x) { 1485 return compareAndExchangeInt(o, offset, expected, x); 1486 } 1487 1488 @IntrinsicCandidate 1489 public final int compareAndExchangeIntRelease(Object o, long offset, 1490 int expected, 1491 int x) { 1492 return compareAndExchangeInt(o, offset, expected, x); 1493 } 1494 1495 @IntrinsicCandidate 1496 public final boolean weakCompareAndSetIntPlain(Object o, long offset, 1497 int expected, 1498 int x) { 1499 return compareAndSetInt(o, offset, expected, x); 1500 } 1501 1502 @IntrinsicCandidate 1503 public final boolean weakCompareAndSetIntAcquire(Object o, long offset, 1504 int expected, 1505 int x) { 1506 return compareAndSetInt(o, offset, expected, x); 1507 } 1508 1509 @IntrinsicCandidate 1510 public final boolean weakCompareAndSetIntRelease(Object o, long offset, 1511 int expected, 1512 int x) { 1513 return compareAndSetInt(o, offset, expected, x); 1514 } 1515 1516 @IntrinsicCandidate 1517 public final boolean weakCompareAndSetInt(Object o, long offset, 1518 int expected, 1519 int x) { 1520 return compareAndSetInt(o, offset, expected, x); 1521 } 1522 1523 @IntrinsicCandidate 1524 public final byte compareAndExchangeByte(Object o, long offset, 1525 byte expected, 1526 byte x) { 1527 long wordOffset = offset & ~3; 1528 int shift = (int) (offset & 3) << 3; 1529 if (BIG_ENDIAN) { 1530 shift = 24 - shift; 1531 } 1532 int mask = 0xFF << shift; 1533 int maskedExpected = (expected & 0xFF) << shift; 1534 int maskedX = (x & 0xFF) << shift; 1535 int fullWord; 1536 do { 1537 fullWord = getIntVolatile(o, wordOffset); 1538 if ((fullWord & mask) != maskedExpected) 1539 return (byte) ((fullWord & mask) >> shift); 1540 } while (!weakCompareAndSetInt(o, wordOffset, 1541 fullWord, (fullWord & ~mask) | maskedX)); 1542 return expected; 1543 } 1544 1545 @IntrinsicCandidate 1546 public final boolean compareAndSetByte(Object o, long offset, 1547 byte expected, 1548 byte x) { 1549 return compareAndExchangeByte(o, offset, expected, x) == expected; 1550 } 1551 1552 @IntrinsicCandidate 1553 public final boolean weakCompareAndSetByte(Object o, long offset, 1554 byte expected, 1555 byte x) { 1556 return compareAndSetByte(o, offset, expected, x); 1557 } 1558 1559 @IntrinsicCandidate 1560 public final boolean weakCompareAndSetByteAcquire(Object o, long offset, 1561 byte expected, 1562 byte x) { 1563 return weakCompareAndSetByte(o, offset, expected, x); 1564 } 1565 1566 @IntrinsicCandidate 1567 public final boolean weakCompareAndSetByteRelease(Object o, long offset, 1568 byte expected, 1569 byte x) { 1570 return weakCompareAndSetByte(o, offset, expected, x); 1571 } 1572 1573 @IntrinsicCandidate 1574 public final boolean weakCompareAndSetBytePlain(Object o, long offset, 1575 byte expected, 1576 byte x) { 1577 return weakCompareAndSetByte(o, offset, expected, x); 1578 } 1579 1580 @IntrinsicCandidate 1581 public final byte compareAndExchangeByteAcquire(Object o, long offset, 1582 byte expected, 1583 byte x) { 1584 return compareAndExchangeByte(o, offset, expected, x); 1585 } 1586 1587 @IntrinsicCandidate 1588 public final byte compareAndExchangeByteRelease(Object o, long offset, 1589 byte expected, 1590 byte x) { 1591 return compareAndExchangeByte(o, offset, expected, x); 1592 } 1593 1594 @IntrinsicCandidate 1595 public final short compareAndExchangeShort(Object o, long offset, 1596 short expected, 1597 short x) { 1598 if ((offset & 3) == 3) { 1599 throw new IllegalArgumentException("Update spans the word, not supported"); 1600 } 1601 long wordOffset = offset & ~3; 1602 int shift = (int) (offset & 3) << 3; 1603 if (BIG_ENDIAN) { 1604 shift = 16 - shift; 1605 } 1606 int mask = 0xFFFF << shift; 1607 int maskedExpected = (expected & 0xFFFF) << shift; 1608 int maskedX = (x & 0xFFFF) << shift; 1609 int fullWord; 1610 do { 1611 fullWord = getIntVolatile(o, wordOffset); 1612 if ((fullWord & mask) != maskedExpected) { 1613 return (short) ((fullWord & mask) >> shift); 1614 } 1615 } while (!weakCompareAndSetInt(o, wordOffset, 1616 fullWord, (fullWord & ~mask) | maskedX)); 1617 return expected; 1618 } 1619 1620 @IntrinsicCandidate 1621 public final boolean compareAndSetShort(Object o, long offset, 1622 short expected, 1623 short x) { 1624 return compareAndExchangeShort(o, offset, expected, x) == expected; 1625 } 1626 1627 @IntrinsicCandidate 1628 public final boolean weakCompareAndSetShort(Object o, long offset, 1629 short expected, 1630 short x) { 1631 return compareAndSetShort(o, offset, expected, x); 1632 } 1633 1634 @IntrinsicCandidate 1635 public final boolean weakCompareAndSetShortAcquire(Object o, long offset, 1636 short expected, 1637 short x) { 1638 return weakCompareAndSetShort(o, offset, expected, x); 1639 } 1640 1641 @IntrinsicCandidate 1642 public final boolean weakCompareAndSetShortRelease(Object o, long offset, 1643 short expected, 1644 short x) { 1645 return weakCompareAndSetShort(o, offset, expected, x); 1646 } 1647 1648 @IntrinsicCandidate 1649 public final boolean weakCompareAndSetShortPlain(Object o, long offset, 1650 short expected, 1651 short x) { 1652 return weakCompareAndSetShort(o, offset, expected, x); 1653 } 1654 1655 1656 @IntrinsicCandidate 1657 public final short compareAndExchangeShortAcquire(Object o, long offset, 1658 short expected, 1659 short x) { 1660 return compareAndExchangeShort(o, offset, expected, x); 1661 } 1662 1663 @IntrinsicCandidate 1664 public final short compareAndExchangeShortRelease(Object o, long offset, 1665 short expected, 1666 short x) { 1667 return compareAndExchangeShort(o, offset, expected, x); 1668 } 1669 1670 @ForceInline 1671 private char s2c(short s) { 1672 return (char) s; 1673 } 1674 1675 @ForceInline 1676 private short c2s(char s) { 1677 return (short) s; 1678 } 1679 1680 @ForceInline 1681 public final boolean compareAndSetChar(Object o, long offset, 1682 char expected, 1683 char x) { 1684 return compareAndSetShort(o, offset, c2s(expected), c2s(x)); 1685 } 1686 1687 @ForceInline 1688 public final char compareAndExchangeChar(Object o, long offset, 1689 char expected, 1690 char x) { 1691 return s2c(compareAndExchangeShort(o, offset, c2s(expected), c2s(x))); 1692 } 1693 1694 @ForceInline 1695 public final char compareAndExchangeCharAcquire(Object o, long offset, 1696 char expected, 1697 char x) { 1698 return s2c(compareAndExchangeShortAcquire(o, offset, c2s(expected), c2s(x))); 1699 } 1700 1701 @ForceInline 1702 public final char compareAndExchangeCharRelease(Object o, long offset, 1703 char expected, 1704 char x) { 1705 return s2c(compareAndExchangeShortRelease(o, offset, c2s(expected), c2s(x))); 1706 } 1707 1708 @ForceInline 1709 public final boolean weakCompareAndSetChar(Object o, long offset, 1710 char expected, 1711 char x) { 1712 return weakCompareAndSetShort(o, offset, c2s(expected), c2s(x)); 1713 } 1714 1715 @ForceInline 1716 public final boolean weakCompareAndSetCharAcquire(Object o, long offset, 1717 char expected, 1718 char x) { 1719 return weakCompareAndSetShortAcquire(o, offset, c2s(expected), c2s(x)); 1720 } 1721 1722 @ForceInline 1723 public final boolean weakCompareAndSetCharRelease(Object o, long offset, 1724 char expected, 1725 char x) { 1726 return weakCompareAndSetShortRelease(o, offset, c2s(expected), c2s(x)); 1727 } 1728 1729 @ForceInline 1730 public final boolean weakCompareAndSetCharPlain(Object o, long offset, 1731 char expected, 1732 char x) { 1733 return weakCompareAndSetShortPlain(o, offset, c2s(expected), c2s(x)); 1734 } 1735 1736 /** 1737 * The JVM converts integral values to boolean values using two 1738 * different conventions, byte testing against zero and truncation 1739 * to least-significant bit. 1740 * 1741 * <p>The JNI documents specify that, at least for returning 1742 * values from native methods, a Java boolean value is converted 1743 * to the value-set 0..1 by first truncating to a byte (0..255 or 1744 * maybe -128..127) and then testing against zero. Thus, Java 1745 * booleans in non-Java data structures are by convention 1746 * represented as 8-bit containers containing either zero (for 1747 * false) or any non-zero value (for true). 1748 * 1749 * <p>Java booleans in the heap are also stored in bytes, but are 1750 * strongly normalized to the value-set 0..1 (i.e., they are 1751 * truncated to the least-significant bit). 1752 * 1753 * <p>The main reason for having different conventions for 1754 * conversion is performance: Truncation to the least-significant 1755 * bit can be usually implemented with fewer (machine) 1756 * instructions than byte testing against zero. 1757 * 1758 * <p>A number of Unsafe methods load boolean values from the heap 1759 * as bytes. Unsafe converts those values according to the JNI 1760 * rules (i.e, using the "testing against zero" convention). The 1761 * method {@code byte2bool} implements that conversion. 1762 * 1763 * @param b the byte to be converted to boolean 1764 * @return the result of the conversion 1765 */ 1766 @ForceInline 1767 private boolean byte2bool(byte b) { 1768 return b != 0; 1769 } 1770 1771 /** 1772 * Convert a boolean value to a byte. The return value is strongly 1773 * normalized to the value-set 0..1 (i.e., the value is truncated 1774 * to the least-significant bit). See {@link #byte2bool(byte)} for 1775 * more details on conversion conventions. 1776 * 1777 * @param b the boolean to be converted to byte (and then normalized) 1778 * @return the result of the conversion 1779 */ 1780 @ForceInline 1781 private byte bool2byte(boolean b) { 1782 return b ? (byte)1 : (byte)0; 1783 } 1784 1785 @ForceInline 1786 public final boolean compareAndSetBoolean(Object o, long offset, 1787 boolean expected, 1788 boolean x) { 1789 return compareAndSetByte(o, offset, bool2byte(expected), bool2byte(x)); 1790 } 1791 1792 @ForceInline 1793 public final boolean compareAndExchangeBoolean(Object o, long offset, 1794 boolean expected, 1795 boolean x) { 1796 return byte2bool(compareAndExchangeByte(o, offset, bool2byte(expected), bool2byte(x))); 1797 } 1798 1799 @ForceInline 1800 public final boolean compareAndExchangeBooleanAcquire(Object o, long offset, 1801 boolean expected, 1802 boolean x) { 1803 return byte2bool(compareAndExchangeByteAcquire(o, offset, bool2byte(expected), bool2byte(x))); 1804 } 1805 1806 @ForceInline 1807 public final boolean compareAndExchangeBooleanRelease(Object o, long offset, 1808 boolean expected, 1809 boolean x) { 1810 return byte2bool(compareAndExchangeByteRelease(o, offset, bool2byte(expected), bool2byte(x))); 1811 } 1812 1813 @ForceInline 1814 public final boolean weakCompareAndSetBoolean(Object o, long offset, 1815 boolean expected, 1816 boolean x) { 1817 return weakCompareAndSetByte(o, offset, bool2byte(expected), bool2byte(x)); 1818 } 1819 1820 @ForceInline 1821 public final boolean weakCompareAndSetBooleanAcquire(Object o, long offset, 1822 boolean expected, 1823 boolean x) { 1824 return weakCompareAndSetByteAcquire(o, offset, bool2byte(expected), bool2byte(x)); 1825 } 1826 1827 @ForceInline 1828 public final boolean weakCompareAndSetBooleanRelease(Object o, long offset, 1829 boolean expected, 1830 boolean x) { 1831 return weakCompareAndSetByteRelease(o, offset, bool2byte(expected), bool2byte(x)); 1832 } 1833 1834 @ForceInline 1835 public final boolean weakCompareAndSetBooleanPlain(Object o, long offset, 1836 boolean expected, 1837 boolean x) { 1838 return weakCompareAndSetBytePlain(o, offset, bool2byte(expected), bool2byte(x)); 1839 } 1840 1841 /** 1842 * Atomically updates Java variable to {@code x} if it is currently 1843 * holding {@code expected}. 1844 * 1845 * <p>This operation has memory semantics of a {@code volatile} read 1846 * and write. Corresponds to C11 atomic_compare_exchange_strong. 1847 * 1848 * @return {@code true} if successful 1849 */ 1850 @ForceInline 1851 public final boolean compareAndSetFloat(Object o, long offset, 1852 float expected, 1853 float x) { 1854 return compareAndSetInt(o, offset, 1855 Float.floatToRawIntBits(expected), 1856 Float.floatToRawIntBits(x)); 1857 } 1858 1859 @ForceInline 1860 public final float compareAndExchangeFloat(Object o, long offset, 1861 float expected, 1862 float x) { 1863 int w = compareAndExchangeInt(o, offset, 1864 Float.floatToRawIntBits(expected), 1865 Float.floatToRawIntBits(x)); 1866 return Float.intBitsToFloat(w); 1867 } 1868 1869 @ForceInline 1870 public final float compareAndExchangeFloatAcquire(Object o, long offset, 1871 float expected, 1872 float x) { 1873 int w = compareAndExchangeIntAcquire(o, offset, 1874 Float.floatToRawIntBits(expected), 1875 Float.floatToRawIntBits(x)); 1876 return Float.intBitsToFloat(w); 1877 } 1878 1879 @ForceInline 1880 public final float compareAndExchangeFloatRelease(Object o, long offset, 1881 float expected, 1882 float x) { 1883 int w = compareAndExchangeIntRelease(o, offset, 1884 Float.floatToRawIntBits(expected), 1885 Float.floatToRawIntBits(x)); 1886 return Float.intBitsToFloat(w); 1887 } 1888 1889 @ForceInline 1890 public final boolean weakCompareAndSetFloatPlain(Object o, long offset, 1891 float expected, 1892 float x) { 1893 return weakCompareAndSetIntPlain(o, offset, 1894 Float.floatToRawIntBits(expected), 1895 Float.floatToRawIntBits(x)); 1896 } 1897 1898 @ForceInline 1899 public final boolean weakCompareAndSetFloatAcquire(Object o, long offset, 1900 float expected, 1901 float x) { 1902 return weakCompareAndSetIntAcquire(o, offset, 1903 Float.floatToRawIntBits(expected), 1904 Float.floatToRawIntBits(x)); 1905 } 1906 1907 @ForceInline 1908 public final boolean weakCompareAndSetFloatRelease(Object o, long offset, 1909 float expected, 1910 float x) { 1911 return weakCompareAndSetIntRelease(o, offset, 1912 Float.floatToRawIntBits(expected), 1913 Float.floatToRawIntBits(x)); 1914 } 1915 1916 @ForceInline 1917 public final boolean weakCompareAndSetFloat(Object o, long offset, 1918 float expected, 1919 float x) { 1920 return weakCompareAndSetInt(o, offset, 1921 Float.floatToRawIntBits(expected), 1922 Float.floatToRawIntBits(x)); 1923 } 1924 1925 /** 1926 * Atomically updates Java variable to {@code x} if it is currently 1927 * holding {@code expected}. 1928 * 1929 * <p>This operation has memory semantics of a {@code volatile} read 1930 * and write. Corresponds to C11 atomic_compare_exchange_strong. 1931 * 1932 * @return {@code true} if successful 1933 */ 1934 @ForceInline 1935 public final boolean compareAndSetDouble(Object o, long offset, 1936 double expected, 1937 double x) { 1938 return compareAndSetLong(o, offset, 1939 Double.doubleToRawLongBits(expected), 1940 Double.doubleToRawLongBits(x)); 1941 } 1942 1943 @ForceInline 1944 public final double compareAndExchangeDouble(Object o, long offset, 1945 double expected, 1946 double x) { 1947 long w = compareAndExchangeLong(o, offset, 1948 Double.doubleToRawLongBits(expected), 1949 Double.doubleToRawLongBits(x)); 1950 return Double.longBitsToDouble(w); 1951 } 1952 1953 @ForceInline 1954 public final double compareAndExchangeDoubleAcquire(Object o, long offset, 1955 double expected, 1956 double x) { 1957 long w = compareAndExchangeLongAcquire(o, offset, 1958 Double.doubleToRawLongBits(expected), 1959 Double.doubleToRawLongBits(x)); 1960 return Double.longBitsToDouble(w); 1961 } 1962 1963 @ForceInline 1964 public final double compareAndExchangeDoubleRelease(Object o, long offset, 1965 double expected, 1966 double x) { 1967 long w = compareAndExchangeLongRelease(o, offset, 1968 Double.doubleToRawLongBits(expected), 1969 Double.doubleToRawLongBits(x)); 1970 return Double.longBitsToDouble(w); 1971 } 1972 1973 @ForceInline 1974 public final boolean weakCompareAndSetDoublePlain(Object o, long offset, 1975 double expected, 1976 double x) { 1977 return weakCompareAndSetLongPlain(o, offset, 1978 Double.doubleToRawLongBits(expected), 1979 Double.doubleToRawLongBits(x)); 1980 } 1981 1982 @ForceInline 1983 public final boolean weakCompareAndSetDoubleAcquire(Object o, long offset, 1984 double expected, 1985 double x) { 1986 return weakCompareAndSetLongAcquire(o, offset, 1987 Double.doubleToRawLongBits(expected), 1988 Double.doubleToRawLongBits(x)); 1989 } 1990 1991 @ForceInline 1992 public final boolean weakCompareAndSetDoubleRelease(Object o, long offset, 1993 double expected, 1994 double x) { 1995 return weakCompareAndSetLongRelease(o, offset, 1996 Double.doubleToRawLongBits(expected), 1997 Double.doubleToRawLongBits(x)); 1998 } 1999 2000 @ForceInline 2001 public final boolean weakCompareAndSetDouble(Object o, long offset, 2002 double expected, 2003 double x) { 2004 return weakCompareAndSetLong(o, offset, 2005 Double.doubleToRawLongBits(expected), 2006 Double.doubleToRawLongBits(x)); 2007 } 2008 2009 /** 2010 * Atomically updates Java variable to {@code x} if it is currently 2011 * holding {@code expected}. 2012 * 2013 * <p>This operation has memory semantics of a {@code volatile} read 2014 * and write. Corresponds to C11 atomic_compare_exchange_strong. 2015 * 2016 * @return {@code true} if successful 2017 */ 2018 @IntrinsicCandidate 2019 public final native boolean compareAndSetLong(Object o, long offset, 2020 long expected, 2021 long x); 2022 2023 @IntrinsicCandidate 2024 public final native long compareAndExchangeLong(Object o, long offset, 2025 long expected, 2026 long x); 2027 2028 @IntrinsicCandidate 2029 public final long compareAndExchangeLongAcquire(Object o, long offset, 2030 long expected, 2031 long x) { 2032 return compareAndExchangeLong(o, offset, expected, x); 2033 } 2034 2035 @IntrinsicCandidate 2036 public final long compareAndExchangeLongRelease(Object o, long offset, 2037 long expected, 2038 long x) { 2039 return compareAndExchangeLong(o, offset, expected, x); 2040 } 2041 2042 @IntrinsicCandidate 2043 public final boolean weakCompareAndSetLongPlain(Object o, long offset, 2044 long expected, 2045 long x) { 2046 return compareAndSetLong(o, offset, expected, x); 2047 } 2048 2049 @IntrinsicCandidate 2050 public final boolean weakCompareAndSetLongAcquire(Object o, long offset, 2051 long expected, 2052 long x) { 2053 return compareAndSetLong(o, offset, expected, x); 2054 } 2055 2056 @IntrinsicCandidate 2057 public final boolean weakCompareAndSetLongRelease(Object o, long offset, 2058 long expected, 2059 long x) { 2060 return compareAndSetLong(o, offset, expected, x); 2061 } 2062 2063 @IntrinsicCandidate 2064 public final boolean weakCompareAndSetLong(Object o, long offset, 2065 long expected, 2066 long x) { 2067 return compareAndSetLong(o, offset, expected, x); 2068 } 2069 2070 /** 2071 * Fetches a reference value from a given Java variable, with volatile 2072 * load semantics. Otherwise identical to {@link #getReference(Object, long)} 2073 */ 2074 @IntrinsicCandidate 2075 public native Object getReferenceVolatile(Object o, long offset); 2076 2077 /** 2078 * Stores a reference value into a given Java variable, with 2079 * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)} 2080 */ 2081 @IntrinsicCandidate 2082 public native void putReferenceVolatile(Object o, long offset, Object x); 2083 2084 /** Volatile version of {@link #getInt(Object, long)} */ 2085 @IntrinsicCandidate 2086 public native int getIntVolatile(Object o, long offset); 2087 2088 /** Volatile version of {@link #putInt(Object, long, int)} */ 2089 @IntrinsicCandidate 2090 public native void putIntVolatile(Object o, long offset, int x); 2091 2092 /** Volatile version of {@link #getBoolean(Object, long)} */ 2093 @IntrinsicCandidate 2094 public native boolean getBooleanVolatile(Object o, long offset); 2095 2096 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */ 2097 @IntrinsicCandidate 2098 public native void putBooleanVolatile(Object o, long offset, boolean x); 2099 2100 /** Volatile version of {@link #getByte(Object, long)} */ 2101 @IntrinsicCandidate 2102 public native byte getByteVolatile(Object o, long offset); 2103 2104 /** Volatile version of {@link #putByte(Object, long, byte)} */ 2105 @IntrinsicCandidate 2106 public native void putByteVolatile(Object o, long offset, byte x); 2107 2108 /** Volatile version of {@link #getShort(Object, long)} */ 2109 @IntrinsicCandidate 2110 public native short getShortVolatile(Object o, long offset); 2111 2112 /** Volatile version of {@link #putShort(Object, long, short)} */ 2113 @IntrinsicCandidate 2114 public native void putShortVolatile(Object o, long offset, short x); 2115 2116 /** Volatile version of {@link #getChar(Object, long)} */ 2117 @IntrinsicCandidate 2118 public native char getCharVolatile(Object o, long offset); 2119 2120 /** Volatile version of {@link #putChar(Object, long, char)} */ 2121 @IntrinsicCandidate 2122 public native void putCharVolatile(Object o, long offset, char x); 2123 2124 /** Volatile version of {@link #getLong(Object, long)} */ 2125 @IntrinsicCandidate 2126 public native long getLongVolatile(Object o, long offset); 2127 2128 /** Volatile version of {@link #putLong(Object, long, long)} */ 2129 @IntrinsicCandidate 2130 public native void putLongVolatile(Object o, long offset, long x); 2131 2132 /** Volatile version of {@link #getFloat(Object, long)} */ 2133 @IntrinsicCandidate 2134 public native float getFloatVolatile(Object o, long offset); 2135 2136 /** Volatile version of {@link #putFloat(Object, long, float)} */ 2137 @IntrinsicCandidate 2138 public native void putFloatVolatile(Object o, long offset, float x); 2139 2140 /** Volatile version of {@link #getDouble(Object, long)} */ 2141 @IntrinsicCandidate 2142 public native double getDoubleVolatile(Object o, long offset); 2143 2144 /** Volatile version of {@link #putDouble(Object, long, double)} */ 2145 @IntrinsicCandidate 2146 public native void putDoubleVolatile(Object o, long offset, double x); 2147 2148 2149 2150 /** Acquire version of {@link #getReferenceVolatile(Object, long)} */ 2151 @IntrinsicCandidate 2152 public final Object getReferenceAcquire(Object o, long offset) { 2153 return getReferenceVolatile(o, offset); 2154 } 2155 2156 /** Acquire version of {@link #getBooleanVolatile(Object, long)} */ 2157 @IntrinsicCandidate 2158 public final boolean getBooleanAcquire(Object o, long offset) { 2159 return getBooleanVolatile(o, offset); 2160 } 2161 2162 /** Acquire version of {@link #getByteVolatile(Object, long)} */ 2163 @IntrinsicCandidate 2164 public final byte getByteAcquire(Object o, long offset) { 2165 return getByteVolatile(o, offset); 2166 } 2167 2168 /** Acquire version of {@link #getShortVolatile(Object, long)} */ 2169 @IntrinsicCandidate 2170 public final short getShortAcquire(Object o, long offset) { 2171 return getShortVolatile(o, offset); 2172 } 2173 2174 /** Acquire version of {@link #getCharVolatile(Object, long)} */ 2175 @IntrinsicCandidate 2176 public final char getCharAcquire(Object o, long offset) { 2177 return getCharVolatile(o, offset); 2178 } 2179 2180 /** Acquire version of {@link #getIntVolatile(Object, long)} */ 2181 @IntrinsicCandidate 2182 public final int getIntAcquire(Object o, long offset) { 2183 return getIntVolatile(o, offset); 2184 } 2185 2186 /** Acquire version of {@link #getFloatVolatile(Object, long)} */ 2187 @IntrinsicCandidate 2188 public final float getFloatAcquire(Object o, long offset) { 2189 return getFloatVolatile(o, offset); 2190 } 2191 2192 /** Acquire version of {@link #getLongVolatile(Object, long)} */ 2193 @IntrinsicCandidate 2194 public final long getLongAcquire(Object o, long offset) { 2195 return getLongVolatile(o, offset); 2196 } 2197 2198 /** Acquire version of {@link #getDoubleVolatile(Object, long)} */ 2199 @IntrinsicCandidate 2200 public final double getDoubleAcquire(Object o, long offset) { 2201 return getDoubleVolatile(o, offset); 2202 } 2203 2204 /* 2205 * Versions of {@link #putReferenceVolatile(Object, long, Object)} 2206 * that do not guarantee immediate visibility of the store to 2207 * other threads. This method is generally only useful if the 2208 * underlying field is a Java volatile (or if an array cell, one 2209 * that is otherwise only accessed using volatile accesses). 2210 * 2211 * Corresponds to C11 atomic_store_explicit(..., memory_order_release). 2212 */ 2213 2214 /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */ 2215 @IntrinsicCandidate 2216 public final void putReferenceRelease(Object o, long offset, Object x) { 2217 putReferenceVolatile(o, offset, x); 2218 } 2219 2220 /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */ 2221 @IntrinsicCandidate 2222 public final void putBooleanRelease(Object o, long offset, boolean x) { 2223 putBooleanVolatile(o, offset, x); 2224 } 2225 2226 /** Release version of {@link #putByteVolatile(Object, long, byte)} */ 2227 @IntrinsicCandidate 2228 public final void putByteRelease(Object o, long offset, byte x) { 2229 putByteVolatile(o, offset, x); 2230 } 2231 2232 /** Release version of {@link #putShortVolatile(Object, long, short)} */ 2233 @IntrinsicCandidate 2234 public final void putShortRelease(Object o, long offset, short x) { 2235 putShortVolatile(o, offset, x); 2236 } 2237 2238 /** Release version of {@link #putCharVolatile(Object, long, char)} */ 2239 @IntrinsicCandidate 2240 public final void putCharRelease(Object o, long offset, char x) { 2241 putCharVolatile(o, offset, x); 2242 } 2243 2244 /** Release version of {@link #putIntVolatile(Object, long, int)} */ 2245 @IntrinsicCandidate 2246 public final void putIntRelease(Object o, long offset, int x) { 2247 putIntVolatile(o, offset, x); 2248 } 2249 2250 /** Release version of {@link #putFloatVolatile(Object, long, float)} */ 2251 @IntrinsicCandidate 2252 public final void putFloatRelease(Object o, long offset, float x) { 2253 putFloatVolatile(o, offset, x); 2254 } 2255 2256 /** Release version of {@link #putLongVolatile(Object, long, long)} */ 2257 @IntrinsicCandidate 2258 public final void putLongRelease(Object o, long offset, long x) { 2259 putLongVolatile(o, offset, x); 2260 } 2261 2262 /** Release version of {@link #putDoubleVolatile(Object, long, double)} */ 2263 @IntrinsicCandidate 2264 public final void putDoubleRelease(Object o, long offset, double x) { 2265 putDoubleVolatile(o, offset, x); 2266 } 2267 2268 // ------------------------------ Opaque -------------------------------------- 2269 2270 /** Opaque version of {@link #getReferenceVolatile(Object, long)} */ 2271 @IntrinsicCandidate 2272 public final Object getReferenceOpaque(Object o, long offset) { 2273 return getReferenceVolatile(o, offset); 2274 } 2275 2276 /** Opaque version of {@link #getBooleanVolatile(Object, long)} */ 2277 @IntrinsicCandidate 2278 public final boolean getBooleanOpaque(Object o, long offset) { 2279 return getBooleanVolatile(o, offset); 2280 } 2281 2282 /** Opaque version of {@link #getByteVolatile(Object, long)} */ 2283 @IntrinsicCandidate 2284 public final byte getByteOpaque(Object o, long offset) { 2285 return getByteVolatile(o, offset); 2286 } 2287 2288 /** Opaque version of {@link #getShortVolatile(Object, long)} */ 2289 @IntrinsicCandidate 2290 public final short getShortOpaque(Object o, long offset) { 2291 return getShortVolatile(o, offset); 2292 } 2293 2294 /** Opaque version of {@link #getCharVolatile(Object, long)} */ 2295 @IntrinsicCandidate 2296 public final char getCharOpaque(Object o, long offset) { 2297 return getCharVolatile(o, offset); 2298 } 2299 2300 /** Opaque version of {@link #getIntVolatile(Object, long)} */ 2301 @IntrinsicCandidate 2302 public final int getIntOpaque(Object o, long offset) { 2303 return getIntVolatile(o, offset); 2304 } 2305 2306 /** Opaque version of {@link #getFloatVolatile(Object, long)} */ 2307 @IntrinsicCandidate 2308 public final float getFloatOpaque(Object o, long offset) { 2309 return getFloatVolatile(o, offset); 2310 } 2311 2312 /** Opaque version of {@link #getLongVolatile(Object, long)} */ 2313 @IntrinsicCandidate 2314 public final long getLongOpaque(Object o, long offset) { 2315 return getLongVolatile(o, offset); 2316 } 2317 2318 /** Opaque version of {@link #getDoubleVolatile(Object, long)} */ 2319 @IntrinsicCandidate 2320 public final double getDoubleOpaque(Object o, long offset) { 2321 return getDoubleVolatile(o, offset); 2322 } 2323 2324 /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */ 2325 @IntrinsicCandidate 2326 public final void putReferenceOpaque(Object o, long offset, Object x) { 2327 putReferenceVolatile(o, offset, x); 2328 } 2329 2330 /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */ 2331 @IntrinsicCandidate 2332 public final void putBooleanOpaque(Object o, long offset, boolean x) { 2333 putBooleanVolatile(o, offset, x); 2334 } 2335 2336 /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */ 2337 @IntrinsicCandidate 2338 public final void putByteOpaque(Object o, long offset, byte x) { 2339 putByteVolatile(o, offset, x); 2340 } 2341 2342 /** Opaque version of {@link #putShortVolatile(Object, long, short)} */ 2343 @IntrinsicCandidate 2344 public final void putShortOpaque(Object o, long offset, short x) { 2345 putShortVolatile(o, offset, x); 2346 } 2347 2348 /** Opaque version of {@link #putCharVolatile(Object, long, char)} */ 2349 @IntrinsicCandidate 2350 public final void putCharOpaque(Object o, long offset, char x) { 2351 putCharVolatile(o, offset, x); 2352 } 2353 2354 /** Opaque version of {@link #putIntVolatile(Object, long, int)} */ 2355 @IntrinsicCandidate 2356 public final void putIntOpaque(Object o, long offset, int x) { 2357 putIntVolatile(o, offset, x); 2358 } 2359 2360 /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */ 2361 @IntrinsicCandidate 2362 public final void putFloatOpaque(Object o, long offset, float x) { 2363 putFloatVolatile(o, offset, x); 2364 } 2365 2366 /** Opaque version of {@link #putLongVolatile(Object, long, long)} */ 2367 @IntrinsicCandidate 2368 public final void putLongOpaque(Object o, long offset, long x) { 2369 putLongVolatile(o, offset, x); 2370 } 2371 2372 /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */ 2373 @IntrinsicCandidate 2374 public final void putDoubleOpaque(Object o, long offset, double x) { 2375 putDoubleVolatile(o, offset, x); 2376 } 2377 2378 /** 2379 * Unblocks the given thread blocked on {@code park}, or, if it is 2380 * not blocked, causes the subsequent call to {@code park} not to 2381 * block. Note: this operation is "unsafe" solely because the 2382 * caller must somehow ensure that the thread has not been 2383 * destroyed. Nothing special is usually required to ensure this 2384 * when called from Java (in which there will ordinarily be a live 2385 * reference to the thread) but this is not nearly-automatically 2386 * so when calling from native code. 2387 * 2388 * @param thread the thread to unpark. 2389 */ 2390 @IntrinsicCandidate 2391 public native void unpark(Object thread); 2392 2393 /** 2394 * Blocks current thread, returning when a balancing 2395 * {@code unpark} occurs, or a balancing {@code unpark} has 2396 * already occurred, or the thread is interrupted, or, if not 2397 * absolute and time is not zero, the given time nanoseconds have 2398 * elapsed, or if absolute, the given deadline in milliseconds 2399 * since Epoch has passed, or spuriously (i.e., returning for no 2400 * "reason"). Note: This operation is in the Unsafe class only 2401 * because {@code unpark} is, so it would be strange to place it 2402 * elsewhere. 2403 */ 2404 @IntrinsicCandidate 2405 public native void park(boolean isAbsolute, long time); 2406 2407 /** 2408 * Gets the load average in the system run queue assigned 2409 * to the available processors averaged over various periods of time. 2410 * This method retrieves the given {@code nelem} samples and 2411 * assigns to the elements of the given {@code loadavg} array. 2412 * The system imposes a maximum of 3 samples, representing 2413 * averages over the last 1, 5, and 15 minutes, respectively. 2414 * 2415 * @param loadavg an array of double of size nelems 2416 * @param nelems the number of samples to be retrieved and 2417 * must be 1 to 3. 2418 * 2419 * @return the number of samples actually retrieved; or -1 2420 * if the load average is unobtainable. 2421 */ 2422 public int getLoadAverage(double[] loadavg, int nelems) { 2423 if (nelems < 0 || nelems > 3 || nelems > loadavg.length) { 2424 throw new ArrayIndexOutOfBoundsException(); 2425 } 2426 2427 return getLoadAverage0(loadavg, nelems); 2428 } 2429 2430 // The following contain CAS-based Java implementations used on 2431 // platforms not supporting native instructions 2432 2433 /** 2434 * Atomically adds the given value to the current value of a field 2435 * or array element within the given object {@code o} 2436 * at the given {@code offset}. 2437 * 2438 * @param o object/array to update the field/element in 2439 * @param offset field/element offset 2440 * @param delta the value to add 2441 * @return the previous value 2442 * @since 1.8 2443 */ 2444 @IntrinsicCandidate 2445 public final int getAndAddInt(Object o, long offset, int delta) { 2446 int v; 2447 do { 2448 v = getIntVolatile(o, offset); 2449 } while (!weakCompareAndSetInt(o, offset, v, v + delta)); 2450 return v; 2451 } 2452 2453 @ForceInline 2454 public final int getAndAddIntRelease(Object o, long offset, int delta) { 2455 int v; 2456 do { 2457 v = getInt(o, offset); 2458 } while (!weakCompareAndSetIntRelease(o, offset, v, v + delta)); 2459 return v; 2460 } 2461 2462 @ForceInline 2463 public final int getAndAddIntAcquire(Object o, long offset, int delta) { 2464 int v; 2465 do { 2466 v = getIntAcquire(o, offset); 2467 } while (!weakCompareAndSetIntAcquire(o, offset, v, v + delta)); 2468 return v; 2469 } 2470 2471 /** 2472 * Atomically adds the given value to the current value of a field 2473 * or array element within the given object {@code o} 2474 * at the given {@code offset}. 2475 * 2476 * @param o object/array to update the field/element in 2477 * @param offset field/element offset 2478 * @param delta the value to add 2479 * @return the previous value 2480 * @since 1.8 2481 */ 2482 @IntrinsicCandidate 2483 public final long getAndAddLong(Object o, long offset, long delta) { 2484 long v; 2485 do { 2486 v = getLongVolatile(o, offset); 2487 } while (!weakCompareAndSetLong(o, offset, v, v + delta)); 2488 return v; 2489 } 2490 2491 @ForceInline 2492 public final long getAndAddLongRelease(Object o, long offset, long delta) { 2493 long v; 2494 do { 2495 v = getLong(o, offset); 2496 } while (!weakCompareAndSetLongRelease(o, offset, v, v + delta)); 2497 return v; 2498 } 2499 2500 @ForceInline 2501 public final long getAndAddLongAcquire(Object o, long offset, long delta) { 2502 long v; 2503 do { 2504 v = getLongAcquire(o, offset); 2505 } while (!weakCompareAndSetLongAcquire(o, offset, v, v + delta)); 2506 return v; 2507 } 2508 2509 @IntrinsicCandidate 2510 public final byte getAndAddByte(Object o, long offset, byte delta) { 2511 byte v; 2512 do { 2513 v = getByteVolatile(o, offset); 2514 } while (!weakCompareAndSetByte(o, offset, v, (byte) (v + delta))); 2515 return v; 2516 } 2517 2518 @ForceInline 2519 public final byte getAndAddByteRelease(Object o, long offset, byte delta) { 2520 byte v; 2521 do { 2522 v = getByte(o, offset); 2523 } while (!weakCompareAndSetByteRelease(o, offset, v, (byte) (v + delta))); 2524 return v; 2525 } 2526 2527 @ForceInline 2528 public final byte getAndAddByteAcquire(Object o, long offset, byte delta) { 2529 byte v; 2530 do { 2531 v = getByteAcquire(o, offset); 2532 } while (!weakCompareAndSetByteAcquire(o, offset, v, (byte) (v + delta))); 2533 return v; 2534 } 2535 2536 @IntrinsicCandidate 2537 public final short getAndAddShort(Object o, long offset, short delta) { 2538 short v; 2539 do { 2540 v = getShortVolatile(o, offset); 2541 } while (!weakCompareAndSetShort(o, offset, v, (short) (v + delta))); 2542 return v; 2543 } 2544 2545 @ForceInline 2546 public final short getAndAddShortRelease(Object o, long offset, short delta) { 2547 short v; 2548 do { 2549 v = getShort(o, offset); 2550 } while (!weakCompareAndSetShortRelease(o, offset, v, (short) (v + delta))); 2551 return v; 2552 } 2553 2554 @ForceInline 2555 public final short getAndAddShortAcquire(Object o, long offset, short delta) { 2556 short v; 2557 do { 2558 v = getShortAcquire(o, offset); 2559 } while (!weakCompareAndSetShortAcquire(o, offset, v, (short) (v + delta))); 2560 return v; 2561 } 2562 2563 @ForceInline 2564 public final char getAndAddChar(Object o, long offset, char delta) { 2565 return (char) getAndAddShort(o, offset, (short) delta); 2566 } 2567 2568 @ForceInline 2569 public final char getAndAddCharRelease(Object o, long offset, char delta) { 2570 return (char) getAndAddShortRelease(o, offset, (short) delta); 2571 } 2572 2573 @ForceInline 2574 public final char getAndAddCharAcquire(Object o, long offset, char delta) { 2575 return (char) getAndAddShortAcquire(o, offset, (short) delta); 2576 } 2577 2578 @ForceInline 2579 public final float getAndAddFloat(Object o, long offset, float delta) { 2580 int expectedBits; 2581 float v; 2582 do { 2583 // Load and CAS with the raw bits to avoid issues with NaNs and 2584 // possible bit conversion from signaling NaNs to quiet NaNs that 2585 // may result in the loop not terminating. 2586 expectedBits = getIntVolatile(o, offset); 2587 v = Float.intBitsToFloat(expectedBits); 2588 } while (!weakCompareAndSetInt(o, offset, 2589 expectedBits, Float.floatToRawIntBits(v + delta))); 2590 return v; 2591 } 2592 2593 @ForceInline 2594 public final float getAndAddFloatRelease(Object o, long offset, float delta) { 2595 int expectedBits; 2596 float v; 2597 do { 2598 // Load and CAS with the raw bits to avoid issues with NaNs and 2599 // possible bit conversion from signaling NaNs to quiet NaNs that 2600 // may result in the loop not terminating. 2601 expectedBits = getInt(o, offset); 2602 v = Float.intBitsToFloat(expectedBits); 2603 } while (!weakCompareAndSetIntRelease(o, offset, 2604 expectedBits, Float.floatToRawIntBits(v + delta))); 2605 return v; 2606 } 2607 2608 @ForceInline 2609 public final float getAndAddFloatAcquire(Object o, long offset, float delta) { 2610 int expectedBits; 2611 float v; 2612 do { 2613 // Load and CAS with the raw bits to avoid issues with NaNs and 2614 // possible bit conversion from signaling NaNs to quiet NaNs that 2615 // may result in the loop not terminating. 2616 expectedBits = getIntAcquire(o, offset); 2617 v = Float.intBitsToFloat(expectedBits); 2618 } while (!weakCompareAndSetIntAcquire(o, offset, 2619 expectedBits, Float.floatToRawIntBits(v + delta))); 2620 return v; 2621 } 2622 2623 @ForceInline 2624 public final double getAndAddDouble(Object o, long offset, double delta) { 2625 long expectedBits; 2626 double v; 2627 do { 2628 // Load and CAS with the raw bits to avoid issues with NaNs and 2629 // possible bit conversion from signaling NaNs to quiet NaNs that 2630 // may result in the loop not terminating. 2631 expectedBits = getLongVolatile(o, offset); 2632 v = Double.longBitsToDouble(expectedBits); 2633 } while (!weakCompareAndSetLong(o, offset, 2634 expectedBits, Double.doubleToRawLongBits(v + delta))); 2635 return v; 2636 } 2637 2638 @ForceInline 2639 public final double getAndAddDoubleRelease(Object o, long offset, double delta) { 2640 long expectedBits; 2641 double v; 2642 do { 2643 // Load and CAS with the raw bits to avoid issues with NaNs and 2644 // possible bit conversion from signaling NaNs to quiet NaNs that 2645 // may result in the loop not terminating. 2646 expectedBits = getLong(o, offset); 2647 v = Double.longBitsToDouble(expectedBits); 2648 } while (!weakCompareAndSetLongRelease(o, offset, 2649 expectedBits, Double.doubleToRawLongBits(v + delta))); 2650 return v; 2651 } 2652 2653 @ForceInline 2654 public final double getAndAddDoubleAcquire(Object o, long offset, double delta) { 2655 long expectedBits; 2656 double v; 2657 do { 2658 // Load and CAS with the raw bits to avoid issues with NaNs and 2659 // possible bit conversion from signaling NaNs to quiet NaNs that 2660 // may result in the loop not terminating. 2661 expectedBits = getLongAcquire(o, offset); 2662 v = Double.longBitsToDouble(expectedBits); 2663 } while (!weakCompareAndSetLongAcquire(o, offset, 2664 expectedBits, Double.doubleToRawLongBits(v + delta))); 2665 return v; 2666 } 2667 2668 /** 2669 * Atomically exchanges the given value with the current value of 2670 * a field or array element within the given object {@code o} 2671 * at the given {@code offset}. 2672 * 2673 * @param o object/array to update the field/element in 2674 * @param offset field/element offset 2675 * @param newValue new value 2676 * @return the previous value 2677 * @since 1.8 2678 */ 2679 @IntrinsicCandidate 2680 public final int getAndSetInt(Object o, long offset, int newValue) { 2681 int v; 2682 do { 2683 v = getIntVolatile(o, offset); 2684 } while (!weakCompareAndSetInt(o, offset, v, newValue)); 2685 return v; 2686 } 2687 2688 @ForceInline 2689 public final int getAndSetIntRelease(Object o, long offset, int newValue) { 2690 int v; 2691 do { 2692 v = getInt(o, offset); 2693 } while (!weakCompareAndSetIntRelease(o, offset, v, newValue)); 2694 return v; 2695 } 2696 2697 @ForceInline 2698 public final int getAndSetIntAcquire(Object o, long offset, int newValue) { 2699 int v; 2700 do { 2701 v = getIntAcquire(o, offset); 2702 } while (!weakCompareAndSetIntAcquire(o, offset, v, newValue)); 2703 return v; 2704 } 2705 2706 /** 2707 * Atomically exchanges the given value with the current value of 2708 * a field or array element within the given object {@code o} 2709 * at the given {@code offset}. 2710 * 2711 * @param o object/array to update the field/element in 2712 * @param offset field/element offset 2713 * @param newValue new value 2714 * @return the previous value 2715 * @since 1.8 2716 */ 2717 @IntrinsicCandidate 2718 public final long getAndSetLong(Object o, long offset, long newValue) { 2719 long v; 2720 do { 2721 v = getLongVolatile(o, offset); 2722 } while (!weakCompareAndSetLong(o, offset, v, newValue)); 2723 return v; 2724 } 2725 2726 @ForceInline 2727 public final long getAndSetLongRelease(Object o, long offset, long newValue) { 2728 long v; 2729 do { 2730 v = getLong(o, offset); 2731 } while (!weakCompareAndSetLongRelease(o, offset, v, newValue)); 2732 return v; 2733 } 2734 2735 @ForceInline 2736 public final long getAndSetLongAcquire(Object o, long offset, long newValue) { 2737 long v; 2738 do { 2739 v = getLongAcquire(o, offset); 2740 } while (!weakCompareAndSetLongAcquire(o, offset, v, newValue)); 2741 return v; 2742 } 2743 2744 /** 2745 * Atomically exchanges the given reference value with the current 2746 * reference value of a field or array element within the given 2747 * object {@code o} at the given {@code offset}. 2748 * 2749 * @param o object/array to update the field/element in 2750 * @param offset field/element offset 2751 * @param newValue new value 2752 * @return the previous value 2753 * @since 1.8 2754 */ 2755 @IntrinsicCandidate 2756 public final Object getAndSetReference(Object o, long offset, Object newValue) { 2757 Object v; 2758 do { 2759 v = getReferenceVolatile(o, offset); 2760 } while (!weakCompareAndSetReference(o, offset, v, newValue)); 2761 return v; 2762 } 2763 2764 @ForceInline 2765 public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) { 2766 Object v; 2767 do { 2768 v = getReference(o, offset); 2769 } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue)); 2770 return v; 2771 } 2772 2773 @ForceInline 2774 public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) { 2775 Object v; 2776 do { 2777 v = getReferenceAcquire(o, offset); 2778 } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue)); 2779 return v; 2780 } 2781 2782 @IntrinsicCandidate 2783 public final byte getAndSetByte(Object o, long offset, byte newValue) { 2784 byte v; 2785 do { 2786 v = getByteVolatile(o, offset); 2787 } while (!weakCompareAndSetByte(o, offset, v, newValue)); 2788 return v; 2789 } 2790 2791 @ForceInline 2792 public final byte getAndSetByteRelease(Object o, long offset, byte newValue) { 2793 byte v; 2794 do { 2795 v = getByte(o, offset); 2796 } while (!weakCompareAndSetByteRelease(o, offset, v, newValue)); 2797 return v; 2798 } 2799 2800 @ForceInline 2801 public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) { 2802 byte v; 2803 do { 2804 v = getByteAcquire(o, offset); 2805 } while (!weakCompareAndSetByteAcquire(o, offset, v, newValue)); 2806 return v; 2807 } 2808 2809 @ForceInline 2810 public final boolean getAndSetBoolean(Object o, long offset, boolean newValue) { 2811 return byte2bool(getAndSetByte(o, offset, bool2byte(newValue))); 2812 } 2813 2814 @ForceInline 2815 public final boolean getAndSetBooleanRelease(Object o, long offset, boolean newValue) { 2816 return byte2bool(getAndSetByteRelease(o, offset, bool2byte(newValue))); 2817 } 2818 2819 @ForceInline 2820 public final boolean getAndSetBooleanAcquire(Object o, long offset, boolean newValue) { 2821 return byte2bool(getAndSetByteAcquire(o, offset, bool2byte(newValue))); 2822 } 2823 2824 @IntrinsicCandidate 2825 public final short getAndSetShort(Object o, long offset, short newValue) { 2826 short v; 2827 do { 2828 v = getShortVolatile(o, offset); 2829 } while (!weakCompareAndSetShort(o, offset, v, newValue)); 2830 return v; 2831 } 2832 2833 @ForceInline 2834 public final short getAndSetShortRelease(Object o, long offset, short newValue) { 2835 short v; 2836 do { 2837 v = getShort(o, offset); 2838 } while (!weakCompareAndSetShortRelease(o, offset, v, newValue)); 2839 return v; 2840 } 2841 2842 @ForceInline 2843 public final short getAndSetShortAcquire(Object o, long offset, short newValue) { 2844 short v; 2845 do { 2846 v = getShortAcquire(o, offset); 2847 } while (!weakCompareAndSetShortAcquire(o, offset, v, newValue)); 2848 return v; 2849 } 2850 2851 @ForceInline 2852 public final char getAndSetChar(Object o, long offset, char newValue) { 2853 return s2c(getAndSetShort(o, offset, c2s(newValue))); 2854 } 2855 2856 @ForceInline 2857 public final char getAndSetCharRelease(Object o, long offset, char newValue) { 2858 return s2c(getAndSetShortRelease(o, offset, c2s(newValue))); 2859 } 2860 2861 @ForceInline 2862 public final char getAndSetCharAcquire(Object o, long offset, char newValue) { 2863 return s2c(getAndSetShortAcquire(o, offset, c2s(newValue))); 2864 } 2865 2866 @ForceInline 2867 public final float getAndSetFloat(Object o, long offset, float newValue) { 2868 int v = getAndSetInt(o, offset, Float.floatToRawIntBits(newValue)); 2869 return Float.intBitsToFloat(v); 2870 } 2871 2872 @ForceInline 2873 public final float getAndSetFloatRelease(Object o, long offset, float newValue) { 2874 int v = getAndSetIntRelease(o, offset, Float.floatToRawIntBits(newValue)); 2875 return Float.intBitsToFloat(v); 2876 } 2877 2878 @ForceInline 2879 public final float getAndSetFloatAcquire(Object o, long offset, float newValue) { 2880 int v = getAndSetIntAcquire(o, offset, Float.floatToRawIntBits(newValue)); 2881 return Float.intBitsToFloat(v); 2882 } 2883 2884 @ForceInline 2885 public final double getAndSetDouble(Object o, long offset, double newValue) { 2886 long v = getAndSetLong(o, offset, Double.doubleToRawLongBits(newValue)); 2887 return Double.longBitsToDouble(v); 2888 } 2889 2890 @ForceInline 2891 public final double getAndSetDoubleRelease(Object o, long offset, double newValue) { 2892 long v = getAndSetLongRelease(o, offset, Double.doubleToRawLongBits(newValue)); 2893 return Double.longBitsToDouble(v); 2894 } 2895 2896 @ForceInline 2897 public final double getAndSetDoubleAcquire(Object o, long offset, double newValue) { 2898 long v = getAndSetLongAcquire(o, offset, Double.doubleToRawLongBits(newValue)); 2899 return Double.longBitsToDouble(v); 2900 } 2901 2902 2903 // The following contain CAS-based Java implementations used on 2904 // platforms not supporting native instructions 2905 2906 @ForceInline 2907 public final boolean getAndBitwiseOrBoolean(Object o, long offset, boolean mask) { 2908 return byte2bool(getAndBitwiseOrByte(o, offset, bool2byte(mask))); 2909 } 2910 2911 @ForceInline 2912 public final boolean getAndBitwiseOrBooleanRelease(Object o, long offset, boolean mask) { 2913 return byte2bool(getAndBitwiseOrByteRelease(o, offset, bool2byte(mask))); 2914 } 2915 2916 @ForceInline 2917 public final boolean getAndBitwiseOrBooleanAcquire(Object o, long offset, boolean mask) { 2918 return byte2bool(getAndBitwiseOrByteAcquire(o, offset, bool2byte(mask))); 2919 } 2920 2921 @ForceInline 2922 public final boolean getAndBitwiseAndBoolean(Object o, long offset, boolean mask) { 2923 return byte2bool(getAndBitwiseAndByte(o, offset, bool2byte(mask))); 2924 } 2925 2926 @ForceInline 2927 public final boolean getAndBitwiseAndBooleanRelease(Object o, long offset, boolean mask) { 2928 return byte2bool(getAndBitwiseAndByteRelease(o, offset, bool2byte(mask))); 2929 } 2930 2931 @ForceInline 2932 public final boolean getAndBitwiseAndBooleanAcquire(Object o, long offset, boolean mask) { 2933 return byte2bool(getAndBitwiseAndByteAcquire(o, offset, bool2byte(mask))); 2934 } 2935 2936 @ForceInline 2937 public final boolean getAndBitwiseXorBoolean(Object o, long offset, boolean mask) { 2938 return byte2bool(getAndBitwiseXorByte(o, offset, bool2byte(mask))); 2939 } 2940 2941 @ForceInline 2942 public final boolean getAndBitwiseXorBooleanRelease(Object o, long offset, boolean mask) { 2943 return byte2bool(getAndBitwiseXorByteRelease(o, offset, bool2byte(mask))); 2944 } 2945 2946 @ForceInline 2947 public final boolean getAndBitwiseXorBooleanAcquire(Object o, long offset, boolean mask) { 2948 return byte2bool(getAndBitwiseXorByteAcquire(o, offset, bool2byte(mask))); 2949 } 2950 2951 2952 @ForceInline 2953 public final byte getAndBitwiseOrByte(Object o, long offset, byte mask) { 2954 byte current; 2955 do { 2956 current = getByteVolatile(o, offset); 2957 } while (!weakCompareAndSetByte(o, offset, 2958 current, (byte) (current | mask))); 2959 return current; 2960 } 2961 2962 @ForceInline 2963 public final byte getAndBitwiseOrByteRelease(Object o, long offset, byte mask) { 2964 byte current; 2965 do { 2966 current = getByte(o, offset); 2967 } while (!weakCompareAndSetByteRelease(o, offset, 2968 current, (byte) (current | mask))); 2969 return current; 2970 } 2971 2972 @ForceInline 2973 public final byte getAndBitwiseOrByteAcquire(Object o, long offset, byte mask) { 2974 byte current; 2975 do { 2976 // Plain read, the value is a hint, the acquire CAS does the work 2977 current = getByte(o, offset); 2978 } while (!weakCompareAndSetByteAcquire(o, offset, 2979 current, (byte) (current | mask))); 2980 return current; 2981 } 2982 2983 @ForceInline 2984 public final byte getAndBitwiseAndByte(Object o, long offset, byte mask) { 2985 byte current; 2986 do { 2987 current = getByteVolatile(o, offset); 2988 } while (!weakCompareAndSetByte(o, offset, 2989 current, (byte) (current & mask))); 2990 return current; 2991 } 2992 2993 @ForceInline 2994 public final byte getAndBitwiseAndByteRelease(Object o, long offset, byte mask) { 2995 byte current; 2996 do { 2997 current = getByte(o, offset); 2998 } while (!weakCompareAndSetByteRelease(o, offset, 2999 current, (byte) (current & mask))); 3000 return current; 3001 } 3002 3003 @ForceInline 3004 public final byte getAndBitwiseAndByteAcquire(Object o, long offset, byte mask) { 3005 byte current; 3006 do { 3007 // Plain read, the value is a hint, the acquire CAS does the work 3008 current = getByte(o, offset); 3009 } while (!weakCompareAndSetByteAcquire(o, offset, 3010 current, (byte) (current & mask))); 3011 return current; 3012 } 3013 3014 @ForceInline 3015 public final byte getAndBitwiseXorByte(Object o, long offset, byte mask) { 3016 byte current; 3017 do { 3018 current = getByteVolatile(o, offset); 3019 } while (!weakCompareAndSetByte(o, offset, 3020 current, (byte) (current ^ mask))); 3021 return current; 3022 } 3023 3024 @ForceInline 3025 public final byte getAndBitwiseXorByteRelease(Object o, long offset, byte mask) { 3026 byte current; 3027 do { 3028 current = getByte(o, offset); 3029 } while (!weakCompareAndSetByteRelease(o, offset, 3030 current, (byte) (current ^ mask))); 3031 return current; 3032 } 3033 3034 @ForceInline 3035 public final byte getAndBitwiseXorByteAcquire(Object o, long offset, byte mask) { 3036 byte current; 3037 do { 3038 // Plain read, the value is a hint, the acquire CAS does the work 3039 current = getByte(o, offset); 3040 } while (!weakCompareAndSetByteAcquire(o, offset, 3041 current, (byte) (current ^ mask))); 3042 return current; 3043 } 3044 3045 3046 @ForceInline 3047 public final char getAndBitwiseOrChar(Object o, long offset, char mask) { 3048 return s2c(getAndBitwiseOrShort(o, offset, c2s(mask))); 3049 } 3050 3051 @ForceInline 3052 public final char getAndBitwiseOrCharRelease(Object o, long offset, char mask) { 3053 return s2c(getAndBitwiseOrShortRelease(o, offset, c2s(mask))); 3054 } 3055 3056 @ForceInline 3057 public final char getAndBitwiseOrCharAcquire(Object o, long offset, char mask) { 3058 return s2c(getAndBitwiseOrShortAcquire(o, offset, c2s(mask))); 3059 } 3060 3061 @ForceInline 3062 public final char getAndBitwiseAndChar(Object o, long offset, char mask) { 3063 return s2c(getAndBitwiseAndShort(o, offset, c2s(mask))); 3064 } 3065 3066 @ForceInline 3067 public final char getAndBitwiseAndCharRelease(Object o, long offset, char mask) { 3068 return s2c(getAndBitwiseAndShortRelease(o, offset, c2s(mask))); 3069 } 3070 3071 @ForceInline 3072 public final char getAndBitwiseAndCharAcquire(Object o, long offset, char mask) { 3073 return s2c(getAndBitwiseAndShortAcquire(o, offset, c2s(mask))); 3074 } 3075 3076 @ForceInline 3077 public final char getAndBitwiseXorChar(Object o, long offset, char mask) { 3078 return s2c(getAndBitwiseXorShort(o, offset, c2s(mask))); 3079 } 3080 3081 @ForceInline 3082 public final char getAndBitwiseXorCharRelease(Object o, long offset, char mask) { 3083 return s2c(getAndBitwiseXorShortRelease(o, offset, c2s(mask))); 3084 } 3085 3086 @ForceInline 3087 public final char getAndBitwiseXorCharAcquire(Object o, long offset, char mask) { 3088 return s2c(getAndBitwiseXorShortAcquire(o, offset, c2s(mask))); 3089 } 3090 3091 3092 @ForceInline 3093 public final short getAndBitwiseOrShort(Object o, long offset, short mask) { 3094 short current; 3095 do { 3096 current = getShortVolatile(o, offset); 3097 } while (!weakCompareAndSetShort(o, offset, 3098 current, (short) (current | mask))); 3099 return current; 3100 } 3101 3102 @ForceInline 3103 public final short getAndBitwiseOrShortRelease(Object o, long offset, short mask) { 3104 short current; 3105 do { 3106 current = getShort(o, offset); 3107 } while (!weakCompareAndSetShortRelease(o, offset, 3108 current, (short) (current | mask))); 3109 return current; 3110 } 3111 3112 @ForceInline 3113 public final short getAndBitwiseOrShortAcquire(Object o, long offset, short mask) { 3114 short current; 3115 do { 3116 // Plain read, the value is a hint, the acquire CAS does the work 3117 current = getShort(o, offset); 3118 } while (!weakCompareAndSetShortAcquire(o, offset, 3119 current, (short) (current | mask))); 3120 return current; 3121 } 3122 3123 @ForceInline 3124 public final short getAndBitwiseAndShort(Object o, long offset, short mask) { 3125 short current; 3126 do { 3127 current = getShortVolatile(o, offset); 3128 } while (!weakCompareAndSetShort(o, offset, 3129 current, (short) (current & mask))); 3130 return current; 3131 } 3132 3133 @ForceInline 3134 public final short getAndBitwiseAndShortRelease(Object o, long offset, short mask) { 3135 short current; 3136 do { 3137 current = getShort(o, offset); 3138 } while (!weakCompareAndSetShortRelease(o, offset, 3139 current, (short) (current & mask))); 3140 return current; 3141 } 3142 3143 @ForceInline 3144 public final short getAndBitwiseAndShortAcquire(Object o, long offset, short mask) { 3145 short current; 3146 do { 3147 // Plain read, the value is a hint, the acquire CAS does the work 3148 current = getShort(o, offset); 3149 } while (!weakCompareAndSetShortAcquire(o, offset, 3150 current, (short) (current & mask))); 3151 return current; 3152 } 3153 3154 @ForceInline 3155 public final short getAndBitwiseXorShort(Object o, long offset, short mask) { 3156 short current; 3157 do { 3158 current = getShortVolatile(o, offset); 3159 } while (!weakCompareAndSetShort(o, offset, 3160 current, (short) (current ^ mask))); 3161 return current; 3162 } 3163 3164 @ForceInline 3165 public final short getAndBitwiseXorShortRelease(Object o, long offset, short mask) { 3166 short current; 3167 do { 3168 current = getShort(o, offset); 3169 } while (!weakCompareAndSetShortRelease(o, offset, 3170 current, (short) (current ^ mask))); 3171 return current; 3172 } 3173 3174 @ForceInline 3175 public final short getAndBitwiseXorShortAcquire(Object o, long offset, short mask) { 3176 short current; 3177 do { 3178 // Plain read, the value is a hint, the acquire CAS does the work 3179 current = getShort(o, offset); 3180 } while (!weakCompareAndSetShortAcquire(o, offset, 3181 current, (short) (current ^ mask))); 3182 return current; 3183 } 3184 3185 3186 @ForceInline 3187 public final int getAndBitwiseOrInt(Object o, long offset, int mask) { 3188 int current; 3189 do { 3190 current = getIntVolatile(o, offset); 3191 } while (!weakCompareAndSetInt(o, offset, 3192 current, current | mask)); 3193 return current; 3194 } 3195 3196 @ForceInline 3197 public final int getAndBitwiseOrIntRelease(Object o, long offset, int mask) { 3198 int current; 3199 do { 3200 current = getInt(o, offset); 3201 } while (!weakCompareAndSetIntRelease(o, offset, 3202 current, current | mask)); 3203 return current; 3204 } 3205 3206 @ForceInline 3207 public final int getAndBitwiseOrIntAcquire(Object o, long offset, int mask) { 3208 int current; 3209 do { 3210 // Plain read, the value is a hint, the acquire CAS does the work 3211 current = getInt(o, offset); 3212 } while (!weakCompareAndSetIntAcquire(o, offset, 3213 current, current | mask)); 3214 return current; 3215 } 3216 3217 /** 3218 * Atomically replaces the current value of a field or array element within 3219 * the given object with the result of bitwise AND between the current value 3220 * and mask. 3221 * 3222 * @param o object/array to update the field/element in 3223 * @param offset field/element offset 3224 * @param mask the mask value 3225 * @return the previous value 3226 * @since 9 3227 */ 3228 @ForceInline 3229 public final int getAndBitwiseAndInt(Object o, long offset, int mask) { 3230 int current; 3231 do { 3232 current = getIntVolatile(o, offset); 3233 } while (!weakCompareAndSetInt(o, offset, 3234 current, current & mask)); 3235 return current; 3236 } 3237 3238 @ForceInline 3239 public final int getAndBitwiseAndIntRelease(Object o, long offset, int mask) { 3240 int current; 3241 do { 3242 current = getInt(o, offset); 3243 } while (!weakCompareAndSetIntRelease(o, offset, 3244 current, current & mask)); 3245 return current; 3246 } 3247 3248 @ForceInline 3249 public final int getAndBitwiseAndIntAcquire(Object o, long offset, int mask) { 3250 int current; 3251 do { 3252 // Plain read, the value is a hint, the acquire CAS does the work 3253 current = getInt(o, offset); 3254 } while (!weakCompareAndSetIntAcquire(o, offset, 3255 current, current & mask)); 3256 return current; 3257 } 3258 3259 @ForceInline 3260 public final int getAndBitwiseXorInt(Object o, long offset, int mask) { 3261 int current; 3262 do { 3263 current = getIntVolatile(o, offset); 3264 } while (!weakCompareAndSetInt(o, offset, 3265 current, current ^ mask)); 3266 return current; 3267 } 3268 3269 @ForceInline 3270 public final int getAndBitwiseXorIntRelease(Object o, long offset, int mask) { 3271 int current; 3272 do { 3273 current = getInt(o, offset); 3274 } while (!weakCompareAndSetIntRelease(o, offset, 3275 current, current ^ mask)); 3276 return current; 3277 } 3278 3279 @ForceInline 3280 public final int getAndBitwiseXorIntAcquire(Object o, long offset, int mask) { 3281 int current; 3282 do { 3283 // Plain read, the value is a hint, the acquire CAS does the work 3284 current = getInt(o, offset); 3285 } while (!weakCompareAndSetIntAcquire(o, offset, 3286 current, current ^ mask)); 3287 return current; 3288 } 3289 3290 3291 @ForceInline 3292 public final long getAndBitwiseOrLong(Object o, long offset, long mask) { 3293 long current; 3294 do { 3295 current = getLongVolatile(o, offset); 3296 } while (!weakCompareAndSetLong(o, offset, 3297 current, current | mask)); 3298 return current; 3299 } 3300 3301 @ForceInline 3302 public final long getAndBitwiseOrLongRelease(Object o, long offset, long mask) { 3303 long current; 3304 do { 3305 current = getLong(o, offset); 3306 } while (!weakCompareAndSetLongRelease(o, offset, 3307 current, current | mask)); 3308 return current; 3309 } 3310 3311 @ForceInline 3312 public final long getAndBitwiseOrLongAcquire(Object o, long offset, long mask) { 3313 long current; 3314 do { 3315 // Plain read, the value is a hint, the acquire CAS does the work 3316 current = getLong(o, offset); 3317 } while (!weakCompareAndSetLongAcquire(o, offset, 3318 current, current | mask)); 3319 return current; 3320 } 3321 3322 @ForceInline 3323 public final long getAndBitwiseAndLong(Object o, long offset, long mask) { 3324 long current; 3325 do { 3326 current = getLongVolatile(o, offset); 3327 } while (!weakCompareAndSetLong(o, offset, 3328 current, current & mask)); 3329 return current; 3330 } 3331 3332 @ForceInline 3333 public final long getAndBitwiseAndLongRelease(Object o, long offset, long mask) { 3334 long current; 3335 do { 3336 current = getLong(o, offset); 3337 } while (!weakCompareAndSetLongRelease(o, offset, 3338 current, current & mask)); 3339 return current; 3340 } 3341 3342 @ForceInline 3343 public final long getAndBitwiseAndLongAcquire(Object o, long offset, long mask) { 3344 long current; 3345 do { 3346 // Plain read, the value is a hint, the acquire CAS does the work 3347 current = getLong(o, offset); 3348 } while (!weakCompareAndSetLongAcquire(o, offset, 3349 current, current & mask)); 3350 return current; 3351 } 3352 3353 @ForceInline 3354 public final long getAndBitwiseXorLong(Object o, long offset, long mask) { 3355 long current; 3356 do { 3357 current = getLongVolatile(o, offset); 3358 } while (!weakCompareAndSetLong(o, offset, 3359 current, current ^ mask)); 3360 return current; 3361 } 3362 3363 @ForceInline 3364 public final long getAndBitwiseXorLongRelease(Object o, long offset, long mask) { 3365 long current; 3366 do { 3367 current = getLong(o, offset); 3368 } while (!weakCompareAndSetLongRelease(o, offset, 3369 current, current ^ mask)); 3370 return current; 3371 } 3372 3373 @ForceInline 3374 public final long getAndBitwiseXorLongAcquire(Object o, long offset, long mask) { 3375 long current; 3376 do { 3377 // Plain read, the value is a hint, the acquire CAS does the work 3378 current = getLong(o, offset); 3379 } while (!weakCompareAndSetLongAcquire(o, offset, 3380 current, current ^ mask)); 3381 return current; 3382 } 3383 3384 3385 3386 /** 3387 * Ensures that loads before the fence will not be reordered with loads and 3388 * stores after the fence; a "LoadLoad plus LoadStore barrier". 3389 * 3390 * Corresponds to C11 atomic_thread_fence(memory_order_acquire) 3391 * (an "acquire fence"). 3392 * 3393 * Provides a LoadLoad barrier followed by a LoadStore barrier. 3394 * 3395 * @since 1.8 3396 */ 3397 @IntrinsicCandidate 3398 public final void loadFence() { 3399 // If loadFence intrinsic is not available, fall back to full fence. 3400 fullFence(); 3401 } 3402 3403 /** 3404 * Ensures that loads and stores before the fence will not be reordered with 3405 * stores after the fence; a "StoreStore plus LoadStore barrier". 3406 * 3407 * Corresponds to C11 atomic_thread_fence(memory_order_release) 3408 * (a "release fence"). 3409 * 3410 * Provides a StoreStore barrier followed by a LoadStore barrier. 3411 * 3412 * @since 1.8 3413 */ 3414 @IntrinsicCandidate 3415 public final void storeFence() { 3416 // If storeFence intrinsic is not available, fall back to full fence. 3417 fullFence(); 3418 } 3419 3420 /** 3421 * Ensures that loads and stores before the fence will not be reordered 3422 * with loads and stores after the fence. Implies the effects of both 3423 * loadFence() and storeFence(), and in addition, the effect of a StoreLoad 3424 * barrier. 3425 * 3426 * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst). 3427 * @since 1.8 3428 */ 3429 @IntrinsicCandidate 3430 public native void fullFence(); 3431 3432 /** 3433 * Ensures that loads before the fence will not be reordered with 3434 * loads after the fence. 3435 * 3436 * @implNote 3437 * This method is operationally equivalent to {@link #loadFence()}. 3438 * 3439 * @since 9 3440 */ 3441 public final void loadLoadFence() { 3442 loadFence(); 3443 } 3444 3445 /** 3446 * Ensures that stores before the fence will not be reordered with 3447 * stores after the fence. 3448 * 3449 * @since 9 3450 */ 3451 @IntrinsicCandidate 3452 public final void storeStoreFence() { 3453 // If storeStoreFence intrinsic is not available, fall back to storeFence. 3454 storeFence(); 3455 } 3456 3457 /** 3458 * Throws IllegalAccessError; for use by the VM for access control 3459 * error support. 3460 * @since 1.8 3461 */ 3462 private static void throwIllegalAccessError() { 3463 throw new IllegalAccessError(); 3464 } 3465 3466 /** 3467 * Throws NoSuchMethodError; for use by the VM for redefinition support. 3468 * @since 13 3469 */ 3470 private static void throwNoSuchMethodError() { 3471 throw new NoSuchMethodError(); 3472 } 3473 3474 /** 3475 * @return Returns true if the native byte ordering of this 3476 * platform is big-endian, false if it is little-endian. 3477 */ 3478 public final boolean isBigEndian() { return BIG_ENDIAN; } 3479 3480 /** 3481 * @return Returns true if this platform is capable of performing 3482 * accesses at addresses which are not aligned for the type of the 3483 * primitive type being accessed, false otherwise. 3484 */ 3485 public final boolean unalignedAccess() { return UNALIGNED_ACCESS; } 3486 3487 /** 3488 * Fetches a value at some byte offset into a given Java object. 3489 * More specifically, fetches a value within the given object 3490 * <code>o</code> at the given offset, or (if <code>o</code> is 3491 * null) from the memory address whose numerical value is the 3492 * given offset. <p> 3493 * 3494 * The specification of this method is the same as {@link 3495 * #getLong(Object, long)} except that the offset does not need to 3496 * have been obtained from {@link #objectFieldOffset} on the 3497 * {@link java.lang.reflect.Field} of some Java field. The value 3498 * in memory is raw data, and need not correspond to any Java 3499 * variable. Unless <code>o</code> is null, the value accessed 3500 * must be entirely within the allocated object. The endianness 3501 * of the value in memory is the endianness of the native platform. 3502 * 3503 * <p> The read will be atomic with respect to the largest power 3504 * of two that divides the GCD of the offset and the storage size. 3505 * For example, getLongUnaligned will make atomic reads of 2-, 4-, 3506 * or 8-byte storage units if the offset is zero mod 2, 4, or 8, 3507 * respectively. There are no other guarantees of atomicity. 3508 * <p> 3509 * 8-byte atomicity is only guaranteed on platforms on which 3510 * support atomic accesses to longs. 3511 * 3512 * @param o Java heap object in which the value resides, if any, else 3513 * null 3514 * @param offset The offset in bytes from the start of the object 3515 * @return the value fetched from the indicated object 3516 * @throws RuntimeException No defined exceptions are thrown, not even 3517 * {@link NullPointerException} 3518 * @since 9 3519 */ 3520 @IntrinsicCandidate 3521 public final long getLongUnaligned(Object o, long offset) { 3522 if ((offset & 7) == 0) { 3523 return getLong(o, offset); 3524 } else if ((offset & 3) == 0) { 3525 return makeLong(getInt(o, offset), 3526 getInt(o, offset + 4)); 3527 } else if ((offset & 1) == 0) { 3528 return makeLong(getShort(o, offset), 3529 getShort(o, offset + 2), 3530 getShort(o, offset + 4), 3531 getShort(o, offset + 6)); 3532 } else { 3533 return makeLong(getByte(o, offset), 3534 getByte(o, offset + 1), 3535 getByte(o, offset + 2), 3536 getByte(o, offset + 3), 3537 getByte(o, offset + 4), 3538 getByte(o, offset + 5), 3539 getByte(o, offset + 6), 3540 getByte(o, offset + 7)); 3541 } 3542 } 3543 /** 3544 * As {@link #getLongUnaligned(Object, long)} but with an 3545 * additional argument which specifies the endianness of the value 3546 * as stored in memory. 3547 * 3548 * @param o Java heap object in which the variable resides 3549 * @param offset The offset in bytes from the start of the object 3550 * @param bigEndian The endianness of the value 3551 * @return the value fetched from the indicated object 3552 * @since 9 3553 */ 3554 public final long getLongUnaligned(Object o, long offset, boolean bigEndian) { 3555 return convEndian(bigEndian, getLongUnaligned(o, offset)); 3556 } 3557 3558 /** @see #getLongUnaligned(Object, long) */ 3559 @IntrinsicCandidate 3560 public final int getIntUnaligned(Object o, long offset) { 3561 if ((offset & 3) == 0) { 3562 return getInt(o, offset); 3563 } else if ((offset & 1) == 0) { 3564 return makeInt(getShort(o, offset), 3565 getShort(o, offset + 2)); 3566 } else { 3567 return makeInt(getByte(o, offset), 3568 getByte(o, offset + 1), 3569 getByte(o, offset + 2), 3570 getByte(o, offset + 3)); 3571 } 3572 } 3573 /** @see #getLongUnaligned(Object, long, boolean) */ 3574 public final int getIntUnaligned(Object o, long offset, boolean bigEndian) { 3575 return convEndian(bigEndian, getIntUnaligned(o, offset)); 3576 } 3577 3578 /** @see #getLongUnaligned(Object, long) */ 3579 @IntrinsicCandidate 3580 public final short getShortUnaligned(Object o, long offset) { 3581 if ((offset & 1) == 0) { 3582 return getShort(o, offset); 3583 } else { 3584 return makeShort(getByte(o, offset), 3585 getByte(o, offset + 1)); 3586 } 3587 } 3588 /** @see #getLongUnaligned(Object, long, boolean) */ 3589 public final short getShortUnaligned(Object o, long offset, boolean bigEndian) { 3590 return convEndian(bigEndian, getShortUnaligned(o, offset)); 3591 } 3592 3593 /** @see #getLongUnaligned(Object, long) */ 3594 @IntrinsicCandidate 3595 public final char getCharUnaligned(Object o, long offset) { 3596 if ((offset & 1) == 0) { 3597 return getChar(o, offset); 3598 } else { 3599 return (char)makeShort(getByte(o, offset), 3600 getByte(o, offset + 1)); 3601 } 3602 } 3603 3604 /** @see #getLongUnaligned(Object, long, boolean) */ 3605 public final char getCharUnaligned(Object o, long offset, boolean bigEndian) { 3606 return convEndian(bigEndian, getCharUnaligned(o, offset)); 3607 } 3608 3609 /** 3610 * Stores a value at some byte offset into a given Java object. 3611 * <p> 3612 * The specification of this method is the same as {@link 3613 * #getLong(Object, long)} except that the offset does not need to 3614 * have been obtained from {@link #objectFieldOffset} on the 3615 * {@link java.lang.reflect.Field} of some Java field. The value 3616 * in memory is raw data, and need not correspond to any Java 3617 * variable. The endianness of the value in memory is the 3618 * endianness of the native platform. 3619 * <p> 3620 * The write will be atomic with respect to the largest power of 3621 * two that divides the GCD of the offset and the storage size. 3622 * For example, putLongUnaligned will make atomic writes of 2-, 4-, 3623 * or 8-byte storage units if the offset is zero mod 2, 4, or 8, 3624 * respectively. There are no other guarantees of atomicity. 3625 * <p> 3626 * 8-byte atomicity is only guaranteed on platforms on which 3627 * support atomic accesses to longs. 3628 * 3629 * @param o Java heap object in which the value resides, if any, else 3630 * null 3631 * @param offset The offset in bytes from the start of the object 3632 * @param x the value to store 3633 * @throws RuntimeException No defined exceptions are thrown, not even 3634 * {@link NullPointerException} 3635 * @since 9 3636 */ 3637 @IntrinsicCandidate 3638 public final void putLongUnaligned(Object o, long offset, long x) { 3639 if ((offset & 7) == 0) { 3640 putLong(o, offset, x); 3641 } else if ((offset & 3) == 0) { 3642 putLongParts(o, offset, 3643 (int)(x >> 0), 3644 (int)(x >>> 32)); 3645 } else if ((offset & 1) == 0) { 3646 putLongParts(o, offset, 3647 (short)(x >>> 0), 3648 (short)(x >>> 16), 3649 (short)(x >>> 32), 3650 (short)(x >>> 48)); 3651 } else { 3652 putLongParts(o, offset, 3653 (byte)(x >>> 0), 3654 (byte)(x >>> 8), 3655 (byte)(x >>> 16), 3656 (byte)(x >>> 24), 3657 (byte)(x >>> 32), 3658 (byte)(x >>> 40), 3659 (byte)(x >>> 48), 3660 (byte)(x >>> 56)); 3661 } 3662 } 3663 3664 /** 3665 * As {@link #putLongUnaligned(Object, long, long)} but with an additional 3666 * argument which specifies the endianness of the value as stored in memory. 3667 * @param o Java heap object in which the value resides 3668 * @param offset The offset in bytes from the start of the object 3669 * @param x the value to store 3670 * @param bigEndian The endianness of the value 3671 * @throws RuntimeException No defined exceptions are thrown, not even 3672 * {@link NullPointerException} 3673 * @since 9 3674 */ 3675 public final void putLongUnaligned(Object o, long offset, long x, boolean bigEndian) { 3676 putLongUnaligned(o, offset, convEndian(bigEndian, x)); 3677 } 3678 3679 /** @see #putLongUnaligned(Object, long, long) */ 3680 @IntrinsicCandidate 3681 public final void putIntUnaligned(Object o, long offset, int x) { 3682 if ((offset & 3) == 0) { 3683 putInt(o, offset, x); 3684 } else if ((offset & 1) == 0) { 3685 putIntParts(o, offset, 3686 (short)(x >> 0), 3687 (short)(x >>> 16)); 3688 } else { 3689 putIntParts(o, offset, 3690 (byte)(x >>> 0), 3691 (byte)(x >>> 8), 3692 (byte)(x >>> 16), 3693 (byte)(x >>> 24)); 3694 } 3695 } 3696 /** @see #putLongUnaligned(Object, long, long, boolean) */ 3697 public final void putIntUnaligned(Object o, long offset, int x, boolean bigEndian) { 3698 putIntUnaligned(o, offset, convEndian(bigEndian, x)); 3699 } 3700 3701 /** @see #putLongUnaligned(Object, long, long) */ 3702 @IntrinsicCandidate 3703 public final void putShortUnaligned(Object o, long offset, short x) { 3704 if ((offset & 1) == 0) { 3705 putShort(o, offset, x); 3706 } else { 3707 putShortParts(o, offset, 3708 (byte)(x >>> 0), 3709 (byte)(x >>> 8)); 3710 } 3711 } 3712 /** @see #putLongUnaligned(Object, long, long, boolean) */ 3713 public final void putShortUnaligned(Object o, long offset, short x, boolean bigEndian) { 3714 putShortUnaligned(o, offset, convEndian(bigEndian, x)); 3715 } 3716 3717 /** @see #putLongUnaligned(Object, long, long) */ 3718 @IntrinsicCandidate 3719 public final void putCharUnaligned(Object o, long offset, char x) { 3720 putShortUnaligned(o, offset, (short)x); 3721 } 3722 /** @see #putLongUnaligned(Object, long, long, boolean) */ 3723 public final void putCharUnaligned(Object o, long offset, char x, boolean bigEndian) { 3724 putCharUnaligned(o, offset, convEndian(bigEndian, x)); 3725 } 3726 3727 private static int pickPos(int top, int pos) { return BIG_ENDIAN ? top - pos : pos; } 3728 3729 // These methods construct integers from bytes. The byte ordering 3730 // is the native endianness of this platform. 3731 private static long makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) { 3732 return ((toUnsignedLong(i0) << pickPos(56, 0)) 3733 | (toUnsignedLong(i1) << pickPos(56, 8)) 3734 | (toUnsignedLong(i2) << pickPos(56, 16)) 3735 | (toUnsignedLong(i3) << pickPos(56, 24)) 3736 | (toUnsignedLong(i4) << pickPos(56, 32)) 3737 | (toUnsignedLong(i5) << pickPos(56, 40)) 3738 | (toUnsignedLong(i6) << pickPos(56, 48)) 3739 | (toUnsignedLong(i7) << pickPos(56, 56))); 3740 } 3741 private static long makeLong(short i0, short i1, short i2, short i3) { 3742 return ((toUnsignedLong(i0) << pickPos(48, 0)) 3743 | (toUnsignedLong(i1) << pickPos(48, 16)) 3744 | (toUnsignedLong(i2) << pickPos(48, 32)) 3745 | (toUnsignedLong(i3) << pickPos(48, 48))); 3746 } 3747 private static long makeLong(int i0, int i1) { 3748 return (toUnsignedLong(i0) << pickPos(32, 0)) 3749 | (toUnsignedLong(i1) << pickPos(32, 32)); 3750 } 3751 private static int makeInt(short i0, short i1) { 3752 return (toUnsignedInt(i0) << pickPos(16, 0)) 3753 | (toUnsignedInt(i1) << pickPos(16, 16)); 3754 } 3755 private static int makeInt(byte i0, byte i1, byte i2, byte i3) { 3756 return ((toUnsignedInt(i0) << pickPos(24, 0)) 3757 | (toUnsignedInt(i1) << pickPos(24, 8)) 3758 | (toUnsignedInt(i2) << pickPos(24, 16)) 3759 | (toUnsignedInt(i3) << pickPos(24, 24))); 3760 } 3761 private static short makeShort(byte i0, byte i1) { 3762 return (short)((toUnsignedInt(i0) << pickPos(8, 0)) 3763 | (toUnsignedInt(i1) << pickPos(8, 8))); 3764 } 3765 3766 private static byte pick(byte le, byte be) { return BIG_ENDIAN ? be : le; } 3767 private static short pick(short le, short be) { return BIG_ENDIAN ? be : le; } 3768 private static int pick(int le, int be) { return BIG_ENDIAN ? be : le; } 3769 3770 // These methods write integers to memory from smaller parts 3771 // provided by their caller. The ordering in which these parts 3772 // are written is the native endianness of this platform. 3773 private void putLongParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) { 3774 putByte(o, offset + 0, pick(i0, i7)); 3775 putByte(o, offset + 1, pick(i1, i6)); 3776 putByte(o, offset + 2, pick(i2, i5)); 3777 putByte(o, offset + 3, pick(i3, i4)); 3778 putByte(o, offset + 4, pick(i4, i3)); 3779 putByte(o, offset + 5, pick(i5, i2)); 3780 putByte(o, offset + 6, pick(i6, i1)); 3781 putByte(o, offset + 7, pick(i7, i0)); 3782 } 3783 private void putLongParts(Object o, long offset, short i0, short i1, short i2, short i3) { 3784 putShort(o, offset + 0, pick(i0, i3)); 3785 putShort(o, offset + 2, pick(i1, i2)); 3786 putShort(o, offset + 4, pick(i2, i1)); 3787 putShort(o, offset + 6, pick(i3, i0)); 3788 } 3789 private void putLongParts(Object o, long offset, int i0, int i1) { 3790 putInt(o, offset + 0, pick(i0, i1)); 3791 putInt(o, offset + 4, pick(i1, i0)); 3792 } 3793 private void putIntParts(Object o, long offset, short i0, short i1) { 3794 putShort(o, offset + 0, pick(i0, i1)); 3795 putShort(o, offset + 2, pick(i1, i0)); 3796 } 3797 private void putIntParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3) { 3798 putByte(o, offset + 0, pick(i0, i3)); 3799 putByte(o, offset + 1, pick(i1, i2)); 3800 putByte(o, offset + 2, pick(i2, i1)); 3801 putByte(o, offset + 3, pick(i3, i0)); 3802 } 3803 private void putShortParts(Object o, long offset, byte i0, byte i1) { 3804 putByte(o, offset + 0, pick(i0, i1)); 3805 putByte(o, offset + 1, pick(i1, i0)); 3806 } 3807 3808 // Zero-extend an integer 3809 private static int toUnsignedInt(byte n) { return n & 0xff; } 3810 private static int toUnsignedInt(short n) { return n & 0xffff; } 3811 private static long toUnsignedLong(byte n) { return n & 0xffl; } 3812 private static long toUnsignedLong(short n) { return n & 0xffffl; } 3813 private static long toUnsignedLong(int n) { return n & 0xffffffffl; } 3814 3815 // Maybe byte-reverse an integer 3816 private static char convEndian(boolean big, char n) { return big == BIG_ENDIAN ? n : Character.reverseBytes(n); } 3817 private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n) ; } 3818 private static int convEndian(boolean big, int n) { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n) ; } 3819 private static long convEndian(boolean big, long n) { return big == BIG_ENDIAN ? n : Long.reverseBytes(n) ; } 3820 3821 3822 3823 private native long allocateMemory0(long bytes); 3824 private native long reallocateMemory0(long address, long bytes); 3825 private native void freeMemory0(long address); 3826 private native void setMemory0(Object o, long offset, long bytes, byte value); 3827 @IntrinsicCandidate 3828 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes); 3829 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize); 3830 private native long objectFieldOffset0(Field f); 3831 private native long objectFieldOffset1(Class<?> c, String name); 3832 private native long staticFieldOffset0(Field f); 3833 private native Object staticFieldBase0(Field f); 3834 private native boolean shouldBeInitialized0(Class<?> c); 3835 private native void ensureClassInitialized0(Class<?> c); 3836 private native int arrayBaseOffset0(Class<?> arrayClass); 3837 private native int arrayIndexScale0(Class<?> arrayClass); 3838 private native int getLoadAverage0(double[] loadavg, int nelems); 3839 3840 3841 /** 3842 * Invokes the given direct byte buffer's cleaner, if any. 3843 * 3844 * @param directBuffer a direct byte buffer 3845 * @throws NullPointerException if {@code directBuffer} is null 3846 * @throws IllegalArgumentException if {@code directBuffer} is non-direct, 3847 * or is a {@link java.nio.Buffer#slice slice}, or is a 3848 * {@link java.nio.Buffer#duplicate duplicate} 3849 */ 3850 public void invokeCleaner(java.nio.ByteBuffer directBuffer) { 3851 if (!directBuffer.isDirect()) 3852 throw new IllegalArgumentException("buffer is non-direct"); 3853 3854 DirectBuffer db = (DirectBuffer) directBuffer; 3855 if (db.attachment() != null) 3856 throw new IllegalArgumentException("duplicate or slice"); 3857 3858 Cleaner cleaner = db.cleaner(); 3859 if (cleaner != null) { 3860 cleaner.clean(); 3861 } 3862 } 3863 3864 // The following deprecated methods are used by JSR 166. 3865 3866 @Deprecated(since="12", forRemoval=true) 3867 public final Object getObject(Object o, long offset) { 3868 return getReference(o, offset); 3869 } 3870 @Deprecated(since="12", forRemoval=true) 3871 public final Object getObjectVolatile(Object o, long offset) { 3872 return getReferenceVolatile(o, offset); 3873 } 3874 @Deprecated(since="12", forRemoval=true) 3875 public final Object getObjectAcquire(Object o, long offset) { 3876 return getReferenceAcquire(o, offset); 3877 } 3878 @Deprecated(since="12", forRemoval=true) 3879 public final Object getObjectOpaque(Object o, long offset) { 3880 return getReferenceOpaque(o, offset); 3881 } 3882 3883 3884 @Deprecated(since="12", forRemoval=true) 3885 public final void putObject(Object o, long offset, Object x) { 3886 putReference(o, offset, x); 3887 } 3888 @Deprecated(since="12", forRemoval=true) 3889 public final void putObjectVolatile(Object o, long offset, Object x) { 3890 putReferenceVolatile(o, offset, x); 3891 } 3892 @Deprecated(since="12", forRemoval=true) 3893 public final void putObjectOpaque(Object o, long offset, Object x) { 3894 putReferenceOpaque(o, offset, x); 3895 } 3896 @Deprecated(since="12", forRemoval=true) 3897 public final void putObjectRelease(Object o, long offset, Object x) { 3898 putReferenceRelease(o, offset, x); 3899 } 3900 3901 3902 @Deprecated(since="12", forRemoval=true) 3903 public final Object getAndSetObject(Object o, long offset, Object newValue) { 3904 return getAndSetReference(o, offset, newValue); 3905 } 3906 @Deprecated(since="12", forRemoval=true) 3907 public final Object getAndSetObjectAcquire(Object o, long offset, Object newValue) { 3908 return getAndSetReferenceAcquire(o, offset, newValue); 3909 } 3910 @Deprecated(since="12", forRemoval=true) 3911 public final Object getAndSetObjectRelease(Object o, long offset, Object newValue) { 3912 return getAndSetReferenceRelease(o, offset, newValue); 3913 } 3914 3915 3916 @Deprecated(since="12", forRemoval=true) 3917 public final boolean compareAndSetObject(Object o, long offset, Object expected, Object x) { 3918 return compareAndSetReference(o, offset, expected, x); 3919 } 3920 @Deprecated(since="12", forRemoval=true) 3921 public final Object compareAndExchangeObject(Object o, long offset, Object expected, Object x) { 3922 return compareAndExchangeReference(o, offset, expected, x); 3923 } 3924 @Deprecated(since="12", forRemoval=true) 3925 public final Object compareAndExchangeObjectAcquire(Object o, long offset, Object expected, Object x) { 3926 return compareAndExchangeReferenceAcquire(o, offset, expected, x); 3927 } 3928 @Deprecated(since="12", forRemoval=true) 3929 public final Object compareAndExchangeObjectRelease(Object o, long offset, Object expected, Object x) { 3930 return compareAndExchangeReferenceRelease(o, offset, expected, x); 3931 } 3932 3933 3934 @Deprecated(since="12", forRemoval=true) 3935 public final boolean weakCompareAndSetObject(Object o, long offset, Object expected, Object x) { 3936 return weakCompareAndSetReference(o, offset, expected, x); 3937 } 3938 @Deprecated(since="12", forRemoval=true) 3939 public final boolean weakCompareAndSetObjectAcquire(Object o, long offset, Object expected, Object x) { 3940 return weakCompareAndSetReferenceAcquire(o, offset, expected, x); 3941 } 3942 @Deprecated(since="12", forRemoval=true) 3943 public final boolean weakCompareAndSetObjectPlain(Object o, long offset, Object expected, Object x) { 3944 return weakCompareAndSetReferencePlain(o, offset, expected, x); 3945 } 3946 @Deprecated(since="12", forRemoval=true) 3947 public final boolean weakCompareAndSetObjectRelease(Object o, long offset, Object expected, Object x) { 3948 return weakCompareAndSetReferenceRelease(o, offset, expected, x); 3949 } 3950 }