001 /*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
003 *
004 * This code is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU General Public License version 2 only, as
006 * published by the Free Software Foundation. Sun designates this
007 * particular file as subject to the "Classpath" exception as provided
008 * by Sun in the LICENSE file that accompanied this code.
009 *
010 * This code is distributed in the hope that it will be useful, but WITHOUT
011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013 * version 2 for more details (a copy is included in the LICENSE file that
014 * accompanied this code).
015 *
016 * You should have received a copy of the GNU General Public License version
017 * 2 along with this work; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
019 *
020 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
021 * CA 95054 USA or visit www.sun.com if you need additional information or
022 * have any questions.
023 */
024
025 /*
026 * This file is available under and governed by the GNU General Public
027 * License version 2 only, as published by the Free Software Foundation.
028 * However, the following notice accompanied the original version of this
029 * file:
030 *
031 * Written by Doug Lea with assistance from members of JCP JSR-166
032 * Expert Group and released to the public domain, as explained at
033 * http://creativecommons.org/licenses/publicdomain
034 */
035
036 package java.util.concurrent.atomic;
037
038 import sun.misc.Unsafe;
039 import java.util.*;
040
041 /**
042 * An array of object references in which elements may be updated
043 * atomically. See the {@link java.util.concurrent.atomic} package
044 * specification for description of the properties of atomic
045 * variables.
046 * @since 1.5
047 * @author Doug Lea
048 * @param <E> The base class of elements held in this array
049 */
050 public class AtomicReferenceArray<E> implements java.io.Serializable {
051 private static final long serialVersionUID = -6209656149925076980L;
052
053 private static final Unsafe unsafe = Unsafe.getUnsafe();
054 private static final int base = unsafe
055 .arrayBaseOffset(Object[].class);
056 private static final int scale = unsafe
057 .arrayIndexScale(Object[].class);
058 private final Object[] array;
059
060 private long rawIndex(int i) {
061 if (i < 0 || i >= array.length)
062 throw new IndexOutOfBoundsException("index " + i);
063 return base + i * scale;
064 }
065
066 /**
067 * Creates a new AtomicReferenceArray of given length.
068 * @param length the length of the array
069 */
070 public AtomicReferenceArray(int length) {
071 array = new Object[length];
072 // must perform at least one volatile write to conform to JMM
073 if (length > 0)
074 unsafe.putObjectVolatile(array, rawIndex(0), null);
075 }
076
077 /**
078 * Creates a new AtomicReferenceArray with the same length as, and
079 * all elements copied from, the given array.
080 *
081 * @param array the array to copy elements from
082 * @throws NullPointerException if array is null
083 */
084 public AtomicReferenceArray(E[] array) {
085 if (array == null)
086 throw new NullPointerException();
087 int length = array.length;
088 this .array = new Object[length];
089 if (length > 0) {
090 int last = length - 1;
091 for (int i = 0; i < last; ++i)
092 this .array[i] = array[i];
093 // Do the last write as volatile
094 E e = array[last];
095 unsafe.putObjectVolatile(this .array, rawIndex(last), e);
096 }
097 }
098
099 /**
100 * Returns the length of the array.
101 *
102 * @return the length of the array
103 */
104 public final int length() {
105 return array.length;
106 }
107
108 /**
109 * Gets the current value at position {@code i}.
110 *
111 * @param i the index
112 * @return the current value
113 */
114 public final E get(int i) {
115 return (E) unsafe.getObjectVolatile(array, rawIndex(i));
116 }
117
118 /**
119 * Sets the element at position {@code i} to the given value.
120 *
121 * @param i the index
122 * @param newValue the new value
123 */
124 public final void set(int i, E newValue) {
125 unsafe.putObjectVolatile(array, rawIndex(i), newValue);
126 }
127
128 /**
129 * Eventually sets the element at position {@code i} to the given value.
130 *
131 * @param i the index
132 * @param newValue the new value
133 * @since 1.6
134 */
135 public final void lazySet(int i, E newValue) {
136 unsafe.putOrderedObject(array, rawIndex(i), newValue);
137 }
138
139 /**
140 * Atomically sets the element at position {@code i} to the given
141 * value and returns the old value.
142 *
143 * @param i the index
144 * @param newValue the new value
145 * @return the previous value
146 */
147 public final E getAndSet(int i, E newValue) {
148 while (true) {
149 E current = get(i);
150 if (compareAndSet(i, current, newValue))
151 return current;
152 }
153 }
154
155 /**
156 * Atomically sets the element at position {@code i} to the given
157 * updated value if the current value {@code ==} the expected value.
158 * @param i the index
159 * @param expect the expected value
160 * @param update the new value
161 * @return true if successful. False return indicates that
162 * the actual value was not equal to the expected value.
163 */
164 public final boolean compareAndSet(int i, E expect, E update) {
165 return unsafe.compareAndSwapObject(array, rawIndex(i), expect,
166 update);
167 }
168
169 /**
170 * Atomically sets the element at position {@code i} to the given
171 * updated value if the current value {@code ==} the expected value.
172 *
173 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
174 * and does not provide ordering guarantees, so is only rarely an
175 * appropriate alternative to {@code compareAndSet}.
176 *
177 * @param i the index
178 * @param expect the expected value
179 * @param update the new value
180 * @return true if successful.
181 */
182 public final boolean weakCompareAndSet(int i, E expect, E update) {
183 return compareAndSet(i, expect, update);
184 }
185
186 /**
187 * Returns the String representation of the current values of array.
188 * @return the String representation of the current values of array.
189 */
190 public String toString() {
191 if (array.length > 0) // force volatile read
192 get(0);
193 return Arrays.toString(array);
194 }
195
196 }
|