author | chegar |
Wed, 11 Nov 2015 09:19:12 +0000 | |
changeset 33674 | 566777f73c32 |
parent 32991 | b27c76b82713 |
child 36936 | bfcdf736a998 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
7976
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent.atomic; |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
37 |
|
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
38 |
import java.lang.reflect.Array; |
18576 | 39 |
import java.util.Arrays; |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
40 |
import java.util.function.BinaryOperator; |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
41 |
import java.util.function.UnaryOperator; |
2 | 42 |
|
43 |
/** |
|
44 |
* An array of object references in which elements may be updated |
|
45 |
* atomically. See the {@link java.util.concurrent.atomic} package |
|
46 |
* specification for description of the properties of atomic |
|
47 |
* variables. |
|
48 |
* @since 1.5 |
|
49 |
* @author Doug Lea |
|
50 |
* @param <E> The base class of elements held in this array |
|
51 |
*/ |
|
52 |
public class AtomicReferenceArray<E> implements java.io.Serializable { |
|
53 |
private static final long serialVersionUID = -6209656149925076980L; |
|
54 |
||
33674
566777f73c32
8140606: Update library code to use internal Unsafe
chegar
parents:
32991
diff
changeset
|
55 |
private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe(); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
32838
diff
changeset
|
56 |
private static final long ARRAY; |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
57 |
private static final int ABASE; |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
58 |
private static final int ASHIFT; |
11892
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
59 |
private final Object[] array; // must have exact type Object[] |
2 | 60 |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
61 |
static { |
11892
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
62 |
try { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
63 |
ARRAY = U.objectFieldOffset |
11892
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
64 |
(AtomicReferenceArray.class.getDeclaredField("array")); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
65 |
ABASE = U.arrayBaseOffset(Object[].class); |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
66 |
int scale = U.arrayIndexScale(Object[].class); |
18576 | 67 |
if ((scale & (scale - 1)) != 0) |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
68 |
throw new Error("array index scale not a power of two"); |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
69 |
ASHIFT = 31 - Integer.numberOfLeadingZeros(scale); |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
70 |
} catch (ReflectiveOperationException e) { |
11892
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
71 |
throw new Error(e); |
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
72 |
} |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
73 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
74 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
75 |
private long checkedByteOffset(int i) { |
2 | 76 |
if (i < 0 || i >= array.length) |
77 |
throw new IndexOutOfBoundsException("index " + i); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
78 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
79 |
return byteOffset(i); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
80 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
81 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
82 |
private static long byteOffset(int i) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
83 |
return ((long) i << ASHIFT) + ABASE; |
2 | 84 |
} |
85 |
||
86 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
87 |
* Creates a new AtomicReferenceArray of the given length, with all |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
88 |
* elements initially null. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
89 |
* |
2 | 90 |
* @param length the length of the array |
91 |
*/ |
|
92 |
public AtomicReferenceArray(int length) { |
|
93 |
array = new Object[length]; |
|
94 |
} |
|
95 |
||
96 |
/** |
|
97 |
* Creates a new AtomicReferenceArray with the same length as, and |
|
98 |
* all elements copied from, the given array. |
|
99 |
* |
|
100 |
* @param array the array to copy elements from |
|
101 |
* @throws NullPointerException if array is null |
|
102 |
*/ |
|
103 |
public AtomicReferenceArray(E[] array) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
104 |
// Visibility guaranteed by final field guarantees |
11892
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
105 |
this.array = Arrays.copyOf(array, array.length, Object[].class); |
2 | 106 |
} |
107 |
||
108 |
/** |
|
109 |
* Returns the length of the array. |
|
110 |
* |
|
111 |
* @return the length of the array |
|
112 |
*/ |
|
113 |
public final int length() { |
|
114 |
return array.length; |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* Gets the current value at position {@code i}. |
|
119 |
* |
|
120 |
* @param i the index |
|
121 |
* @return the current value |
|
122 |
*/ |
|
123 |
public final E get(int i) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
124 |
return getRaw(checkedByteOffset(i)); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
125 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
126 |
|
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
9242
diff
changeset
|
127 |
@SuppressWarnings("unchecked") |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
128 |
private E getRaw(long offset) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
129 |
return (E) U.getObjectVolatile(array, offset); |
2 | 130 |
} |
131 |
||
132 |
/** |
|
133 |
* Sets the element at position {@code i} to the given value. |
|
134 |
* |
|
135 |
* @param i the index |
|
136 |
* @param newValue the new value |
|
137 |
*/ |
|
138 |
public final void set(int i, E newValue) { |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
139 |
U.putObjectVolatile(array, checkedByteOffset(i), newValue); |
2 | 140 |
} |
141 |
||
142 |
/** |
|
143 |
* Eventually sets the element at position {@code i} to the given value. |
|
144 |
* |
|
145 |
* @param i the index |
|
146 |
* @param newValue the new value |
|
147 |
* @since 1.6 |
|
148 |
*/ |
|
149 |
public final void lazySet(int i, E newValue) { |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
150 |
U.putOrderedObject(array, checkedByteOffset(i), newValue); |
2 | 151 |
} |
152 |
||
153 |
/** |
|
154 |
* Atomically sets the element at position {@code i} to the given |
|
155 |
* value and returns the old value. |
|
156 |
* |
|
157 |
* @param i the index |
|
158 |
* @param newValue the new value |
|
159 |
* @return the previous value |
|
160 |
*/ |
|
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
161 |
@SuppressWarnings("unchecked") |
2 | 162 |
public final E getAndSet(int i, E newValue) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
163 |
return (E)U.getAndSetObject(array, checkedByteOffset(i), newValue); |
2 | 164 |
} |
165 |
||
166 |
/** |
|
167 |
* Atomically sets the element at position {@code i} to the given |
|
168 |
* updated value if the current value {@code ==} the expected value. |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
169 |
* |
2 | 170 |
* @param i the index |
171 |
* @param expect the expected value |
|
172 |
* @param update the new value |
|
18576 | 173 |
* @return {@code true} if successful. False return indicates that |
2 | 174 |
* the actual value was not equal to the expected value. |
175 |
*/ |
|
176 |
public final boolean compareAndSet(int i, E expect, E update) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
177 |
return compareAndSetRaw(checkedByteOffset(i), expect, update); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
178 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
179 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
180 |
private boolean compareAndSetRaw(long offset, E expect, E update) { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
181 |
return U.compareAndSwapObject(array, offset, expect, update); |
2 | 182 |
} |
183 |
||
184 |
/** |
|
185 |
* Atomically sets the element at position {@code i} to the given |
|
186 |
* updated value if the current value {@code ==} the expected value. |
|
187 |
* |
|
18576 | 188 |
* <p><a href="package-summary.html#weakCompareAndSet">May fail |
189 |
* spuriously and does not provide ordering guarantees</a>, so is |
|
190 |
* only rarely an appropriate alternative to {@code compareAndSet}. |
|
2 | 191 |
* |
192 |
* @param i the index |
|
193 |
* @param expect the expected value |
|
194 |
* @param update the new value |
|
18576 | 195 |
* @return {@code true} if successful |
2 | 196 |
*/ |
197 |
public final boolean weakCompareAndSet(int i, E expect, E update) { |
|
198 |
return compareAndSet(i, expect, update); |
|
199 |
} |
|
200 |
||
18576 | 201 |
/** |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
202 |
* Atomically updates the element at index {@code i} with the results |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
203 |
* of applying the given function, returning the previous value. The |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
204 |
* function should be side-effect-free, since it may be re-applied |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
205 |
* when attempted updates fail due to contention among threads. |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
206 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
207 |
* @param i the index |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
208 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
209 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
210 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
211 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
212 |
public final E getAndUpdate(int i, UnaryOperator<E> updateFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
213 |
long offset = checkedByteOffset(i); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
214 |
E prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
215 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
216 |
prev = getRaw(offset); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
217 |
next = updateFunction.apply(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
218 |
} while (!compareAndSetRaw(offset, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
219 |
return prev; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
220 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
221 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
222 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
223 |
* Atomically updates the element at index {@code i} with the results |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
224 |
* of applying the given function, returning the updated value. The |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
225 |
* function should be side-effect-free, since it may be re-applied |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
226 |
* when attempted updates fail due to contention among threads. |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
227 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
228 |
* @param i the index |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
229 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
230 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
231 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
232 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
233 |
public final E updateAndGet(int i, UnaryOperator<E> updateFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
234 |
long offset = checkedByteOffset(i); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
235 |
E prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
236 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
237 |
prev = getRaw(offset); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
238 |
next = updateFunction.apply(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
239 |
} while (!compareAndSetRaw(offset, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
240 |
return next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
241 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
242 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
243 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
244 |
* Atomically updates the element at index {@code i} with the |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
245 |
* results of applying the given function to the current and |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
246 |
* given values, returning the previous value. The function should |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
247 |
* be side-effect-free, since it may be re-applied when attempted |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
248 |
* updates fail due to contention among threads. The function is |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
249 |
* applied with the current value at index {@code i} as its first |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
250 |
* argument, and the given update as the second argument. |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
251 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
252 |
* @param i the index |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
253 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
254 |
* @param accumulatorFunction a side-effect-free function of two arguments |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
255 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
256 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
257 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
258 |
public final E getAndAccumulate(int i, E x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
259 |
BinaryOperator<E> accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
260 |
long offset = checkedByteOffset(i); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
261 |
E prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
262 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
263 |
prev = getRaw(offset); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
264 |
next = accumulatorFunction.apply(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
265 |
} while (!compareAndSetRaw(offset, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
266 |
return prev; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
267 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
268 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
269 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
270 |
* Atomically updates the element at index {@code i} with the |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
271 |
* results of applying the given function to the current and |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
272 |
* given values, returning the updated value. The function should |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
273 |
* be side-effect-free, since it may be re-applied when attempted |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
274 |
* updates fail due to contention among threads. The function is |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
275 |
* applied with the current value at index {@code i} as its first |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
276 |
* argument, and the given update as the second argument. |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
277 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
278 |
* @param i the index |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
279 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
280 |
* @param accumulatorFunction a side-effect-free function of two arguments |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
281 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
282 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
283 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
284 |
public final E accumulateAndGet(int i, E x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
285 |
BinaryOperator<E> accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
286 |
long offset = checkedByteOffset(i); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
287 |
E prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
288 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
289 |
prev = getRaw(offset); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
290 |
next = accumulatorFunction.apply(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
291 |
} while (!compareAndSetRaw(offset, prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
292 |
return next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
293 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
294 |
|
2 | 295 |
/** |
296 |
* Returns the String representation of the current values of array. |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
297 |
* @return the String representation of the current values of array |
2 | 298 |
*/ |
299 |
public String toString() { |
|
11892
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
300 |
int iMax = array.length - 1; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
301 |
if (iMax == -1) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
302 |
return "[]"; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
303 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
304 |
StringBuilder b = new StringBuilder(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
305 |
b.append('['); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
306 |
for (int i = 0; ; i++) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
307 |
b.append(getRaw(byteOffset(i))); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
308 |
if (i == iMax) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
309 |
return b.append(']').toString(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
310 |
b.append(',').append(' '); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
311 |
} |
2 | 312 |
} |
313 |
||
11892
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
314 |
/** |
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
315 |
* Reconstitutes the instance from a stream (that is, deserializes it). |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
316 |
* @param s the stream |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
317 |
* @throws ClassNotFoundException if the class of a serialized object |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
318 |
* could not be found |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
319 |
* @throws java.io.IOException if an I/O error occurs |
11892
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
320 |
*/ |
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
321 |
private void readObject(java.io.ObjectInputStream s) |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
322 |
throws java.io.IOException, ClassNotFoundException { |
11892
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
323 |
// Note: This must be changed if any additional fields are defined |
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
324 |
Object a = s.readFields().get("array", null); |
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
325 |
if (a == null || !a.getClass().isArray()) |
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
326 |
throw new java.io.InvalidObjectException("Not array type"); |
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
327 |
if (a.getClass() != Object[].class) |
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
328 |
a = Arrays.copyOf((Object[])a, Array.getLength(a), Object[].class); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
329 |
U.putObjectVolatile(this, ARRAY, a); |
11892
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
330 |
} |
1b260c532b70
7082299: AtomicReferenceArray should ensure that array is Object[]
dl
parents:
9242
diff
changeset
|
331 |
|
2 | 332 |
} |