src/java.base/share/classes/jdk/internal/misc/Unsafe.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8161720 Sdiff src/java.base/share/classes/jdk/internal/misc

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

Print this page




1583     public final boolean weakCompareAndSwapCharAcquire(Object o, long offset,
1584                                             char expected,
1585                                             char x) {
1586         return weakCompareAndSwapShortAcquire(o, offset, c2s(expected), c2s(x));
1587     }
1588 
1589     @ForceInline
1590     public final boolean weakCompareAndSwapCharRelease(Object o, long offset,
1591                                             char expected,
1592                                             char x) {
1593         return weakCompareAndSwapShortRelease(o, offset, c2s(expected), c2s(x));
1594     }
1595 
1596     @ForceInline
1597     public final boolean weakCompareAndSwapChar(Object o, long offset,
1598                                             char expected,
1599                                             char x) {
1600         return weakCompareAndSwapShort(o, offset, c2s(expected), c2s(x));
1601     }
1602 






























1603     @ForceInline
1604     private boolean byte2bool(byte b) {
1605         return b > 0;
1606     }
1607 









1608     @ForceInline
1609     private byte bool2byte(boolean b) {
1610         return b ? (byte)1 : (byte)0;
1611     }
1612 
1613     @ForceInline
1614     public final boolean compareAndSwapBoolean(Object o, long offset,
1615                                                boolean expected,
1616                                                boolean x) {
1617         return compareAndSwapByte(o, offset, bool2byte(expected), bool2byte(x));
1618     }
1619 
1620     @ForceInline
1621     public final boolean compareAndExchangeBooleanVolatile(Object o, long offset,
1622                                                         boolean expected,
1623                                                         boolean x) {
1624         return byte2bool(compareAndExchangeByteVolatile(o, offset, bool2byte(expected), bool2byte(x)));
1625     }
1626 
1627     @ForceInline




1583     public final boolean weakCompareAndSwapCharAcquire(Object o, long offset,
1584                                             char expected,
1585                                             char x) {
1586         return weakCompareAndSwapShortAcquire(o, offset, c2s(expected), c2s(x));
1587     }
1588 
1589     @ForceInline
1590     public final boolean weakCompareAndSwapCharRelease(Object o, long offset,
1591                                             char expected,
1592                                             char x) {
1593         return weakCompareAndSwapShortRelease(o, offset, c2s(expected), c2s(x));
1594     }
1595 
1596     @ForceInline
1597     public final boolean weakCompareAndSwapChar(Object o, long offset,
1598                                             char expected,
1599                                             char x) {
1600         return weakCompareAndSwapShort(o, offset, c2s(expected), c2s(x));
1601     }
1602 
1603     /**
1604      * The JVM converts ints to booleans using two different
1605      * conventions, byte testing against zero and truncation to
1606      * least-significant bit.
1607      * 
1608      * <p>The JNI documents specify that, at least for returning
1609      * values from native methods, a Java boolean value is converted
1610      * to the value-set 0..1 by first truncating to a byte (0..255 or
1611      * maybe -128..127) and then testing against zero. Thus, Java
1612      * booleans in non-Java data structures are by convention
1613      * represented as 8-bit containers containing either zero (for
1614      * false) or any non-zero value (for true).
1615      *
1616      * <p>Java booleans in the heap are also stored in bytes, but are
1617      * strongly normalized to the value-set 0..1 (i.e., they are
1618      * truncated to the least-significant bit).
1619      *
1620      * <p>The main reason for having different conventions for
1621      * conversion is performance: Truncation to the least-significant
1622      * bit can be usually implemented with fewer (machine)
1623      * instructions than byte testing against zero.
1624      *
1625      * <p>A number of Unsafe methods load boolean values from the heap
1626      * as bytes. Unsafe converts those values according to the JNI
1627      * rules (i.e, using the "testing against zero" convention). The
1628      * method {@code byte2bool} implements that conversion.
1629      *
1630      * @param b the byte to be converted to boolean
1631      * @return the result of the conversion
1632      */
1633     @ForceInline
1634     private boolean byte2bool(byte b) {
1635         return b != 0;
1636     }
1637 
1638     /**
1639      * Convert a boolean value to a byte. The return value is strongly
1640      * normalized to the value-set 0..1 (i.e., the value is truncated
1641      * to the least-significant bit). See {@link #byte2bool(byte)} for
1642      * more details on conversion conventions.
1643      *
1644      * @param b the boolean to be converted to byte (and then normalized)
1645      * @return the result of the conversion
1646      */
1647     @ForceInline
1648     private byte bool2byte(boolean b) {
1649         return b ? (byte)1 : (byte)0;
1650     }
1651 
1652     @ForceInline
1653     public final boolean compareAndSwapBoolean(Object o, long offset,
1654                                                boolean expected,
1655                                                boolean x) {
1656         return compareAndSwapByte(o, offset, bool2byte(expected), bool2byte(x));
1657     }
1658 
1659     @ForceInline
1660     public final boolean compareAndExchangeBooleanVolatile(Object o, long offset,
1661                                                         boolean expected,
1662                                                         boolean x) {
1663         return byte2bool(compareAndExchangeByteVolatile(o, offset, bool2byte(expected), bool2byte(x)));
1664     }
1665 
1666     @ForceInline


src/java.base/share/classes/jdk/internal/misc/Unsafe.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File