/jdk11/src/libcore/ojluni/src/main/java/sun/misc/Unsafe.java

https://github.com/google/desugar_jdk_libs · Java · 835 lines · 199 code · 79 blank · 557 comment · 15 complexity · d6ab21325fe94c371c6b081921f04ba6 MD5 · raw file

  1. /*
  2. * Copyright (c) 2000, 2013, 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. package sun.misc;
  26. import dalvik.annotation.optimization.FastNative;
  27. import sun.reflect.Reflection;
  28. import java.lang.reflect.Field;
  29. import java.lang.reflect.Modifier;
  30. /**
  31. * A collection of methods for performing low-level, unsafe operations.
  32. * Although the class and all methods are public, use of this class is
  33. * limited because only trusted code can obtain instances of it.
  34. *
  35. * @author John R. Rose
  36. * @see #getUnsafe
  37. */
  38. public final class Unsafe {
  39. /** Traditional dalvik name. */
  40. private static final Unsafe THE_ONE = new Unsafe();
  41. private static final Unsafe theUnsafe = THE_ONE;
  42. public static final int INVALID_FIELD_OFFSET = -1;
  43. /**
  44. * This class is only privately instantiable.
  45. */
  46. private Unsafe() {}
  47. /**
  48. * Gets the unique instance of this class. This is only allowed in
  49. * very limited situations.
  50. */
  51. public static Unsafe getUnsafe() {
  52. Class<?> caller = Reflection.getCallerClass();
  53. /*
  54. * Only code on the bootclasspath is allowed to get at the
  55. * Unsafe instance.
  56. */
  57. ClassLoader calling = (caller == null) ? null : caller.getClassLoader();
  58. if ((calling != null) && (calling != Unsafe.class.getClassLoader())) {
  59. throw new SecurityException("Unsafe access denied");
  60. }
  61. return THE_ONE;
  62. }
  63. /**
  64. * Gets the raw byte offset from the start of an object's memory to
  65. * the memory used to store the indicated instance field.
  66. *
  67. * @param field non-{@code null}; the field in question, which must be an
  68. * instance field
  69. * @return the offset to the field
  70. */
  71. public long objectFieldOffset(Field field) {
  72. if (Modifier.isStatic(field.getModifiers())) {
  73. throw new IllegalArgumentException("valid for instance fields only");
  74. }
  75. return field.getOffset();
  76. }
  77. /**
  78. * Gets the offset from the start of an array object's memory to
  79. * the memory used to store its initial (zeroeth) element.
  80. *
  81. * @param clazz non-{@code null}; class in question; must be an array class
  82. * @return the offset to the initial element
  83. */
  84. public int arrayBaseOffset(Class clazz) {
  85. Class<?> component = clazz.getComponentType();
  86. if (component == null) {
  87. throw new IllegalArgumentException("Valid for array classes only: " + clazz);
  88. }
  89. return getArrayBaseOffsetForComponentType(component);
  90. }
  91. /**
  92. * Gets the size of each element of the given array class.
  93. *
  94. * @param clazz non-{@code null}; class in question; must be an array class
  95. * @return &gt; 0; the size of each element of the array
  96. */
  97. public int arrayIndexScale(Class clazz) {
  98. Class<?> component = clazz.getComponentType();
  99. if (component == null) {
  100. throw new IllegalArgumentException("Valid for array classes only: " + clazz);
  101. }
  102. return getArrayIndexScaleForComponentType(component);
  103. }
  104. @FastNative
  105. private static native int getArrayBaseOffsetForComponentType(Class component_class);
  106. @FastNative
  107. private static native int getArrayIndexScaleForComponentType(Class component_class);
  108. /**
  109. * Performs a compare-and-set operation on an {@code int}
  110. * field within the given object.
  111. *
  112. * @param obj non-{@code null}; object containing the field
  113. * @param offset offset to the field within {@code obj}
  114. * @param expectedValue expected value of the field
  115. * @param newValue new value to store in the field if the contents are
  116. * as expected
  117. * @return {@code true} if the new value was in fact stored, and
  118. * {@code false} if not
  119. */
  120. @FastNative
  121. public native boolean compareAndSwapInt(Object obj, long offset,
  122. int expectedValue, int newValue);
  123. /**
  124. * Performs a compare-and-set operation on a {@code long}
  125. * field within the given object.
  126. *
  127. * @param obj non-{@code null}; object containing the field
  128. * @param offset offset to the field within {@code obj}
  129. * @param expectedValue expected value of the field
  130. * @param newValue new value to store in the field if the contents are
  131. * as expected
  132. * @return {@code true} if the new value was in fact stored, and
  133. * {@code false} if not
  134. */
  135. @FastNative
  136. public native boolean compareAndSwapLong(Object obj, long offset,
  137. long expectedValue, long newValue);
  138. /**
  139. * Performs a compare-and-set operation on an {@code obj}
  140. * field (that is, a reference field) within the given object.
  141. *
  142. * @param obj non-{@code null}; object containing the field
  143. * @param offset offset to the field within {@code obj}
  144. * @param expectedValue expected value of the field
  145. * @param newValue new value to store in the field if the contents are
  146. * as expected
  147. * @return {@code true} if the new value was in fact stored, and
  148. * {@code false} if not
  149. */
  150. @FastNative
  151. public native boolean compareAndSwapObject(Object obj, long offset,
  152. Object expectedValue, Object newValue);
  153. /**
  154. * Gets an {@code int} field from the given object,
  155. * using {@code volatile} semantics.
  156. *
  157. * @param obj non-{@code null}; object containing the field
  158. * @param offset offset to the field within {@code obj}
  159. * @return the retrieved value
  160. */
  161. @FastNative
  162. public native int getIntVolatile(Object obj, long offset);
  163. /**
  164. * Stores an {@code int} field into the given object,
  165. * using {@code volatile} semantics.
  166. *
  167. * @param obj non-{@code null}; object containing the field
  168. * @param offset offset to the field within {@code obj}
  169. * @param newValue the value to store
  170. */
  171. @FastNative
  172. public native void putIntVolatile(Object obj, long offset, int newValue);
  173. /**
  174. * Gets a {@code long} field from the given object,
  175. * using {@code volatile} semantics.
  176. *
  177. * @param obj non-{@code null}; object containing the field
  178. * @param offset offset to the field within {@code obj}
  179. * @return the retrieved value
  180. */
  181. @FastNative
  182. public native long getLongVolatile(Object obj, long offset);
  183. /**
  184. * Stores a {@code long} field into the given object,
  185. * using {@code volatile} semantics.
  186. *
  187. * @param obj non-{@code null}; object containing the field
  188. * @param offset offset to the field within {@code obj}
  189. * @param newValue the value to store
  190. */
  191. @FastNative
  192. public native void putLongVolatile(Object obj, long offset, long newValue);
  193. /**
  194. * Gets an {@code obj} field from the given object,
  195. * using {@code volatile} semantics.
  196. *
  197. * @param obj non-{@code null}; object containing the field
  198. * @param offset offset to the field within {@code obj}
  199. * @return the retrieved value
  200. */
  201. @FastNative
  202. public native Object getObjectVolatile(Object obj, long offset);
  203. /**
  204. * Stores an {@code obj} field into the given object,
  205. * using {@code volatile} semantics.
  206. *
  207. * @param obj non-{@code null}; object containing the field
  208. * @param offset offset to the field within {@code obj}
  209. * @param newValue the value to store
  210. */
  211. @FastNative
  212. public native void putObjectVolatile(Object obj, long offset,
  213. Object newValue);
  214. /**
  215. * Gets an {@code int} field from the given object.
  216. *
  217. * @param obj non-{@code null}; object containing int field
  218. * @param offset offset to the field within {@code obj}
  219. * @return the retrieved value
  220. */
  221. @FastNative
  222. public native int getInt(Object obj, long offset);
  223. /**
  224. * Stores an {@code int} field into the given object.
  225. *
  226. * @param obj non-{@code null}; object containing int field
  227. * @param offset offset to the field within {@code obj}
  228. * @param newValue the value to store
  229. */
  230. @FastNative
  231. public native void putInt(Object obj, long offset, int newValue);
  232. /**
  233. * Lazy set an int field.
  234. *
  235. * @param obj non-{@code null}; object containing the field
  236. * @param offset offset to the field within {@code obj}
  237. * @param newValue the value to store
  238. */
  239. @FastNative
  240. public native void putOrderedInt(Object obj, long offset, int newValue);
  241. /**
  242. * Gets a {@code long} field from the given object.
  243. *
  244. * @param obj non-{@code null}; object containing the field
  245. * @param offset offset to the field within {@code obj}
  246. * @return the retrieved value
  247. */
  248. @FastNative
  249. public native long getLong(Object obj, long offset);
  250. /**
  251. * Stores a {@code long} field into the given object.
  252. *
  253. * @param obj non-{@code null}; object containing the field
  254. * @param offset offset to the field within {@code obj}
  255. * @param newValue the value to store
  256. */
  257. @FastNative
  258. public native void putLong(Object obj, long offset, long newValue);
  259. /**
  260. * Lazy set a long field.
  261. *
  262. * @param obj non-{@code null}; object containing the field
  263. * @param offset offset to the field within {@code obj}
  264. * @param newValue the value to store
  265. */
  266. @FastNative
  267. public native void putOrderedLong(Object obj, long offset, long newValue);
  268. /**
  269. * Gets an {@code obj} field from the given object.
  270. *
  271. * @param obj non-{@code null}; object containing the field
  272. * @param offset offset to the field within {@code obj}
  273. * @return the retrieved value
  274. */
  275. @FastNative
  276. public native Object getObject(Object obj, long offset);
  277. /**
  278. * Stores an {@code obj} field into the given object.
  279. *
  280. * @param obj non-{@code null}; object containing the field
  281. * @param offset offset to the field within {@code obj}
  282. * @param newValue the value to store
  283. */
  284. @FastNative
  285. public native void putObject(Object obj, long offset, Object newValue);
  286. /**
  287. * Lazy set an object field.
  288. *
  289. * @param obj non-{@code null}; object containing the field
  290. * @param offset offset to the field within {@code obj}
  291. * @param newValue the value to store
  292. */
  293. @FastNative
  294. public native void putOrderedObject(Object obj, long offset,
  295. Object newValue);
  296. /**
  297. * Gets a {@code boolean} field from the given object.
  298. *
  299. * @param obj non-{@code null}; object containing boolean field
  300. * @param offset offset to the field within {@code obj}
  301. * @return the retrieved value
  302. */
  303. @FastNative
  304. public native boolean getBoolean(Object obj, long offset);
  305. /**
  306. * Stores a {@code boolean} field into the given object.
  307. *
  308. * @param obj non-{@code null}; object containing boolean field
  309. * @param offset offset to the field within {@code obj}
  310. * @param newValue the value to store
  311. */
  312. @FastNative
  313. public native void putBoolean(Object obj, long offset, boolean newValue);
  314. /**
  315. * Gets a {@code byte} field from the given object.
  316. *
  317. * @param obj non-{@code null}; object containing byte field
  318. * @param offset offset to the field within {@code obj}
  319. * @return the retrieved value
  320. */
  321. @FastNative
  322. public native byte getByte(Object obj, long offset);
  323. /**
  324. * Stores a {@code byte} field into the given object.
  325. *
  326. * @param obj non-{@code null}; object containing byte field
  327. * @param offset offset to the field within {@code obj}
  328. * @param newValue the value to store
  329. */
  330. @FastNative
  331. public native void putByte(Object obj, long offset, byte newValue);
  332. /**
  333. * Gets a {@code char} field from the given object.
  334. *
  335. * @param obj non-{@code null}; object containing char field
  336. * @param offset offset to the field within {@code obj}
  337. * @return the retrieved value
  338. */
  339. @FastNative
  340. public native char getChar(Object obj, long offset);
  341. /**
  342. * Stores a {@code char} field into the given object.
  343. *
  344. * @param obj non-{@code null}; object containing char field
  345. * @param offset offset to the field within {@code obj}
  346. * @param newValue the value to store
  347. */
  348. @FastNative
  349. public native void putChar(Object obj, long offset, char newValue);
  350. /**
  351. * Gets a {@code short} field from the given object.
  352. *
  353. * @param obj non-{@code null}; object containing short field
  354. * @param offset offset to the field within {@code obj}
  355. * @return the retrieved value
  356. */
  357. @FastNative
  358. public native short getShort(Object obj, long offset);
  359. /**
  360. * Stores a {@code short} field into the given object.
  361. *
  362. * @param obj non-{@code null}; object containing short field
  363. * @param offset offset to the field within {@code obj}
  364. * @param newValue the value to store
  365. */
  366. @FastNative
  367. public native void putShort(Object obj, long offset, short newValue);
  368. /**
  369. * Gets a {@code float} field from the given object.
  370. *
  371. * @param obj non-{@code null}; object containing float field
  372. * @param offset offset to the field within {@code obj}
  373. * @return the retrieved value
  374. */
  375. @FastNative
  376. public native float getFloat(Object obj, long offset);
  377. /**
  378. * Stores a {@code float} field into the given object.
  379. *
  380. * @param obj non-{@code null}; object containing float field
  381. * @param offset offset to the field within {@code obj}
  382. * @param newValue the value to store
  383. */
  384. @FastNative
  385. public native void putFloat(Object obj, long offset, float newValue);
  386. /**
  387. * Gets a {@code double} field from the given object.
  388. *
  389. * @param obj non-{@code null}; object containing double field
  390. * @param offset offset to the field within {@code obj}
  391. * @return the retrieved value
  392. */
  393. @FastNative
  394. public native double getDouble(Object obj, long offset);
  395. /**
  396. * Stores a {@code double} field into the given object.
  397. *
  398. * @param obj non-{@code null}; object containing double field
  399. * @param offset offset to the field within {@code obj}
  400. * @param newValue the value to store
  401. */
  402. @FastNative
  403. public native void putDouble(Object obj, long offset, double newValue);
  404. /**
  405. * Parks the calling thread for the specified amount of time,
  406. * unless the "permit" for the thread is already available (due to
  407. * a previous call to {@link #unpark}. This method may also return
  408. * spuriously (that is, without the thread being told to unpark
  409. * and without the indicated amount of time elapsing).
  410. *
  411. * <p>See {@link java.util.concurrent.locks.LockSupport} for more
  412. * in-depth information of the behavior of this method.</p>
  413. *
  414. * @param absolute whether the given time value is absolute
  415. * milliseconds-since-the-epoch ({@code true}) or relative
  416. * nanoseconds-from-now ({@code false})
  417. * @param time the (absolute millis or relative nanos) time value
  418. */
  419. public native void park(boolean absolute, long time);
  420. /**
  421. * Unparks the given object, which must be a {@link Thread}.
  422. *
  423. * <p>See {@link java.util.concurrent.locks.LockSupport} for more
  424. * in-depth information of the behavior of this method.</p>
  425. *
  426. * @param obj non-{@code null}; the object to unpark
  427. */
  428. @FastNative
  429. public native void unpark(Object obj);
  430. /**
  431. * Allocates an instance of the given class without running the constructor.
  432. * The class' <clinit> will be run, if necessary.
  433. */
  434. public native Object allocateInstance(Class<?> c);
  435. /**
  436. * Gets the size of the address value, in bytes.
  437. *
  438. * @return the size of the address, in bytes
  439. */
  440. @FastNative
  441. public native int addressSize();
  442. /**
  443. * Gets the size of the memory page, in bytes.
  444. *
  445. * @return the size of the page
  446. */
  447. @FastNative
  448. public native int pageSize();
  449. /**
  450. * Allocates a memory block of size {@code bytes}.
  451. *
  452. * @param bytes size of the memory block
  453. * @return address of the allocated memory
  454. */
  455. @FastNative
  456. public native long allocateMemory(long bytes);
  457. /**
  458. * Frees previously allocated memory at given address.
  459. *
  460. * @param address address of the freed memory
  461. */
  462. @FastNative
  463. public native void freeMemory(long address);
  464. /**
  465. * Fills given memory block with a given value.
  466. *
  467. * @param address address of the memoory block
  468. * @param bytes length of the memory block, in bytes
  469. * @param value fills memory with this value
  470. */
  471. @FastNative
  472. public native void setMemory(long address, long bytes, byte value);
  473. /**
  474. * Gets {@code byte} from given address in memory.
  475. *
  476. * @param address address in memory
  477. * @return {@code byte} value
  478. */
  479. @FastNative
  480. public native byte getByte(long address);
  481. /**
  482. * Stores a {@code byte} into the given memory address.
  483. *
  484. * @param address address in memory where to store the value
  485. * @param newValue the value to store
  486. */
  487. @FastNative
  488. public native void putByte(long address, byte x);
  489. /**
  490. * Gets {@code short} from given address in memory.
  491. *
  492. * @param address address in memory
  493. * @return {@code short} value
  494. */
  495. @FastNative
  496. public native short getShort(long address);
  497. /**
  498. * Stores a {@code short} into the given memory address.
  499. *
  500. * @param address address in memory where to store the value
  501. * @param newValue the value to store
  502. */
  503. @FastNative
  504. public native void putShort(long address, short x);
  505. /**
  506. * Gets {@code char} from given address in memory.
  507. *
  508. * @param address address in memory
  509. * @return {@code char} value
  510. */
  511. @FastNative
  512. public native char getChar(long address);
  513. /**
  514. * Stores a {@code char} into the given memory address.
  515. *
  516. * @param address address in memory where to store the value
  517. * @param newValue the value to store
  518. */
  519. @FastNative
  520. public native void putChar(long address, char x);
  521. /**
  522. * Gets {@code int} from given address in memory.
  523. *
  524. * @param address address in memory
  525. * @return {@code int} value
  526. */
  527. @FastNative
  528. public native int getInt(long address);
  529. /**
  530. * Stores a {@code int} into the given memory address.
  531. *
  532. * @param address address in memory where to store the value
  533. * @param newValue the value to store
  534. */
  535. @FastNative
  536. public native void putInt(long address, int x);
  537. /**
  538. * Gets {@code long} from given address in memory.
  539. *
  540. * @param address address in memory
  541. * @return {@code long} value
  542. */
  543. @FastNative
  544. public native long getLong(long address);
  545. /**
  546. * Stores a {@code long} into the given memory address.
  547. *
  548. * @param address address in memory where to store the value
  549. * @param newValue the value to store
  550. */
  551. @FastNative
  552. public native void putLong(long address, long x);
  553. /**
  554. * Gets {@code long} from given address in memory.
  555. *
  556. * @param address address in memory
  557. * @return {@code long} value
  558. */
  559. @FastNative
  560. public native float getFloat(long address);
  561. /**
  562. * Stores a {@code float} into the given memory address.
  563. *
  564. * @param address address in memory where to store the value
  565. * @param newValue the value to store
  566. */
  567. @FastNative
  568. public native void putFloat(long address, float x);
  569. /**
  570. * Gets {@code double} from given address in memory.
  571. *
  572. * @param address address in memory
  573. * @return {@code double} value
  574. */
  575. @FastNative
  576. public native double getDouble(long address);
  577. /**
  578. * Stores a {@code double} into the given memory address.
  579. *
  580. * @param address address in memory where to store the value
  581. * @param newValue the value to store
  582. */
  583. @FastNative
  584. public native void putDouble(long address, double x);
  585. /**
  586. * Copies given memory block to a primitive array.
  587. *
  588. * @param srcAddr address to copy memory from
  589. * @param dst address to copy memory to
  590. * @param dstOffset offset in {@code dst}
  591. * @param bytes number of bytes to copy
  592. */
  593. @FastNative
  594. public native void copyMemoryToPrimitiveArray(long srcAddr,
  595. Object dst, long dstOffset, long bytes);
  596. /**
  597. * Treat given primitive array as a continuous memory block and
  598. * copy it to given memory address.
  599. *
  600. * @param src primitive array to copy data from
  601. * @param srcOffset offset in {@code src} to copy from
  602. * @param dstAddr memory address to copy data to
  603. * @param bytes number of bytes to copy
  604. */
  605. @FastNative
  606. public native void copyMemoryFromPrimitiveArray(Object src, long srcOffset,
  607. long dstAddr, long bytes);
  608. /**
  609. * Sets all bytes in a given block of memory to a copy of another block.
  610. *
  611. * @param srcAddr address of the source memory to be copied from
  612. * @param dstAddr address of the destination memory to copy to
  613. * @param bytes number of bytes to copy
  614. */
  615. @FastNative
  616. public native void copyMemory(long srcAddr, long dstAddr, long bytes);
  617. // The following contain CAS-based Java implementations used on
  618. // platforms not supporting native instructions
  619. /**
  620. * Atomically adds the given value to the current value of a field
  621. * or array element within the given object {@code o}
  622. * at the given {@code offset}.
  623. *
  624. * @param o object/array to update the field/element in
  625. * @param offset field/element offset
  626. * @param delta the value to add
  627. * @return the previous value
  628. * @since 1.8
  629. */
  630. // @HotSpotIntrinsicCandidate
  631. public final int getAndAddInt(Object o, long offset, int delta) {
  632. int v;
  633. do {
  634. v = getIntVolatile(o, offset);
  635. } while (!compareAndSwapInt(o, offset, v, v + delta));
  636. return v;
  637. }
  638. /**
  639. * Atomically adds the given value to the current value of a field
  640. * or array element within the given object {@code o}
  641. * at the given {@code offset}.
  642. *
  643. * @param o object/array to update the field/element in
  644. * @param offset field/element offset
  645. * @param delta the value to add
  646. * @return the previous value
  647. * @since 1.8
  648. */
  649. // @HotSpotIntrinsicCandidate
  650. public final long getAndAddLong(Object o, long offset, long delta) {
  651. long v;
  652. do {
  653. v = getLongVolatile(o, offset);
  654. } while (!compareAndSwapLong(o, offset, v, v + delta));
  655. return v;
  656. }
  657. /**
  658. * Atomically exchanges the given value with the current value of
  659. * a field or array element within the given object {@code o}
  660. * at the given {@code offset}.
  661. *
  662. * @param o object/array to update the field/element in
  663. * @param offset field/element offset
  664. * @param newValue new value
  665. * @return the previous value
  666. * @since 1.8
  667. */
  668. // @HotSpotIntrinsicCandidate
  669. public final int getAndSetInt(Object o, long offset, int newValue) {
  670. int v;
  671. do {
  672. v = getIntVolatile(o, offset);
  673. } while (!compareAndSwapInt(o, offset, v, newValue));
  674. return v;
  675. }
  676. /**
  677. * Atomically exchanges the given value with the current value of
  678. * a field or array element within the given object {@code o}
  679. * at the given {@code offset}.
  680. *
  681. * @param o object/array to update the field/element in
  682. * @param offset field/element offset
  683. * @param newValue new value
  684. * @return the previous value
  685. * @since 1.8
  686. */
  687. // @HotSpotIntrinsicCandidate
  688. public final long getAndSetLong(Object o, long offset, long newValue) {
  689. long v;
  690. do {
  691. v = getLongVolatile(o, offset);
  692. } while (!compareAndSwapLong(o, offset, v, newValue));
  693. return v;
  694. }
  695. /**
  696. * Atomically exchanges the given reference value with the current
  697. * reference value of a field or array element within the given
  698. * object {@code o} at the given {@code offset}.
  699. *
  700. * @param o object/array to update the field/element in
  701. * @param offset field/element offset
  702. * @param newValue new value
  703. * @return the previous value
  704. * @since 1.8
  705. */
  706. // @HotSpotIntrinsicCandidate
  707. public final Object getAndSetObject(Object o, long offset, Object newValue) {
  708. Object v;
  709. do {
  710. v = getObjectVolatile(o, offset);
  711. } while (!compareAndSwapObject(o, offset, v, newValue));
  712. return v;
  713. }
  714. /**
  715. * Ensures that loads before the fence will not be reordered with loads and
  716. * stores after the fence; a "LoadLoad plus LoadStore barrier".
  717. *
  718. * Corresponds to C11 atomic_thread_fence(memory_order_acquire)
  719. * (an "acquire fence").
  720. *
  721. * A pure LoadLoad fence is not provided, since the addition of LoadStore
  722. * is almost always desired, and most current hardware instructions that
  723. * provide a LoadLoad barrier also provide a LoadStore barrier for free.
  724. * @since 1.8
  725. */
  726. // @HotSpotIntrinsicCandidate
  727. @FastNative
  728. public native void loadFence();
  729. /**
  730. * Ensures that loads and stores before the fence will not be reordered with
  731. * stores after the fence; a "StoreStore plus LoadStore barrier".
  732. *
  733. * Corresponds to C11 atomic_thread_fence(memory_order_release)
  734. * (a "release fence").
  735. *
  736. * A pure StoreStore fence is not provided, since the addition of LoadStore
  737. * is almost always desired, and most current hardware instructions that
  738. * provide a StoreStore barrier also provide a LoadStore barrier for free.
  739. * @since 1.8
  740. */
  741. // @HotSpotIntrinsicCandidate
  742. @FastNative
  743. public native void storeFence();
  744. /**
  745. * Ensures that loads and stores before the fence will not be reordered
  746. * with loads and stores after the fence. Implies the effects of both
  747. * loadFence() and storeFence(), and in addition, the effect of a StoreLoad
  748. * barrier.
  749. *
  750. * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst).
  751. * @since 1.8
  752. */
  753. // @HotSpotIntrinsicCandidate
  754. @FastNative
  755. public native void fullFence();
  756. }