1. /*
  2. * @(#)AtomicBoolean.java 1.7 04/07/12
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.concurrent.atomic;
  8. import sun.misc.Unsafe;
  9. /**
  10. * A <tt>boolean</tt> value that may be updated atomically. See the
  11. * {@link java.util.concurrent.atomic} package specification for
  12. * description of the properties of atomic variables. An
  13. * <tt>AtomicBoolean</tt> is used in applications such as atomically
  14. * updated flags, and cannot be used as a replacement for a
  15. * {@link java.lang.Boolean}.
  16. *
  17. * @since 1.5
  18. * @author Doug Lea
  19. */
  20. public class AtomicBoolean implements java.io.Serializable {
  21. private static final long serialVersionUID = 4654671469794556979L;
  22. // setup to use Unsafe.compareAndSwapInt for updates
  23. private static final Unsafe unsafe = Unsafe.getUnsafe();
  24. private static final long valueOffset;
  25. static {
  26. try {
  27. valueOffset = unsafe.objectFieldOffset
  28. (AtomicBoolean.class.getDeclaredField("value"));
  29. } catch (Exception ex) { throw new Error(ex); }
  30. }
  31. private volatile int value;
  32. /**
  33. * Creates a new <tt>AtomicBoolean</tt> with the given initial value.
  34. *
  35. * @param initialValue the initial value
  36. */
  37. public AtomicBoolean(boolean initialValue) {
  38. value = initialValue ? 1 : 0;
  39. }
  40. /**
  41. * Creates a new <tt>AtomicBoolean</tt> with initial value <tt>false</tt>.
  42. */
  43. public AtomicBoolean() {
  44. }
  45. /**
  46. * Returns the current value.
  47. *
  48. * @return the current value
  49. */
  50. public final boolean get() {
  51. return value != 0;
  52. }
  53. /**
  54. * Atomically set the value to the given updated value
  55. * if the current value <tt>==</tt> the expected value.
  56. * @param expect the expected value
  57. * @param update the new value
  58. * @return true if successful. False return indicates that
  59. * the actual value was not equal to the expected value.
  60. */
  61. public final boolean compareAndSet(boolean expect, boolean update) {
  62. int e = expect ? 1 : 0;
  63. int u = update ? 1 : 0;
  64. return unsafe.compareAndSwapInt(this, valueOffset, e, u);
  65. }
  66. /**
  67. * Atomically set the value to the given updated value
  68. * if the current value <tt>==</tt> the expected value.
  69. * May fail spuriously.
  70. * @param expect the expected value
  71. * @param update the new value
  72. * @return true if successful.
  73. */
  74. public boolean weakCompareAndSet(boolean expect, boolean update) {
  75. int e = expect ? 1 : 0;
  76. int u = update ? 1 : 0;
  77. return unsafe.compareAndSwapInt(this, valueOffset, e, u);
  78. }
  79. /**
  80. * Unconditionally sets to the given value.
  81. *
  82. * @param newValue the new value
  83. */
  84. public final void set(boolean newValue) {
  85. value = newValue ? 1 : 0;
  86. }
  87. /**
  88. * Sets to the given value and returns the previous value.
  89. *
  90. * @param newValue the new value
  91. * @return the previous value
  92. */
  93. public final boolean getAndSet(boolean newValue) {
  94. for (;;) {
  95. boolean current = get();
  96. if (compareAndSet(current, newValue))
  97. return current;
  98. }
  99. }
  100. /**
  101. * Returns the String representation of the current value.
  102. * @return the String representation of the current value.
  103. */
  104. public String toString() {
  105. return Boolean.toString(get());
  106. }
  107. }