author | dl |
Mon, 10 Apr 2017 13:46:16 -0700 | |
changeset 44588 | fd9d4d384425 |
parent 40547 | 51759e487197 |
child 44926 | 32eeee1c0abd |
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:
7518
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
37 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
38 |
import java.io.ObjectStreamField; |
2 | 39 |
import java.io.Serializable; |
17945 | 40 |
import java.lang.reflect.ParameterizedType; |
41 |
import java.lang.reflect.Type; |
|
42 |
import java.util.AbstractMap; |
|
43 |
import java.util.Arrays; |
|
44 |
import java.util.Collection; |
|
45 |
import java.util.Enumeration; |
|
46 |
import java.util.HashMap; |
|
47 |
import java.util.Hashtable; |
|
48 |
import java.util.Iterator; |
|
49 |
import java.util.Map; |
|
50 |
import java.util.NoSuchElementException; |
|
51 |
import java.util.Set; |
|
52 |
import java.util.Spliterator; |
|
53 |
import java.util.concurrent.atomic.AtomicReference; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
54 |
import java.util.concurrent.locks.LockSupport; |
17945 | 55 |
import java.util.concurrent.locks.ReentrantLock; |
56 |
import java.util.function.BiConsumer; |
|
57 |
import java.util.function.BiFunction; |
|
58 |
import java.util.function.Consumer; |
|
59 |
import java.util.function.DoubleBinaryOperator; |
|
60 |
import java.util.function.Function; |
|
61 |
import java.util.function.IntBinaryOperator; |
|
62 |
import java.util.function.LongBinaryOperator; |
|
30441
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
63 |
import java.util.function.Predicate; |
17945 | 64 |
import java.util.function.ToDoubleBiFunction; |
65 |
import java.util.function.ToDoubleFunction; |
|
66 |
import java.util.function.ToIntBiFunction; |
|
67 |
import java.util.function.ToIntFunction; |
|
68 |
import java.util.function.ToLongBiFunction; |
|
69 |
import java.util.function.ToLongFunction; |
|
70 |
import java.util.stream.Stream; |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
38551
diff
changeset
|
71 |
import jdk.internal.misc.Unsafe; |
2 | 72 |
|
73 |
/** |
|
74 |
* A hash table supporting full concurrency of retrievals and |
|
17945 | 75 |
* high expected concurrency for updates. This class obeys the |
2 | 76 |
* same functional specification as {@link java.util.Hashtable}, and |
77 |
* includes versions of methods corresponding to each method of |
|
17717 | 78 |
* {@code Hashtable}. However, even though all operations are |
2 | 79 |
* thread-safe, retrieval operations do <em>not</em> entail locking, |
80 |
* and there is <em>not</em> any support for locking the entire table |
|
81 |
* in a way that prevents all access. This class is fully |
|
17717 | 82 |
* interoperable with {@code Hashtable} in programs that rely on its |
2 | 83 |
* thread safety but not on its synchronization details. |
84 |
* |
|
17945 | 85 |
* <p>Retrieval operations (including {@code get}) generally do not |
86 |
* block, so may overlap with update operations (including {@code put} |
|
87 |
* and {@code remove}). Retrievals reflect the results of the most |
|
88 |
* recently <em>completed</em> update operations holding upon their |
|
89 |
* onset. (More formally, an update operation for a given key bears a |
|
90 |
* <em>happens-before</em> relation with any (non-null) retrieval for |
|
91 |
* that key reporting the updated value.) For aggregate operations |
|
92 |
* such as {@code putAll} and {@code clear}, concurrent retrievals may |
|
93 |
* reflect insertion or removal of only some entries. Similarly, |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
94 |
* Iterators, Spliterators and Enumerations return elements reflecting the |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
95 |
* state of the hash table at some point at or since the creation of the |
17945 | 96 |
* iterator/enumeration. They do <em>not</em> throw {@link |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
97 |
* java.util.ConcurrentModificationException ConcurrentModificationException}. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
98 |
* However, iterators are designed to be used by only one thread at a time. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
99 |
* Bear in mind that the results of aggregate status methods including |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
100 |
* {@code size}, {@code isEmpty}, and {@code containsValue} are typically |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
101 |
* useful only when a map is not undergoing concurrent updates in other threads. |
17945 | 102 |
* Otherwise the results of these methods reflect transient states |
103 |
* that may be adequate for monitoring or estimation purposes, but not |
|
104 |
* for program control. |
|
2 | 105 |
* |
17945 | 106 |
* <p>The table is dynamically expanded when there are too many |
107 |
* collisions (i.e., keys that have distinct hash codes but fall into |
|
108 |
* the same slot modulo the table size), with the expected average |
|
109 |
* effect of maintaining roughly two bins per mapping (corresponding |
|
110 |
* to a 0.75 load factor threshold for resizing). There may be much |
|
111 |
* variance around this average as mappings are added and removed, but |
|
112 |
* overall, this maintains a commonly accepted time/space tradeoff for |
|
113 |
* hash tables. However, resizing this or any other kind of hash |
|
114 |
* table may be a relatively slow operation. When possible, it is a |
|
115 |
* good idea to provide a size estimate as an optional {@code |
|
116 |
* initialCapacity} constructor argument. An additional optional |
|
117 |
* {@code loadFactor} constructor argument provides a further means of |
|
118 |
* customizing initial table capacity by specifying the table density |
|
119 |
* to be used in calculating the amount of space to allocate for the |
|
120 |
* given number of elements. Also, for compatibility with previous |
|
121 |
* versions of this class, constructors may optionally specify an |
|
122 |
* expected {@code concurrencyLevel} as an additional hint for |
|
123 |
* internal sizing. Note that using many keys with exactly the same |
|
124 |
* {@code hashCode()} is a sure way to slow down performance of any |
|
125 |
* hash table. To ameliorate impact, when keys are {@link Comparable}, |
|
126 |
* this class may use comparison order among keys to help break ties. |
|
127 |
* |
|
128 |
* <p>A {@link Set} projection of a ConcurrentHashMap may be created |
|
129 |
* (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed |
|
130 |
* (using {@link #keySet(Object)} when only keys are of interest, and the |
|
131 |
* mapped values are (perhaps transiently) not used or all take the |
|
132 |
* same mapping value. |
|
133 |
* |
|
24972
d3304c4e66b5
8044730: small errors in ConcurrentHashMap and LongAdder docs
smarks
parents:
21981
diff
changeset
|
134 |
* <p>A ConcurrentHashMap can be used as a scalable frequency map (a |
17945 | 135 |
* form of histogram or multiset) by using {@link |
136 |
* java.util.concurrent.atomic.LongAdder} values and initializing via |
|
137 |
* {@link #computeIfAbsent computeIfAbsent}. For example, to add a count |
|
138 |
* to a {@code ConcurrentHashMap<String,LongAdder> freqs}, you can use |
|
24972
d3304c4e66b5
8044730: small errors in ConcurrentHashMap and LongAdder docs
smarks
parents:
21981
diff
changeset
|
139 |
* {@code freqs.computeIfAbsent(key, k -> new LongAdder()).increment();} |
2 | 140 |
* |
141 |
* <p>This class and its views and iterators implement all of the |
|
142 |
* <em>optional</em> methods of the {@link Map} and {@link Iterator} |
|
143 |
* interfaces. |
|
144 |
* |
|
17717 | 145 |
* <p>Like {@link Hashtable} but unlike {@link HashMap}, this class |
146 |
* does <em>not</em> allow {@code null} to be used as a key or value. |
|
2 | 147 |
* |
17945 | 148 |
* <p>ConcurrentHashMaps support a set of sequential and parallel bulk |
149 |
* operations that, unlike most {@link Stream} methods, are designed |
|
150 |
* to be safely, and often sensibly, applied even with maps that are |
|
151 |
* being concurrently updated by other threads; for example, when |
|
152 |
* computing a snapshot summary of the values in a shared registry. |
|
153 |
* There are three kinds of operation, each with four forms, accepting |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
154 |
* functions with keys, values, entries, and (key, value) pairs as |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
155 |
* arguments and/or return values. Because the elements of a |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
156 |
* ConcurrentHashMap are not ordered in any particular way, and may be |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
157 |
* processed in different orders in different parallel executions, the |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
158 |
* correctness of supplied functions should not depend on any |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
159 |
* ordering, or on any other objects or values that may transiently |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
160 |
* change while computation is in progress; and except for forEach |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
161 |
* actions, should ideally be side-effect-free. Bulk operations on |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
162 |
* {@link java.util.Map.Entry} objects do not support method {@code |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
163 |
* setValue}. |
17945 | 164 |
* |
165 |
* <ul> |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
166 |
* <li>forEach: Performs a given action on each element. |
17945 | 167 |
* A variant form applies a given transformation on each element |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
168 |
* before performing the action. |
17945 | 169 |
* |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
170 |
* <li>search: Returns the first available non-null result of |
17945 | 171 |
* applying a given function on each element; skipping further |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
172 |
* search when a result is found. |
17945 | 173 |
* |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
174 |
* <li>reduce: Accumulates each element. The supplied reduction |
17945 | 175 |
* function cannot rely on ordering (more formally, it should be |
176 |
* both associative and commutative). There are five variants: |
|
177 |
* |
|
178 |
* <ul> |
|
179 |
* |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
180 |
* <li>Plain reductions. (There is not a form of this method for |
17945 | 181 |
* (key, value) function arguments since there is no corresponding |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
182 |
* return type.) |
17945 | 183 |
* |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
184 |
* <li>Mapped reductions that accumulate the results of a given |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
185 |
* function applied to each element. |
17945 | 186 |
* |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
187 |
* <li>Reductions to scalar doubles, longs, and ints, using a |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
188 |
* given basis value. |
17945 | 189 |
* |
190 |
* </ul> |
|
191 |
* </ul> |
|
192 |
* |
|
193 |
* <p>These bulk operations accept a {@code parallelismThreshold} |
|
194 |
* argument. Methods proceed sequentially if the current map size is |
|
195 |
* estimated to be less than the given threshold. Using a value of |
|
196 |
* {@code Long.MAX_VALUE} suppresses all parallelism. Using a value |
|
197 |
* of {@code 1} results in maximal parallelism by partitioning into |
|
198 |
* enough subtasks to fully utilize the {@link |
|
199 |
* ForkJoinPool#commonPool()} that is used for all parallel |
|
200 |
* computations. Normally, you would initially choose one of these |
|
201 |
* extreme values, and then measure performance of using in-between |
|
202 |
* values that trade off overhead versus throughput. |
|
203 |
* |
|
204 |
* <p>The concurrency properties of bulk operations follow |
|
205 |
* from those of ConcurrentHashMap: Any non-null result returned |
|
206 |
* from {@code get(key)} and related access methods bears a |
|
207 |
* happens-before relation with the associated insertion or |
|
208 |
* update. The result of any bulk operation reflects the |
|
209 |
* composition of these per-element relations (but is not |
|
210 |
* necessarily atomic with respect to the map as a whole unless it |
|
211 |
* is somehow known to be quiescent). Conversely, because keys |
|
212 |
* and values in the map are never null, null serves as a reliable |
|
213 |
* atomic indicator of the current lack of any result. To |
|
214 |
* maintain this property, null serves as an implicit basis for |
|
215 |
* all non-scalar reduction operations. For the double, long, and |
|
216 |
* int versions, the basis should be one that, when combined with |
|
217 |
* any other value, returns that other value (more formally, it |
|
218 |
* should be the identity element for the reduction). Most common |
|
219 |
* reductions have these properties; for example, computing a sum |
|
220 |
* with basis 0 or a minimum with basis MAX_VALUE. |
|
221 |
* |
|
222 |
* <p>Search and transformation functions provided as arguments |
|
223 |
* should similarly return null to indicate the lack of any result |
|
224 |
* (in which case it is not used). In the case of mapped |
|
225 |
* reductions, this also enables transformations to serve as |
|
226 |
* filters, returning null (or, in the case of primitive |
|
227 |
* specializations, the identity basis) if the element should not |
|
228 |
* be combined. You can create compound transformations and |
|
229 |
* filterings by composing them yourself under this "null means |
|
230 |
* there is nothing there now" rule before using them in search or |
|
231 |
* reduce operations. |
|
232 |
* |
|
233 |
* <p>Methods accepting and/or returning Entry arguments maintain |
|
234 |
* key-value associations. They may be useful for example when |
|
235 |
* finding the key for the greatest value. Note that "plain" Entry |
|
236 |
* arguments can be supplied using {@code new |
|
237 |
* AbstractMap.SimpleEntry(k,v)}. |
|
238 |
* |
|
239 |
* <p>Bulk operations may complete abruptly, throwing an |
|
240 |
* exception encountered in the application of a supplied |
|
241 |
* function. Bear in mind when handling such exceptions that other |
|
242 |
* concurrently executing functions could also have thrown |
|
243 |
* exceptions, or would have done so if the first exception had |
|
244 |
* not occurred. |
|
245 |
* |
|
246 |
* <p>Speedups for parallel compared to sequential forms are common |
|
247 |
* but not guaranteed. Parallel operations involving brief functions |
|
248 |
* on small maps may execute more slowly than sequential forms if the |
|
249 |
* underlying work to parallelize the computation is more expensive |
|
250 |
* than the computation itself. Similarly, parallelization may not |
|
251 |
* lead to much actual parallelism if all processors are busy |
|
252 |
* performing unrelated tasks. |
|
253 |
* |
|
254 |
* <p>All arguments to all task methods must be non-null. |
|
255 |
* |
|
2 | 256 |
* <p>This class is a member of the |
257 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
258 |
* Java Collections Framework</a>. |
|
259 |
* |
|
260 |
* @since 1.5 |
|
261 |
* @author Doug Lea |
|
262 |
* @param <K> the type of keys maintained by this map |
|
263 |
* @param <V> the type of mapped values |
|
264 |
*/ |
|
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
265 |
public class ConcurrentHashMap<K,V> extends AbstractMap<K,V> |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
266 |
implements ConcurrentMap<K,V>, Serializable { |
2 | 267 |
private static final long serialVersionUID = 7249069246763182397L; |
268 |
||
269 |
/* |
|
17945 | 270 |
* Overview: |
271 |
* |
|
272 |
* The primary design goal of this hash table is to maintain |
|
273 |
* concurrent readability (typically method get(), but also |
|
274 |
* iterators and related methods) while minimizing update |
|
275 |
* contention. Secondary goals are to keep space consumption about |
|
276 |
* the same or better than java.util.HashMap, and to support high |
|
277 |
* initial insertion rates on an empty table by many threads. |
|
278 |
* |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
279 |
* This map usually acts as a binned (bucketed) hash table. Each |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
280 |
* key-value mapping is held in a Node. Most nodes are instances |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
281 |
* of the basic Node class with hash, key, value, and next |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
282 |
* fields. However, various subclasses exist: TreeNodes are |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
283 |
* arranged in balanced trees, not lists. TreeBins hold the roots |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
284 |
* of sets of TreeNodes. ForwardingNodes are placed at the heads |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
285 |
* of bins during resizing. ReservationNodes are used as |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
286 |
* placeholders while establishing values in computeIfAbsent and |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
287 |
* related methods. The types TreeBin, ForwardingNode, and |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
288 |
* ReservationNode do not hold normal user keys, values, or |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
289 |
* hashes, and are readily distinguishable during search etc |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
290 |
* because they have negative hash fields and null key and value |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
291 |
* fields. (These special nodes are either uncommon or transient, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
292 |
* so the impact of carrying around some unused fields is |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
293 |
* insignificant.) |
17945 | 294 |
* |
295 |
* The table is lazily initialized to a power-of-two size upon the |
|
296 |
* first insertion. Each bin in the table normally contains a |
|
297 |
* list of Nodes (most often, the list has only zero or one Node). |
|
298 |
* Table accesses require volatile/atomic reads, writes, and |
|
299 |
* CASes. Because there is no other way to arrange this without |
|
300 |
* adding further indirections, we use intrinsics |
|
33674
566777f73c32
8140606: Update library code to use internal Unsafe
chegar
parents:
32991
diff
changeset
|
301 |
* (jdk.internal.misc.Unsafe) operations. |
17945 | 302 |
* |
303 |
* We use the top (sign) bit of Node hash fields for control |
|
304 |
* purposes -- it is available anyway because of addressing |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
305 |
* constraints. Nodes with negative hash fields are specially |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
306 |
* handled or ignored in map methods. |
17945 | 307 |
* |
308 |
* Insertion (via put or its variants) of the first node in an |
|
309 |
* empty bin is performed by just CASing it to the bin. This is |
|
310 |
* by far the most common case for put operations under most |
|
311 |
* key/hash distributions. Other update operations (insert, |
|
312 |
* delete, and replace) require locks. We do not want to waste |
|
313 |
* the space required to associate a distinct lock object with |
|
314 |
* each bin, so instead use the first node of a bin list itself as |
|
315 |
* a lock. Locking support for these locks relies on builtin |
|
316 |
* "synchronized" monitors. |
|
317 |
* |
|
318 |
* Using the first node of a list as a lock does not by itself |
|
319 |
* suffice though: When a node is locked, any update must first |
|
320 |
* validate that it is still the first node after locking it, and |
|
321 |
* retry if not. Because new nodes are always appended to lists, |
|
322 |
* once a node is first in a bin, it remains first until deleted |
|
323 |
* or the bin becomes invalidated (upon resizing). |
|
324 |
* |
|
325 |
* The main disadvantage of per-bin locks is that other update |
|
326 |
* operations on other nodes in a bin list protected by the same |
|
327 |
* lock can stall, for example when user equals() or mapping |
|
328 |
* functions take a long time. However, statistically, under |
|
329 |
* random hash codes, this is not a common problem. Ideally, the |
|
330 |
* frequency of nodes in bins follows a Poisson distribution |
|
331 |
* (http://en.wikipedia.org/wiki/Poisson_distribution) with a |
|
332 |
* parameter of about 0.5 on average, given the resizing threshold |
|
333 |
* of 0.75, although with a large variance because of resizing |
|
334 |
* granularity. Ignoring variance, the expected occurrences of |
|
335 |
* list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The |
|
336 |
* first values are: |
|
337 |
* |
|
338 |
* 0: 0.60653066 |
|
339 |
* 1: 0.30326533 |
|
340 |
* 2: 0.07581633 |
|
341 |
* 3: 0.01263606 |
|
342 |
* 4: 0.00157952 |
|
343 |
* 5: 0.00015795 |
|
344 |
* 6: 0.00001316 |
|
345 |
* 7: 0.00000094 |
|
346 |
* 8: 0.00000006 |
|
347 |
* more: less than 1 in ten million |
|
348 |
* |
|
349 |
* Lock contention probability for two threads accessing distinct |
|
350 |
* elements is roughly 1 / (8 * #elements) under random hashes. |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
351 |
* |
17945 | 352 |
* Actual hash code distributions encountered in practice |
353 |
* sometimes deviate significantly from uniform randomness. This |
|
354 |
* includes the case when N > (1<<30), so some keys MUST collide. |
|
355 |
* Similarly for dumb or hostile usages in which multiple keys are |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
356 |
* designed to have identical hash codes or ones that differs only |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
357 |
* in masked-out high bits. So we use a secondary strategy that |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
358 |
* applies when the number of nodes in a bin exceeds a |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
359 |
* threshold. These TreeBins use a balanced tree to hold nodes (a |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
360 |
* specialized form of red-black trees), bounding search time to |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
361 |
* O(log N). Each search step in a TreeBin is at least twice as |
17945 | 362 |
* slow as in a regular list, but given that N cannot exceed |
363 |
* (1<<64) (before running out of addresses) this bounds search |
|
364 |
* steps, lock hold times, etc, to reasonable constants (roughly |
|
365 |
* 100 nodes inspected per operation worst case) so long as keys |
|
366 |
* are Comparable (which is very common -- String, Long, etc). |
|
367 |
* TreeBin nodes (TreeNodes) also maintain the same "next" |
|
368 |
* traversal pointers as regular nodes, so can be traversed in |
|
369 |
* iterators in the same way. |
|
370 |
* |
|
371 |
* The table is resized when occupancy exceeds a percentage |
|
372 |
* threshold (nominally, 0.75, but see below). Any thread |
|
373 |
* noticing an overfull bin may assist in resizing after the |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
374 |
* initiating thread allocates and sets up the replacement array. |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
375 |
* However, rather than stalling, these other threads may proceed |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
376 |
* with insertions etc. The use of TreeBins shields us from the |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
377 |
* worst case effects of overfilling while resizes are in |
17945 | 378 |
* progress. Resizing proceeds by transferring bins, one by one, |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
379 |
* from the table to the next table. However, threads claim small |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
380 |
* blocks of indices to transfer (via field transferIndex) before |
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
381 |
* doing so, reducing contention. A generation stamp in field |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
382 |
* sizeCtl ensures that resizings do not overlap. Because we are |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
383 |
* using power-of-two expansion, the elements from each bin must |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
384 |
* either stay at same index, or move with a power of two |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
385 |
* offset. We eliminate unnecessary node creation by catching |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
386 |
* cases where old nodes can be reused because their next fields |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
387 |
* won't change. On average, only about one-sixth of them need |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
388 |
* cloning when a table doubles. The nodes they replace will be |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
389 |
* garbage collectable as soon as they are no longer referenced by |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
390 |
* any reader thread that may be in the midst of concurrently |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
391 |
* traversing table. Upon transfer, the old table bin contains |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
392 |
* only a special forwarding node (with hash field "MOVED") that |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
393 |
* contains the next table as its key. On encountering a |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
394 |
* forwarding node, access and update operations restart, using |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
395 |
* the new table. |
17945 | 396 |
* |
397 |
* Each bin transfer requires its bin lock, which can stall |
|
398 |
* waiting for locks while resizing. However, because other |
|
399 |
* threads can join in and help resize rather than contend for |
|
400 |
* locks, average aggregate waits become shorter as resizing |
|
401 |
* progresses. The transfer operation must also ensure that all |
|
402 |
* accessible bins in both the old and new table are usable by any |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
403 |
* traversal. This is arranged in part by proceeding from the |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
404 |
* last bin (table.length - 1) up towards the first. Upon seeing |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
405 |
* a forwarding node, traversals (see class Traverser) arrange to |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
406 |
* move to the new table without revisiting nodes. To ensure that |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
407 |
* no intervening nodes are skipped even when moved out of order, |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
408 |
* a stack (see class TableStack) is created on first encounter of |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
409 |
* a forwarding node during a traversal, to maintain its place if |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
410 |
* later processing the current table. The need for these |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
411 |
* save/restore mechanics is relatively rare, but when one |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
412 |
* forwarding node is encountered, typically many more will be. |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
413 |
* So Traversers use a simple caching scheme to avoid creating so |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
414 |
* many new TableStack nodes. (Thanks to Peter Levart for |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
415 |
* suggesting use of a stack here.) |
17945 | 416 |
* |
417 |
* The traversal scheme also applies to partial traversals of |
|
418 |
* ranges of bins (via an alternate Traverser constructor) |
|
419 |
* to support partitioned aggregate operations. Also, read-only |
|
420 |
* operations give up if ever forwarded to a null table, which |
|
421 |
* provides support for shutdown-style clearing, which is also not |
|
422 |
* currently implemented. |
|
423 |
* |
|
424 |
* Lazy table initialization minimizes footprint until first use, |
|
425 |
* and also avoids resizings when the first operation is from a |
|
426 |
* putAll, constructor with map argument, or deserialization. |
|
427 |
* These cases attempt to override the initial capacity settings, |
|
428 |
* but harmlessly fail to take effect in cases of races. |
|
429 |
* |
|
430 |
* The element count is maintained using a specialization of |
|
431 |
* LongAdder. We need to incorporate a specialization rather than |
|
432 |
* just use a LongAdder in order to access implicit |
|
433 |
* contention-sensing that leads to creation of multiple |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
434 |
* CounterCells. The counter mechanics avoid contention on |
17945 | 435 |
* updates but can encounter cache thrashing if read too |
436 |
* frequently during concurrent access. To avoid reading so often, |
|
437 |
* resizing under contention is attempted only upon adding to a |
|
438 |
* bin already holding two or more nodes. Under uniform hash |
|
439 |
* distributions, the probability of this occurring at threshold |
|
440 |
* is around 13%, meaning that only about 1 in 8 puts check |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
441 |
* threshold (and after resizing, many fewer do so). |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
442 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
443 |
* TreeBins use a special form of comparison for search and |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
444 |
* related operations (which is the main reason we cannot use |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
445 |
* existing collections such as TreeMaps). TreeBins contain |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
446 |
* Comparable elements, but may contain others, as well as |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
447 |
* elements that are Comparable but not necessarily Comparable for |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
448 |
* the same T, so we cannot invoke compareTo among them. To handle |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
449 |
* this, the tree is ordered primarily by hash value, then by |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
450 |
* Comparable.compareTo order if applicable. On lookup at a node, |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
451 |
* if elements are not comparable or compare as 0 then both left |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
452 |
* and right children may need to be searched in the case of tied |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
453 |
* hash values. (This corresponds to the full list search that |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
454 |
* would be necessary if all elements were non-Comparable and had |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
455 |
* tied hashes.) On insertion, to keep a total ordering (or as |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
456 |
* close as is required here) across rebalancings, we compare |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
457 |
* classes and identityHashCodes as tie-breakers. The red-black |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
458 |
* balancing code is updated from pre-jdk-collections |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
459 |
* (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
460 |
* based in turn on Cormen, Leiserson, and Rivest "Introduction to |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
461 |
* Algorithms" (CLR). |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
462 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
463 |
* TreeBins also require an additional locking mechanism. While |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
464 |
* list traversal is always possible by readers even during |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
465 |
* updates, tree traversal is not, mainly because of tree-rotations |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
466 |
* that may change the root node and/or its linkages. TreeBins |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
467 |
* include a simple read-write lock mechanism parasitic on the |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
468 |
* main bin-synchronization strategy: Structural adjustments |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
469 |
* associated with an insertion or removal are already bin-locked |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
470 |
* (and so cannot conflict with other writers) but must wait for |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
471 |
* ongoing readers to finish. Since there can be only one such |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
472 |
* waiter, we use a simple scheme using a single "waiter" field to |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
473 |
* block writers. However, readers need never block. If the root |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
474 |
* lock is held, they proceed along the slow traversal path (via |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
475 |
* next-pointers) until the lock becomes available or the list is |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
476 |
* exhausted, whichever comes first. These cases are not fast, but |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
477 |
* maximize aggregate expected throughput. |
17945 | 478 |
* |
479 |
* Maintaining API and serialization compatibility with previous |
|
480 |
* versions of this class introduces several oddities. Mainly: We |
|
30655 | 481 |
* leave untouched but unused constructor arguments referring to |
17945 | 482 |
* concurrencyLevel. We accept a loadFactor constructor argument, |
483 |
* but apply it only to initial table capacity (which is the only |
|
484 |
* time that we can guarantee to honor it.) We also declare an |
|
485 |
* unused "Segment" class that is instantiated in minimal form |
|
486 |
* only when serializing. |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
487 |
* |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
488 |
* Also, solely for compatibility with previous versions of this |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
489 |
* class, it extends AbstractMap, even though all of its methods |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
490 |
* are overridden, so it is just useless baggage. |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
491 |
* |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
492 |
* This file is organized to make things a little easier to follow |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
493 |
* while reading than they might otherwise: First the main static |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
494 |
* declarations and utilities, then fields, then main public |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
495 |
* methods (with a few factorings of multiple public methods into |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
496 |
* internal ones), then sizing methods, trees, traversers, and |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
497 |
* bulk operations. |
2 | 498 |
*/ |
499 |
||
500 |
/* ---------------- Constants -------------- */ |
|
501 |
||
502 |
/** |
|
17945 | 503 |
* The largest possible table capacity. This value must be |
504 |
* exactly 1<<30 to stay within Java array allocation and indexing |
|
505 |
* bounds for power of two table sizes, and is further required |
|
506 |
* because the top two bits of 32bit hash fields are used for |
|
507 |
* control purposes. |
|
2 | 508 |
*/ |
17945 | 509 |
private static final int MAXIMUM_CAPACITY = 1 << 30; |
510 |
||
511 |
/** |
|
512 |
* The default initial table capacity. Must be a power of 2 |
|
513 |
* (i.e., at least 1) and at most MAXIMUM_CAPACITY. |
|
514 |
*/ |
|
515 |
private static final int DEFAULT_CAPACITY = 16; |
|
2 | 516 |
|
517 |
/** |
|
17945 | 518 |
* The largest possible (non-power of two) array size. |
519 |
* Needed by toArray and related methods. |
|
2 | 520 |
*/ |
17945 | 521 |
static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
2 | 522 |
|
523 |
/** |
|
17945 | 524 |
* The default concurrency level for this table. Unused but |
525 |
* defined for compatibility with previous versions of this class. |
|
2 | 526 |
*/ |
17945 | 527 |
private static final int DEFAULT_CONCURRENCY_LEVEL = 16; |
528 |
||
529 |
/** |
|
530 |
* The load factor for this table. Overrides of this value in |
|
531 |
* constructors affect only the initial table capacity. The |
|
532 |
* actual floating point value isn't normally used -- it is |
|
533 |
* simpler to use expressions such as {@code n - (n >>> 2)} for |
|
534 |
* the associated resizing threshold. |
|
535 |
*/ |
|
536 |
private static final float LOAD_FACTOR = 0.75f; |
|
2 | 537 |
|
538 |
/** |
|
17945 | 539 |
* The bin count threshold for using a tree rather than list for a |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
540 |
* bin. Bins are converted to trees when adding an element to a |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
541 |
* bin with at least this many nodes. The value must be greater |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
542 |
* than 2, and should be at least 8 to mesh with assumptions in |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
543 |
* tree removal about conversion back to plain bins upon |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
544 |
* shrinkage. |
2 | 545 |
*/ |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
546 |
static final int TREEIFY_THRESHOLD = 8; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
547 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
548 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
549 |
* The bin count threshold for untreeifying a (split) bin during a |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
550 |
* resize operation. Should be less than TREEIFY_THRESHOLD, and at |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
551 |
* most 6 to mesh with shrinkage detection under removal. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
552 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
553 |
static final int UNTREEIFY_THRESHOLD = 6; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
554 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
555 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
556 |
* The smallest table capacity for which bins may be treeified. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
557 |
* (Otherwise the table is resized if too many nodes in a bin.) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
558 |
* The value should be at least 4 * TREEIFY_THRESHOLD to avoid |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
559 |
* conflicts between resizing and treeification thresholds. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
560 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
561 |
static final int MIN_TREEIFY_CAPACITY = 64; |
2 | 562 |
|
563 |
/** |
|
17945 | 564 |
* Minimum number of rebinnings per transfer step. Ranges are |
565 |
* subdivided to allow multiple resizer threads. This value |
|
566 |
* serves as a lower bound to avoid resizers encountering |
|
567 |
* excessive memory contention. The value should be at least |
|
568 |
* DEFAULT_CAPACITY. |
|
569 |
*/ |
|
570 |
private static final int MIN_TRANSFER_STRIDE = 16; |
|
571 |
||
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
572 |
/** |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
573 |
* The number of bits used for generation stamp in sizeCtl. |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
574 |
* Must be at least 6 for 32bit arrays. |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
575 |
*/ |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
576 |
private static final int RESIZE_STAMP_BITS = 16; |
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
577 |
|
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
578 |
/** |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
579 |
* The maximum number of threads that can help resize. |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
580 |
* Must fit in 32 - RESIZE_STAMP_BITS bits. |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
581 |
*/ |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
582 |
private static final int MAX_RESIZERS = (1 << (32 - RESIZE_STAMP_BITS)) - 1; |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
583 |
|
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
584 |
/** |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
585 |
* The bit shift for recording size stamp in sizeCtl. |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
586 |
*/ |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
587 |
private static final int RESIZE_STAMP_SHIFT = 32 - RESIZE_STAMP_BITS; |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
588 |
|
17945 | 589 |
/* |
590 |
* Encodings for Node hash fields. See above for explanation. |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
591 |
*/ |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
592 |
static final int MOVED = -1; // hash for forwarding nodes |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
593 |
static final int TREEBIN = -2; // hash for roots of trees |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
594 |
static final int RESERVED = -3; // hash for transient reservations |
17945 | 595 |
static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash |
596 |
||
597 |
/** Number of CPUS, to place bounds on some sizings */ |
|
598 |
static final int NCPU = Runtime.getRuntime().availableProcessors(); |
|
599 |
||
34340
3b7a5a01c627
8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents:
33674
diff
changeset
|
600 |
/** |
3b7a5a01c627
8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents:
33674
diff
changeset
|
601 |
* Serialized pseudo-fields, provided only for jdk7 compatibility. |
3b7a5a01c627
8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents:
33674
diff
changeset
|
602 |
* @serialField segments Segment[] |
3b7a5a01c627
8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents:
33674
diff
changeset
|
603 |
* The segments, each of which is a specialized hash table. |
3b7a5a01c627
8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents:
33674
diff
changeset
|
604 |
* @serialField segmentMask int |
3b7a5a01c627
8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents:
33674
diff
changeset
|
605 |
* Mask value for indexing into segments. The upper bits of a |
3b7a5a01c627
8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents:
33674
diff
changeset
|
606 |
* key's hash code are used to choose the segment. |
3b7a5a01c627
8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents:
33674
diff
changeset
|
607 |
* @serialField segmentShift int |
3b7a5a01c627
8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents:
33674
diff
changeset
|
608 |
* Shift value for indexing within segments. |
3b7a5a01c627
8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents:
33674
diff
changeset
|
609 |
*/ |
17945 | 610 |
private static final ObjectStreamField[] serialPersistentFields = { |
611 |
new ObjectStreamField("segments", Segment[].class), |
|
612 |
new ObjectStreamField("segmentMask", Integer.TYPE), |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
613 |
new ObjectStreamField("segmentShift", Integer.TYPE), |
17945 | 614 |
}; |
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
615 |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
616 |
/* ---------------- Nodes -------------- */ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
617 |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
618 |
/** |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
619 |
* Key-value entry. This class is never exported out as a |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
620 |
* user-mutable Map.Entry (i.e., one supporting setValue; see |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
621 |
* MapEntry below), but can be used for read-only traversals used |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
622 |
* in bulk tasks. Subclasses of Node with a negative hash field |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
623 |
* are special, and contain null keys and values (but are never |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
624 |
* exported). Otherwise, keys and vals are never null. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
625 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
626 |
static class Node<K,V> implements Map.Entry<K,V> { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
627 |
final int hash; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
628 |
final K key; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
629 |
volatile V val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
630 |
volatile Node<K,V> next; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
631 |
|
36958 | 632 |
Node(int hash, K key, V val) { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
633 |
this.hash = hash; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
634 |
this.key = key; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
635 |
this.val = val; |
36958 | 636 |
} |
637 |
||
638 |
Node(int hash, K key, V val, Node<K,V> next) { |
|
639 |
this(hash, key, val); |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
640 |
this.next = next; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
641 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
642 |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
643 |
public final K getKey() { return key; } |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
644 |
public final V getValue() { return val; } |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
645 |
public final int hashCode() { return key.hashCode() ^ val.hashCode(); } |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
646 |
public final String toString() { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
647 |
return Helpers.mapEntryToString(key, val); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
648 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
649 |
public final V setValue(V value) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
650 |
throw new UnsupportedOperationException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
651 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
652 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
653 |
public final boolean equals(Object o) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
654 |
Object k, v, u; Map.Entry<?,?> e; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
655 |
return ((o instanceof Map.Entry) && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
656 |
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
657 |
(v = e.getValue()) != null && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
658 |
(k == key || k.equals(key)) && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
659 |
(v == (u = val) || v.equals(u))); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
660 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
661 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
662 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
663 |
* Virtualized support for map.get(); overridden in subclasses. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
664 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
665 |
Node<K,V> find(int h, Object k) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
666 |
Node<K,V> e = this; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
667 |
if (k != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
668 |
do { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
669 |
K ek; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
670 |
if (e.hash == h && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
671 |
((ek = e.key) == k || (ek != null && k.equals(ek)))) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
672 |
return e; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
673 |
} while ((e = e.next) != null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
674 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
675 |
return null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
676 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
677 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
678 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
679 |
/* ---------------- Static utilities -------------- */ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
680 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
681 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
682 |
* Spreads (XORs) higher bits of hash to lower and also forces top |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
683 |
* bit to 0. Because the table uses power-of-two masking, sets of |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
684 |
* hashes that vary only in bits above the current mask will |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
685 |
* always collide. (Among known examples are sets of Float keys |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
686 |
* holding consecutive whole numbers in small tables.) So we |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
687 |
* apply a transform that spreads the impact of higher bits |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
688 |
* downward. There is a tradeoff between speed, utility, and |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
689 |
* quality of bit-spreading. Because many common sets of hashes |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
690 |
* are already reasonably distributed (so don't benefit from |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
691 |
* spreading), and because we use trees to handle large sets of |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
692 |
* collisions in bins, we just XOR some shifted bits in the |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
693 |
* cheapest possible way to reduce systematic lossage, as well as |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
694 |
* to incorporate impact of the highest bits that would otherwise |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
695 |
* never be used in index calculations because of table bounds. |
2 | 696 |
*/ |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
697 |
static final int spread(int h) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
698 |
return (h ^ (h >>> 16)) & HASH_BITS; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
699 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
700 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
701 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
702 |
* Returns a power of two table size for the given desired capacity. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
703 |
* See Hackers Delight, sec 3.2 |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
704 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
705 |
private static final int tableSizeFor(int c) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
706 |
int n = c - 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
707 |
n |= n >>> 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
708 |
n |= n >>> 2; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
709 |
n |= n >>> 4; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
710 |
n |= n >>> 8; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
711 |
n |= n >>> 16; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
712 |
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
713 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
714 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
715 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
716 |
* Returns x's Class if it is of the form "class C implements |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
717 |
* Comparable<C>", else null. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
718 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
719 |
static Class<?> comparableClassFor(Object x) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
720 |
if (x instanceof Comparable) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
721 |
Class<?> c; Type[] ts, as; Type t; ParameterizedType p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
722 |
if ((c = x.getClass()) == String.class) // bypass checks |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
723 |
return c; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
724 |
if ((ts = c.getGenericInterfaces()) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
725 |
for (int i = 0; i < ts.length; ++i) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
726 |
if (((t = ts[i]) instanceof ParameterizedType) && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
727 |
((p = (ParameterizedType)t).getRawType() == |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
728 |
Comparable.class) && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
729 |
(as = p.getActualTypeArguments()) != null && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
730 |
as.length == 1 && as[0] == c) // type arg is c |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
731 |
return c; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
732 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
733 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
734 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
735 |
return null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
736 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
737 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
738 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
739 |
* Returns k.compareTo(x) if x matches kc (k's screened comparable |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
740 |
* class), else 0. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
741 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
742 |
@SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
743 |
static int compareComparables(Class<?> kc, Object k, Object x) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
744 |
return (x == null || x.getClass() != kc ? 0 : |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
745 |
((Comparable)k).compareTo(x)); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
746 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
747 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
748 |
/* ---------------- Table element access -------------- */ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
749 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
750 |
/* |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
38551
diff
changeset
|
751 |
* Atomic access methods are used for table elements as well as |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
752 |
* elements of in-progress next table while resizing. All uses of |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
753 |
* the tab arguments must be null checked by callers. All callers |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
754 |
* also paranoically precheck that tab's length is not zero (or an |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
755 |
* equivalent check), thus ensuring that any index argument taking |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
756 |
* the form of a hash value anded with (length - 1) is a valid |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
757 |
* index. Note that, to be correct wrt arbitrary concurrency |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
758 |
* errors by users, these checks must operate on local variables, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
759 |
* which accounts for some odd-looking inline assignments below. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
760 |
* Note that calls to setTabAt always occur within locked regions, |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
38551
diff
changeset
|
761 |
* and so require only release ordering. |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
762 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
763 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
764 |
@SuppressWarnings("unchecked") |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
765 |
static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
38551
diff
changeset
|
766 |
return (Node<K,V>)U.getObjectAcquire(tab, ((long)i << ASHIFT) + ABASE); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
767 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
768 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
769 |
static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
770 |
Node<K,V> c, Node<K,V> v) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
771 |
return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
772 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
773 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
774 |
static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
38551
diff
changeset
|
775 |
U.putObjectRelease(tab, ((long)i << ASHIFT) + ABASE, v); |
17945 | 776 |
} |
2 | 777 |
|
778 |
/* ---------------- Fields -------------- */ |
|
779 |
||
780 |
/** |
|
17945 | 781 |
* The array of bins. Lazily initialized upon first insertion. |
782 |
* Size is always a power of two. Accessed directly by iterators. |
|
783 |
*/ |
|
784 |
transient volatile Node<K,V>[] table; |
|
785 |
||
786 |
/** |
|
787 |
* The next table to use; non-null only while resizing. |
|
788 |
*/ |
|
789 |
private transient volatile Node<K,V>[] nextTable; |
|
790 |
||
791 |
/** |
|
792 |
* Base counter value, used mainly when there is no contention, |
|
793 |
* but also as a fallback during table initialization |
|
794 |
* races. Updated via CAS. |
|
795 |
*/ |
|
796 |
private transient volatile long baseCount; |
|
797 |
||
798 |
/** |
|
799 |
* Table initialization and resizing control. When negative, the |
|
800 |
* table is being initialized or resized: -1 for initialization, |
|
801 |
* else -(1 + the number of active resizing threads). Otherwise, |
|
802 |
* when table is null, holds the initial table size to use upon |
|
803 |
* creation, or 0 for default. After initialization, holds the |
|
804 |
* next element count value upon which to resize the table. |
|
805 |
*/ |
|
806 |
private transient volatile int sizeCtl; |
|
807 |
||
808 |
/** |
|
809 |
* The next table index (plus one) to split while resizing. |
|
810 |
*/ |
|
811 |
private transient volatile int transferIndex; |
|
812 |
||
813 |
/** |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
814 |
* Spinlock (locked via CAS) used when resizing and/or creating CounterCells. |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
11279
diff
changeset
|
815 |
*/ |
17945 | 816 |
private transient volatile int cellsBusy; |
817 |
||
818 |
/** |
|
819 |
* Table of counter cells. When non-null, size is a power of 2. |
|
820 |
*/ |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
821 |
private transient volatile CounterCell[] counterCells; |
17945 | 822 |
|
823 |
// views |
|
824 |
private transient KeySetView<K,V> keySet; |
|
825 |
private transient ValuesView<K,V> values; |
|
826 |
private transient EntrySetView<K,V> entrySet; |
|
827 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
828 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
829 |
/* ---------------- Public operations -------------- */ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
830 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
831 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
832 |
* Creates a new, empty map with the default initial table size (16). |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
833 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
834 |
public ConcurrentHashMap() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
835 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
836 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
837 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
838 |
* Creates a new, empty map with an initial table size |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
839 |
* accommodating the specified number of elements without the need |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
840 |
* to dynamically resize. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
841 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
842 |
* @param initialCapacity The implementation performs internal |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
843 |
* sizing to accommodate this many elements. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
844 |
* @throws IllegalArgumentException if the initial capacity of |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
845 |
* elements is negative |
17945 | 846 |
*/ |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
847 |
public ConcurrentHashMap(int initialCapacity) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
848 |
if (initialCapacity < 0) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
849 |
throw new IllegalArgumentException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
850 |
int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ? |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
851 |
MAXIMUM_CAPACITY : |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
852 |
tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1)); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
853 |
this.sizeCtl = cap; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
854 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
855 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
856 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
857 |
* Creates a new map with the same mappings as the given map. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
858 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
859 |
* @param m the map |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
860 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
861 |
public ConcurrentHashMap(Map<? extends K, ? extends V> m) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
862 |
this.sizeCtl = DEFAULT_CAPACITY; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
863 |
putAll(m); |
17945 | 864 |
} |
865 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
866 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
867 |
* Creates a new, empty map with an initial table size based on |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
868 |
* the given number of elements ({@code initialCapacity}) and |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
869 |
* initial table density ({@code loadFactor}). |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
870 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
871 |
* @param initialCapacity the initial capacity. The implementation |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
872 |
* performs internal sizing to accommodate this many elements, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
873 |
* given the specified load factor. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
874 |
* @param loadFactor the load factor (table density) for |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
875 |
* establishing the initial table size |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
876 |
* @throws IllegalArgumentException if the initial capacity of |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
877 |
* elements is negative or the load factor is nonpositive |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
878 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
879 |
* @since 1.6 |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
880 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
881 |
public ConcurrentHashMap(int initialCapacity, float loadFactor) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
882 |
this(initialCapacity, loadFactor, 1); |
17945 | 883 |
} |
884 |
||
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
11279
diff
changeset
|
885 |
/** |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
886 |
* Creates a new, empty map with an initial table size based on |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
887 |
* the given number of elements ({@code initialCapacity}), table |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
888 |
* density ({@code loadFactor}), and number of concurrently |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
889 |
* updating threads ({@code concurrencyLevel}). |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
890 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
891 |
* @param initialCapacity the initial capacity. The implementation |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
892 |
* performs internal sizing to accommodate this many elements, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
893 |
* given the specified load factor. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
894 |
* @param loadFactor the load factor (table density) for |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
895 |
* establishing the initial table size |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
896 |
* @param concurrencyLevel the estimated number of concurrently |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
897 |
* updating threads. The implementation may use this value as |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
898 |
* a sizing hint. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
899 |
* @throws IllegalArgumentException if the initial capacity is |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
900 |
* negative or the load factor or concurrencyLevel are |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
901 |
* nonpositive |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
902 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
903 |
public ConcurrentHashMap(int initialCapacity, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
904 |
float loadFactor, int concurrencyLevel) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
905 |
if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
906 |
throw new IllegalArgumentException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
907 |
if (initialCapacity < concurrencyLevel) // Use at least as many bins |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
908 |
initialCapacity = concurrencyLevel; // as estimated threads |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
909 |
long size = (long)(1.0 + (long)initialCapacity / loadFactor); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
910 |
int cap = (size >= (long)MAXIMUM_CAPACITY) ? |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
911 |
MAXIMUM_CAPACITY : tableSizeFor((int)size); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
912 |
this.sizeCtl = cap; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
913 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
914 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
915 |
// Original (since JDK1.2) Map methods |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
916 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
917 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
918 |
* {@inheritDoc} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
919 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
920 |
public int size() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
921 |
long n = sumCount(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
922 |
return ((n < 0L) ? 0 : |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
923 |
(n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE : |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
924 |
(int)n); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
925 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
926 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
927 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
928 |
* {@inheritDoc} |
2 | 929 |
*/ |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
930 |
public boolean isEmpty() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
931 |
return sumCount() <= 0L; // ignore transient negative values |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
932 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
933 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
934 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
935 |
* Returns the value to which the specified key is mapped, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
936 |
* or {@code null} if this map contains no mapping for the key. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
937 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
938 |
* <p>More formally, if this map contains a mapping from a key |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
939 |
* {@code k} to a value {@code v} such that {@code key.equals(k)}, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
940 |
* then this method returns {@code v}; otherwise it returns |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
941 |
* {@code null}. (There can be at most one such mapping.) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
942 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
943 |
* @throws NullPointerException if the specified key is null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
944 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
945 |
public V get(Object key) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
946 |
Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
947 |
int h = spread(key.hashCode()); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
948 |
if ((tab = table) != null && (n = tab.length) > 0 && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
949 |
(e = tabAt(tab, (n - 1) & h)) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
950 |
if ((eh = e.hash) == h) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
951 |
if ((ek = e.key) == key || (ek != null && key.equals(ek))) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
952 |
return e.val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
953 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
954 |
else if (eh < 0) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
955 |
return (p = e.find(h, key)) != null ? p.val : null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
956 |
while ((e = e.next) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
957 |
if (e.hash == h && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
958 |
((ek = e.key) == key || (ek != null && key.equals(ek)))) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
959 |
return e.val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
960 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
961 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
962 |
return null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
963 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
964 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
965 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
966 |
* Tests if the specified object is a key in this table. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
967 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
968 |
* @param key possible key |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
969 |
* @return {@code true} if and only if the specified object |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
970 |
* is a key in this table, as determined by the |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
971 |
* {@code equals} method; {@code false} otherwise |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
972 |
* @throws NullPointerException if the specified key is null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
973 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
974 |
public boolean containsKey(Object key) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
975 |
return get(key) != null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
976 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
977 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
978 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
979 |
* Returns {@code true} if this map maps one or more keys to the |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
980 |
* specified value. Note: This method may require a full traversal |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
981 |
* of the map, and is much slower than method {@code containsKey}. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
982 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
983 |
* @param value value whose presence in this map is to be tested |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
984 |
* @return {@code true} if this map maps one or more keys to the |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
985 |
* specified value |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
986 |
* @throws NullPointerException if the specified value is null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
987 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
988 |
public boolean containsValue(Object value) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
989 |
if (value == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
990 |
throw new NullPointerException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
991 |
Node<K,V>[] t; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
992 |
if ((t = table) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
993 |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
994 |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
995 |
V v; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
996 |
if ((v = p.val) == value || (v != null && value.equals(v))) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
997 |
return true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
998 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
999 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1000 |
return false; |
17945 | 1001 |
} |
2 | 1002 |
|
1003 |
/** |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1004 |
* Maps the specified key to the specified value in this table. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1005 |
* Neither the key nor the value can be null. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1006 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1007 |
* <p>The value can be retrieved by calling the {@code get} method |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1008 |
* with a key that is equal to the original key. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1009 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1010 |
* @param key key with which the specified value is to be associated |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1011 |
* @param value value to be associated with the specified key |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1012 |
* @return the previous value associated with {@code key}, or |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1013 |
* {@code null} if there was no mapping for {@code key} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1014 |
* @throws NullPointerException if the specified key or value is null |
2 | 1015 |
*/ |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1016 |
public V put(K key, V value) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1017 |
return putVal(key, value, false); |
17945 | 1018 |
} |
1019 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1020 |
/** Implementation for put and putIfAbsent */ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1021 |
final V putVal(K key, V value, boolean onlyIfAbsent) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1022 |
if (key == null || value == null) throw new NullPointerException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1023 |
int hash = spread(key.hashCode()); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1024 |
int binCount = 0; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1025 |
for (Node<K,V>[] tab = table;;) { |
39779
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
1026 |
Node<K,V> f; int n, i, fh; K fk; V fv; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1027 |
if (tab == null || (n = tab.length) == 0) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1028 |
tab = initTable(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1029 |
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) { |
36958 | 1030 |
if (casTabAt(tab, i, null, new Node<K,V>(hash, key, value))) |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1031 |
break; // no lock when adding to empty bin |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1032 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1033 |
else if ((fh = f.hash) == MOVED) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1034 |
tab = helpTransfer(tab, f); |
44588
fd9d4d384425
8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents:
40547
diff
changeset
|
1035 |
else if (onlyIfAbsent // check first node without acquiring lock |
fd9d4d384425
8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents:
40547
diff
changeset
|
1036 |
&& fh == hash |
fd9d4d384425
8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents:
40547
diff
changeset
|
1037 |
&& ((fk = f.key) == key || (fk != null && key.equals(fk))) |
fd9d4d384425
8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents:
40547
diff
changeset
|
1038 |
&& (fv = f.val) != null) |
39779
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
1039 |
return fv; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1040 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1041 |
V oldVal = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1042 |
synchronized (f) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1043 |
if (tabAt(tab, i) == f) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1044 |
if (fh >= 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1045 |
binCount = 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1046 |
for (Node<K,V> e = f;; ++binCount) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1047 |
K ek; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1048 |
if (e.hash == hash && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1049 |
((ek = e.key) == key || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1050 |
(ek != null && key.equals(ek)))) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1051 |
oldVal = e.val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1052 |
if (!onlyIfAbsent) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1053 |
e.val = value; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1054 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1055 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1056 |
Node<K,V> pred = e; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1057 |
if ((e = e.next) == null) { |
36958 | 1058 |
pred.next = new Node<K,V>(hash, key, value); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1059 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1060 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1061 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1062 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1063 |
else if (f instanceof TreeBin) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1064 |
Node<K,V> p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1065 |
binCount = 2; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1066 |
if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1067 |
value)) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1068 |
oldVal = p.val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1069 |
if (!onlyIfAbsent) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1070 |
p.val = value; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1071 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1072 |
} |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1073 |
else if (f instanceof ReservationNode) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1074 |
throw new IllegalStateException("Recursive update"); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1075 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1076 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1077 |
if (binCount != 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1078 |
if (binCount >= TREEIFY_THRESHOLD) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1079 |
treeifyBin(tab, i); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1080 |
if (oldVal != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1081 |
return oldVal; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1082 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1083 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1084 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1085 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1086 |
addCount(1L, binCount); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1087 |
return null; |
17945 | 1088 |
} |
1089 |
||
1090 |
/** |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1091 |
* Copies all of the mappings from the specified map to this one. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1092 |
* These mappings replace any mappings that this map had for any of the |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1093 |
* keys currently in the specified map. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1094 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1095 |
* @param m mappings to be stored in this map |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1096 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1097 |
public void putAll(Map<? extends K, ? extends V> m) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1098 |
tryPresize(m.size()); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1099 |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1100 |
putVal(e.getKey(), e.getValue(), false); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1101 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1102 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1103 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1104 |
* Removes the key (and its corresponding value) from this map. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1105 |
* This method does nothing if the key is not in the map. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1106 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1107 |
* @param key the key that needs to be removed |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1108 |
* @return the previous value associated with {@code key}, or |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1109 |
* {@code null} if there was no mapping for {@code key} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1110 |
* @throws NullPointerException if the specified key is null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1111 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1112 |
public V remove(Object key) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1113 |
return replaceNode(key, null, null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1114 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1115 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1116 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1117 |
* Implementation for the four public remove/replace methods: |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1118 |
* Replaces node value with v, conditional upon match of cv if |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1119 |
* non-null. If resulting value is null, delete. |
17945 | 1120 |
*/ |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1121 |
final V replaceNode(Object key, V value, Object cv) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1122 |
int hash = spread(key.hashCode()); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1123 |
for (Node<K,V>[] tab = table;;) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1124 |
Node<K,V> f; int n, i, fh; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1125 |
if (tab == null || (n = tab.length) == 0 || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1126 |
(f = tabAt(tab, i = (n - 1) & hash)) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1127 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1128 |
else if ((fh = f.hash) == MOVED) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1129 |
tab = helpTransfer(tab, f); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1130 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1131 |
V oldVal = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1132 |
boolean validated = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1133 |
synchronized (f) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1134 |
if (tabAt(tab, i) == f) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1135 |
if (fh >= 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1136 |
validated = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1137 |
for (Node<K,V> e = f, pred = null;;) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1138 |
K ek; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1139 |
if (e.hash == hash && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1140 |
((ek = e.key) == key || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1141 |
(ek != null && key.equals(ek)))) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1142 |
V ev = e.val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1143 |
if (cv == null || cv == ev || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1144 |
(ev != null && cv.equals(ev))) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1145 |
oldVal = ev; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1146 |
if (value != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1147 |
e.val = value; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1148 |
else if (pred != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1149 |
pred.next = e.next; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1150 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1151 |
setTabAt(tab, i, e.next); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1152 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1153 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1154 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1155 |
pred = e; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1156 |
if ((e = e.next) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1157 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1158 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1159 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1160 |
else if (f instanceof TreeBin) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1161 |
validated = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1162 |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1163 |
TreeNode<K,V> r, p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1164 |
if ((r = t.root) != null && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1165 |
(p = r.findTreeNode(hash, key, null)) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1166 |
V pv = p.val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1167 |
if (cv == null || cv == pv || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1168 |
(pv != null && cv.equals(pv))) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1169 |
oldVal = pv; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1170 |
if (value != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1171 |
p.val = value; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1172 |
else if (t.removeTreeNode(p)) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1173 |
setTabAt(tab, i, untreeify(t.first)); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1174 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1175 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1176 |
} |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1177 |
else if (f instanceof ReservationNode) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1178 |
throw new IllegalStateException("Recursive update"); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1179 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1180 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1181 |
if (validated) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1182 |
if (oldVal != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1183 |
if (value == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1184 |
addCount(-1L, -1); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1185 |
return oldVal; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1186 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1187 |
break; |
17945 | 1188 |
} |
1189 |
} |
|
1190 |
} |
|
1191 |
return null; |
|
1192 |
} |
|
2 | 1193 |
|
1194 |
/** |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1195 |
* Removes all of the mappings from this map. |
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
1196 |
*/ |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1197 |
public void clear() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1198 |
long delta = 0L; // negative number of deletions |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1199 |
int i = 0; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1200 |
Node<K,V>[] tab = table; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1201 |
while (tab != null && i < tab.length) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1202 |
int fh; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1203 |
Node<K,V> f = tabAt(tab, i); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1204 |
if (f == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1205 |
++i; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1206 |
else if ((fh = f.hash) == MOVED) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1207 |
tab = helpTransfer(tab, f); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1208 |
i = 0; // restart |
17945 | 1209 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1210 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1211 |
synchronized (f) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1212 |
if (tabAt(tab, i) == f) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1213 |
Node<K,V> p = (fh >= 0 ? f : |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1214 |
(f instanceof TreeBin) ? |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1215 |
((TreeBin<K,V>)f).first : null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1216 |
while (p != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1217 |
--delta; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1218 |
p = p.next; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1219 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1220 |
setTabAt(tab, i++, null); |
17945 | 1221 |
} |
1222 |
} |
|
1223 |
} |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1224 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1225 |
if (delta != 0L) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1226 |
addCount(delta, -1); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1227 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1228 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1229 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1230 |
* Returns a {@link Set} view of the keys contained in this map. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1231 |
* The set is backed by the map, so changes to the map are |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1232 |
* reflected in the set, and vice-versa. The set supports element |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1233 |
* removal, which removes the corresponding mapping from this map, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1234 |
* via the {@code Iterator.remove}, {@code Set.remove}, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1235 |
* {@code removeAll}, {@code retainAll}, and {@code clear} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1236 |
* operations. It does not support the {@code add} or |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1237 |
* {@code addAll} operations. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1238 |
* |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1239 |
* <p>The view's iterators and spliterators are |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1240 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1241 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1242 |
* <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT}, |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1243 |
* {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}. |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1244 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1245 |
* @return the set view |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1246 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1247 |
public KeySetView<K,V> keySet() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1248 |
KeySetView<K,V> ks; |
38551
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36958
diff
changeset
|
1249 |
if ((ks = keySet) != null) return ks; |
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36958
diff
changeset
|
1250 |
return keySet = new KeySetView<K,V>(this, null); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1251 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1252 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1253 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1254 |
* Returns a {@link Collection} view of the values contained in this map. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1255 |
* The collection is backed by the map, so changes to the map are |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1256 |
* reflected in the collection, and vice-versa. The collection |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1257 |
* supports element removal, which removes the corresponding |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1258 |
* mapping from this map, via the {@code Iterator.remove}, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1259 |
* {@code Collection.remove}, {@code removeAll}, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1260 |
* {@code retainAll}, and {@code clear} operations. It does not |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1261 |
* support the {@code add} or {@code addAll} operations. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1262 |
* |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1263 |
* <p>The view's iterators and spliterators are |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1264 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1265 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1266 |
* <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT} |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1267 |
* and {@link Spliterator#NONNULL}. |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1268 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1269 |
* @return the collection view |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1270 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1271 |
public Collection<V> values() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1272 |
ValuesView<K,V> vs; |
38551
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36958
diff
changeset
|
1273 |
if ((vs = values) != null) return vs; |
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36958
diff
changeset
|
1274 |
return values = new ValuesView<K,V>(this); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1275 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1276 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1277 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1278 |
* Returns a {@link Set} view of the mappings contained in this map. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1279 |
* The set is backed by the map, so changes to the map are |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1280 |
* reflected in the set, and vice-versa. The set supports element |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1281 |
* removal, which removes the corresponding mapping from the map, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1282 |
* via the {@code Iterator.remove}, {@code Set.remove}, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1283 |
* {@code removeAll}, {@code retainAll}, and {@code clear} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1284 |
* operations. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1285 |
* |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1286 |
* <p>The view's iterators and spliterators are |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1287 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1288 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1289 |
* <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT}, |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
1290 |
* {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}. |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1291 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1292 |
* @return the set view |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1293 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1294 |
public Set<Map.Entry<K,V>> entrySet() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1295 |
EntrySetView<K,V> es; |
38551
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36958
diff
changeset
|
1296 |
if ((es = entrySet) != null) return es; |
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36958
diff
changeset
|
1297 |
return entrySet = new EntrySetView<K,V>(this); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1298 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1299 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1300 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1301 |
* Returns the hash code value for this {@link Map}, i.e., |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1302 |
* the sum of, for each key-value pair in the map, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1303 |
* {@code key.hashCode() ^ value.hashCode()}. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1304 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1305 |
* @return the hash code value for this map |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1306 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1307 |
public int hashCode() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1308 |
int h = 0; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1309 |
Node<K,V>[] t; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1310 |
if ((t = table) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1311 |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1312 |
for (Node<K,V> p; (p = it.advance()) != null; ) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1313 |
h += p.key.hashCode() ^ p.val.hashCode(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1314 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1315 |
return h; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1316 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1317 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1318 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1319 |
* Returns a string representation of this map. The string |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1320 |
* representation consists of a list of key-value mappings (in no |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1321 |
* particular order) enclosed in braces ("{@code {}}"). Adjacent |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1322 |
* mappings are separated by the characters {@code ", "} (comma |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1323 |
* and space). Each key-value mapping is rendered as the key |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1324 |
* followed by an equals sign ("{@code =}") followed by the |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1325 |
* associated value. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1326 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1327 |
* @return a string representation of this map |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1328 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1329 |
public String toString() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1330 |
Node<K,V>[] t; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1331 |
int f = (t = table) == null ? 0 : t.length; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1332 |
Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1333 |
StringBuilder sb = new StringBuilder(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1334 |
sb.append('{'); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1335 |
Node<K,V> p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1336 |
if ((p = it.advance()) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1337 |
for (;;) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1338 |
K k = p.key; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1339 |
V v = p.val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1340 |
sb.append(k == this ? "(this Map)" : k); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1341 |
sb.append('='); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1342 |
sb.append(v == this ? "(this Map)" : v); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1343 |
if ((p = it.advance()) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1344 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1345 |
sb.append(',').append(' '); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1346 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1347 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1348 |
return sb.append('}').toString(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1349 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1350 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1351 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1352 |
* Compares the specified object with this map for equality. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1353 |
* Returns {@code true} if the given object is a map with the same |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1354 |
* mappings as this map. This operation may return misleading |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1355 |
* results if either map is concurrently modified during execution |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1356 |
* of this method. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1357 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1358 |
* @param o object to be compared for equality with this map |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1359 |
* @return {@code true} if the specified object is equal to this map |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1360 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1361 |
public boolean equals(Object o) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1362 |
if (o != this) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1363 |
if (!(o instanceof Map)) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1364 |
return false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1365 |
Map<?,?> m = (Map<?,?>) o; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1366 |
Node<K,V>[] t; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1367 |
int f = (t = table) == null ? 0 : t.length; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1368 |
Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1369 |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1370 |
V val = p.val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1371 |
Object v = m.get(p.key); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1372 |
if (v == null || (v != val && !v.equals(val))) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1373 |
return false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1374 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1375 |
for (Map.Entry<?,?> e : m.entrySet()) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1376 |
Object mk, mv, v; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1377 |
if ((mk = e.getKey()) == null || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1378 |
(mv = e.getValue()) == null || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1379 |
(v = get(mk)) == null || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1380 |
(mv != v && !mv.equals(v))) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1381 |
return false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1382 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1383 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1384 |
return true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1385 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1386 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1387 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1388 |
* Stripped-down version of helper class used in previous version, |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1389 |
* declared for the sake of serialization compatibility. |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1390 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1391 |
static class Segment<K,V> extends ReentrantLock implements Serializable { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1392 |
private static final long serialVersionUID = 2249069246763182397L; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1393 |
final float loadFactor; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1394 |
Segment(float lf) { this.loadFactor = lf; } |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1395 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1396 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1397 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1398 |
* Saves the state of the {@code ConcurrentHashMap} instance to a |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1399 |
* stream (i.e., serializes it). |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1400 |
* @param s the stream |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
1401 |
* @throws java.io.IOException if an I/O error occurs |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1402 |
* @serialData |
34340
3b7a5a01c627
8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents:
33674
diff
changeset
|
1403 |
* the serialized fields, followed by the key (Object) and value |
3b7a5a01c627
8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents:
33674
diff
changeset
|
1404 |
* (Object) for each key-value mapping, followed by a null pair. |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1405 |
* The key-value mappings are emitted in no particular order. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1406 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1407 |
private void writeObject(java.io.ObjectOutputStream s) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1408 |
throws java.io.IOException { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1409 |
// For serialization compatibility |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1410 |
// Emulate segment calculation from previous version of this class |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1411 |
int sshift = 0; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1412 |
int ssize = 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1413 |
while (ssize < DEFAULT_CONCURRENCY_LEVEL) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1414 |
++sshift; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1415 |
ssize <<= 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1416 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1417 |
int segmentShift = 32 - sshift; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1418 |
int segmentMask = ssize - 1; |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
1419 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
1420 |
Segment<K,V>[] segments = (Segment<K,V>[]) |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1421 |
new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL]; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1422 |
for (int i = 0; i < segments.length; ++i) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1423 |
segments[i] = new Segment<K,V>(LOAD_FACTOR); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1424 |
java.io.ObjectOutputStream.PutField streamFields = s.putFields(); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1425 |
streamFields.put("segments", segments); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1426 |
streamFields.put("segmentShift", segmentShift); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1427 |
streamFields.put("segmentMask", segmentMask); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1428 |
s.writeFields(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1429 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1430 |
Node<K,V>[] t; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1431 |
if ((t = table) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1432 |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1433 |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1434 |
s.writeObject(p.key); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1435 |
s.writeObject(p.val); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1436 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1437 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1438 |
s.writeObject(null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1439 |
s.writeObject(null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1440 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1441 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1442 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1443 |
* Reconstitutes the instance from a stream (that is, deserializes it). |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1444 |
* @param s the stream |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
1445 |
* @throws ClassNotFoundException if the class of a serialized object |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
1446 |
* could not be found |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
1447 |
* @throws java.io.IOException if an I/O error occurs |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1448 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1449 |
private void readObject(java.io.ObjectInputStream s) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1450 |
throws java.io.IOException, ClassNotFoundException { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1451 |
/* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1452 |
* To improve performance in typical cases, we create nodes |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1453 |
* while reading, then place in table once size is known. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1454 |
* However, we must also validate uniqueness and deal with |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1455 |
* overpopulated bins while doing so, which requires |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1456 |
* specialized versions of putVal mechanics. |
17945 | 1457 |
*/ |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1458 |
sizeCtl = -1; // force exclusion for table construction |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1459 |
s.defaultReadObject(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1460 |
long size = 0L; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1461 |
Node<K,V> p = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1462 |
for (;;) { |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
1463 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
1464 |
K k = (K) s.readObject(); |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
1465 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
1466 |
V v = (V) s.readObject(); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1467 |
if (k != null && v != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1468 |
p = new Node<K,V>(spread(k.hashCode()), k, v, p); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1469 |
++size; |
17945 | 1470 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1471 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1472 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1473 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1474 |
if (size == 0L) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1475 |
sizeCtl = 0; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1476 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1477 |
int n; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1478 |
if (size >= (long)(MAXIMUM_CAPACITY >>> 1)) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1479 |
n = MAXIMUM_CAPACITY; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1480 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1481 |
int sz = (int)size; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1482 |
n = tableSizeFor(sz + (sz >>> 1) + 1); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1483 |
} |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
1484 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
1485 |
Node<K,V>[] tab = (Node<K,V>[])new Node<?,?>[n]; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1486 |
int mask = n - 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1487 |
long added = 0L; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1488 |
while (p != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1489 |
boolean insertAtFront; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1490 |
Node<K,V> next = p.next, first; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1491 |
int h = p.hash, j = h & mask; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1492 |
if ((first = tabAt(tab, j)) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1493 |
insertAtFront = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1494 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1495 |
K k = p.key; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1496 |
if (first.hash < 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1497 |
TreeBin<K,V> t = (TreeBin<K,V>)first; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1498 |
if (t.putTreeVal(h, k, p.val) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1499 |
++added; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1500 |
insertAtFront = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1501 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1502 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1503 |
int binCount = 0; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1504 |
insertAtFront = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1505 |
Node<K,V> q; K qk; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1506 |
for (q = first; q != null; q = q.next) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1507 |
if (q.hash == h && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1508 |
((qk = q.key) == k || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1509 |
(qk != null && k.equals(qk)))) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1510 |
insertAtFront = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1511 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1512 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1513 |
++binCount; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1514 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1515 |
if (insertAtFront && binCount >= TREEIFY_THRESHOLD) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1516 |
insertAtFront = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1517 |
++added; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1518 |
p.next = first; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1519 |
TreeNode<K,V> hd = null, tl = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1520 |
for (q = p; q != null; q = q.next) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1521 |
TreeNode<K,V> t = new TreeNode<K,V> |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1522 |
(q.hash, q.key, q.val, null, null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1523 |
if ((t.prev = tl) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1524 |
hd = t; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1525 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1526 |
tl.next = t; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1527 |
tl = t; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1528 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1529 |
setTabAt(tab, j, new TreeBin<K,V>(hd)); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1530 |
} |
17945 | 1531 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1532 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1533 |
if (insertAtFront) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1534 |
++added; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1535 |
p.next = first; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1536 |
setTabAt(tab, j, p); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1537 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1538 |
p = next; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1539 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1540 |
table = tab; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1541 |
sizeCtl = n - (n >>> 2); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1542 |
baseCount = added; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1543 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1544 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1545 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1546 |
// ConcurrentMap methods |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1547 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1548 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1549 |
* {@inheritDoc} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1550 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1551 |
* @return the previous value associated with the specified key, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1552 |
* or {@code null} if there was no mapping for the key |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1553 |
* @throws NullPointerException if the specified key or value is null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1554 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1555 |
public V putIfAbsent(K key, V value) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1556 |
return putVal(key, value, true); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1557 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1558 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1559 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1560 |
* {@inheritDoc} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1561 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1562 |
* @throws NullPointerException if the specified key is null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1563 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1564 |
public boolean remove(Object key, Object value) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1565 |
if (key == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1566 |
throw new NullPointerException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1567 |
return value != null && replaceNode(key, null, value) != null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1568 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1569 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1570 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1571 |
* {@inheritDoc} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1572 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1573 |
* @throws NullPointerException if any of the arguments are null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1574 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1575 |
public boolean replace(K key, V oldValue, V newValue) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1576 |
if (key == null || oldValue == null || newValue == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1577 |
throw new NullPointerException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1578 |
return replaceNode(key, newValue, oldValue) != null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1579 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1580 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1581 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1582 |
* {@inheritDoc} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1583 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1584 |
* @return the previous value associated with the specified key, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1585 |
* or {@code null} if there was no mapping for the key |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1586 |
* @throws NullPointerException if the specified key or value is null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1587 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1588 |
public V replace(K key, V value) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1589 |
if (key == null || value == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1590 |
throw new NullPointerException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1591 |
return replaceNode(key, value, null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1592 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1593 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1594 |
// Overrides of JDK8+ Map extension method defaults |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1595 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1596 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1597 |
* Returns the value to which the specified key is mapped, or the |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1598 |
* given default value if this map contains no mapping for the |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1599 |
* key. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1600 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1601 |
* @param key the key whose associated value is to be returned |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1602 |
* @param defaultValue the value to return if this map contains |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1603 |
* no mapping for the given key |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1604 |
* @return the mapping for the key, if present; else the default value |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1605 |
* @throws NullPointerException if the specified key is null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1606 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1607 |
public V getOrDefault(Object key, V defaultValue) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1608 |
V v; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1609 |
return (v = get(key)) == null ? defaultValue : v; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1610 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1611 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1612 |
public void forEach(BiConsumer<? super K, ? super V> action) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1613 |
if (action == null) throw new NullPointerException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1614 |
Node<K,V>[] t; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1615 |
if ((t = table) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1616 |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1617 |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1618 |
action.accept(p.key, p.val); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1619 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1620 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1621 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1622 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1623 |
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1624 |
if (function == null) throw new NullPointerException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1625 |
Node<K,V>[] t; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1626 |
if ((t = table) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1627 |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1628 |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1629 |
V oldValue = p.val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1630 |
for (K key = p.key;;) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1631 |
V newValue = function.apply(key, oldValue); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1632 |
if (newValue == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1633 |
throw new NullPointerException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1634 |
if (replaceNode(key, newValue, oldValue) != null || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1635 |
(oldValue = get(key)) == null) |
17945 | 1636 |
break; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1637 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1638 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1639 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1640 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1641 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1642 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1643 |
* Helper method for EntrySetView.removeIf. |
30441
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1644 |
*/ |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1645 |
boolean removeEntryIf(Predicate<? super Entry<K,V>> function) { |
30441
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1646 |
if (function == null) throw new NullPointerException(); |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1647 |
Node<K,V>[] t; |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1648 |
boolean removed = false; |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1649 |
if ((t = table) != null) { |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1650 |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1651 |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1652 |
K k = p.key; |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1653 |
V v = p.val; |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1654 |
Map.Entry<K,V> e = new AbstractMap.SimpleImmutableEntry<>(k, v); |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1655 |
if (function.test(e) && replaceNode(k, null, v) != null) |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1656 |
removed = true; |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1657 |
} |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1658 |
} |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1659 |
return removed; |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1660 |
} |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1661 |
|
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1662 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1663 |
* Helper method for ValuesView.removeIf. |
30441
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1664 |
*/ |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1665 |
boolean removeValueIf(Predicate<? super V> function) { |
30441
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1666 |
if (function == null) throw new NullPointerException(); |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1667 |
Node<K,V>[] t; |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1668 |
boolean removed = false; |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1669 |
if ((t = table) != null) { |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1670 |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1671 |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1672 |
K k = p.key; |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1673 |
V v = p.val; |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1674 |
if (function.test(v) && replaceNode(k, null, v) != null) |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1675 |
removed = true; |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1676 |
} |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1677 |
} |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1678 |
return removed; |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1679 |
} |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1680 |
|
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
1681 |
/** |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1682 |
* If the specified key is not already associated with a value, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1683 |
* attempts to compute its value using the given mapping function |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1684 |
* and enters it into this map unless {@code null}. The entire |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1685 |
* method invocation is performed atomically, so the function is |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1686 |
* applied at most once per key. Some attempted update operations |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1687 |
* on this map by other threads may be blocked while computation |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1688 |
* is in progress, so the computation should be short and simple, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1689 |
* and must not attempt to update any other mappings of this map. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1690 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1691 |
* @param key key with which the specified value is to be associated |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1692 |
* @param mappingFunction the function to compute a value |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1693 |
* @return the current (existing or computed) value associated with |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1694 |
* the specified key, or null if the computed value is null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1695 |
* @throws NullPointerException if the specified key or mappingFunction |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1696 |
* is null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1697 |
* @throws IllegalStateException if the computation detectably |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1698 |
* attempts a recursive update to this map that would |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1699 |
* otherwise never complete |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1700 |
* @throws RuntimeException or Error if the mappingFunction does so, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1701 |
* in which case the mapping is left unestablished |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1702 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1703 |
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1704 |
if (key == null || mappingFunction == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1705 |
throw new NullPointerException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1706 |
int h = spread(key.hashCode()); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1707 |
V val = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1708 |
int binCount = 0; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1709 |
for (Node<K,V>[] tab = table;;) { |
39779
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
1710 |
Node<K,V> f; int n, i, fh; K fk; V fv; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1711 |
if (tab == null || (n = tab.length) == 0) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1712 |
tab = initTable(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1713 |
else if ((f = tabAt(tab, i = (n - 1) & h)) == null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1714 |
Node<K,V> r = new ReservationNode<K,V>(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1715 |
synchronized (r) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1716 |
if (casTabAt(tab, i, null, r)) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1717 |
binCount = 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1718 |
Node<K,V> node = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1719 |
try { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1720 |
if ((val = mappingFunction.apply(key)) != null) |
36958 | 1721 |
node = new Node<K,V>(h, key, val); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1722 |
} finally { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1723 |
setTabAt(tab, i, node); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1724 |
} |
17945 | 1725 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1726 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1727 |
if (binCount != 0) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1728 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1729 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1730 |
else if ((fh = f.hash) == MOVED) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1731 |
tab = helpTransfer(tab, f); |
44588
fd9d4d384425
8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents:
40547
diff
changeset
|
1732 |
else if (fh == h // check first node without acquiring lock |
fd9d4d384425
8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents:
40547
diff
changeset
|
1733 |
&& ((fk = f.key) == key || (fk != null && key.equals(fk))) |
fd9d4d384425
8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents:
40547
diff
changeset
|
1734 |
&& (fv = f.val) != null) |
39779
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
1735 |
return fv; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1736 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1737 |
boolean added = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1738 |
synchronized (f) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1739 |
if (tabAt(tab, i) == f) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1740 |
if (fh >= 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1741 |
binCount = 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1742 |
for (Node<K,V> e = f;; ++binCount) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1743 |
K ek; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1744 |
if (e.hash == h && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1745 |
((ek = e.key) == key || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1746 |
(ek != null && key.equals(ek)))) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1747 |
val = e.val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1748 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1749 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1750 |
Node<K,V> pred = e; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1751 |
if ((e = e.next) == null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1752 |
if ((val = mappingFunction.apply(key)) != null) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1753 |
if (pred.next != null) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1754 |
throw new IllegalStateException("Recursive update"); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1755 |
added = true; |
36958 | 1756 |
pred.next = new Node<K,V>(h, key, val); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1757 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1758 |
break; |
17945 | 1759 |
} |
1760 |
} |
|
1761 |
} |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1762 |
else if (f instanceof TreeBin) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1763 |
binCount = 2; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1764 |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1765 |
TreeNode<K,V> r, p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1766 |
if ((r = t.root) != null && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1767 |
(p = r.findTreeNode(h, key, null)) != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1768 |
val = p.val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1769 |
else if ((val = mappingFunction.apply(key)) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1770 |
added = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1771 |
t.putTreeVal(h, key, val); |
17945 | 1772 |
} |
1773 |
} |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1774 |
else if (f instanceof ReservationNode) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1775 |
throw new IllegalStateException("Recursive update"); |
17945 | 1776 |
} |
1777 |
} |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1778 |
if (binCount != 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1779 |
if (binCount >= TREEIFY_THRESHOLD) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1780 |
treeifyBin(tab, i); |
17945 | 1781 |
if (!added) |
1782 |
return val; |
|
1783 |
break; |
|
1784 |
} |
|
1785 |
} |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
1786 |
} |
17945 | 1787 |
if (val != null) |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1788 |
addCount(1L, binCount); |
17945 | 1789 |
return val; |
1790 |
} |
|
1791 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1792 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1793 |
* If the value for the specified key is present, attempts to |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1794 |
* compute a new mapping given the key and its current mapped |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1795 |
* value. The entire method invocation is performed atomically. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1796 |
* Some attempted update operations on this map by other threads |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1797 |
* may be blocked while computation is in progress, so the |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1798 |
* computation should be short and simple, and must not attempt to |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1799 |
* update any other mappings of this map. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1800 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1801 |
* @param key key with which a value may be associated |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1802 |
* @param remappingFunction the function to compute a value |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1803 |
* @return the new value associated with the specified key, or null if none |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1804 |
* @throws NullPointerException if the specified key or remappingFunction |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1805 |
* is null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1806 |
* @throws IllegalStateException if the computation detectably |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1807 |
* attempts a recursive update to this map that would |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1808 |
* otherwise never complete |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1809 |
* @throws RuntimeException or Error if the remappingFunction does so, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1810 |
* in which case the mapping is unchanged |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1811 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1812 |
public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1813 |
if (key == null || remappingFunction == null) |
17945 | 1814 |
throw new NullPointerException(); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1815 |
int h = spread(key.hashCode()); |
17945 | 1816 |
V val = null; |
1817 |
int delta = 0; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1818 |
int binCount = 0; |
17945 | 1819 |
for (Node<K,V>[] tab = table;;) { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1820 |
Node<K,V> f; int n, i, fh; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1821 |
if (tab == null || (n = tab.length) == 0) |
17945 | 1822 |
tab = initTable(); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1823 |
else if ((f = tabAt(tab, i = (n - 1) & h)) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1824 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1825 |
else if ((fh = f.hash) == MOVED) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1826 |
tab = helpTransfer(tab, f); |
17945 | 1827 |
else { |
1828 |
synchronized (f) { |
|
1829 |
if (tabAt(tab, i) == f) { |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1830 |
if (fh >= 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1831 |
binCount = 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1832 |
for (Node<K,V> e = f, pred = null;; ++binCount) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1833 |
K ek; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1834 |
if (e.hash == h && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1835 |
((ek = e.key) == key || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1836 |
(ek != null && key.equals(ek)))) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1837 |
val = remappingFunction.apply(key, e.val); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1838 |
if (val != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1839 |
e.val = val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1840 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1841 |
delta = -1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1842 |
Node<K,V> en = e.next; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1843 |
if (pred != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1844 |
pred.next = en; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1845 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1846 |
setTabAt(tab, i, en); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1847 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1848 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1849 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1850 |
pred = e; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1851 |
if ((e = e.next) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1852 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1853 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1854 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1855 |
else if (f instanceof TreeBin) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1856 |
binCount = 2; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1857 |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1858 |
TreeNode<K,V> r, p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1859 |
if ((r = t.root) != null && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1860 |
(p = r.findTreeNode(h, key, null)) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1861 |
val = remappingFunction.apply(key, p.val); |
17945 | 1862 |
if (val != null) |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1863 |
p.val = val; |
17945 | 1864 |
else { |
1865 |
delta = -1; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1866 |
if (t.removeTreeNode(p)) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1867 |
setTabAt(tab, i, untreeify(t.first)); |
17945 | 1868 |
} |
1869 |
} |
|
1870 |
} |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1871 |
else if (f instanceof ReservationNode) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1872 |
throw new IllegalStateException("Recursive update"); |
17945 | 1873 |
} |
1874 |
} |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1875 |
if (binCount != 0) |
17945 | 1876 |
break; |
1877 |
} |
|
1878 |
} |
|
1879 |
if (delta != 0) |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1880 |
addCount((long)delta, binCount); |
17945 | 1881 |
return val; |
1882 |
} |
|
1883 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1884 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1885 |
* Attempts to compute a mapping for the specified key and its |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1886 |
* current mapped value (or {@code null} if there is no current |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1887 |
* mapping). The entire method invocation is performed atomically. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1888 |
* Some attempted update operations on this map by other threads |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1889 |
* may be blocked while computation is in progress, so the |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1890 |
* computation should be short and simple, and must not attempt to |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1891 |
* update any other mappings of this Map. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1892 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1893 |
* @param key key with which the specified value is to be associated |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1894 |
* @param remappingFunction the function to compute a value |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1895 |
* @return the new value associated with the specified key, or null if none |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1896 |
* @throws NullPointerException if the specified key or remappingFunction |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1897 |
* is null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1898 |
* @throws IllegalStateException if the computation detectably |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1899 |
* attempts a recursive update to this map that would |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1900 |
* otherwise never complete |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1901 |
* @throws RuntimeException or Error if the remappingFunction does so, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1902 |
* in which case the mapping is unchanged |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1903 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1904 |
public V compute(K key, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1905 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1906 |
if (key == null || remappingFunction == null) |
17945 | 1907 |
throw new NullPointerException(); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1908 |
int h = spread(key.hashCode()); |
17945 | 1909 |
V val = null; |
1910 |
int delta = 0; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1911 |
int binCount = 0; |
17945 | 1912 |
for (Node<K,V>[] tab = table;;) { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1913 |
Node<K,V> f; int n, i, fh; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1914 |
if (tab == null || (n = tab.length) == 0) |
17945 | 1915 |
tab = initTable(); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1916 |
else if ((f = tabAt(tab, i = (n - 1) & h)) == null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1917 |
Node<K,V> r = new ReservationNode<K,V>(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1918 |
synchronized (r) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1919 |
if (casTabAt(tab, i, null, r)) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1920 |
binCount = 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1921 |
Node<K,V> node = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1922 |
try { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1923 |
if ((val = remappingFunction.apply(key, null)) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1924 |
delta = 1; |
36958 | 1925 |
node = new Node<K,V>(h, key, val); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1926 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1927 |
} finally { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1928 |
setTabAt(tab, i, node); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1929 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1930 |
} |
17945 | 1931 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1932 |
if (binCount != 0) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1933 |
break; |
17945 | 1934 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1935 |
else if ((fh = f.hash) == MOVED) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1936 |
tab = helpTransfer(tab, f); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1937 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1938 |
synchronized (f) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1939 |
if (tabAt(tab, i) == f) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1940 |
if (fh >= 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1941 |
binCount = 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1942 |
for (Node<K,V> e = f, pred = null;; ++binCount) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1943 |
K ek; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1944 |
if (e.hash == h && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1945 |
((ek = e.key) == key || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1946 |
(ek != null && key.equals(ek)))) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1947 |
val = remappingFunction.apply(key, e.val); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1948 |
if (val != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1949 |
e.val = val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1950 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1951 |
delta = -1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1952 |
Node<K,V> en = e.next; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1953 |
if (pred != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1954 |
pred.next = en; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1955 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1956 |
setTabAt(tab, i, en); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1957 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1958 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1959 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1960 |
pred = e; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1961 |
if ((e = e.next) == null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1962 |
val = remappingFunction.apply(key, null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1963 |
if (val != null) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1964 |
if (pred.next != null) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1965 |
throw new IllegalStateException("Recursive update"); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1966 |
delta = 1; |
36958 | 1967 |
pred.next = new Node<K,V>(h, key, val); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1968 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1969 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1970 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1971 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1972 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1973 |
else if (f instanceof TreeBin) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1974 |
binCount = 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1975 |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1976 |
TreeNode<K,V> r, p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1977 |
if ((r = t.root) != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1978 |
p = r.findTreeNode(h, key, null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1979 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1980 |
p = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1981 |
V pv = (p == null) ? null : p.val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1982 |
val = remappingFunction.apply(key, pv); |
17945 | 1983 |
if (val != null) { |
1984 |
if (p != null) |
|
1985 |
p.val = val; |
|
1986 |
else { |
|
1987 |
delta = 1; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1988 |
t.putTreeVal(h, key, val); |
17945 | 1989 |
} |
1990 |
} |
|
1991 |
else if (p != null) { |
|
1992 |
delta = -1; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1993 |
if (t.removeTreeNode(p)) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
1994 |
setTabAt(tab, i, untreeify(t.first)); |
17945 | 1995 |
} |
1996 |
} |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1997 |
else if (f instanceof ReservationNode) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
1998 |
throw new IllegalStateException("Recursive update"); |
17945 | 1999 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2000 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2001 |
if (binCount != 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2002 |
if (binCount >= TREEIFY_THRESHOLD) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2003 |
treeifyBin(tab, i); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2004 |
break; |
17945 | 2005 |
} |
2006 |
} |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2007 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2008 |
if (delta != 0) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2009 |
addCount((long)delta, binCount); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2010 |
return val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2011 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2012 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2013 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2014 |
* If the specified key is not already associated with a |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2015 |
* (non-null) value, associates it with the given value. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2016 |
* Otherwise, replaces the value with the results of the given |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2017 |
* remapping function, or removes if {@code null}. The entire |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2018 |
* method invocation is performed atomically. Some attempted |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2019 |
* update operations on this map by other threads may be blocked |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2020 |
* while computation is in progress, so the computation should be |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2021 |
* short and simple, and must not attempt to update any other |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2022 |
* mappings of this Map. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2023 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2024 |
* @param key key with which the specified value is to be associated |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2025 |
* @param value the value to use if absent |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2026 |
* @param remappingFunction the function to recompute a value if present |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2027 |
* @return the new value associated with the specified key, or null if none |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2028 |
* @throws NullPointerException if the specified key or the |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2029 |
* remappingFunction is null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2030 |
* @throws RuntimeException or Error if the remappingFunction does so, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2031 |
* in which case the mapping is unchanged |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2032 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2033 |
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2034 |
if (key == null || value == null || remappingFunction == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2035 |
throw new NullPointerException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2036 |
int h = spread(key.hashCode()); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2037 |
V val = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2038 |
int delta = 0; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2039 |
int binCount = 0; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2040 |
for (Node<K,V>[] tab = table;;) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2041 |
Node<K,V> f; int n, i, fh; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2042 |
if (tab == null || (n = tab.length) == 0) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2043 |
tab = initTable(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2044 |
else if ((f = tabAt(tab, i = (n - 1) & h)) == null) { |
36958 | 2045 |
if (casTabAt(tab, i, null, new Node<K,V>(h, key, value))) { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2046 |
delta = 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2047 |
val = value; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2048 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2049 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2050 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2051 |
else if ((fh = f.hash) == MOVED) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2052 |
tab = helpTransfer(tab, f); |
17945 | 2053 |
else { |
2054 |
synchronized (f) { |
|
2055 |
if (tabAt(tab, i) == f) { |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2056 |
if (fh >= 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2057 |
binCount = 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2058 |
for (Node<K,V> e = f, pred = null;; ++binCount) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2059 |
K ek; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2060 |
if (e.hash == h && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2061 |
((ek = e.key) == key || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2062 |
(ek != null && key.equals(ek)))) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2063 |
val = remappingFunction.apply(e.val, value); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2064 |
if (val != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2065 |
e.val = val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2066 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2067 |
delta = -1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2068 |
Node<K,V> en = e.next; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2069 |
if (pred != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2070 |
pred.next = en; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2071 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2072 |
setTabAt(tab, i, en); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2073 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2074 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2075 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2076 |
pred = e; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2077 |
if ((e = e.next) == null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2078 |
delta = 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2079 |
val = value; |
36958 | 2080 |
pred.next = new Node<K,V>(h, key, val); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2081 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2082 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2083 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2084 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2085 |
else if (f instanceof TreeBin) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2086 |
binCount = 2; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2087 |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2088 |
TreeNode<K,V> r = t.root; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2089 |
TreeNode<K,V> p = (r == null) ? null : |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2090 |
r.findTreeNode(h, key, null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2091 |
val = (p == null) ? value : |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2092 |
remappingFunction.apply(p.val, value); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2093 |
if (val != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2094 |
if (p != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2095 |
p.val = val; |
17945 | 2096 |
else { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2097 |
delta = 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2098 |
t.putTreeVal(h, key, val); |
17945 | 2099 |
} |
2100 |
} |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2101 |
else if (p != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2102 |
delta = -1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2103 |
if (t.removeTreeNode(p)) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2104 |
setTabAt(tab, i, untreeify(t.first)); |
17945 | 2105 |
} |
2106 |
} |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
2107 |
else if (f instanceof ReservationNode) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
2108 |
throw new IllegalStateException("Recursive update"); |
17945 | 2109 |
} |
2110 |
} |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2111 |
if (binCount != 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2112 |
if (binCount >= TREEIFY_THRESHOLD) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2113 |
treeifyBin(tab, i); |
17945 | 2114 |
break; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2115 |
} |
17945 | 2116 |
} |
2117 |
} |
|
2118 |
if (delta != 0) |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2119 |
addCount((long)delta, binCount); |
17945 | 2120 |
return val; |
2121 |
} |
|
2122 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2123 |
// Hashtable legacy methods |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2124 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2125 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
2126 |
* Tests if some key maps into the specified value in this table. |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
2127 |
* |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
2128 |
* <p>Note that this method is identical in functionality to |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2129 |
* {@link #containsValue(Object)}, and exists solely to ensure |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2130 |
* full compatibility with class {@link java.util.Hashtable}, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2131 |
* which supported this method prior to introduction of the |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
2132 |
* Java Collections Framework. |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2133 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2134 |
* @param value a value to search for |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2135 |
* @return {@code true} if and only if some key maps to the |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2136 |
* {@code value} argument in this table as |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2137 |
* determined by the {@code equals} method; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2138 |
* {@code false} otherwise |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2139 |
* @throws NullPointerException if the specified value is null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2140 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2141 |
public boolean contains(Object value) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2142 |
return containsValue(value); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2143 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2144 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2145 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2146 |
* Returns an enumeration of the keys in this table. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2147 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2148 |
* @return an enumeration of the keys in this table |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2149 |
* @see #keySet() |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2150 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2151 |
public Enumeration<K> keys() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2152 |
Node<K,V>[] t; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2153 |
int f = (t = table) == null ? 0 : t.length; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2154 |
return new KeyIterator<K,V>(t, f, 0, f, this); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2155 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2156 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2157 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2158 |
* Returns an enumeration of the values in this table. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2159 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2160 |
* @return an enumeration of the values in this table |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2161 |
* @see #values() |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2162 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2163 |
public Enumeration<V> elements() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2164 |
Node<K,V>[] t; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2165 |
int f = (t = table) == null ? 0 : t.length; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2166 |
return new ValueIterator<K,V>(t, f, 0, f, this); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2167 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2168 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2169 |
// ConcurrentHashMap-only methods |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2170 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2171 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2172 |
* Returns the number of mappings. This method should be used |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2173 |
* instead of {@link #size} because a ConcurrentHashMap may |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2174 |
* contain more mappings than can be represented as an int. The |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2175 |
* value returned is an estimate; the actual count may differ if |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2176 |
* there are concurrent insertions or removals. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2177 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2178 |
* @return the number of mappings |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2179 |
* @since 1.8 |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2180 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2181 |
public long mappingCount() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2182 |
long n = sumCount(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2183 |
return (n < 0L) ? 0L : n; // ignore transient negative values |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2184 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2185 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2186 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2187 |
* Creates a new {@link Set} backed by a ConcurrentHashMap |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2188 |
* from the given type to {@code Boolean.TRUE}. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2189 |
* |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2190 |
* @param <K> the element type of the returned set |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2191 |
* @return the new set |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2192 |
* @since 1.8 |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2193 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2194 |
public static <K> KeySetView<K,Boolean> newKeySet() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2195 |
return new KeySetView<K,Boolean> |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2196 |
(new ConcurrentHashMap<K,Boolean>(), Boolean.TRUE); |
17945 | 2197 |
} |
2198 |
||
2199 |
/** |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2200 |
* Creates a new {@link Set} backed by a ConcurrentHashMap |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2201 |
* from the given type to {@code Boolean.TRUE}. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2202 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2203 |
* @param initialCapacity The implementation performs internal |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2204 |
* sizing to accommodate this many elements. |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2205 |
* @param <K> the element type of the returned set |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2206 |
* @return the new set |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2207 |
* @throws IllegalArgumentException if the initial capacity of |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2208 |
* elements is negative |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2209 |
* @since 1.8 |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2210 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2211 |
public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2212 |
return new KeySetView<K,Boolean> |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2213 |
(new ConcurrentHashMap<K,Boolean>(initialCapacity), Boolean.TRUE); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2214 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2215 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2216 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2217 |
* Returns a {@link Set} view of the keys in this map, using the |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2218 |
* given common mapped value for any additions (i.e., {@link |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2219 |
* Collection#add} and {@link Collection#addAll(Collection)}). |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2220 |
* This is of course only appropriate if it is acceptable to use |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2221 |
* the same value for all additions from this view. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2222 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2223 |
* @param mappedValue the mapped value to use for any additions |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2224 |
* @return the set view |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2225 |
* @throws NullPointerException if the mappedValue is null |
17945 | 2226 |
*/ |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2227 |
public KeySetView<K,V> keySet(V mappedValue) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2228 |
if (mappedValue == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2229 |
throw new NullPointerException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2230 |
return new KeySetView<K,V>(this, mappedValue); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2231 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2232 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2233 |
/* ---------------- Special Nodes -------------- */ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2234 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2235 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2236 |
* A node inserted at head of bins during transfer operations. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2237 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2238 |
static final class ForwardingNode<K,V> extends Node<K,V> { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2239 |
final Node<K,V>[] nextTable; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2240 |
ForwardingNode(Node<K,V>[] tab) { |
36958 | 2241 |
super(MOVED, null, null); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2242 |
this.nextTable = tab; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2243 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2244 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2245 |
Node<K,V> find(int h, Object k) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2246 |
// loop to avoid arbitrarily deep recursion on forwarding nodes |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2247 |
outer: for (Node<K,V>[] tab = nextTable;;) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2248 |
Node<K,V> e; int n; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2249 |
if (k == null || tab == null || (n = tab.length) == 0 || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2250 |
(e = tabAt(tab, (n - 1) & h)) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2251 |
return null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2252 |
for (;;) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2253 |
int eh; K ek; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2254 |
if ((eh = e.hash) == h && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2255 |
((ek = e.key) == k || (ek != null && k.equals(ek)))) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2256 |
return e; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2257 |
if (eh < 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2258 |
if (e instanceof ForwardingNode) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2259 |
tab = ((ForwardingNode<K,V>)e).nextTable; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2260 |
continue outer; |
17945 | 2261 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2262 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2263 |
return e.find(h, k); |
17945 | 2264 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2265 |
if ((e = e.next) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2266 |
return null; |
17945 | 2267 |
} |
2268 |
} |
|
2269 |
} |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2270 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2271 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2272 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
2273 |
* A place-holder node used in computeIfAbsent and compute. |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2274 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2275 |
static final class ReservationNode<K,V> extends Node<K,V> { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2276 |
ReservationNode() { |
36958 | 2277 |
super(RESERVED, null, null); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2278 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2279 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2280 |
Node<K,V> find(int h, Object k) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2281 |
return null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2282 |
} |
17945 | 2283 |
} |
2284 |
||
2285 |
/* ---------------- Table Initialization and Resizing -------------- */ |
|
2286 |
||
2287 |
/** |
|
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2288 |
* Returns the stamp bits for resizing a table of size n. |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2289 |
* Must be negative when shifted left by RESIZE_STAMP_SHIFT. |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2290 |
*/ |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2291 |
static final int resizeStamp(int n) { |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2292 |
return Integer.numberOfLeadingZeros(n) | (1 << (RESIZE_STAMP_BITS - 1)); |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2293 |
} |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2294 |
|
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2295 |
/** |
17945 | 2296 |
* Initializes table, using the size recorded in sizeCtl. |
2297 |
*/ |
|
2298 |
private final Node<K,V>[] initTable() { |
|
2299 |
Node<K,V>[] tab; int sc; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2300 |
while ((tab = table) == null || tab.length == 0) { |
17945 | 2301 |
if ((sc = sizeCtl) < 0) |
2302 |
Thread.yield(); // lost initialization race; just spin |
|
2303 |
else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) { |
|
2304 |
try { |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2305 |
if ((tab = table) == null || tab.length == 0) { |
17945 | 2306 |
int n = (sc > 0) ? sc : DEFAULT_CAPACITY; |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2307 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2308 |
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n]; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2309 |
table = tab = nt; |
17945 | 2310 |
sc = n - (n >>> 2); |
2311 |
} |
|
2312 |
} finally { |
|
2313 |
sizeCtl = sc; |
|
2314 |
} |
|
2315 |
break; |
|
2316 |
} |
|
2317 |
} |
|
2318 |
return tab; |
|
2319 |
} |
|
2320 |
||
2321 |
/** |
|
2322 |
* Adds to count, and if table is too small and not already |
|
2323 |
* resizing, initiates transfer. If already resizing, helps |
|
2324 |
* perform transfer if work is available. Rechecks occupancy |
|
2325 |
* after a transfer to see if another resize is already needed |
|
2326 |
* because resizings are lagging additions. |
|
2327 |
* |
|
2328 |
* @param x the count to add |
|
2329 |
* @param check if <0, don't check resize, if <= 1 only check if uncontended |
|
2330 |
*/ |
|
2331 |
private final void addCount(long x, int check) { |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2332 |
CounterCell[] as; long b, s; |
17945 | 2333 |
if ((as = counterCells) != null || |
2334 |
!U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) { |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2335 |
CounterCell a; long v; int m; |
17945 | 2336 |
boolean uncontended = true; |
2337 |
if (as == null || (m = as.length - 1) < 0 || |
|
2338 |
(a = as[ThreadLocalRandom.getProbe() & m]) == null || |
|
2339 |
!(uncontended = |
|
2340 |
U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) { |
|
2341 |
fullAddCount(x, uncontended); |
|
2342 |
return; |
|
2343 |
} |
|
2344 |
if (check <= 1) |
|
2345 |
return; |
|
2346 |
s = sumCount(); |
|
2347 |
} |
|
2348 |
if (check >= 0) { |
|
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2349 |
Node<K,V>[] tab, nt; int n, sc; |
17945 | 2350 |
while (s >= (long)(sc = sizeCtl) && (tab = table) != null && |
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2351 |
(n = tab.length) < MAXIMUM_CAPACITY) { |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2352 |
int rs = resizeStamp(n); |
17945 | 2353 |
if (sc < 0) { |
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2354 |
if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 || |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2355 |
sc == rs + MAX_RESIZERS || (nt = nextTable) == null || |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2356 |
transferIndex <= 0) |
17945 | 2357 |
break; |
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2358 |
if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) |
17945 | 2359 |
transfer(tab, nt); |
2360 |
} |
|
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2361 |
else if (U.compareAndSwapInt(this, SIZECTL, sc, |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2362 |
(rs << RESIZE_STAMP_SHIFT) + 2)) |
17945 | 2363 |
transfer(tab, null); |
2364 |
s = sumCount(); |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
2365 |
} |
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
2366 |
} |
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
2367 |
} |
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
2368 |
|
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
2369 |
/** |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2370 |
* Helps transfer if a resize is in progress. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2371 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2372 |
final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2373 |
Node<K,V>[] nextTab; int sc; |
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2374 |
if (tab != null && (f instanceof ForwardingNode) && |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2375 |
(nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) { |
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2376 |
int rs = resizeStamp(tab.length); |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2377 |
while (nextTab == nextTable && table == tab && |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2378 |
(sc = sizeCtl) < 0) { |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2379 |
if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 || |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2380 |
sc == rs + MAX_RESIZERS || transferIndex <= 0) |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2381 |
break; |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2382 |
if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) { |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2383 |
transfer(tab, nextTab); |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2384 |
break; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2385 |
} |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2386 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2387 |
return nextTab; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2388 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2389 |
return table; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2390 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2391 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2392 |
/** |
17945 | 2393 |
* Tries to presize table to accommodate the given number of elements. |
2394 |
* |
|
2395 |
* @param size number of elements (doesn't need to be perfectly accurate) |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
2396 |
*/ |
17945 | 2397 |
private final void tryPresize(int size) { |
2398 |
int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY : |
|
2399 |
tableSizeFor(size + (size >>> 1) + 1); |
|
2400 |
int sc; |
|
2401 |
while ((sc = sizeCtl) >= 0) { |
|
2402 |
Node<K,V>[] tab = table; int n; |
|
2403 |
if (tab == null || (n = tab.length) == 0) { |
|
2404 |
n = (sc > c) ? sc : c; |
|
2405 |
if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) { |
|
2406 |
try { |
|
2407 |
if (table == tab) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2408 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2409 |
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n]; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2410 |
table = nt; |
17945 | 2411 |
sc = n - (n >>> 2); |
2412 |
} |
|
2413 |
} finally { |
|
2414 |
sizeCtl = sc; |
|
2415 |
} |
|
2416 |
} |
|
2417 |
} |
|
2418 |
else if (c <= sc || n >= MAXIMUM_CAPACITY) |
|
2419 |
break; |
|
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2420 |
else if (tab == table) { |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2421 |
int rs = resizeStamp(n); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
2422 |
if (U.compareAndSwapInt(this, SIZECTL, sc, |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
2423 |
(rs << RESIZE_STAMP_SHIFT) + 2)) |
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2424 |
transfer(tab, null); |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2425 |
} |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
11279
diff
changeset
|
2426 |
} |
2 | 2427 |
} |
2428 |
||
2429 |
/** |
|
17945 | 2430 |
* Moves and/or copies the nodes in each bin to new table. See |
2431 |
* above for explanation. |
|
2 | 2432 |
*/ |
17945 | 2433 |
private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) { |
2434 |
int n = tab.length, stride; |
|
2435 |
if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE) |
|
2436 |
stride = MIN_TRANSFER_STRIDE; // subdivide range |
|
2437 |
if (nextTab == null) { // initiating |
|
2438 |
try { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2439 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2440 |
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1]; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2441 |
nextTab = nt; |
17945 | 2442 |
} catch (Throwable ex) { // try to cope with OOME |
2443 |
sizeCtl = Integer.MAX_VALUE; |
|
2444 |
return; |
|
2445 |
} |
|
2446 |
nextTable = nextTab; |
|
2447 |
transferIndex = n; |
|
2 | 2448 |
} |
17945 | 2449 |
int nextn = nextTab.length; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2450 |
ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab); |
17945 | 2451 |
boolean advance = true; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2452 |
boolean finishing = false; // to ensure sweep before committing nextTab |
17945 | 2453 |
for (int i = 0, bound = 0;;) { |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2454 |
Node<K,V> f; int fh; |
17945 | 2455 |
while (advance) { |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2456 |
int nextIndex, nextBound; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2457 |
if (--i >= bound || finishing) |
17945 | 2458 |
advance = false; |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2459 |
else if ((nextIndex = transferIndex) <= 0) { |
17945 | 2460 |
i = -1; |
2461 |
advance = false; |
|
2462 |
} |
|
2463 |
else if (U.compareAndSwapInt |
|
2464 |
(this, TRANSFERINDEX, nextIndex, |
|
2465 |
nextBound = (nextIndex > stride ? |
|
2466 |
nextIndex - stride : 0))) { |
|
2467 |
bound = nextBound; |
|
2468 |
i = nextIndex - 1; |
|
2469 |
advance = false; |
|
2470 |
} |
|
2471 |
} |
|
2472 |
if (i < 0 || i >= n || i + n >= nextn) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2473 |
int sc; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2474 |
if (finishing) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2475 |
nextTable = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2476 |
table = nextTab; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2477 |
sizeCtl = (n << 1) - (n >>> 1); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2478 |
return; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2479 |
} |
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2480 |
if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, sc - 1)) { |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2481 |
if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT) |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2482 |
return; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2483 |
finishing = advance = true; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2484 |
i = n; // recheck before commit |
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
2485 |
} |
17945 | 2486 |
} |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2487 |
else if ((f = tabAt(tab, i)) == null) |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
2488 |
advance = casTabAt(tab, i, null, fwd); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2489 |
else if ((fh = f.hash) == MOVED) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2490 |
advance = true; // already processed |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2491 |
else { |
17945 | 2492 |
synchronized (f) { |
2493 |
if (tabAt(tab, i) == f) { |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2494 |
Node<K,V> ln, hn; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2495 |
if (fh >= 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2496 |
int runBit = fh & n; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2497 |
Node<K,V> lastRun = f; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2498 |
for (Node<K,V> p = f.next; p != null; p = p.next) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2499 |
int b = p.hash & n; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2500 |
if (b != runBit) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2501 |
runBit = b; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2502 |
lastRun = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2503 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2504 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2505 |
if (runBit == 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2506 |
ln = lastRun; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2507 |
hn = null; |
2 | 2508 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2509 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2510 |
hn = lastRun; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2511 |
ln = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2512 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2513 |
for (Node<K,V> p = f; p != lastRun; p = p.next) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2514 |
int ph = p.hash; K pk = p.key; V pv = p.val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2515 |
if ((ph & n) == 0) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2516 |
ln = new Node<K,V>(ph, pk, pv, ln); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2517 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2518 |
hn = new Node<K,V>(ph, pk, pv, hn); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2519 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2520 |
setTabAt(nextTab, i, ln); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2521 |
setTabAt(nextTab, i + n, hn); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2522 |
setTabAt(tab, i, fwd); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2523 |
advance = true; |
2 | 2524 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2525 |
else if (f instanceof TreeBin) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2526 |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2527 |
TreeNode<K,V> lo = null, loTail = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2528 |
TreeNode<K,V> hi = null, hiTail = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2529 |
int lc = 0, hc = 0; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2530 |
for (Node<K,V> e = t.first; e != null; e = e.next) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2531 |
int h = e.hash; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2532 |
TreeNode<K,V> p = new TreeNode<K,V> |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2533 |
(h, e.key, e.val, null, null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2534 |
if ((h & n) == 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2535 |
if ((p.prev = loTail) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2536 |
lo = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2537 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2538 |
loTail.next = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2539 |
loTail = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2540 |
++lc; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2541 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2542 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2543 |
if ((p.prev = hiTail) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2544 |
hi = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2545 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2546 |
hiTail.next = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2547 |
hiTail = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2548 |
++hc; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2549 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2550 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2551 |
ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) : |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2552 |
(hc != 0) ? new TreeBin<K,V>(lo) : t; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2553 |
hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) : |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2554 |
(lc != 0) ? new TreeBin<K,V>(hi) : t; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2555 |
setTabAt(nextTab, i, ln); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2556 |
setTabAt(nextTab, i + n, hn); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2557 |
setTabAt(tab, i, fwd); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2558 |
advance = true; |
2 | 2559 |
} |
2560 |
} |
|
2561 |
} |
|
2562 |
} |
|
17945 | 2563 |
} |
2564 |
} |
|
2565 |
||
2566 |
/* ---------------- Counter support -------------- */ |
|
2567 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2568 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2569 |
* A padded cell for distributing counts. Adapted from LongAdder |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2570 |
* and Striped64. See their internal docs for explanation. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2571 |
*/ |
34369
b6df4cc80001
8140687: Move @Contended to the jdk.internal.vm.annotation package
chegar
parents:
33674
diff
changeset
|
2572 |
@jdk.internal.vm.annotation.Contended static final class CounterCell { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2573 |
volatile long value; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2574 |
CounterCell(long x) { value = x; } |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2575 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2576 |
|
17945 | 2577 |
final long sumCount() { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2578 |
CounterCell[] as = counterCells; CounterCell a; |
17945 | 2579 |
long sum = baseCount; |
2580 |
if (as != null) { |
|
2581 |
for (int i = 0; i < as.length; ++i) { |
|
2582 |
if ((a = as[i]) != null) |
|
2583 |
sum += a.value; |
|
2584 |
} |
|
2585 |
} |
|
2586 |
return sum; |
|
2587 |
} |
|
2588 |
||
2589 |
// See LongAdder version for explanation |
|
2590 |
private final void fullAddCount(long x, boolean wasUncontended) { |
|
2591 |
int h; |
|
2592 |
if ((h = ThreadLocalRandom.getProbe()) == 0) { |
|
2593 |
ThreadLocalRandom.localInit(); // force initialization |
|
2594 |
h = ThreadLocalRandom.getProbe(); |
|
2595 |
wasUncontended = true; |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
2596 |
} |
17945 | 2597 |
boolean collide = false; // True if last slot nonempty |
2598 |
for (;;) { |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2599 |
CounterCell[] as; CounterCell a; int n; long v; |
17945 | 2600 |
if ((as = counterCells) != null && (n = as.length) > 0) { |
2601 |
if ((a = as[(n - 1) & h]) == null) { |
|
2602 |
if (cellsBusy == 0) { // Try to attach new Cell |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2603 |
CounterCell r = new CounterCell(x); // Optimistic create |
17945 | 2604 |
if (cellsBusy == 0 && |
2605 |
U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) { |
|
2606 |
boolean created = false; |
|
2607 |
try { // Recheck under lock |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2608 |
CounterCell[] rs; int m, j; |
17945 | 2609 |
if ((rs = counterCells) != null && |
2610 |
(m = rs.length) > 0 && |
|
2611 |
rs[j = (m - 1) & h] == null) { |
|
2612 |
rs[j] = r; |
|
2613 |
created = true; |
|
2614 |
} |
|
2615 |
} finally { |
|
2616 |
cellsBusy = 0; |
|
2617 |
} |
|
2618 |
if (created) |
|
2619 |
break; |
|
2620 |
continue; // Slot is now non-empty |
|
2621 |
} |
|
2622 |
} |
|
2623 |
collide = false; |
|
2624 |
} |
|
2625 |
else if (!wasUncontended) // CAS already known to fail |
|
2626 |
wasUncontended = true; // Continue after rehash |
|
2627 |
else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x)) |
|
2628 |
break; |
|
2629 |
else if (counterCells != as || n >= NCPU) |
|
2630 |
collide = false; // At max size or stale |
|
2631 |
else if (!collide) |
|
2632 |
collide = true; |
|
2633 |
else if (cellsBusy == 0 && |
|
2634 |
U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) { |
|
2635 |
try { |
|
2636 |
if (counterCells == as) {// Expand table unless stale |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2637 |
CounterCell[] rs = new CounterCell[n << 1]; |
17945 | 2638 |
for (int i = 0; i < n; ++i) |
2639 |
rs[i] = as[i]; |
|
2640 |
counterCells = rs; |
|
2641 |
} |
|
2642 |
} finally { |
|
2643 |
cellsBusy = 0; |
|
2644 |
} |
|
2645 |
collide = false; |
|
2646 |
continue; // Retry with expanded table |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
2647 |
} |
17945 | 2648 |
h = ThreadLocalRandom.advanceProbe(h); |
2649 |
} |
|
2650 |
else if (cellsBusy == 0 && counterCells == as && |
|
2651 |
U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) { |
|
2652 |
boolean init = false; |
|
2653 |
try { // Initialize table |
|
2654 |
if (counterCells == as) { |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2655 |
CounterCell[] rs = new CounterCell[2]; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2656 |
rs[h & 1] = new CounterCell(x); |
17945 | 2657 |
counterCells = rs; |
2658 |
init = true; |
|
2659 |
} |
|
2660 |
} finally { |
|
2661 |
cellsBusy = 0; |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
2662 |
} |
17945 | 2663 |
if (init) |
2664 |
break; |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
2665 |
} |
17945 | 2666 |
else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x)) |
2667 |
break; // Fall back on using base |
|
2668 |
} |
|
2669 |
} |
|
2670 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2671 |
/* ---------------- Conversion from/to TreeBins -------------- */ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2672 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2673 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2674 |
* Replaces all linked nodes in bin at given index unless table is |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2675 |
* too small, in which case resizes instead. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2676 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2677 |
private final void treeifyBin(Node<K,V>[] tab, int index) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
2678 |
Node<K,V> b; int n; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2679 |
if (tab != null) { |
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2680 |
if ((n = tab.length) < MIN_TREEIFY_CAPACITY) |
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2681 |
tryPresize(n << 1); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2682 |
else if ((b = tabAt(tab, index)) != null && b.hash >= 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2683 |
synchronized (b) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2684 |
if (tabAt(tab, index) == b) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2685 |
TreeNode<K,V> hd = null, tl = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2686 |
for (Node<K,V> e = b; e != null; e = e.next) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2687 |
TreeNode<K,V> p = |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2688 |
new TreeNode<K,V>(e.hash, e.key, e.val, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2689 |
null, null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2690 |
if ((p.prev = tl) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2691 |
hd = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2692 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2693 |
tl.next = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2694 |
tl = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2695 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2696 |
setTabAt(tab, index, new TreeBin<K,V>(hd)); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2697 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2698 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2699 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2700 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2701 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2702 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2703 |
/** |
36958 | 2704 |
* Returns a list of non-TreeNodes replacing those in given list. |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2705 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2706 |
static <K,V> Node<K,V> untreeify(Node<K,V> b) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2707 |
Node<K,V> hd = null, tl = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2708 |
for (Node<K,V> q = b; q != null; q = q.next) { |
36958 | 2709 |
Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2710 |
if (tl == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2711 |
hd = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2712 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2713 |
tl.next = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2714 |
tl = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2715 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2716 |
return hd; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2717 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2718 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2719 |
/* ---------------- TreeNodes -------------- */ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2720 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2721 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
2722 |
* Nodes for use in TreeBins. |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2723 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2724 |
static final class TreeNode<K,V> extends Node<K,V> { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2725 |
TreeNode<K,V> parent; // red-black tree links |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2726 |
TreeNode<K,V> left; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2727 |
TreeNode<K,V> right; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2728 |
TreeNode<K,V> prev; // needed to unlink next upon deletion |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2729 |
boolean red; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2730 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2731 |
TreeNode(int hash, K key, V val, Node<K,V> next, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2732 |
TreeNode<K,V> parent) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2733 |
super(hash, key, val, next); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2734 |
this.parent = parent; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2735 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2736 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2737 |
Node<K,V> find(int h, Object k) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2738 |
return findTreeNode(h, k, null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2739 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2740 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2741 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2742 |
* Returns the TreeNode (or null if not found) for the given key |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2743 |
* starting at given root. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2744 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2745 |
final TreeNode<K,V> findTreeNode(int h, Object k, Class<?> kc) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2746 |
if (k != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2747 |
TreeNode<K,V> p = this; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
2748 |
do { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2749 |
int ph, dir; K pk; TreeNode<K,V> q; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2750 |
TreeNode<K,V> pl = p.left, pr = p.right; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2751 |
if ((ph = p.hash) > h) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2752 |
p = pl; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2753 |
else if (ph < h) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2754 |
p = pr; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2755 |
else if ((pk = p.key) == k || (pk != null && k.equals(pk))) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2756 |
return p; |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2757 |
else if (pl == null) |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2758 |
p = pr; |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2759 |
else if (pr == null) |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2760 |
p = pl; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2761 |
else if ((kc != null || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2762 |
(kc = comparableClassFor(k)) != null) && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2763 |
(dir = compareComparables(kc, k, pk)) != 0) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2764 |
p = (dir < 0) ? pl : pr; |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2765 |
else if ((q = pr.findTreeNode(h, k, kc)) != null) |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2766 |
return q; |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2767 |
else |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2768 |
p = pl; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2769 |
} while (p != null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2770 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2771 |
return null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2772 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2773 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2774 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2775 |
/* ---------------- TreeBins -------------- */ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2776 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2777 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2778 |
* TreeNodes used at the heads of bins. TreeBins do not hold user |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2779 |
* keys or values, but instead point to list of TreeNodes and |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2780 |
* their root. They also maintain a parasitic read-write lock |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2781 |
* forcing writers (who hold bin lock) to wait for readers (who do |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2782 |
* not) to complete before tree restructuring operations. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2783 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2784 |
static final class TreeBin<K,V> extends Node<K,V> { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2785 |
TreeNode<K,V> root; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2786 |
volatile TreeNode<K,V> first; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2787 |
volatile Thread waiter; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2788 |
volatile int lockState; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2789 |
// values for lockState |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2790 |
static final int WRITER = 1; // set while holding write lock |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2791 |
static final int WAITER = 2; // set when waiting for write lock |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2792 |
static final int READER = 4; // increment value for setting read lock |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2793 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2794 |
/** |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2795 |
* Tie-breaking utility for ordering insertions when equal |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2796 |
* hashCodes and non-comparable. We don't require a total |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2797 |
* order, just a consistent insertion rule to maintain |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2798 |
* equivalence across rebalancings. Tie-breaking further than |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2799 |
* necessary simplifies testing a bit. |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2800 |
*/ |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2801 |
static int tieBreakOrder(Object a, Object b) { |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2802 |
int d; |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2803 |
if (a == null || b == null || |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2804 |
(d = a.getClass().getName(). |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2805 |
compareTo(b.getClass().getName())) == 0) |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2806 |
d = (System.identityHashCode(a) <= System.identityHashCode(b) ? |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2807 |
-1 : 1); |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2808 |
return d; |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2809 |
} |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2810 |
|
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2811 |
/** |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2812 |
* Creates bin with initial set of nodes headed by b. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2813 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2814 |
TreeBin(TreeNode<K,V> b) { |
36958 | 2815 |
super(TREEBIN, null, null); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2816 |
this.first = b; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2817 |
TreeNode<K,V> r = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2818 |
for (TreeNode<K,V> x = b, next; x != null; x = next) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2819 |
next = (TreeNode<K,V>)x.next; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2820 |
x.left = x.right = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2821 |
if (r == null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2822 |
x.parent = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2823 |
x.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2824 |
r = x; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2825 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2826 |
else { |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2827 |
K k = x.key; |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2828 |
int h = x.hash; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2829 |
Class<?> kc = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2830 |
for (TreeNode<K,V> p = r;;) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2831 |
int dir, ph; |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2832 |
K pk = p.key; |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2833 |
if ((ph = p.hash) > h) |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2834 |
dir = -1; |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2835 |
else if (ph < h) |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2836 |
dir = 1; |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2837 |
else if ((kc == null && |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2838 |
(kc = comparableClassFor(k)) == null) || |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2839 |
(dir = compareComparables(kc, k, pk)) == 0) |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2840 |
dir = tieBreakOrder(k, pk); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
2841 |
TreeNode<K,V> xp = p; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2842 |
if ((p = (dir <= 0) ? p.left : p.right) == null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2843 |
x.parent = xp; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2844 |
if (dir <= 0) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2845 |
xp.left = x; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2846 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2847 |
xp.right = x; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2848 |
r = balanceInsertion(r, x); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2849 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2850 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2851 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2852 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2853 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2854 |
this.root = r; |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2855 |
assert checkInvariants(root); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2856 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2857 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2858 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2859 |
* Acquires write lock for tree restructuring. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2860 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2861 |
private final void lockRoot() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2862 |
if (!U.compareAndSwapInt(this, LOCKSTATE, 0, WRITER)) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2863 |
contendedLock(); // offload to separate method |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2864 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2865 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2866 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2867 |
* Releases write lock for tree restructuring. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2868 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2869 |
private final void unlockRoot() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2870 |
lockState = 0; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2871 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2872 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2873 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2874 |
* Possibly blocks awaiting root lock. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2875 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2876 |
private final void contendedLock() { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2877 |
boolean waiting = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2878 |
for (int s;;) { |
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2879 |
if (((s = lockState) & ~WAITER) == 0) { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2880 |
if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER)) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2881 |
if (waiting) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2882 |
waiter = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2883 |
return; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2884 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2885 |
} |
19413 | 2886 |
else if ((s & WAITER) == 0) { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2887 |
if (U.compareAndSwapInt(this, LOCKSTATE, s, s | WAITER)) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2888 |
waiting = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2889 |
waiter = Thread.currentThread(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2890 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2891 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2892 |
else if (waiting) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2893 |
LockSupport.park(this); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2894 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2895 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2896 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2897 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2898 |
* Returns matching node or null if none. Tries to search |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2899 |
* using tree comparisons from root, but continues linear |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2900 |
* search when lock not available. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2901 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2902 |
final Node<K,V> find(int h, Object k) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2903 |
if (k != null) { |
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2904 |
for (Node<K,V> e = first; e != null; ) { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2905 |
int s; K ek; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2906 |
if (((s = lockState) & (WAITER|WRITER)) != 0) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2907 |
if (e.hash == h && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2908 |
((ek = e.key) == k || (ek != null && k.equals(ek)))) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2909 |
return e; |
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
2910 |
e = e.next; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2911 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2912 |
else if (U.compareAndSwapInt(this, LOCKSTATE, s, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2913 |
s + READER)) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2914 |
TreeNode<K,V> r, p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2915 |
try { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2916 |
p = ((r = root) == null ? null : |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2917 |
r.findTreeNode(h, k, null)); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2918 |
} finally { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2919 |
Thread w; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2920 |
if (U.getAndAddInt(this, LOCKSTATE, -READER) == |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2921 |
(READER|WAITER) && (w = waiter) != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2922 |
LockSupport.unpark(w); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2923 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2924 |
return p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2925 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2926 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2927 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2928 |
return null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2929 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2930 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2931 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2932 |
* Finds or adds a node. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2933 |
* @return null if added |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2934 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2935 |
final TreeNode<K,V> putTreeVal(int h, K k, V v) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2936 |
Class<?> kc = null; |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2937 |
boolean searched = false; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2938 |
for (TreeNode<K,V> p = root;;) { |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2939 |
int dir, ph; K pk; |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2940 |
if (p == null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2941 |
first = root = new TreeNode<K,V>(h, k, v, null, null); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2942 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2943 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2944 |
else if ((ph = p.hash) > h) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2945 |
dir = -1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2946 |
else if (ph < h) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2947 |
dir = 1; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2948 |
else if ((pk = p.key) == k || (pk != null && k.equals(pk))) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2949 |
return p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2950 |
else if ((kc == null && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2951 |
(kc = comparableClassFor(k)) == null) || |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2952 |
(dir = compareComparables(kc, k, pk)) == 0) { |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2953 |
if (!searched) { |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2954 |
TreeNode<K,V> q, ch; |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2955 |
searched = true; |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2956 |
if (((ch = p.left) != null && |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2957 |
(q = ch.findTreeNode(h, k, kc)) != null) || |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2958 |
((ch = p.right) != null && |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2959 |
(q = ch.findTreeNode(h, k, kc)) != null)) |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2960 |
return q; |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2961 |
} |
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2962 |
dir = tieBreakOrder(k, pk); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2963 |
} |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2964 |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2965 |
TreeNode<K,V> xp = p; |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2966 |
if ((p = (dir <= 0) ? p.left : p.right) == null) { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2967 |
TreeNode<K,V> x, f = first; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2968 |
first = x = new TreeNode<K,V>(h, k, v, f, xp); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2969 |
if (f != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2970 |
f.prev = x; |
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
2971 |
if (dir <= 0) |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2972 |
xp.left = x; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2973 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2974 |
xp.right = x; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2975 |
if (!xp.red) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2976 |
x.red = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2977 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2978 |
lockRoot(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2979 |
try { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2980 |
root = balanceInsertion(root, x); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2981 |
} finally { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2982 |
unlockRoot(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2983 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2984 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2985 |
break; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2986 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2987 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2988 |
assert checkInvariants(root); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2989 |
return null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2990 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2991 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2992 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2993 |
* Removes the given node, that must be present before this |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2994 |
* call. This is messier than typical red-black deletion code |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2995 |
* because we cannot swap the contents of an interior node |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2996 |
* with a leaf successor that is pinned by "next" pointers |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2997 |
* that are accessible independently of lock. So instead we |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2998 |
* swap the tree linkages. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
2999 |
* |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3000 |
* @return true if now too small, so should be untreeified |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3001 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3002 |
final boolean removeTreeNode(TreeNode<K,V> p) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3003 |
TreeNode<K,V> next = (TreeNode<K,V>)p.next; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3004 |
TreeNode<K,V> pred = p.prev; // unlink traversal pointers |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3005 |
TreeNode<K,V> r, rl; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3006 |
if (pred == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3007 |
first = next; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3008 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3009 |
pred.next = next; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3010 |
if (next != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3011 |
next.prev = pred; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3012 |
if (first == null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3013 |
root = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3014 |
return true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3015 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3016 |
if ((r = root) == null || r.right == null || // too small |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3017 |
(rl = r.left) == null || rl.left == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3018 |
return true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3019 |
lockRoot(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3020 |
try { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3021 |
TreeNode<K,V> replacement; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3022 |
TreeNode<K,V> pl = p.left; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3023 |
TreeNode<K,V> pr = p.right; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3024 |
if (pl != null && pr != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3025 |
TreeNode<K,V> s = pr, sl; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3026 |
while ((sl = s.left) != null) // find successor |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3027 |
s = sl; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3028 |
boolean c = s.red; s.red = p.red; p.red = c; // swap colors |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3029 |
TreeNode<K,V> sr = s.right; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3030 |
TreeNode<K,V> pp = p.parent; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3031 |
if (s == pr) { // p was s's direct parent |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3032 |
p.parent = s; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3033 |
s.right = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3034 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3035 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3036 |
TreeNode<K,V> sp = s.parent; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3037 |
if ((p.parent = sp) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3038 |
if (s == sp.left) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3039 |
sp.left = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3040 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3041 |
sp.right = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3042 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3043 |
if ((s.right = pr) != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3044 |
pr.parent = s; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3045 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3046 |
p.left = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3047 |
if ((p.right = sr) != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3048 |
sr.parent = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3049 |
if ((s.left = pl) != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3050 |
pl.parent = s; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3051 |
if ((s.parent = pp) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3052 |
r = s; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3053 |
else if (p == pp.left) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3054 |
pp.left = s; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3055 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3056 |
pp.right = s; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3057 |
if (sr != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3058 |
replacement = sr; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3059 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3060 |
replacement = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3061 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3062 |
else if (pl != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3063 |
replacement = pl; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3064 |
else if (pr != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3065 |
replacement = pr; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3066 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3067 |
replacement = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3068 |
if (replacement != p) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3069 |
TreeNode<K,V> pp = replacement.parent = p.parent; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3070 |
if (pp == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3071 |
r = replacement; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3072 |
else if (p == pp.left) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3073 |
pp.left = replacement; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3074 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3075 |
pp.right = replacement; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3076 |
p.left = p.right = p.parent = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3077 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3078 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3079 |
root = (p.red) ? r : balanceDeletion(r, replacement); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3080 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3081 |
if (p == replacement) { // detach pointers |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3082 |
TreeNode<K,V> pp; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3083 |
if ((pp = p.parent) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3084 |
if (p == pp.left) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3085 |
pp.left = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3086 |
else if (p == pp.right) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3087 |
pp.right = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3088 |
p.parent = null; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3089 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3090 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3091 |
} finally { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3092 |
unlockRoot(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3093 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3094 |
assert checkInvariants(root); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3095 |
return false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3096 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3097 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3098 |
/* ------------------------------------------------------------ */ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3099 |
// Red-black tree methods, all adapted from CLR |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3100 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3101 |
static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3102 |
TreeNode<K,V> p) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3103 |
TreeNode<K,V> r, pp, rl; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3104 |
if (p != null && (r = p.right) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3105 |
if ((rl = p.right = r.left) != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3106 |
rl.parent = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3107 |
if ((pp = r.parent = p.parent) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3108 |
(root = r).red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3109 |
else if (pp.left == p) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3110 |
pp.left = r; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3111 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3112 |
pp.right = r; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3113 |
r.left = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3114 |
p.parent = r; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3115 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3116 |
return root; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3117 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3118 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3119 |
static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3120 |
TreeNode<K,V> p) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3121 |
TreeNode<K,V> l, pp, lr; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3122 |
if (p != null && (l = p.left) != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3123 |
if ((lr = p.left = l.right) != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3124 |
lr.parent = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3125 |
if ((pp = l.parent = p.parent) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3126 |
(root = l).red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3127 |
else if (pp.right == p) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3128 |
pp.right = l; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3129 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3130 |
pp.left = l; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3131 |
l.right = p; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3132 |
p.parent = l; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3133 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3134 |
return root; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3135 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3136 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3137 |
static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3138 |
TreeNode<K,V> x) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3139 |
x.red = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3140 |
for (TreeNode<K,V> xp, xpp, xppl, xppr;;) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3141 |
if ((xp = x.parent) == null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3142 |
x.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3143 |
return x; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3144 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3145 |
else if (!xp.red || (xpp = xp.parent) == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3146 |
return root; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3147 |
if (xp == (xppl = xpp.left)) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3148 |
if ((xppr = xpp.right) != null && xppr.red) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3149 |
xppr.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3150 |
xp.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3151 |
xpp.red = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3152 |
x = xpp; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3153 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3154 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3155 |
if (x == xp.right) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3156 |
root = rotateLeft(root, x = xp); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3157 |
xpp = (xp = x.parent) == null ? null : xp.parent; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3158 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3159 |
if (xp != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3160 |
xp.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3161 |
if (xpp != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3162 |
xpp.red = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3163 |
root = rotateRight(root, xpp); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3164 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3165 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3166 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3167 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3168 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3169 |
if (xppl != null && xppl.red) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3170 |
xppl.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3171 |
xp.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3172 |
xpp.red = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3173 |
x = xpp; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3174 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3175 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3176 |
if (x == xp.left) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3177 |
root = rotateRight(root, x = xp); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3178 |
xpp = (xp = x.parent) == null ? null : xp.parent; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3179 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3180 |
if (xp != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3181 |
xp.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3182 |
if (xpp != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3183 |
xpp.red = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3184 |
root = rotateLeft(root, xpp); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3185 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3186 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3187 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3188 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3189 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3190 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3191 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3192 |
static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3193 |
TreeNode<K,V> x) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
3194 |
for (TreeNode<K,V> xp, xpl, xpr;;) { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3195 |
if (x == null || x == root) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3196 |
return root; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3197 |
else if ((xp = x.parent) == null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3198 |
x.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3199 |
return x; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3200 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3201 |
else if (x.red) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3202 |
x.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3203 |
return root; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3204 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3205 |
else if ((xpl = xp.left) == x) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3206 |
if ((xpr = xp.right) != null && xpr.red) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3207 |
xpr.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3208 |
xp.red = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3209 |
root = rotateLeft(root, xp); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3210 |
xpr = (xp = x.parent) == null ? null : xp.right; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3211 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3212 |
if (xpr == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3213 |
x = xp; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3214 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3215 |
TreeNode<K,V> sl = xpr.left, sr = xpr.right; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3216 |
if ((sr == null || !sr.red) && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3217 |
(sl == null || !sl.red)) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3218 |
xpr.red = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3219 |
x = xp; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3220 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3221 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3222 |
if (sr == null || !sr.red) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3223 |
if (sl != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3224 |
sl.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3225 |
xpr.red = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3226 |
root = rotateRight(root, xpr); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3227 |
xpr = (xp = x.parent) == null ? |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3228 |
null : xp.right; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3229 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3230 |
if (xpr != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3231 |
xpr.red = (xp == null) ? false : xp.red; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3232 |
if ((sr = xpr.right) != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3233 |
sr.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3234 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3235 |
if (xp != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3236 |
xp.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3237 |
root = rotateLeft(root, xp); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3238 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3239 |
x = root; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3240 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3241 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3242 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3243 |
else { // symmetric |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3244 |
if (xpl != null && xpl.red) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3245 |
xpl.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3246 |
xp.red = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3247 |
root = rotateRight(root, xp); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3248 |
xpl = (xp = x.parent) == null ? null : xp.left; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3249 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3250 |
if (xpl == null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3251 |
x = xp; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3252 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3253 |
TreeNode<K,V> sl = xpl.left, sr = xpl.right; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3254 |
if ((sl == null || !sl.red) && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3255 |
(sr == null || !sr.red)) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3256 |
xpl.red = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3257 |
x = xp; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3258 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3259 |
else { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3260 |
if (sl == null || !sl.red) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3261 |
if (sr != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3262 |
sr.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3263 |
xpl.red = true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3264 |
root = rotateLeft(root, xpl); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3265 |
xpl = (xp = x.parent) == null ? |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3266 |
null : xp.left; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3267 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3268 |
if (xpl != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3269 |
xpl.red = (xp == null) ? false : xp.red; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3270 |
if ((sl = xpl.left) != null) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3271 |
sl.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3272 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3273 |
if (xp != null) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3274 |
xp.red = false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3275 |
root = rotateRight(root, xp); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3276 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3277 |
x = root; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3278 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3279 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3280 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3281 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3282 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3283 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3284 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
3285 |
* Checks invariants recursively for the tree of Nodes rooted at t. |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3286 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3287 |
static <K,V> boolean checkInvariants(TreeNode<K,V> t) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3288 |
TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3289 |
tb = t.prev, tn = (TreeNode<K,V>)t.next; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3290 |
if (tb != null && tb.next != t) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3291 |
return false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3292 |
if (tn != null && tn.prev != t) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3293 |
return false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3294 |
if (tp != null && t != tp.left && t != tp.right) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3295 |
return false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3296 |
if (tl != null && (tl.parent != t || tl.hash > t.hash)) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3297 |
return false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3298 |
if (tr != null && (tr.parent != t || tr.hash < t.hash)) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3299 |
return false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3300 |
if (t.red && tl != null && tl.red && tr != null && tr.red) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3301 |
return false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3302 |
if (tl != null && !checkInvariants(tl)) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3303 |
return false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3304 |
if (tr != null && !checkInvariants(tr)) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3305 |
return false; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3306 |
return true; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3307 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3308 |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
38551
diff
changeset
|
3309 |
private static final Unsafe U = Unsafe.getUnsafe(); |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3310 |
private static final long LOCKSTATE; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3311 |
static { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3312 |
try { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3313 |
LOCKSTATE = U.objectFieldOffset |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
3314 |
(TreeBin.class.getDeclaredField("lockState")); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
3315 |
} catch (ReflectiveOperationException e) { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3316 |
throw new Error(e); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3317 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3318 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3319 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3320 |
|
17945 | 3321 |
/* ----------------Table Traversal -------------- */ |
3322 |
||
3323 |
/** |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3324 |
* Records the table, its length, and current traversal index for a |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3325 |
* traverser that must process a region of a forwarded table before |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3326 |
* proceeding with current table. |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3327 |
*/ |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3328 |
static final class TableStack<K,V> { |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3329 |
int length; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3330 |
int index; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3331 |
Node<K,V>[] tab; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3332 |
TableStack<K,V> next; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3333 |
} |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3334 |
|
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3335 |
/** |
17945 | 3336 |
* Encapsulates traversal for methods such as containsValue; also |
3337 |
* serves as a base class for other iterators and spliterators. |
|
3338 |
* |
|
3339 |
* Method advance visits once each still-valid node that was |
|
3340 |
* reachable upon iterator construction. It might miss some that |
|
3341 |
* were added to a bin after the bin was visited, which is OK wrt |
|
3342 |
* consistency guarantees. Maintaining this property in the face |
|
3343 |
* of possible ongoing resizes requires a fair amount of |
|
3344 |
* bookkeeping state that is difficult to optimize away amidst |
|
3345 |
* volatile accesses. Even so, traversal maintains reasonable |
|
3346 |
* throughput. |
|
3347 |
* |
|
3348 |
* Normally, iteration proceeds bin-by-bin traversing lists. |
|
3349 |
* However, if the table has been resized, then all future steps |
|
3350 |
* must traverse both the bin at the current index as well as at |
|
3351 |
* (index + baseSize); and so on for further resizings. To |
|
3352 |
* paranoically cope with potential sharing by users of iterators |
|
3353 |
* across threads, iteration terminates if a bounds checks fails |
|
3354 |
* for a table read. |
|
3355 |
*/ |
|
3356 |
static class Traverser<K,V> { |
|
3357 |
Node<K,V>[] tab; // current table; updated if resized |
|
3358 |
Node<K,V> next; // the next entry to use |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3359 |
TableStack<K,V> stack, spare; // to save/restore on ForwardingNodes |
17945 | 3360 |
int index; // index of bin to use next |
3361 |
int baseIndex; // current index of initial table |
|
3362 |
int baseLimit; // index bound for initial table |
|
3363 |
final int baseSize; // initial table size |
|
3364 |
||
3365 |
Traverser(Node<K,V>[] tab, int size, int index, int limit) { |
|
3366 |
this.tab = tab; |
|
3367 |
this.baseSize = size; |
|
3368 |
this.baseIndex = this.index = index; |
|
3369 |
this.baseLimit = limit; |
|
3370 |
this.next = null; |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
3371 |
} |
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
3372 |
|
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
3373 |
/** |
17945 | 3374 |
* Advances if possible, returning next valid node, or null if none. |
2 | 3375 |
*/ |
17945 | 3376 |
final Node<K,V> advance() { |
3377 |
Node<K,V> e; |
|
3378 |
if ((e = next) != null) |
|
3379 |
e = e.next; |
|
3380 |
for (;;) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3381 |
Node<K,V>[] t; int i, n; // must use locals in checks |
17945 | 3382 |
if (e != null) |
3383 |
return next = e; |
|
3384 |
if (baseIndex >= baseLimit || (t = tab) == null || |
|
3385 |
(n = t.length) <= (i = index) || i < 0) |
|
3386 |
return next = null; |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3387 |
if ((e = tabAt(t, i)) != null && e.hash < 0) { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3388 |
if (e instanceof ForwardingNode) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3389 |
tab = ((ForwardingNode<K,V>)e).nextTable; |
17945 | 3390 |
e = null; |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3391 |
pushState(t, i, n); |
17945 | 3392 |
continue; |
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
3393 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3394 |
else if (e instanceof TreeBin) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3395 |
e = ((TreeBin<K,V>)e).first; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3396 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3397 |
e = null; |
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
3398 |
} |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3399 |
if (stack != null) |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3400 |
recoverState(n); |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3401 |
else if ((index = i + baseSize) >= n) |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3402 |
index = ++baseIndex; // visit upper slots if present |
2 | 3403 |
} |
3404 |
} |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3405 |
|
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3406 |
/** |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3407 |
* Saves traversal state upon encountering a forwarding node. |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3408 |
*/ |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3409 |
private void pushState(Node<K,V>[] t, int i, int n) { |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3410 |
TableStack<K,V> s = spare; // reuse if possible |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3411 |
if (s != null) |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3412 |
spare = s.next; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3413 |
else |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3414 |
s = new TableStack<K,V>(); |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3415 |
s.tab = t; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3416 |
s.length = n; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3417 |
s.index = i; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3418 |
s.next = stack; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3419 |
stack = s; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3420 |
} |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3421 |
|
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3422 |
/** |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3423 |
* Possibly pops traversal state. |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3424 |
* |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3425 |
* @param n length of current table |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3426 |
*/ |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3427 |
private void recoverState(int n) { |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3428 |
TableStack<K,V> s; int len; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3429 |
while ((s = stack) != null && (index += (len = s.length)) >= n) { |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3430 |
n = len; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3431 |
index = s.index; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3432 |
tab = s.tab; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3433 |
s.tab = null; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3434 |
TableStack<K,V> next = s.next; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3435 |
s.next = spare; // save for reuse |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3436 |
stack = next; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3437 |
spare = s; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3438 |
} |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3439 |
if (s == null && (index += baseSize) >= n) |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3440 |
index = ++baseIndex; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
3441 |
} |
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
3442 |
} |
2 | 3443 |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
3444 |
/** |
17945 | 3445 |
* Base of key, value, and entry Iterators. Adds fields to |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3446 |
* Traverser to support iterator.remove. |
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
3447 |
*/ |
17945 | 3448 |
static class BaseIterator<K,V> extends Traverser<K,V> { |
3449 |
final ConcurrentHashMap<K,V> map; |
|
3450 |
Node<K,V> lastReturned; |
|
3451 |
BaseIterator(Node<K,V>[] tab, int size, int index, int limit, |
|
3452 |
ConcurrentHashMap<K,V> map) { |
|
3453 |
super(tab, size, index, limit); |
|
3454 |
this.map = map; |
|
3455 |
advance(); |
|
3456 |
} |
|
3457 |
||
3458 |
public final boolean hasNext() { return next != null; } |
|
3459 |
public final boolean hasMoreElements() { return next != null; } |
|
3460 |
||
3461 |
public final void remove() { |
|
3462 |
Node<K,V> p; |
|
3463 |
if ((p = lastReturned) == null) |
|
3464 |
throw new IllegalStateException(); |
|
3465 |
lastReturned = null; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3466 |
map.replaceNode(p.key, null, null); |
17945 | 3467 |
} |
3468 |
} |
|
3469 |
||
3470 |
static final class KeyIterator<K,V> extends BaseIterator<K,V> |
|
3471 |
implements Iterator<K>, Enumeration<K> { |
|
44588
fd9d4d384425
8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents:
40547
diff
changeset
|
3472 |
KeyIterator(Node<K,V>[] tab, int size, int index, int limit, |
17945 | 3473 |
ConcurrentHashMap<K,V> map) { |
44588
fd9d4d384425
8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents:
40547
diff
changeset
|
3474 |
super(tab, size, index, limit, map); |
17945 | 3475 |
} |
3476 |
||
3477 |
public final K next() { |
|
3478 |
Node<K,V> p; |
|
3479 |
if ((p = next) == null) |
|
3480 |
throw new NoSuchElementException(); |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3481 |
K k = p.key; |
17945 | 3482 |
lastReturned = p; |
3483 |
advance(); |
|
3484 |
return k; |
|
2 | 3485 |
} |
17945 | 3486 |
|
3487 |
public final K nextElement() { return next(); } |
|
3488 |
} |
|
3489 |
||
3490 |
static final class ValueIterator<K,V> extends BaseIterator<K,V> |
|
3491 |
implements Iterator<V>, Enumeration<V> { |
|
44588
fd9d4d384425
8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents:
40547
diff
changeset
|
3492 |
ValueIterator(Node<K,V>[] tab, int size, int index, int limit, |
17945 | 3493 |
ConcurrentHashMap<K,V> map) { |
44588
fd9d4d384425
8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents:
40547
diff
changeset
|
3494 |
super(tab, size, index, limit, map); |
17945 | 3495 |
} |
3496 |
||
3497 |
public final V next() { |
|
3498 |
Node<K,V> p; |
|
3499 |
if ((p = next) == null) |
|
3500 |
throw new NoSuchElementException(); |
|
3501 |
V v = p.val; |
|
3502 |
lastReturned = p; |
|
3503 |
advance(); |
|
3504 |
return v; |
|
3505 |
} |
|
3506 |
||
3507 |
public final V nextElement() { return next(); } |
|
3508 |
} |
|
3509 |
||
3510 |
static final class EntryIterator<K,V> extends BaseIterator<K,V> |
|
3511 |
implements Iterator<Map.Entry<K,V>> { |
|
44588
fd9d4d384425
8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents:
40547
diff
changeset
|
3512 |
EntryIterator(Node<K,V>[] tab, int size, int index, int limit, |
17945 | 3513 |
ConcurrentHashMap<K,V> map) { |
44588
fd9d4d384425
8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents:
40547
diff
changeset
|
3514 |
super(tab, size, index, limit, map); |
17945 | 3515 |
} |
3516 |
||
3517 |
public final Map.Entry<K,V> next() { |
|
3518 |
Node<K,V> p; |
|
3519 |
if ((p = next) == null) |
|
3520 |
throw new NoSuchElementException(); |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3521 |
K k = p.key; |
17945 | 3522 |
V v = p.val; |
3523 |
lastReturned = p; |
|
3524 |
advance(); |
|
3525 |
return new MapEntry<K,V>(k, v, map); |
|
3526 |
} |
|
2 | 3527 |
} |
3528 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3529 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
3530 |
* Exported Entry for EntryIterator. |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3531 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3532 |
static final class MapEntry<K,V> implements Map.Entry<K,V> { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3533 |
final K key; // non-null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3534 |
V val; // non-null |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3535 |
final ConcurrentHashMap<K,V> map; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3536 |
MapEntry(K key, V val, ConcurrentHashMap<K,V> map) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3537 |
this.key = key; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3538 |
this.val = val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3539 |
this.map = map; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3540 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3541 |
public K getKey() { return key; } |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3542 |
public V getValue() { return val; } |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3543 |
public int hashCode() { return key.hashCode() ^ val.hashCode(); } |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
3544 |
public String toString() { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
3545 |
return Helpers.mapEntryToString(key, val); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
3546 |
} |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3547 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3548 |
public boolean equals(Object o) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3549 |
Object k, v; Map.Entry<?,?> e; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3550 |
return ((o instanceof Map.Entry) && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3551 |
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3552 |
(v = e.getValue()) != null && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3553 |
(k == key || k.equals(key)) && |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3554 |
(v == val || v.equals(val))); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3555 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3556 |
|
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3557 |
/** |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3558 |
* Sets our entry's value and writes through to the map. The |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3559 |
* value to return is somewhat arbitrary here. Since we do not |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3560 |
* necessarily track asynchronous changes, the most recent |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3561 |
* "previous" value could be different from what we return (or |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3562 |
* could even have been removed, in which case the put will |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3563 |
* re-establish). We do not and cannot guarantee more. |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3564 |
*/ |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3565 |
public V setValue(V value) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3566 |
if (value == null) throw new NullPointerException(); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3567 |
V v = val; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3568 |
val = value; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3569 |
map.put(key, value); |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3570 |
return v; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3571 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3572 |
} |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3573 |
|
17945 | 3574 |
static final class KeySpliterator<K,V> extends Traverser<K,V> |
3575 |
implements Spliterator<K> { |
|
3576 |
long est; // size estimate |
|
3577 |
KeySpliterator(Node<K,V>[] tab, int size, int index, int limit, |
|
3578 |
long est) { |
|
3579 |
super(tab, size, index, limit); |
|
3580 |
this.est = est; |
|
3581 |
} |
|
3582 |
||
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
3583 |
public KeySpliterator<K,V> trySplit() { |
17945 | 3584 |
int i, f, h; |
3585 |
return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null : |
|
3586 |
new KeySpliterator<K,V>(tab, baseSize, baseLimit = h, |
|
3587 |
f, est >>>= 1); |
|
3588 |
} |
|
3589 |
||
3590 |
public void forEachRemaining(Consumer<? super K> action) { |
|
3591 |
if (action == null) throw new NullPointerException(); |
|
3592 |
for (Node<K,V> p; (p = advance()) != null;) |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3593 |
action.accept(p.key); |
17945 | 3594 |
} |
3595 |
||
3596 |
public boolean tryAdvance(Consumer<? super K> action) { |
|
3597 |
if (action == null) throw new NullPointerException(); |
|
3598 |
Node<K,V> p; |
|
3599 |
if ((p = advance()) == null) |
|
3600 |
return false; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3601 |
action.accept(p.key); |
17945 | 3602 |
return true; |
3603 |
} |
|
3604 |
||
3605 |
public long estimateSize() { return est; } |
|
3606 |
||
3607 |
public int characteristics() { |
|
3608 |
return Spliterator.DISTINCT | Spliterator.CONCURRENT | |
|
3609 |
Spliterator.NONNULL; |
|
3610 |
} |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
3611 |
} |
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
3612 |
|
17945 | 3613 |
static final class ValueSpliterator<K,V> extends Traverser<K,V> |
3614 |
implements Spliterator<V> { |
|
3615 |
long est; // size estimate |
|
3616 |
ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit, |
|
3617 |
long est) { |
|
3618 |
super(tab, size, index, limit); |
|
3619 |
this.est = est; |
|
3620 |
} |
|
3621 |
||
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
3622 |
public ValueSpliterator<K,V> trySplit() { |
17945 | 3623 |
int i, f, h; |
3624 |
return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null : |
|
3625 |
new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h, |
|
3626 |
f, est >>>= 1); |
|
3627 |
} |
|
3628 |
||
3629 |
public void forEachRemaining(Consumer<? super V> action) { |
|
3630 |
if (action == null) throw new NullPointerException(); |
|
3631 |
for (Node<K,V> p; (p = advance()) != null;) |
|
3632 |
action.accept(p.val); |
|
3633 |
} |
|
3634 |
||
3635 |
public boolean tryAdvance(Consumer<? super V> action) { |
|
3636 |
if (action == null) throw new NullPointerException(); |
|
3637 |
Node<K,V> p; |
|
3638 |
if ((p = advance()) == null) |
|
3639 |
return false; |
|
3640 |
action.accept(p.val); |
|
3641 |
return true; |
|
3642 |
} |
|
3643 |
||
3644 |
public long estimateSize() { return est; } |
|
3645 |
||
3646 |
public int characteristics() { |
|
3647 |
return Spliterator.CONCURRENT | Spliterator.NONNULL; |
|
3648 |
} |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
3649 |
} |
2 | 3650 |
|
17945 | 3651 |
static final class EntrySpliterator<K,V> extends Traverser<K,V> |
3652 |
implements Spliterator<Map.Entry<K,V>> { |
|
3653 |
final ConcurrentHashMap<K,V> map; // To export MapEntry |
|
3654 |
long est; // size estimate |
|
3655 |
EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit, |
|
3656 |
long est, ConcurrentHashMap<K,V> map) { |
|
3657 |
super(tab, size, index, limit); |
|
3658 |
this.map = map; |
|
3659 |
this.est = est; |
|
3660 |
} |
|
3661 |
||
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
3662 |
public EntrySpliterator<K,V> trySplit() { |
17945 | 3663 |
int i, f, h; |
3664 |
return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null : |
|
3665 |
new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h, |
|
3666 |
f, est >>>= 1, map); |
|
3667 |
} |
|
3668 |
||
3669 |
public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) { |
|
3670 |
if (action == null) throw new NullPointerException(); |
|
3671 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3672 |
action.accept(new MapEntry<K,V>(p.key, p.val, map)); |
17945 | 3673 |
} |
3674 |
||
3675 |
public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) { |
|
3676 |
if (action == null) throw new NullPointerException(); |
|
3677 |
Node<K,V> p; |
|
3678 |
if ((p = advance()) == null) |
|
3679 |
return false; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3680 |
action.accept(new MapEntry<K,V>(p.key, p.val, map)); |
17945 | 3681 |
return true; |
3682 |
} |
|
3683 |
||
3684 |
public long estimateSize() { return est; } |
|
3685 |
||
3686 |
public int characteristics() { |
|
3687 |
return Spliterator.DISTINCT | Spliterator.CONCURRENT | |
|
3688 |
Spliterator.NONNULL; |
|
3689 |
} |
|
3690 |
} |
|
3691 |
||
3692 |
// Parallel bulk operations |
|
3693 |
||
3694 |
/** |
|
3695 |
* Computes initial batch value for bulk tasks. The returned value |
|
3696 |
* is approximately exp2 of the number of times (minus one) to |
|
3697 |
* split task by two before executing leaf action. This value is |
|
3698 |
* faster to compute and more convenient to use as a guide to |
|
3699 |
* splitting than is the depth, since it is used while dividing by |
|
3700 |
* two anyway. |
|
3701 |
*/ |
|
3702 |
final int batchFor(long b) { |
|
3703 |
long n; |
|
3704 |
if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b) |
|
3705 |
return 0; |
|
3706 |
int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4 |
|
3707 |
return (b <= 0L || (n /= b) >= sp) ? sp : (int)n; |
|
3708 |
} |
|
3709 |
||
3710 |
/** |
|
3711 |
* Performs the given action for each (key, value). |
|
3712 |
* |
|
3713 |
* @param parallelismThreshold the (estimated) number of elements |
|
3714 |
* needed for this operation to be executed in parallel |
|
3715 |
* @param action the action |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
3716 |
* @since 1.8 |
17945 | 3717 |
*/ |
3718 |
public void forEach(long parallelismThreshold, |
|
3719 |
BiConsumer<? super K,? super V> action) { |
|
3720 |
if (action == null) throw new NullPointerException(); |
|
3721 |
new ForEachMappingTask<K,V> |
|
3722 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
3723 |
action).invoke(); |
|
3724 |
} |
|
3725 |
||
3726 |
/** |
|
3727 |
* Performs the given action for each non-null transformation |
|
3728 |
* of each (key, value). |
|
3729 |
* |
|
3730 |
* @param parallelismThreshold the (estimated) number of elements |
|
3731 |
* needed for this operation to be executed in parallel |
|
3732 |
* @param transformer a function returning the transformation |
|
3733 |
* for an element, or null if there is no transformation (in |
|
3734 |
* which case the action is not applied) |
|
3735 |
* @param action the action |
|
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
3736 |
* @param <U> the return type of the transformer |
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
3737 |
* @since 1.8 |
17945 | 3738 |
*/ |
3739 |
public <U> void forEach(long parallelismThreshold, |
|
3740 |
BiFunction<? super K, ? super V, ? extends U> transformer, |
|
3741 |
Consumer<? super U> action) { |
|
3742 |
if (transformer == null || action == null) |
|
3743 |
throw new NullPointerException(); |
|
3744 |
new ForEachTransformedMappingTask<K,V,U> |
|
3745 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
3746 |
transformer, action).invoke(); |
|
3747 |
} |
|
3748 |
||
3749 |
/** |
|
3750 |
* Returns a non-null result from applying the given search |
|
3751 |
* function on each (key, value), or null if none. Upon |
|
3752 |
* success, further element processing is suppressed and the |
|
3753 |
* results of any other parallel invocations of the search |
|
3754 |
* function are ignored. |
|
3755 |
* |
|
3756 |
* @param parallelismThreshold the (estimated) number of elements |
|
3757 |
* needed for this operation to be executed in parallel |
|
3758 |
* @param searchFunction a function returning a non-null |
|
3759 |
* result on success, else null |
|
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
3760 |
* @param <U> the return type of the search function |
17945 | 3761 |
* @return a non-null result from applying the given search |
3762 |
* function on each (key, value), or null if none |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
3763 |
* @since 1.8 |
17945 | 3764 |
*/ |
3765 |
public <U> U search(long parallelismThreshold, |
|
3766 |
BiFunction<? super K, ? super V, ? extends U> searchFunction) { |
|
3767 |
if (searchFunction == null) throw new NullPointerException(); |
|
3768 |
return new SearchMappingsTask<K,V,U> |
|
3769 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
3770 |
searchFunction, new AtomicReference<U>()).invoke(); |
|
3771 |
} |
|
3772 |
||
3773 |
/** |
|
3774 |
* Returns the result of accumulating the given transformation |
|
3775 |
* of all (key, value) pairs using the given reducer to |
|
3776 |
* combine values, or null if none. |
|
3777 |
* |
|
3778 |
* @param parallelismThreshold the (estimated) number of elements |
|
3779 |
* needed for this operation to be executed in parallel |
|
3780 |
* @param transformer a function returning the transformation |
|
3781 |
* for an element, or null if there is no transformation (in |
|
3782 |
* which case it is not combined) |
|
3783 |
* @param reducer a commutative associative combining function |
|
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
3784 |
* @param <U> the return type of the transformer |
17945 | 3785 |
* @return the result of accumulating the given transformation |
3786 |
* of all (key, value) pairs |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
3787 |
* @since 1.8 |
17945 | 3788 |
*/ |
3789 |
public <U> U reduce(long parallelismThreshold, |
|
3790 |
BiFunction<? super K, ? super V, ? extends U> transformer, |
|
3791 |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
|
3792 |
if (transformer == null || reducer == null) |
|
3793 |
throw new NullPointerException(); |
|
3794 |
return new MapReduceMappingsTask<K,V,U> |
|
3795 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
3796 |
null, transformer, reducer).invoke(); |
|
3797 |
} |
|
3798 |
||
3799 |
/** |
|
3800 |
* Returns the result of accumulating the given transformation |
|
3801 |
* of all (key, value) pairs using the given reducer to |
|
3802 |
* combine values, and the given basis as an identity value. |
|
3803 |
* |
|
3804 |
* @param parallelismThreshold the (estimated) number of elements |
|
3805 |
* needed for this operation to be executed in parallel |
|
3806 |
* @param transformer a function returning the transformation |
|
3807 |
* for an element |
|
3808 |
* @param basis the identity (initial default value) for the reduction |
|
3809 |
* @param reducer a commutative associative combining function |
|
3810 |
* @return the result of accumulating the given transformation |
|
3811 |
* of all (key, value) pairs |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
3812 |
* @since 1.8 |
17945 | 3813 |
*/ |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3814 |
public double reduceToDouble(long parallelismThreshold, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3815 |
ToDoubleBiFunction<? super K, ? super V> transformer, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3816 |
double basis, |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
3817 |
DoubleBinaryOperator reducer) { |
17945 | 3818 |
if (transformer == null || reducer == null) |
3819 |
throw new NullPointerException(); |
|
3820 |
return new MapReduceMappingsToDoubleTask<K,V> |
|
3821 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
3822 |
null, transformer, basis, reducer).invoke(); |
|
3823 |
} |
|
3824 |
||
3825 |
/** |
|
3826 |
* Returns the result of accumulating the given transformation |
|
3827 |
* of all (key, value) pairs using the given reducer to |
|
3828 |
* combine values, and the given basis as an identity value. |
|
3829 |
* |
|
3830 |
* @param parallelismThreshold the (estimated) number of elements |
|
3831 |
* needed for this operation to be executed in parallel |
|
3832 |
* @param transformer a function returning the transformation |
|
3833 |
* for an element |
|
3834 |
* @param basis the identity (initial default value) for the reduction |
|
3835 |
* @param reducer a commutative associative combining function |
|
3836 |
* @return the result of accumulating the given transformation |
|
3837 |
* of all (key, value) pairs |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
3838 |
* @since 1.8 |
17945 | 3839 |
*/ |
3840 |
public long reduceToLong(long parallelismThreshold, |
|
3841 |
ToLongBiFunction<? super K, ? super V> transformer, |
|
3842 |
long basis, |
|
3843 |
LongBinaryOperator reducer) { |
|
3844 |
if (transformer == null || reducer == null) |
|
3845 |
throw new NullPointerException(); |
|
3846 |
return new MapReduceMappingsToLongTask<K,V> |
|
3847 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
3848 |
null, transformer, basis, reducer).invoke(); |
|
3849 |
} |
|
3850 |
||
3851 |
/** |
|
3852 |
* Returns the result of accumulating the given transformation |
|
3853 |
* of all (key, value) pairs using the given reducer to |
|
3854 |
* combine values, and the given basis as an identity value. |
|
3855 |
* |
|
3856 |
* @param parallelismThreshold the (estimated) number of elements |
|
3857 |
* needed for this operation to be executed in parallel |
|
3858 |
* @param transformer a function returning the transformation |
|
3859 |
* for an element |
|
3860 |
* @param basis the identity (initial default value) for the reduction |
|
3861 |
* @param reducer a commutative associative combining function |
|
3862 |
* @return the result of accumulating the given transformation |
|
3863 |
* of all (key, value) pairs |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
3864 |
* @since 1.8 |
17945 | 3865 |
*/ |
3866 |
public int reduceToInt(long parallelismThreshold, |
|
3867 |
ToIntBiFunction<? super K, ? super V> transformer, |
|
3868 |
int basis, |
|
3869 |
IntBinaryOperator reducer) { |
|
3870 |
if (transformer == null || reducer == null) |
|
3871 |
throw new NullPointerException(); |
|
3872 |
return new MapReduceMappingsToIntTask<K,V> |
|
3873 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
3874 |
null, transformer, basis, reducer).invoke(); |
|
3875 |
} |
|
3876 |
||
3877 |
/** |
|
3878 |
* Performs the given action for each key. |
|
3879 |
* |
|
3880 |
* @param parallelismThreshold the (estimated) number of elements |
|
3881 |
* needed for this operation to be executed in parallel |
|
3882 |
* @param action the action |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
3883 |
* @since 1.8 |
17945 | 3884 |
*/ |
3885 |
public void forEachKey(long parallelismThreshold, |
|
3886 |
Consumer<? super K> action) { |
|
3887 |
if (action == null) throw new NullPointerException(); |
|
3888 |
new ForEachKeyTask<K,V> |
|
3889 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
3890 |
action).invoke(); |
|
3891 |
} |
|
3892 |
||
3893 |
/** |
|
3894 |
* Performs the given action for each non-null transformation |
|
3895 |
* of each key. |
|
3896 |
* |
|
3897 |
* @param parallelismThreshold the (estimated) number of elements |
|
3898 |
* needed for this operation to be executed in parallel |
|
3899 |
* @param transformer a function returning the transformation |
|
3900 |
* for an element, or null if there is no transformation (in |
|
3901 |
* which case the action is not applied) |
|
3902 |
* @param action the action |
|
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
3903 |
* @param <U> the return type of the transformer |
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
3904 |
* @since 1.8 |
17945 | 3905 |
*/ |
3906 |
public <U> void forEachKey(long parallelismThreshold, |
|
3907 |
Function<? super K, ? extends U> transformer, |
|
3908 |
Consumer<? super U> action) { |
|
3909 |
if (transformer == null || action == null) |
|
3910 |
throw new NullPointerException(); |
|
3911 |
new ForEachTransformedKeyTask<K,V,U> |
|
3912 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
3913 |
transformer, action).invoke(); |
|
3914 |
} |
|
3915 |
||
3916 |
/** |
|
3917 |
* Returns a non-null result from applying the given search |
|
3918 |
* function on each key, or null if none. Upon success, |
|
3919 |
* further element processing is suppressed and the results of |
|
3920 |
* any other parallel invocations of the search function are |
|
3921 |
* ignored. |
|
3922 |
* |
|
3923 |
* @param parallelismThreshold the (estimated) number of elements |
|
3924 |
* needed for this operation to be executed in parallel |
|
3925 |
* @param searchFunction a function returning a non-null |
|
3926 |
* result on success, else null |
|
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
3927 |
* @param <U> the return type of the search function |
17945 | 3928 |
* @return a non-null result from applying the given search |
3929 |
* function on each key, or null if none |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
3930 |
* @since 1.8 |
17945 | 3931 |
*/ |
3932 |
public <U> U searchKeys(long parallelismThreshold, |
|
3933 |
Function<? super K, ? extends U> searchFunction) { |
|
3934 |
if (searchFunction == null) throw new NullPointerException(); |
|
3935 |
return new SearchKeysTask<K,V,U> |
|
3936 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
3937 |
searchFunction, new AtomicReference<U>()).invoke(); |
|
3938 |
} |
|
3939 |
||
3940 |
/** |
|
3941 |
* Returns the result of accumulating all keys using the given |
|
3942 |
* reducer to combine values, or null if none. |
|
3943 |
* |
|
3944 |
* @param parallelismThreshold the (estimated) number of elements |
|
3945 |
* needed for this operation to be executed in parallel |
|
3946 |
* @param reducer a commutative associative combining function |
|
3947 |
* @return the result of accumulating all keys using the given |
|
3948 |
* reducer to combine values, or null if none |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
3949 |
* @since 1.8 |
17945 | 3950 |
*/ |
3951 |
public K reduceKeys(long parallelismThreshold, |
|
3952 |
BiFunction<? super K, ? super K, ? extends K> reducer) { |
|
3953 |
if (reducer == null) throw new NullPointerException(); |
|
3954 |
return new ReduceKeysTask<K,V> |
|
3955 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
3956 |
null, reducer).invoke(); |
|
3957 |
} |
|
3958 |
||
3959 |
/** |
|
3960 |
* Returns the result of accumulating the given transformation |
|
3961 |
* of all keys using the given reducer to combine values, or |
|
3962 |
* null if none. |
|
3963 |
* |
|
3964 |
* @param parallelismThreshold the (estimated) number of elements |
|
3965 |
* needed for this operation to be executed in parallel |
|
3966 |
* @param transformer a function returning the transformation |
|
3967 |
* for an element, or null if there is no transformation (in |
|
3968 |
* which case it is not combined) |
|
3969 |
* @param reducer a commutative associative combining function |
|
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
3970 |
* @param <U> the return type of the transformer |
17945 | 3971 |
* @return the result of accumulating the given transformation |
3972 |
* of all keys |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
3973 |
* @since 1.8 |
17945 | 3974 |
*/ |
3975 |
public <U> U reduceKeys(long parallelismThreshold, |
|
3976 |
Function<? super K, ? extends U> transformer, |
|
3977 |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
|
3978 |
if (transformer == null || reducer == null) |
|
3979 |
throw new NullPointerException(); |
|
3980 |
return new MapReduceKeysTask<K,V,U> |
|
3981 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
3982 |
null, transformer, reducer).invoke(); |
|
3983 |
} |
|
3984 |
||
3985 |
/** |
|
3986 |
* Returns the result of accumulating the given transformation |
|
3987 |
* of all keys using the given reducer to combine values, and |
|
3988 |
* the given basis as an identity value. |
|
3989 |
* |
|
3990 |
* @param parallelismThreshold the (estimated) number of elements |
|
3991 |
* needed for this operation to be executed in parallel |
|
3992 |
* @param transformer a function returning the transformation |
|
3993 |
* for an element |
|
3994 |
* @param basis the identity (initial default value) for the reduction |
|
3995 |
* @param reducer a commutative associative combining function |
|
3996 |
* @return the result of accumulating the given transformation |
|
3997 |
* of all keys |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
3998 |
* @since 1.8 |
17945 | 3999 |
*/ |
4000 |
public double reduceKeysToDouble(long parallelismThreshold, |
|
4001 |
ToDoubleFunction<? super K> transformer, |
|
4002 |
double basis, |
|
4003 |
DoubleBinaryOperator reducer) { |
|
4004 |
if (transformer == null || reducer == null) |
|
4005 |
throw new NullPointerException(); |
|
4006 |
return new MapReduceKeysToDoubleTask<K,V> |
|
4007 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4008 |
null, transformer, basis, reducer).invoke(); |
|
4009 |
} |
|
4010 |
||
4011 |
/** |
|
4012 |
* Returns the result of accumulating the given transformation |
|
4013 |
* of all keys using the given reducer to combine values, and |
|
4014 |
* the given basis as an identity value. |
|
4015 |
* |
|
4016 |
* @param parallelismThreshold the (estimated) number of elements |
|
4017 |
* needed for this operation to be executed in parallel |
|
4018 |
* @param transformer a function returning the transformation |
|
4019 |
* for an element |
|
4020 |
* @param basis the identity (initial default value) for the reduction |
|
4021 |
* @param reducer a commutative associative combining function |
|
4022 |
* @return the result of accumulating the given transformation |
|
4023 |
* of all keys |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4024 |
* @since 1.8 |
17945 | 4025 |
*/ |
4026 |
public long reduceKeysToLong(long parallelismThreshold, |
|
4027 |
ToLongFunction<? super K> transformer, |
|
4028 |
long basis, |
|
4029 |
LongBinaryOperator reducer) { |
|
4030 |
if (transformer == null || reducer == null) |
|
4031 |
throw new NullPointerException(); |
|
4032 |
return new MapReduceKeysToLongTask<K,V> |
|
4033 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4034 |
null, transformer, basis, reducer).invoke(); |
|
4035 |
} |
|
4036 |
||
4037 |
/** |
|
4038 |
* Returns the result of accumulating the given transformation |
|
4039 |
* of all keys using the given reducer to combine values, and |
|
4040 |
* the given basis as an identity value. |
|
4041 |
* |
|
4042 |
* @param parallelismThreshold the (estimated) number of elements |
|
4043 |
* needed for this operation to be executed in parallel |
|
4044 |
* @param transformer a function returning the transformation |
|
4045 |
* for an element |
|
4046 |
* @param basis the identity (initial default value) for the reduction |
|
4047 |
* @param reducer a commutative associative combining function |
|
4048 |
* @return the result of accumulating the given transformation |
|
4049 |
* of all keys |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4050 |
* @since 1.8 |
17945 | 4051 |
*/ |
4052 |
public int reduceKeysToInt(long parallelismThreshold, |
|
4053 |
ToIntFunction<? super K> transformer, |
|
4054 |
int basis, |
|
4055 |
IntBinaryOperator reducer) { |
|
4056 |
if (transformer == null || reducer == null) |
|
4057 |
throw new NullPointerException(); |
|
4058 |
return new MapReduceKeysToIntTask<K,V> |
|
4059 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4060 |
null, transformer, basis, reducer).invoke(); |
|
4061 |
} |
|
4062 |
||
4063 |
/** |
|
4064 |
* Performs the given action for each value. |
|
4065 |
* |
|
4066 |
* @param parallelismThreshold the (estimated) number of elements |
|
4067 |
* needed for this operation to be executed in parallel |
|
4068 |
* @param action the action |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4069 |
* @since 1.8 |
17945 | 4070 |
*/ |
4071 |
public void forEachValue(long parallelismThreshold, |
|
4072 |
Consumer<? super V> action) { |
|
4073 |
if (action == null) |
|
4074 |
throw new NullPointerException(); |
|
4075 |
new ForEachValueTask<K,V> |
|
4076 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4077 |
action).invoke(); |
|
4078 |
} |
|
4079 |
||
4080 |
/** |
|
4081 |
* Performs the given action for each non-null transformation |
|
4082 |
* of each value. |
|
4083 |
* |
|
4084 |
* @param parallelismThreshold the (estimated) number of elements |
|
4085 |
* needed for this operation to be executed in parallel |
|
4086 |
* @param transformer a function returning the transformation |
|
4087 |
* for an element, or null if there is no transformation (in |
|
4088 |
* which case the action is not applied) |
|
4089 |
* @param action the action |
|
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
4090 |
* @param <U> the return type of the transformer |
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4091 |
* @since 1.8 |
17945 | 4092 |
*/ |
4093 |
public <U> void forEachValue(long parallelismThreshold, |
|
4094 |
Function<? super V, ? extends U> transformer, |
|
4095 |
Consumer<? super U> action) { |
|
4096 |
if (transformer == null || action == null) |
|
4097 |
throw new NullPointerException(); |
|
4098 |
new ForEachTransformedValueTask<K,V,U> |
|
4099 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4100 |
transformer, action).invoke(); |
|
4101 |
} |
|
4102 |
||
4103 |
/** |
|
4104 |
* Returns a non-null result from applying the given search |
|
4105 |
* function on each value, or null if none. Upon success, |
|
4106 |
* further element processing is suppressed and the results of |
|
4107 |
* any other parallel invocations of the search function are |
|
4108 |
* ignored. |
|
4109 |
* |
|
4110 |
* @param parallelismThreshold the (estimated) number of elements |
|
4111 |
* needed for this operation to be executed in parallel |
|
4112 |
* @param searchFunction a function returning a non-null |
|
4113 |
* result on success, else null |
|
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
4114 |
* @param <U> the return type of the search function |
17945 | 4115 |
* @return a non-null result from applying the given search |
4116 |
* function on each value, or null if none |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4117 |
* @since 1.8 |
17945 | 4118 |
*/ |
4119 |
public <U> U searchValues(long parallelismThreshold, |
|
4120 |
Function<? super V, ? extends U> searchFunction) { |
|
4121 |
if (searchFunction == null) throw new NullPointerException(); |
|
4122 |
return new SearchValuesTask<K,V,U> |
|
4123 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4124 |
searchFunction, new AtomicReference<U>()).invoke(); |
|
4125 |
} |
|
4126 |
||
4127 |
/** |
|
4128 |
* Returns the result of accumulating all values using the |
|
4129 |
* given reducer to combine values, or null if none. |
|
4130 |
* |
|
4131 |
* @param parallelismThreshold the (estimated) number of elements |
|
4132 |
* needed for this operation to be executed in parallel |
|
4133 |
* @param reducer a commutative associative combining function |
|
4134 |
* @return the result of accumulating all values |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4135 |
* @since 1.8 |
17945 | 4136 |
*/ |
4137 |
public V reduceValues(long parallelismThreshold, |
|
4138 |
BiFunction<? super V, ? super V, ? extends V> reducer) { |
|
4139 |
if (reducer == null) throw new NullPointerException(); |
|
4140 |
return new ReduceValuesTask<K,V> |
|
4141 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4142 |
null, reducer).invoke(); |
|
4143 |
} |
|
4144 |
||
4145 |
/** |
|
4146 |
* Returns the result of accumulating the given transformation |
|
4147 |
* of all values using the given reducer to combine values, or |
|
4148 |
* null if none. |
|
4149 |
* |
|
4150 |
* @param parallelismThreshold the (estimated) number of elements |
|
4151 |
* needed for this operation to be executed in parallel |
|
4152 |
* @param transformer a function returning the transformation |
|
4153 |
* for an element, or null if there is no transformation (in |
|
4154 |
* which case it is not combined) |
|
4155 |
* @param reducer a commutative associative combining function |
|
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
4156 |
* @param <U> the return type of the transformer |
17945 | 4157 |
* @return the result of accumulating the given transformation |
4158 |
* of all values |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4159 |
* @since 1.8 |
17945 | 4160 |
*/ |
4161 |
public <U> U reduceValues(long parallelismThreshold, |
|
4162 |
Function<? super V, ? extends U> transformer, |
|
4163 |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
|
4164 |
if (transformer == null || reducer == null) |
|
4165 |
throw new NullPointerException(); |
|
4166 |
return new MapReduceValuesTask<K,V,U> |
|
4167 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4168 |
null, transformer, reducer).invoke(); |
|
4169 |
} |
|
4170 |
||
4171 |
/** |
|
4172 |
* Returns the result of accumulating the given transformation |
|
4173 |
* of all values using the given reducer to combine values, |
|
4174 |
* and the given basis as an identity value. |
|
4175 |
* |
|
4176 |
* @param parallelismThreshold the (estimated) number of elements |
|
4177 |
* needed for this operation to be executed in parallel |
|
4178 |
* @param transformer a function returning the transformation |
|
4179 |
* for an element |
|
4180 |
* @param basis the identity (initial default value) for the reduction |
|
4181 |
* @param reducer a commutative associative combining function |
|
4182 |
* @return the result of accumulating the given transformation |
|
4183 |
* of all values |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4184 |
* @since 1.8 |
17945 | 4185 |
*/ |
4186 |
public double reduceValuesToDouble(long parallelismThreshold, |
|
4187 |
ToDoubleFunction<? super V> transformer, |
|
4188 |
double basis, |
|
4189 |
DoubleBinaryOperator reducer) { |
|
4190 |
if (transformer == null || reducer == null) |
|
4191 |
throw new NullPointerException(); |
|
4192 |
return new MapReduceValuesToDoubleTask<K,V> |
|
4193 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4194 |
null, transformer, basis, reducer).invoke(); |
|
4195 |
} |
|
4196 |
||
4197 |
/** |
|
4198 |
* Returns the result of accumulating the given transformation |
|
4199 |
* of all values using the given reducer to combine values, |
|
4200 |
* and the given basis as an identity value. |
|
4201 |
* |
|
4202 |
* @param parallelismThreshold the (estimated) number of elements |
|
4203 |
* needed for this operation to be executed in parallel |
|
4204 |
* @param transformer a function returning the transformation |
|
4205 |
* for an element |
|
4206 |
* @param basis the identity (initial default value) for the reduction |
|
4207 |
* @param reducer a commutative associative combining function |
|
4208 |
* @return the result of accumulating the given transformation |
|
4209 |
* of all values |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4210 |
* @since 1.8 |
17945 | 4211 |
*/ |
4212 |
public long reduceValuesToLong(long parallelismThreshold, |
|
4213 |
ToLongFunction<? super V> transformer, |
|
4214 |
long basis, |
|
4215 |
LongBinaryOperator reducer) { |
|
4216 |
if (transformer == null || reducer == null) |
|
4217 |
throw new NullPointerException(); |
|
4218 |
return new MapReduceValuesToLongTask<K,V> |
|
4219 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4220 |
null, transformer, basis, reducer).invoke(); |
|
4221 |
} |
|
4222 |
||
4223 |
/** |
|
4224 |
* Returns the result of accumulating the given transformation |
|
4225 |
* of all values using the given reducer to combine values, |
|
4226 |
* and the given basis as an identity value. |
|
4227 |
* |
|
4228 |
* @param parallelismThreshold the (estimated) number of elements |
|
4229 |
* needed for this operation to be executed in parallel |
|
4230 |
* @param transformer a function returning the transformation |
|
4231 |
* for an element |
|
4232 |
* @param basis the identity (initial default value) for the reduction |
|
4233 |
* @param reducer a commutative associative combining function |
|
4234 |
* @return the result of accumulating the given transformation |
|
4235 |
* of all values |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4236 |
* @since 1.8 |
17945 | 4237 |
*/ |
4238 |
public int reduceValuesToInt(long parallelismThreshold, |
|
4239 |
ToIntFunction<? super V> transformer, |
|
4240 |
int basis, |
|
4241 |
IntBinaryOperator reducer) { |
|
4242 |
if (transformer == null || reducer == null) |
|
4243 |
throw new NullPointerException(); |
|
4244 |
return new MapReduceValuesToIntTask<K,V> |
|
4245 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4246 |
null, transformer, basis, reducer).invoke(); |
|
4247 |
} |
|
4248 |
||
4249 |
/** |
|
4250 |
* Performs the given action for each entry. |
|
4251 |
* |
|
4252 |
* @param parallelismThreshold the (estimated) number of elements |
|
4253 |
* needed for this operation to be executed in parallel |
|
4254 |
* @param action the action |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4255 |
* @since 1.8 |
17945 | 4256 |
*/ |
4257 |
public void forEachEntry(long parallelismThreshold, |
|
4258 |
Consumer<? super Map.Entry<K,V>> action) { |
|
4259 |
if (action == null) throw new NullPointerException(); |
|
4260 |
new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4261 |
action).invoke(); |
|
4262 |
} |
|
4263 |
||
4264 |
/** |
|
4265 |
* Performs the given action for each non-null transformation |
|
4266 |
* of each entry. |
|
4267 |
* |
|
4268 |
* @param parallelismThreshold the (estimated) number of elements |
|
4269 |
* needed for this operation to be executed in parallel |
|
4270 |
* @param transformer a function returning the transformation |
|
4271 |
* for an element, or null if there is no transformation (in |
|
4272 |
* which case the action is not applied) |
|
4273 |
* @param action the action |
|
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
4274 |
* @param <U> the return type of the transformer |
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4275 |
* @since 1.8 |
17945 | 4276 |
*/ |
4277 |
public <U> void forEachEntry(long parallelismThreshold, |
|
4278 |
Function<Map.Entry<K,V>, ? extends U> transformer, |
|
4279 |
Consumer<? super U> action) { |
|
4280 |
if (transformer == null || action == null) |
|
4281 |
throw new NullPointerException(); |
|
4282 |
new ForEachTransformedEntryTask<K,V,U> |
|
4283 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4284 |
transformer, action).invoke(); |
|
4285 |
} |
|
4286 |
||
4287 |
/** |
|
4288 |
* Returns a non-null result from applying the given search |
|
4289 |
* function on each entry, or null if none. Upon success, |
|
4290 |
* further element processing is suppressed and the results of |
|
4291 |
* any other parallel invocations of the search function are |
|
4292 |
* ignored. |
|
4293 |
* |
|
4294 |
* @param parallelismThreshold the (estimated) number of elements |
|
4295 |
* needed for this operation to be executed in parallel |
|
4296 |
* @param searchFunction a function returning a non-null |
|
4297 |
* result on success, else null |
|
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
4298 |
* @param <U> the return type of the search function |
17945 | 4299 |
* @return a non-null result from applying the given search |
4300 |
* function on each entry, or null if none |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4301 |
* @since 1.8 |
17945 | 4302 |
*/ |
4303 |
public <U> U searchEntries(long parallelismThreshold, |
|
4304 |
Function<Map.Entry<K,V>, ? extends U> searchFunction) { |
|
4305 |
if (searchFunction == null) throw new NullPointerException(); |
|
4306 |
return new SearchEntriesTask<K,V,U> |
|
4307 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4308 |
searchFunction, new AtomicReference<U>()).invoke(); |
|
4309 |
} |
|
4310 |
||
4311 |
/** |
|
4312 |
* Returns the result of accumulating all entries using the |
|
4313 |
* given reducer to combine values, or null if none. |
|
4314 |
* |
|
4315 |
* @param parallelismThreshold the (estimated) number of elements |
|
4316 |
* needed for this operation to be executed in parallel |
|
4317 |
* @param reducer a commutative associative combining function |
|
4318 |
* @return the result of accumulating all entries |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4319 |
* @since 1.8 |
17945 | 4320 |
*/ |
4321 |
public Map.Entry<K,V> reduceEntries(long parallelismThreshold, |
|
4322 |
BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
|
4323 |
if (reducer == null) throw new NullPointerException(); |
|
4324 |
return new ReduceEntriesTask<K,V> |
|
4325 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4326 |
null, reducer).invoke(); |
|
4327 |
} |
|
4328 |
||
4329 |
/** |
|
4330 |
* Returns the result of accumulating the given transformation |
|
4331 |
* of all entries using the given reducer to combine values, |
|
4332 |
* or null if none. |
|
4333 |
* |
|
4334 |
* @param parallelismThreshold the (estimated) number of elements |
|
4335 |
* needed for this operation to be executed in parallel |
|
4336 |
* @param transformer a function returning the transformation |
|
4337 |
* for an element, or null if there is no transformation (in |
|
4338 |
* which case it is not combined) |
|
4339 |
* @param reducer a commutative associative combining function |
|
19036
7e9050510b51
8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents:
18802
diff
changeset
|
4340 |
* @param <U> the return type of the transformer |
17945 | 4341 |
* @return the result of accumulating the given transformation |
4342 |
* of all entries |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4343 |
* @since 1.8 |
17945 | 4344 |
*/ |
4345 |
public <U> U reduceEntries(long parallelismThreshold, |
|
4346 |
Function<Map.Entry<K,V>, ? extends U> transformer, |
|
4347 |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
|
4348 |
if (transformer == null || reducer == null) |
|
4349 |
throw new NullPointerException(); |
|
4350 |
return new MapReduceEntriesTask<K,V,U> |
|
4351 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4352 |
null, transformer, reducer).invoke(); |
|
4353 |
} |
|
4354 |
||
4355 |
/** |
|
4356 |
* Returns the result of accumulating the given transformation |
|
4357 |
* of all entries using the given reducer to combine values, |
|
4358 |
* and the given basis as an identity value. |
|
4359 |
* |
|
4360 |
* @param parallelismThreshold the (estimated) number of elements |
|
4361 |
* needed for this operation to be executed in parallel |
|
4362 |
* @param transformer a function returning the transformation |
|
4363 |
* for an element |
|
4364 |
* @param basis the identity (initial default value) for the reduction |
|
4365 |
* @param reducer a commutative associative combining function |
|
4366 |
* @return the result of accumulating the given transformation |
|
4367 |
* of all entries |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4368 |
* @since 1.8 |
17945 | 4369 |
*/ |
4370 |
public double reduceEntriesToDouble(long parallelismThreshold, |
|
4371 |
ToDoubleFunction<Map.Entry<K,V>> transformer, |
|
4372 |
double basis, |
|
4373 |
DoubleBinaryOperator reducer) { |
|
4374 |
if (transformer == null || reducer == null) |
|
4375 |
throw new NullPointerException(); |
|
4376 |
return new MapReduceEntriesToDoubleTask<K,V> |
|
4377 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4378 |
null, transformer, basis, reducer).invoke(); |
|
4379 |
} |
|
4380 |
||
4381 |
/** |
|
4382 |
* Returns the result of accumulating the given transformation |
|
4383 |
* of all entries using the given reducer to combine values, |
|
4384 |
* and the given basis as an identity value. |
|
4385 |
* |
|
4386 |
* @param parallelismThreshold the (estimated) number of elements |
|
4387 |
* needed for this operation to be executed in parallel |
|
4388 |
* @param transformer a function returning the transformation |
|
4389 |
* for an element |
|
4390 |
* @param basis the identity (initial default value) for the reduction |
|
4391 |
* @param reducer a commutative associative combining function |
|
4392 |
* @return the result of accumulating the given transformation |
|
4393 |
* of all entries |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4394 |
* @since 1.8 |
17945 | 4395 |
*/ |
4396 |
public long reduceEntriesToLong(long parallelismThreshold, |
|
4397 |
ToLongFunction<Map.Entry<K,V>> transformer, |
|
4398 |
long basis, |
|
4399 |
LongBinaryOperator reducer) { |
|
4400 |
if (transformer == null || reducer == null) |
|
4401 |
throw new NullPointerException(); |
|
4402 |
return new MapReduceEntriesToLongTask<K,V> |
|
4403 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4404 |
null, transformer, basis, reducer).invoke(); |
|
4405 |
} |
|
4406 |
||
4407 |
/** |
|
4408 |
* Returns the result of accumulating the given transformation |
|
4409 |
* of all entries using the given reducer to combine values, |
|
4410 |
* and the given basis as an identity value. |
|
4411 |
* |
|
4412 |
* @param parallelismThreshold the (estimated) number of elements |
|
4413 |
* needed for this operation to be executed in parallel |
|
4414 |
* @param transformer a function returning the transformation |
|
4415 |
* for an element |
|
4416 |
* @param basis the identity (initial default value) for the reduction |
|
4417 |
* @param reducer a commutative associative combining function |
|
4418 |
* @return the result of accumulating the given transformation |
|
4419 |
* of all entries |
|
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4420 |
* @since 1.8 |
17945 | 4421 |
*/ |
4422 |
public int reduceEntriesToInt(long parallelismThreshold, |
|
4423 |
ToIntFunction<Map.Entry<K,V>> transformer, |
|
4424 |
int basis, |
|
4425 |
IntBinaryOperator reducer) { |
|
4426 |
if (transformer == null || reducer == null) |
|
4427 |
throw new NullPointerException(); |
|
4428 |
return new MapReduceEntriesToIntTask<K,V> |
|
4429 |
(null, batchFor(parallelismThreshold), 0, 0, table, |
|
4430 |
null, transformer, basis, reducer).invoke(); |
|
4431 |
} |
|
4432 |
||
4433 |
||
4434 |
/* ----------------Views -------------- */ |
|
4435 |
||
4436 |
/** |
|
4437 |
* Base class for views. |
|
4438 |
*/ |
|
4439 |
abstract static class CollectionView<K,V,E> |
|
4440 |
implements Collection<E>, java.io.Serializable { |
|
4441 |
private static final long serialVersionUID = 7249069246763182397L; |
|
4442 |
final ConcurrentHashMap<K,V> map; |
|
4443 |
CollectionView(ConcurrentHashMap<K,V> map) { this.map = map; } |
|
4444 |
||
4445 |
/** |
|
4446 |
* Returns the map backing this view. |
|
4447 |
* |
|
4448 |
* @return the map backing this view |
|
4449 |
*/ |
|
4450 |
public ConcurrentHashMap<K,V> getMap() { return map; } |
|
4451 |
||
4452 |
/** |
|
4453 |
* Removes all of the elements from this view, by removing all |
|
4454 |
* the mappings from the map backing this view. |
|
4455 |
*/ |
|
4456 |
public final void clear() { map.clear(); } |
|
4457 |
public final int size() { return map.size(); } |
|
4458 |
public final boolean isEmpty() { return map.isEmpty(); } |
|
4459 |
||
4460 |
// implementations below rely on concrete classes supplying these |
|
4461 |
// abstract methods |
|
4462 |
/** |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
4463 |
* Returns an iterator over the elements in this collection. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
4464 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
4465 |
* <p>The returned iterator is |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
4466 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
4467 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
19413
diff
changeset
|
4468 |
* @return an iterator over the elements in this collection |
17945 | 4469 |
*/ |
4470 |
public abstract Iterator<E> iterator(); |
|
4471 |
public abstract boolean contains(Object o); |
|
4472 |
public abstract boolean remove(Object o); |
|
4473 |
||
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
4474 |
private static final String OOME_MSG = "Required array size too large"; |
17945 | 4475 |
|
4476 |
public final Object[] toArray() { |
|
4477 |
long sz = map.mappingCount(); |
|
4478 |
if (sz > MAX_ARRAY_SIZE) |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
4479 |
throw new OutOfMemoryError(OOME_MSG); |
17945 | 4480 |
int n = (int)sz; |
4481 |
Object[] r = new Object[n]; |
|
4482 |
int i = 0; |
|
4483 |
for (E e : this) { |
|
4484 |
if (i == n) { |
|
4485 |
if (n >= MAX_ARRAY_SIZE) |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
4486 |
throw new OutOfMemoryError(OOME_MSG); |
17945 | 4487 |
if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1) |
4488 |
n = MAX_ARRAY_SIZE; |
|
4489 |
else |
|
4490 |
n += (n >>> 1) + 1; |
|
4491 |
r = Arrays.copyOf(r, n); |
|
4492 |
} |
|
4493 |
r[i++] = e; |
|
4494 |
} |
|
4495 |
return (i == n) ? r : Arrays.copyOf(r, i); |
|
4496 |
} |
|
4497 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
4498 |
@SuppressWarnings("unchecked") |
17945 | 4499 |
public final <T> T[] toArray(T[] a) { |
4500 |
long sz = map.mappingCount(); |
|
4501 |
if (sz > MAX_ARRAY_SIZE) |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
4502 |
throw new OutOfMemoryError(OOME_MSG); |
17945 | 4503 |
int m = (int)sz; |
4504 |
T[] r = (a.length >= m) ? a : |
|
4505 |
(T[])java.lang.reflect.Array |
|
4506 |
.newInstance(a.getClass().getComponentType(), m); |
|
4507 |
int n = r.length; |
|
4508 |
int i = 0; |
|
4509 |
for (E e : this) { |
|
4510 |
if (i == n) { |
|
4511 |
if (n >= MAX_ARRAY_SIZE) |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
4512 |
throw new OutOfMemoryError(OOME_MSG); |
17945 | 4513 |
if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1) |
4514 |
n = MAX_ARRAY_SIZE; |
|
4515 |
else |
|
4516 |
n += (n >>> 1) + 1; |
|
4517 |
r = Arrays.copyOf(r, n); |
|
4518 |
} |
|
4519 |
r[i++] = (T)e; |
|
4520 |
} |
|
4521 |
if (a == r && i < n) { |
|
4522 |
r[i] = null; // null-terminate |
|
4523 |
return r; |
|
4524 |
} |
|
4525 |
return (i == n) ? r : Arrays.copyOf(r, i); |
|
4526 |
} |
|
4527 |
||
4528 |
/** |
|
4529 |
* Returns a string representation of this collection. |
|
4530 |
* The string representation consists of the string representations |
|
4531 |
* of the collection's elements in the order they are returned by |
|
4532 |
* its iterator, enclosed in square brackets ({@code "[]"}). |
|
4533 |
* Adjacent elements are separated by the characters {@code ", "} |
|
4534 |
* (comma and space). Elements are converted to strings as by |
|
4535 |
* {@link String#valueOf(Object)}. |
|
4536 |
* |
|
4537 |
* @return a string representation of this collection |
|
4538 |
*/ |
|
4539 |
public final String toString() { |
|
4540 |
StringBuilder sb = new StringBuilder(); |
|
4541 |
sb.append('['); |
|
4542 |
Iterator<E> it = iterator(); |
|
4543 |
if (it.hasNext()) { |
|
4544 |
for (;;) { |
|
4545 |
Object e = it.next(); |
|
4546 |
sb.append(e == this ? "(this Collection)" : e); |
|
4547 |
if (!it.hasNext()) |
|
4548 |
break; |
|
4549 |
sb.append(',').append(' '); |
|
4550 |
} |
|
4551 |
} |
|
4552 |
return sb.append(']').toString(); |
|
4553 |
} |
|
4554 |
||
4555 |
public final boolean containsAll(Collection<?> c) { |
|
4556 |
if (c != this) { |
|
4557 |
for (Object e : c) { |
|
4558 |
if (e == null || !contains(e)) |
|
4559 |
return false; |
|
4560 |
} |
|
4561 |
} |
|
4562 |
return true; |
|
4563 |
} |
|
4564 |
||
39779
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4565 |
public boolean removeAll(Collection<?> c) { |
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
4566 |
if (c == null) throw new NullPointerException(); |
17945 | 4567 |
boolean modified = false; |
39779
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4568 |
// Use (c instanceof Set) as a hint that lookup in c is as |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4569 |
// efficient as this view |
40547 | 4570 |
Node<K,V>[] t; |
4571 |
if ((t = map.table) == null) { |
|
4572 |
return false; |
|
4573 |
} else if (c instanceof Set<?> && c.size() > t.length) { |
|
39779
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4574 |
for (Iterator<?> it = iterator(); it.hasNext(); ) { |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4575 |
if (c.contains(it.next())) { |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4576 |
it.remove(); |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4577 |
modified = true; |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4578 |
} |
17945 | 4579 |
} |
39779
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4580 |
} else { |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4581 |
for (Object e : c) |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4582 |
modified |= remove(e); |
17945 | 4583 |
} |
4584 |
return modified; |
|
4585 |
} |
|
4586 |
||
4587 |
public final boolean retainAll(Collection<?> c) { |
|
21981
48b31d370bc9
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
19862
diff
changeset
|
4588 |
if (c == null) throw new NullPointerException(); |
17945 | 4589 |
boolean modified = false; |
4590 |
for (Iterator<E> it = iterator(); it.hasNext();) { |
|
4591 |
if (!c.contains(it.next())) { |
|
4592 |
it.remove(); |
|
4593 |
modified = true; |
|
4594 |
} |
|
4595 |
} |
|
4596 |
return modified; |
|
4597 |
} |
|
4598 |
||
4599 |
} |
|
4600 |
||
4601 |
/** |
|
4602 |
* A view of a ConcurrentHashMap as a {@link Set} of keys, in |
|
4603 |
* which additions may optionally be enabled by mapping to a |
|
4604 |
* common value. This class cannot be directly instantiated. |
|
4605 |
* See {@link #keySet() keySet()}, |
|
4606 |
* {@link #keySet(Object) keySet(V)}, |
|
4607 |
* {@link #newKeySet() newKeySet()}, |
|
4608 |
* {@link #newKeySet(int) newKeySet(int)}. |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
4609 |
* |
17953
9a56976d1ab2
8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents:
17945
diff
changeset
|
4610 |
* @since 1.8 |
17945 | 4611 |
*/ |
4612 |
public static class KeySetView<K,V> extends CollectionView<K,V,K> |
|
4613 |
implements Set<K>, java.io.Serializable { |
|
4614 |
private static final long serialVersionUID = 7249069246763182397L; |
|
4615 |
private final V value; |
|
4616 |
KeySetView(ConcurrentHashMap<K,V> map, V value) { // non-public |
|
4617 |
super(map); |
|
4618 |
this.value = value; |
|
4619 |
} |
|
4620 |
||
4621 |
/** |
|
4622 |
* Returns the default mapped value for additions, |
|
4623 |
* or {@code null} if additions are not supported. |
|
4624 |
* |
|
4625 |
* @return the default mapped value for additions, or {@code null} |
|
4626 |
* if not supported |
|
4627 |
*/ |
|
4628 |
public V getMappedValue() { return value; } |
|
4629 |
||
4630 |
/** |
|
4631 |
* {@inheritDoc} |
|
4632 |
* @throws NullPointerException if the specified key is null |
|
4633 |
*/ |
|
4634 |
public boolean contains(Object o) { return map.containsKey(o); } |
|
4635 |
||
4636 |
/** |
|
4637 |
* Removes the key from this map view, by removing the key (and its |
|
4638 |
* corresponding value) from the backing map. This method does |
|
4639 |
* nothing if the key is not in the map. |
|
4640 |
* |
|
4641 |
* @param o the key to be removed from the backing map |
|
4642 |
* @return {@code true} if the backing map contained the specified key |
|
4643 |
* @throws NullPointerException if the specified key is null |
|
4644 |
*/ |
|
4645 |
public boolean remove(Object o) { return map.remove(o) != null; } |
|
4646 |
||
4647 |
/** |
|
4648 |
* @return an iterator over the keys of the backing map |
|
4649 |
*/ |
|
4650 |
public Iterator<K> iterator() { |
|
4651 |
Node<K,V>[] t; |
|
4652 |
ConcurrentHashMap<K,V> m = map; |
|
4653 |
int f = (t = m.table) == null ? 0 : t.length; |
|
4654 |
return new KeyIterator<K,V>(t, f, 0, f, m); |
|
4655 |
} |
|
4656 |
||
4657 |
/** |
|
4658 |
* Adds the specified key to this set view by mapping the key to |
|
4659 |
* the default mapped value in the backing map, if defined. |
|
4660 |
* |
|
4661 |
* @param e key to be added |
|
4662 |
* @return {@code true} if this set changed as a result of the call |
|
4663 |
* @throws NullPointerException if the specified key is null |
|
4664 |
* @throws UnsupportedOperationException if no default mapped value |
|
4665 |
* for additions was provided |
|
4666 |
*/ |
|
4667 |
public boolean add(K e) { |
|
4668 |
V v; |
|
4669 |
if ((v = value) == null) |
|
4670 |
throw new UnsupportedOperationException(); |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
4671 |
return map.putVal(e, v, true) == null; |
17945 | 4672 |
} |
4673 |
||
4674 |
/** |
|
4675 |
* Adds all of the elements in the specified collection to this set, |
|
4676 |
* as if by calling {@link #add} on each one. |
|
4677 |
* |
|
4678 |
* @param c the elements to be inserted into this set |
|
4679 |
* @return {@code true} if this set changed as a result of the call |
|
4680 |
* @throws NullPointerException if the collection or any of its |
|
4681 |
* elements are {@code null} |
|
4682 |
* @throws UnsupportedOperationException if no default mapped value |
|
4683 |
* for additions was provided |
|
4684 |
*/ |
|
4685 |
public boolean addAll(Collection<? extends K> c) { |
|
4686 |
boolean added = false; |
|
4687 |
V v; |
|
4688 |
if ((v = value) == null) |
|
4689 |
throw new UnsupportedOperationException(); |
|
4690 |
for (K e : c) { |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
4691 |
if (map.putVal(e, v, true) == null) |
17945 | 4692 |
added = true; |
4693 |
} |
|
4694 |
return added; |
|
4695 |
} |
|
4696 |
||
4697 |
public int hashCode() { |
|
4698 |
int h = 0; |
|
4699 |
for (K e : this) |
|
4700 |
h += e.hashCode(); |
|
4701 |
return h; |
|
4702 |
} |
|
4703 |
||
4704 |
public boolean equals(Object o) { |
|
4705 |
Set<?> c; |
|
4706 |
return ((o instanceof Set) && |
|
4707 |
((c = (Set<?>)o) == this || |
|
4708 |
(containsAll(c) && c.containsAll(this)))); |
|
4709 |
} |
|
4710 |
||
4711 |
public Spliterator<K> spliterator() { |
|
4712 |
Node<K,V>[] t; |
|
4713 |
ConcurrentHashMap<K,V> m = map; |
|
4714 |
long n = m.sumCount(); |
|
4715 |
int f = (t = m.table) == null ? 0 : t.length; |
|
4716 |
return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n); |
|
4717 |
} |
|
4718 |
||
4719 |
public void forEach(Consumer<? super K> action) { |
|
4720 |
if (action == null) throw new NullPointerException(); |
|
4721 |
Node<K,V>[] t; |
|
4722 |
if ((t = map.table) != null) { |
|
4723 |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
|
4724 |
for (Node<K,V> p; (p = it.advance()) != null; ) |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
4725 |
action.accept(p.key); |
17945 | 4726 |
} |
4727 |
} |
|
4728 |
} |
|
4729 |
||
4730 |
/** |
|
4731 |
* A view of a ConcurrentHashMap as a {@link Collection} of |
|
4732 |
* values, in which additions are disabled. This class cannot be |
|
4733 |
* directly instantiated. See {@link #values()}. |
|
4734 |
*/ |
|
4735 |
static final class ValuesView<K,V> extends CollectionView<K,V,V> |
|
4736 |
implements Collection<V>, java.io.Serializable { |
|
4737 |
private static final long serialVersionUID = 2249069246763182397L; |
|
4738 |
ValuesView(ConcurrentHashMap<K,V> map) { super(map); } |
|
4739 |
public final boolean contains(Object o) { |
|
4740 |
return map.containsValue(o); |
|
4741 |
} |
|
4742 |
||
4743 |
public final boolean remove(Object o) { |
|
4744 |
if (o != null) { |
|
4745 |
for (Iterator<V> it = iterator(); it.hasNext();) { |
|
4746 |
if (o.equals(it.next())) { |
|
4747 |
it.remove(); |
|
4748 |
return true; |
|
4749 |
} |
|
4750 |
} |
|
4751 |
} |
|
4752 |
return false; |
|
4753 |
} |
|
4754 |
||
4755 |
public final Iterator<V> iterator() { |
|
4756 |
ConcurrentHashMap<K,V> m = map; |
|
4757 |
Node<K,V>[] t; |
|
4758 |
int f = (t = m.table) == null ? 0 : t.length; |
|
4759 |
return new ValueIterator<K,V>(t, f, 0, f, m); |
|
4760 |
} |
|
4761 |
||
4762 |
public final boolean add(V e) { |
|
4763 |
throw new UnsupportedOperationException(); |
|
4764 |
} |
|
4765 |
public final boolean addAll(Collection<? extends V> c) { |
|
4766 |
throw new UnsupportedOperationException(); |
|
4767 |
} |
|
4768 |
||
39779
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4769 |
@Override public boolean removeAll(Collection<?> c) { |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4770 |
if (c == null) throw new NullPointerException(); |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4771 |
boolean modified = false; |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4772 |
for (Iterator<V> it = iterator(); it.hasNext();) { |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4773 |
if (c.contains(it.next())) { |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4774 |
it.remove(); |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4775 |
modified = true; |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4776 |
} |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4777 |
} |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4778 |
return modified; |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4779 |
} |
4666307d3155
8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents:
39725
diff
changeset
|
4780 |
|
30441
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
4781 |
public boolean removeIf(Predicate<? super V> filter) { |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
4782 |
return map.removeValueIf(filter); |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
4783 |
} |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
4784 |
|
17945 | 4785 |
public Spliterator<V> spliterator() { |
4786 |
Node<K,V>[] t; |
|
4787 |
ConcurrentHashMap<K,V> m = map; |
|
4788 |
long n = m.sumCount(); |
|
4789 |
int f = (t = m.table) == null ? 0 : t.length; |
|
4790 |
return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n); |
|
4791 |
} |
|
4792 |
||
4793 |
public void forEach(Consumer<? super V> action) { |
|
4794 |
if (action == null) throw new NullPointerException(); |
|
4795 |
Node<K,V>[] t; |
|
4796 |
if ((t = map.table) != null) { |
|
4797 |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
|
4798 |
for (Node<K,V> p; (p = it.advance()) != null; ) |
|
4799 |
action.accept(p.val); |
|
4800 |
} |
|
4801 |
} |
|
4802 |
} |
|
4803 |
||
4804 |
/** |
|
4805 |
* A view of a ConcurrentHashMap as a {@link Set} of (key, value) |
|
4806 |
* entries. This class cannot be directly instantiated. See |
|
4807 |
* {@link #entrySet()}. |
|
4808 |
*/ |
|
4809 |
static final class EntrySetView<K,V> extends CollectionView<K,V,Map.Entry<K,V>> |
|
4810 |
implements Set<Map.Entry<K,V>>, java.io.Serializable { |
|
4811 |
private static final long serialVersionUID = 2249069246763182397L; |
|
4812 |
EntrySetView(ConcurrentHashMap<K,V> map) { super(map); } |
|
4813 |
||
4814 |
public boolean contains(Object o) { |
|
4815 |
Object k, v, r; Map.Entry<?,?> e; |
|
4816 |
return ((o instanceof Map.Entry) && |
|
4817 |
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
|
4818 |
(r = map.get(k)) != null && |
|
4819 |
(v = e.getValue()) != null && |
|
4820 |
(v == r || v.equals(r))); |
|
4821 |
} |
|
4822 |
||
4823 |
public boolean remove(Object o) { |
|
4824 |
Object k, v; Map.Entry<?,?> e; |
|
4825 |
return ((o instanceof Map.Entry) && |
|
4826 |
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
|
4827 |
(v = e.getValue()) != null && |
|
4828 |
map.remove(k, v)); |
|
4829 |
} |
|
4830 |
||
4831 |
/** |
|
4832 |
* @return an iterator over the entries of the backing map |
|
4833 |
*/ |
|
4834 |
public Iterator<Map.Entry<K,V>> iterator() { |
|
4835 |
ConcurrentHashMap<K,V> m = map; |
|
4836 |
Node<K,V>[] t; |
|
4837 |
int f = (t = m.table) == null ? 0 : t.length; |
|
4838 |
return new EntryIterator<K,V>(t, f, 0, f, m); |
|
4839 |
} |
|
4840 |
||
4841 |
public boolean add(Entry<K,V> e) { |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
4842 |
return map.putVal(e.getKey(), e.getValue(), false) == null; |
17945 | 4843 |
} |
4844 |
||
4845 |
public boolean addAll(Collection<? extends Entry<K,V>> c) { |
|
4846 |
boolean added = false; |
|
4847 |
for (Entry<K,V> e : c) { |
|
4848 |
if (add(e)) |
|
4849 |
added = true; |
|
4850 |
} |
|
4851 |
return added; |
|
4852 |
} |
|
4853 |
||
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
4854 |
public boolean removeIf(Predicate<? super Entry<K,V>> filter) { |
30441
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
4855 |
return map.removeEntryIf(filter); |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
4856 |
} |
415a5242e046
8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents:
25859
diff
changeset
|
4857 |
|
17945 | 4858 |
public final int hashCode() { |
4859 |
int h = 0; |
|
4860 |
Node<K,V>[] t; |
|
4861 |
if ((t = map.table) != null) { |
|
4862 |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
|
4863 |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
|
4864 |
h += p.hashCode(); |
|
4865 |
} |
|
4866 |
} |
|
4867 |
return h; |
|
4868 |
} |
|
4869 |
||
4870 |
public final boolean equals(Object o) { |
|
4871 |
Set<?> c; |
|
4872 |
return ((o instanceof Set) && |
|
4873 |
((c = (Set<?>)o) == this || |
|
4874 |
(containsAll(c) && c.containsAll(this)))); |
|
4875 |
} |
|
4876 |
||
4877 |
public Spliterator<Map.Entry<K,V>> spliterator() { |
|
4878 |
Node<K,V>[] t; |
|
4879 |
ConcurrentHashMap<K,V> m = map; |
|
4880 |
long n = m.sumCount(); |
|
4881 |
int f = (t = m.table) == null ? 0 : t.length; |
|
4882 |
return new EntrySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n, m); |
|
4883 |
} |
|
4884 |
||
4885 |
public void forEach(Consumer<? super Map.Entry<K,V>> action) { |
|
4886 |
if (action == null) throw new NullPointerException(); |
|
4887 |
Node<K,V>[] t; |
|
4888 |
if ((t = map.table) != null) { |
|
4889 |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
|
4890 |
for (Node<K,V> p; (p = it.advance()) != null; ) |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
4891 |
action.accept(new MapEntry<K,V>(p.key, p.val, map)); |
17945 | 4892 |
} |
4893 |
} |
|
4894 |
||
4895 |
} |
|
4896 |
||
4897 |
// ------------------------------------------------------- |
|
4898 |
||
4899 |
/** |
|
4900 |
* Base class for bulk tasks. Repeats some fields and code from |
|
4901 |
* class Traverser, because we need to subclass CountedCompleter. |
|
4902 |
*/ |
|
19380 | 4903 |
@SuppressWarnings("serial") |
17945 | 4904 |
abstract static class BulkTask<K,V,R> extends CountedCompleter<R> { |
4905 |
Node<K,V>[] tab; // same as Traverser |
|
4906 |
Node<K,V> next; |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4907 |
TableStack<K,V> stack, spare; |
17945 | 4908 |
int index; |
4909 |
int baseIndex; |
|
4910 |
int baseLimit; |
|
4911 |
final int baseSize; |
|
4912 |
int batch; // split control |
|
4913 |
||
4914 |
BulkTask(BulkTask<K,V,?> par, int b, int i, int f, Node<K,V>[] t) { |
|
4915 |
super(par); |
|
4916 |
this.batch = b; |
|
4917 |
this.index = this.baseIndex = i; |
|
4918 |
if ((this.tab = t) == null) |
|
4919 |
this.baseSize = this.baseLimit = 0; |
|
4920 |
else if (par == null) |
|
4921 |
this.baseSize = this.baseLimit = t.length; |
|
4922 |
else { |
|
4923 |
this.baseLimit = f; |
|
4924 |
this.baseSize = par.baseSize; |
|
4925 |
} |
|
4926 |
} |
|
4927 |
||
4928 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
4929 |
* Same as Traverser version. |
17945 | 4930 |
*/ |
4931 |
final Node<K,V> advance() { |
|
4932 |
Node<K,V> e; |
|
4933 |
if ((e = next) != null) |
|
4934 |
e = e.next; |
|
4935 |
for (;;) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4936 |
Node<K,V>[] t; int i, n; |
17945 | 4937 |
if (e != null) |
4938 |
return next = e; |
|
4939 |
if (baseIndex >= baseLimit || (t = tab) == null || |
|
4940 |
(n = t.length) <= (i = index) || i < 0) |
|
4941 |
return next = null; |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4942 |
if ((e = tabAt(t, i)) != null && e.hash < 0) { |
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
4943 |
if (e instanceof ForwardingNode) { |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
4944 |
tab = ((ForwardingNode<K,V>)e).nextTable; |
17945 | 4945 |
e = null; |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4946 |
pushState(t, i, n); |
17945 | 4947 |
continue; |
4948 |
} |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
4949 |
else if (e instanceof TreeBin) |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
4950 |
e = ((TreeBin<K,V>)e).first; |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
4951 |
else |
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
4952 |
e = null; |
17945 | 4953 |
} |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4954 |
if (stack != null) |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4955 |
recoverState(n); |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4956 |
else if ((index = i + baseSize) >= n) |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4957 |
index = ++baseIndex; |
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
4958 |
} |
2 | 4959 |
} |
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4960 |
|
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4961 |
private void pushState(Node<K,V>[] t, int i, int n) { |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4962 |
TableStack<K,V> s = spare; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4963 |
if (s != null) |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4964 |
spare = s.next; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4965 |
else |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4966 |
s = new TableStack<K,V>(); |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4967 |
s.tab = t; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4968 |
s.length = n; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4969 |
s.index = i; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4970 |
s.next = stack; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4971 |
stack = s; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4972 |
} |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4973 |
|
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4974 |
private void recoverState(int n) { |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4975 |
TableStack<K,V> s; int len; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4976 |
while ((s = stack) != null && (index += (len = s.length)) >= n) { |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4977 |
n = len; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4978 |
index = s.index; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4979 |
tab = s.tab; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4980 |
s.tab = null; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4981 |
TableStack<K,V> next = s.next; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4982 |
s.next = spare; // save for reuse |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4983 |
stack = next; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4984 |
spare = s; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4985 |
} |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4986 |
if (s == null && (index += baseSize) >= n) |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4987 |
index = ++baseIndex; |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
4988 |
} |
17945 | 4989 |
} |
4990 |
||
4991 |
/* |
|
4992 |
* Task classes. Coded in a regular but ugly format/style to |
|
4993 |
* simplify checks that each variant differs in the right way from |
|
4994 |
* others. The null screenings exist because compilers cannot tell |
|
4995 |
* that we've already null-checked task arguments, so we force |
|
4996 |
* simplest hoisted bypass to help avoid convoluted traps. |
|
4997 |
*/ |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
4998 |
@SuppressWarnings("serial") |
17945 | 4999 |
static final class ForEachKeyTask<K,V> |
5000 |
extends BulkTask<K,V,Void> { |
|
5001 |
final Consumer<? super K> action; |
|
5002 |
ForEachKeyTask |
|
5003 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5004 |
Consumer<? super K> action) { |
|
5005 |
super(p, b, i, f, t); |
|
5006 |
this.action = action; |
|
5007 |
} |
|
5008 |
public final void compute() { |
|
5009 |
final Consumer<? super K> action; |
|
5010 |
if ((action = this.action) != null) { |
|
5011 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5012 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5013 |
addToPendingCount(1); |
|
5014 |
new ForEachKeyTask<K,V> |
|
5015 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5016 |
action).fork(); |
|
5017 |
} |
|
5018 |
for (Node<K,V> p; (p = advance()) != null;) |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5019 |
action.accept(p.key); |
17945 | 5020 |
propagateCompletion(); |
5021 |
} |
|
5022 |
} |
|
5023 |
} |
|
5024 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5025 |
@SuppressWarnings("serial") |
17945 | 5026 |
static final class ForEachValueTask<K,V> |
5027 |
extends BulkTask<K,V,Void> { |
|
5028 |
final Consumer<? super V> action; |
|
5029 |
ForEachValueTask |
|
5030 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5031 |
Consumer<? super V> action) { |
|
5032 |
super(p, b, i, f, t); |
|
5033 |
this.action = action; |
|
5034 |
} |
|
5035 |
public final void compute() { |
|
5036 |
final Consumer<? super V> action; |
|
5037 |
if ((action = this.action) != null) { |
|
5038 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5039 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5040 |
addToPendingCount(1); |
|
5041 |
new ForEachValueTask<K,V> |
|
5042 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5043 |
action).fork(); |
|
5044 |
} |
|
5045 |
for (Node<K,V> p; (p = advance()) != null;) |
|
5046 |
action.accept(p.val); |
|
5047 |
propagateCompletion(); |
|
5048 |
} |
|
5049 |
} |
|
5050 |
} |
|
5051 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5052 |
@SuppressWarnings("serial") |
17945 | 5053 |
static final class ForEachEntryTask<K,V> |
5054 |
extends BulkTask<K,V,Void> { |
|
5055 |
final Consumer<? super Entry<K,V>> action; |
|
5056 |
ForEachEntryTask |
|
5057 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5058 |
Consumer<? super Entry<K,V>> action) { |
|
5059 |
super(p, b, i, f, t); |
|
5060 |
this.action = action; |
|
5061 |
} |
|
5062 |
public final void compute() { |
|
5063 |
final Consumer<? super Entry<K,V>> action; |
|
5064 |
if ((action = this.action) != null) { |
|
5065 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5066 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5067 |
addToPendingCount(1); |
|
5068 |
new ForEachEntryTask<K,V> |
|
5069 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5070 |
action).fork(); |
|
5071 |
} |
|
5072 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
5073 |
action.accept(p); |
|
5074 |
propagateCompletion(); |
|
5075 |
} |
|
5076 |
} |
|
5077 |
} |
|
5078 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5079 |
@SuppressWarnings("serial") |
17945 | 5080 |
static final class ForEachMappingTask<K,V> |
5081 |
extends BulkTask<K,V,Void> { |
|
5082 |
final BiConsumer<? super K, ? super V> action; |
|
5083 |
ForEachMappingTask |
|
5084 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5085 |
BiConsumer<? super K,? super V> action) { |
|
5086 |
super(p, b, i, f, t); |
|
5087 |
this.action = action; |
|
5088 |
} |
|
5089 |
public final void compute() { |
|
5090 |
final BiConsumer<? super K, ? super V> action; |
|
5091 |
if ((action = this.action) != null) { |
|
5092 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5093 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5094 |
addToPendingCount(1); |
|
5095 |
new ForEachMappingTask<K,V> |
|
5096 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5097 |
action).fork(); |
|
5098 |
} |
|
5099 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5100 |
action.accept(p.key, p.val); |
17945 | 5101 |
propagateCompletion(); |
5102 |
} |
|
5103 |
} |
|
5104 |
} |
|
5105 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5106 |
@SuppressWarnings("serial") |
17945 | 5107 |
static final class ForEachTransformedKeyTask<K,V,U> |
5108 |
extends BulkTask<K,V,Void> { |
|
5109 |
final Function<? super K, ? extends U> transformer; |
|
5110 |
final Consumer<? super U> action; |
|
5111 |
ForEachTransformedKeyTask |
|
5112 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5113 |
Function<? super K, ? extends U> transformer, Consumer<? super U> action) { |
|
5114 |
super(p, b, i, f, t); |
|
5115 |
this.transformer = transformer; this.action = action; |
|
5116 |
} |
|
5117 |
public final void compute() { |
|
5118 |
final Function<? super K, ? extends U> transformer; |
|
5119 |
final Consumer<? super U> action; |
|
5120 |
if ((transformer = this.transformer) != null && |
|
5121 |
(action = this.action) != null) { |
|
5122 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5123 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5124 |
addToPendingCount(1); |
|
5125 |
new ForEachTransformedKeyTask<K,V,U> |
|
5126 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5127 |
transformer, action).fork(); |
|
5128 |
} |
|
5129 |
for (Node<K,V> p; (p = advance()) != null; ) { |
|
5130 |
U u; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5131 |
if ((u = transformer.apply(p.key)) != null) |
17945 | 5132 |
action.accept(u); |
5133 |
} |
|
5134 |
propagateCompletion(); |
|
5135 |
} |
|
5136 |
} |
|
5137 |
} |
|
5138 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5139 |
@SuppressWarnings("serial") |
17945 | 5140 |
static final class ForEachTransformedValueTask<K,V,U> |
5141 |
extends BulkTask<K,V,Void> { |
|
5142 |
final Function<? super V, ? extends U> transformer; |
|
5143 |
final Consumer<? super U> action; |
|
5144 |
ForEachTransformedValueTask |
|
5145 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5146 |
Function<? super V, ? extends U> transformer, Consumer<? super U> action) { |
|
5147 |
super(p, b, i, f, t); |
|
5148 |
this.transformer = transformer; this.action = action; |
|
5149 |
} |
|
5150 |
public final void compute() { |
|
5151 |
final Function<? super V, ? extends U> transformer; |
|
5152 |
final Consumer<? super U> action; |
|
5153 |
if ((transformer = this.transformer) != null && |
|
5154 |
(action = this.action) != null) { |
|
5155 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5156 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5157 |
addToPendingCount(1); |
|
5158 |
new ForEachTransformedValueTask<K,V,U> |
|
5159 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5160 |
transformer, action).fork(); |
|
5161 |
} |
|
5162 |
for (Node<K,V> p; (p = advance()) != null; ) { |
|
5163 |
U u; |
|
5164 |
if ((u = transformer.apply(p.val)) != null) |
|
5165 |
action.accept(u); |
|
5166 |
} |
|
5167 |
propagateCompletion(); |
|
5168 |
} |
|
5169 |
} |
|
5170 |
} |
|
5171 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5172 |
@SuppressWarnings("serial") |
17945 | 5173 |
static final class ForEachTransformedEntryTask<K,V,U> |
5174 |
extends BulkTask<K,V,Void> { |
|
5175 |
final Function<Map.Entry<K,V>, ? extends U> transformer; |
|
5176 |
final Consumer<? super U> action; |
|
5177 |
ForEachTransformedEntryTask |
|
5178 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5179 |
Function<Map.Entry<K,V>, ? extends U> transformer, Consumer<? super U> action) { |
|
5180 |
super(p, b, i, f, t); |
|
5181 |
this.transformer = transformer; this.action = action; |
|
5182 |
} |
|
5183 |
public final void compute() { |
|
5184 |
final Function<Map.Entry<K,V>, ? extends U> transformer; |
|
5185 |
final Consumer<? super U> action; |
|
5186 |
if ((transformer = this.transformer) != null && |
|
5187 |
(action = this.action) != null) { |
|
5188 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5189 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5190 |
addToPendingCount(1); |
|
5191 |
new ForEachTransformedEntryTask<K,V,U> |
|
5192 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5193 |
transformer, action).fork(); |
|
5194 |
} |
|
5195 |
for (Node<K,V> p; (p = advance()) != null; ) { |
|
5196 |
U u; |
|
5197 |
if ((u = transformer.apply(p)) != null) |
|
5198 |
action.accept(u); |
|
5199 |
} |
|
5200 |
propagateCompletion(); |
|
5201 |
} |
|
5202 |
} |
|
5203 |
} |
|
5204 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5205 |
@SuppressWarnings("serial") |
17945 | 5206 |
static final class ForEachTransformedMappingTask<K,V,U> |
5207 |
extends BulkTask<K,V,Void> { |
|
5208 |
final BiFunction<? super K, ? super V, ? extends U> transformer; |
|
5209 |
final Consumer<? super U> action; |
|
5210 |
ForEachTransformedMappingTask |
|
5211 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5212 |
BiFunction<? super K, ? super V, ? extends U> transformer, |
|
5213 |
Consumer<? super U> action) { |
|
5214 |
super(p, b, i, f, t); |
|
5215 |
this.transformer = transformer; this.action = action; |
|
5216 |
} |
|
5217 |
public final void compute() { |
|
5218 |
final BiFunction<? super K, ? super V, ? extends U> transformer; |
|
5219 |
final Consumer<? super U> action; |
|
5220 |
if ((transformer = this.transformer) != null && |
|
5221 |
(action = this.action) != null) { |
|
5222 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5223 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5224 |
addToPendingCount(1); |
|
5225 |
new ForEachTransformedMappingTask<K,V,U> |
|
5226 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5227 |
transformer, action).fork(); |
|
5228 |
} |
|
5229 |
for (Node<K,V> p; (p = advance()) != null; ) { |
|
5230 |
U u; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5231 |
if ((u = transformer.apply(p.key, p.val)) != null) |
17945 | 5232 |
action.accept(u); |
5233 |
} |
|
5234 |
propagateCompletion(); |
|
5235 |
} |
|
5236 |
} |
|
5237 |
} |
|
5238 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5239 |
@SuppressWarnings("serial") |
17945 | 5240 |
static final class SearchKeysTask<K,V,U> |
5241 |
extends BulkTask<K,V,U> { |
|
5242 |
final Function<? super K, ? extends U> searchFunction; |
|
5243 |
final AtomicReference<U> result; |
|
5244 |
SearchKeysTask |
|
5245 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5246 |
Function<? super K, ? extends U> searchFunction, |
|
5247 |
AtomicReference<U> result) { |
|
5248 |
super(p, b, i, f, t); |
|
5249 |
this.searchFunction = searchFunction; this.result = result; |
|
5250 |
} |
|
5251 |
public final U getRawResult() { return result.get(); } |
|
5252 |
public final void compute() { |
|
5253 |
final Function<? super K, ? extends U> searchFunction; |
|
5254 |
final AtomicReference<U> result; |
|
5255 |
if ((searchFunction = this.searchFunction) != null && |
|
5256 |
(result = this.result) != null) { |
|
5257 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5258 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5259 |
if (result.get() != null) |
|
5260 |
return; |
|
5261 |
addToPendingCount(1); |
|
5262 |
new SearchKeysTask<K,V,U> |
|
5263 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5264 |
searchFunction, result).fork(); |
|
5265 |
} |
|
5266 |
while (result.get() == null) { |
|
5267 |
U u; |
|
5268 |
Node<K,V> p; |
|
5269 |
if ((p = advance()) == null) { |
|
5270 |
propagateCompletion(); |
|
5271 |
break; |
|
5272 |
} |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5273 |
if ((u = searchFunction.apply(p.key)) != null) { |
17945 | 5274 |
if (result.compareAndSet(null, u)) |
5275 |
quietlyCompleteRoot(); |
|
5276 |
break; |
|
5277 |
} |
|
5278 |
} |
|
5279 |
} |
|
5280 |
} |
|
5281 |
} |
|
5282 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5283 |
@SuppressWarnings("serial") |
17945 | 5284 |
static final class SearchValuesTask<K,V,U> |
5285 |
extends BulkTask<K,V,U> { |
|
5286 |
final Function<? super V, ? extends U> searchFunction; |
|
5287 |
final AtomicReference<U> result; |
|
5288 |
SearchValuesTask |
|
5289 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5290 |
Function<? super V, ? extends U> searchFunction, |
|
5291 |
AtomicReference<U> result) { |
|
5292 |
super(p, b, i, f, t); |
|
5293 |
this.searchFunction = searchFunction; this.result = result; |
|
5294 |
} |
|
5295 |
public final U getRawResult() { return result.get(); } |
|
5296 |
public final void compute() { |
|
5297 |
final Function<? super V, ? extends U> searchFunction; |
|
5298 |
final AtomicReference<U> result; |
|
5299 |
if ((searchFunction = this.searchFunction) != null && |
|
5300 |
(result = this.result) != null) { |
|
5301 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5302 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5303 |
if (result.get() != null) |
|
5304 |
return; |
|
5305 |
addToPendingCount(1); |
|
5306 |
new SearchValuesTask<K,V,U> |
|
5307 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5308 |
searchFunction, result).fork(); |
|
5309 |
} |
|
5310 |
while (result.get() == null) { |
|
5311 |
U u; |
|
5312 |
Node<K,V> p; |
|
5313 |
if ((p = advance()) == null) { |
|
5314 |
propagateCompletion(); |
|
5315 |
break; |
|
5316 |
} |
|
5317 |
if ((u = searchFunction.apply(p.val)) != null) { |
|
5318 |
if (result.compareAndSet(null, u)) |
|
5319 |
quietlyCompleteRoot(); |
|
5320 |
break; |
|
5321 |
} |
|
5322 |
} |
|
5323 |
} |
|
5324 |
} |
|
5325 |
} |
|
5326 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5327 |
@SuppressWarnings("serial") |
17945 | 5328 |
static final class SearchEntriesTask<K,V,U> |
5329 |
extends BulkTask<K,V,U> { |
|
5330 |
final Function<Entry<K,V>, ? extends U> searchFunction; |
|
5331 |
final AtomicReference<U> result; |
|
5332 |
SearchEntriesTask |
|
5333 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5334 |
Function<Entry<K,V>, ? extends U> searchFunction, |
|
5335 |
AtomicReference<U> result) { |
|
5336 |
super(p, b, i, f, t); |
|
5337 |
this.searchFunction = searchFunction; this.result = result; |
|
5338 |
} |
|
5339 |
public final U getRawResult() { return result.get(); } |
|
5340 |
public final void compute() { |
|
5341 |
final Function<Entry<K,V>, ? extends U> searchFunction; |
|
5342 |
final AtomicReference<U> result; |
|
5343 |
if ((searchFunction = this.searchFunction) != null && |
|
5344 |
(result = this.result) != null) { |
|
5345 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5346 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5347 |
if (result.get() != null) |
|
5348 |
return; |
|
5349 |
addToPendingCount(1); |
|
5350 |
new SearchEntriesTask<K,V,U> |
|
5351 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5352 |
searchFunction, result).fork(); |
|
5353 |
} |
|
5354 |
while (result.get() == null) { |
|
5355 |
U u; |
|
5356 |
Node<K,V> p; |
|
5357 |
if ((p = advance()) == null) { |
|
5358 |
propagateCompletion(); |
|
5359 |
break; |
|
5360 |
} |
|
5361 |
if ((u = searchFunction.apply(p)) != null) { |
|
5362 |
if (result.compareAndSet(null, u)) |
|
5363 |
quietlyCompleteRoot(); |
|
5364 |
return; |
|
5365 |
} |
|
5366 |
} |
|
5367 |
} |
|
5368 |
} |
|
5369 |
} |
|
5370 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5371 |
@SuppressWarnings("serial") |
17945 | 5372 |
static final class SearchMappingsTask<K,V,U> |
5373 |
extends BulkTask<K,V,U> { |
|
5374 |
final BiFunction<? super K, ? super V, ? extends U> searchFunction; |
|
5375 |
final AtomicReference<U> result; |
|
5376 |
SearchMappingsTask |
|
5377 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5378 |
BiFunction<? super K, ? super V, ? extends U> searchFunction, |
|
5379 |
AtomicReference<U> result) { |
|
5380 |
super(p, b, i, f, t); |
|
5381 |
this.searchFunction = searchFunction; this.result = result; |
|
5382 |
} |
|
5383 |
public final U getRawResult() { return result.get(); } |
|
5384 |
public final void compute() { |
|
5385 |
final BiFunction<? super K, ? super V, ? extends U> searchFunction; |
|
5386 |
final AtomicReference<U> result; |
|
5387 |
if ((searchFunction = this.searchFunction) != null && |
|
5388 |
(result = this.result) != null) { |
|
5389 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5390 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5391 |
if (result.get() != null) |
|
5392 |
return; |
|
5393 |
addToPendingCount(1); |
|
5394 |
new SearchMappingsTask<K,V,U> |
|
5395 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5396 |
searchFunction, result).fork(); |
|
5397 |
} |
|
5398 |
while (result.get() == null) { |
|
5399 |
U u; |
|
5400 |
Node<K,V> p; |
|
5401 |
if ((p = advance()) == null) { |
|
5402 |
propagateCompletion(); |
|
5403 |
break; |
|
5404 |
} |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5405 |
if ((u = searchFunction.apply(p.key, p.val)) != null) { |
17945 | 5406 |
if (result.compareAndSet(null, u)) |
5407 |
quietlyCompleteRoot(); |
|
5408 |
break; |
|
5409 |
} |
|
5410 |
} |
|
5411 |
} |
|
5412 |
} |
|
5413 |
} |
|
5414 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5415 |
@SuppressWarnings("serial") |
17945 | 5416 |
static final class ReduceKeysTask<K,V> |
5417 |
extends BulkTask<K,V,K> { |
|
5418 |
final BiFunction<? super K, ? super K, ? extends K> reducer; |
|
5419 |
K result; |
|
5420 |
ReduceKeysTask<K,V> rights, nextRight; |
|
5421 |
ReduceKeysTask |
|
5422 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5423 |
ReduceKeysTask<K,V> nextRight, |
|
5424 |
BiFunction<? super K, ? super K, ? extends K> reducer) { |
|
5425 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
5426 |
this.reducer = reducer; |
|
5427 |
} |
|
5428 |
public final K getRawResult() { return result; } |
|
5429 |
public final void compute() { |
|
5430 |
final BiFunction<? super K, ? super K, ? extends K> reducer; |
|
5431 |
if ((reducer = this.reducer) != null) { |
|
5432 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5433 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5434 |
addToPendingCount(1); |
|
5435 |
(rights = new ReduceKeysTask<K,V> |
|
5436 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5437 |
rights, reducer)).fork(); |
|
5438 |
} |
|
5439 |
K r = null; |
|
5440 |
for (Node<K,V> p; (p = advance()) != null; ) { |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5441 |
K u = p.key; |
17945 | 5442 |
r = (r == null) ? u : u == null ? r : reducer.apply(r, u); |
5443 |
} |
|
5444 |
result = r; |
|
5445 |
CountedCompleter<?> c; |
|
5446 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5447 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5448 |
ReduceKeysTask<K,V> |
17945 | 5449 |
t = (ReduceKeysTask<K,V>)c, |
5450 |
s = t.rights; |
|
5451 |
while (s != null) { |
|
5452 |
K tr, sr; |
|
5453 |
if ((sr = s.result) != null) |
|
5454 |
t.result = (((tr = t.result) == null) ? sr : |
|
5455 |
reducer.apply(tr, sr)); |
|
5456 |
s = t.rights = s.nextRight; |
|
5457 |
} |
|
5458 |
} |
|
5459 |
} |
|
5460 |
} |
|
5461 |
} |
|
5462 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5463 |
@SuppressWarnings("serial") |
17945 | 5464 |
static final class ReduceValuesTask<K,V> |
5465 |
extends BulkTask<K,V,V> { |
|
5466 |
final BiFunction<? super V, ? super V, ? extends V> reducer; |
|
5467 |
V result; |
|
5468 |
ReduceValuesTask<K,V> rights, nextRight; |
|
5469 |
ReduceValuesTask |
|
5470 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5471 |
ReduceValuesTask<K,V> nextRight, |
|
5472 |
BiFunction<? super V, ? super V, ? extends V> reducer) { |
|
5473 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
5474 |
this.reducer = reducer; |
|
5475 |
} |
|
5476 |
public final V getRawResult() { return result; } |
|
5477 |
public final void compute() { |
|
5478 |
final BiFunction<? super V, ? super V, ? extends V> reducer; |
|
5479 |
if ((reducer = this.reducer) != null) { |
|
5480 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5481 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5482 |
addToPendingCount(1); |
|
5483 |
(rights = new ReduceValuesTask<K,V> |
|
5484 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5485 |
rights, reducer)).fork(); |
|
5486 |
} |
|
5487 |
V r = null; |
|
5488 |
for (Node<K,V> p; (p = advance()) != null; ) { |
|
5489 |
V v = p.val; |
|
5490 |
r = (r == null) ? v : reducer.apply(r, v); |
|
5491 |
} |
|
5492 |
result = r; |
|
5493 |
CountedCompleter<?> c; |
|
5494 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5495 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5496 |
ReduceValuesTask<K,V> |
17945 | 5497 |
t = (ReduceValuesTask<K,V>)c, |
5498 |
s = t.rights; |
|
5499 |
while (s != null) { |
|
5500 |
V tr, sr; |
|
5501 |
if ((sr = s.result) != null) |
|
5502 |
t.result = (((tr = t.result) == null) ? sr : |
|
5503 |
reducer.apply(tr, sr)); |
|
5504 |
s = t.rights = s.nextRight; |
|
5505 |
} |
|
5506 |
} |
|
5507 |
} |
|
5508 |
} |
|
5509 |
} |
|
5510 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5511 |
@SuppressWarnings("serial") |
17945 | 5512 |
static final class ReduceEntriesTask<K,V> |
5513 |
extends BulkTask<K,V,Map.Entry<K,V>> { |
|
5514 |
final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer; |
|
5515 |
Map.Entry<K,V> result; |
|
5516 |
ReduceEntriesTask<K,V> rights, nextRight; |
|
5517 |
ReduceEntriesTask |
|
5518 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5519 |
ReduceEntriesTask<K,V> nextRight, |
|
5520 |
BiFunction<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
|
5521 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
5522 |
this.reducer = reducer; |
|
5523 |
} |
|
5524 |
public final Map.Entry<K,V> getRawResult() { return result; } |
|
5525 |
public final void compute() { |
|
5526 |
final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer; |
|
5527 |
if ((reducer = this.reducer) != null) { |
|
5528 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5529 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5530 |
addToPendingCount(1); |
|
5531 |
(rights = new ReduceEntriesTask<K,V> |
|
5532 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5533 |
rights, reducer)).fork(); |
|
5534 |
} |
|
5535 |
Map.Entry<K,V> r = null; |
|
5536 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
5537 |
r = (r == null) ? p : reducer.apply(r, p); |
|
5538 |
result = r; |
|
5539 |
CountedCompleter<?> c; |
|
5540 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5541 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5542 |
ReduceEntriesTask<K,V> |
17945 | 5543 |
t = (ReduceEntriesTask<K,V>)c, |
5544 |
s = t.rights; |
|
5545 |
while (s != null) { |
|
5546 |
Map.Entry<K,V> tr, sr; |
|
5547 |
if ((sr = s.result) != null) |
|
5548 |
t.result = (((tr = t.result) == null) ? sr : |
|
5549 |
reducer.apply(tr, sr)); |
|
5550 |
s = t.rights = s.nextRight; |
|
5551 |
} |
|
5552 |
} |
|
5553 |
} |
|
5554 |
} |
|
5555 |
} |
|
5556 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5557 |
@SuppressWarnings("serial") |
17945 | 5558 |
static final class MapReduceKeysTask<K,V,U> |
5559 |
extends BulkTask<K,V,U> { |
|
5560 |
final Function<? super K, ? extends U> transformer; |
|
5561 |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
|
5562 |
U result; |
|
5563 |
MapReduceKeysTask<K,V,U> rights, nextRight; |
|
5564 |
MapReduceKeysTask |
|
5565 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5566 |
MapReduceKeysTask<K,V,U> nextRight, |
|
5567 |
Function<? super K, ? extends U> transformer, |
|
5568 |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
|
5569 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
5570 |
this.transformer = transformer; |
|
5571 |
this.reducer = reducer; |
|
5572 |
} |
|
5573 |
public final U getRawResult() { return result; } |
|
5574 |
public final void compute() { |
|
5575 |
final Function<? super K, ? extends U> transformer; |
|
5576 |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
|
5577 |
if ((transformer = this.transformer) != null && |
|
5578 |
(reducer = this.reducer) != null) { |
|
5579 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5580 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5581 |
addToPendingCount(1); |
|
5582 |
(rights = new MapReduceKeysTask<K,V,U> |
|
5583 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5584 |
rights, transformer, reducer)).fork(); |
|
5585 |
} |
|
5586 |
U r = null; |
|
5587 |
for (Node<K,V> p; (p = advance()) != null; ) { |
|
5588 |
U u; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5589 |
if ((u = transformer.apply(p.key)) != null) |
17945 | 5590 |
r = (r == null) ? u : reducer.apply(r, u); |
5591 |
} |
|
5592 |
result = r; |
|
5593 |
CountedCompleter<?> c; |
|
5594 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5595 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5596 |
MapReduceKeysTask<K,V,U> |
17945 | 5597 |
t = (MapReduceKeysTask<K,V,U>)c, |
5598 |
s = t.rights; |
|
5599 |
while (s != null) { |
|
5600 |
U tr, sr; |
|
5601 |
if ((sr = s.result) != null) |
|
5602 |
t.result = (((tr = t.result) == null) ? sr : |
|
5603 |
reducer.apply(tr, sr)); |
|
5604 |
s = t.rights = s.nextRight; |
|
5605 |
} |
|
5606 |
} |
|
5607 |
} |
|
5608 |
} |
|
5609 |
} |
|
5610 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5611 |
@SuppressWarnings("serial") |
17945 | 5612 |
static final class MapReduceValuesTask<K,V,U> |
5613 |
extends BulkTask<K,V,U> { |
|
5614 |
final Function<? super V, ? extends U> transformer; |
|
5615 |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
|
5616 |
U result; |
|
5617 |
MapReduceValuesTask<K,V,U> rights, nextRight; |
|
5618 |
MapReduceValuesTask |
|
5619 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5620 |
MapReduceValuesTask<K,V,U> nextRight, |
|
5621 |
Function<? super V, ? extends U> transformer, |
|
5622 |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
|
5623 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
5624 |
this.transformer = transformer; |
|
5625 |
this.reducer = reducer; |
|
5626 |
} |
|
5627 |
public final U getRawResult() { return result; } |
|
5628 |
public final void compute() { |
|
5629 |
final Function<? super V, ? extends U> transformer; |
|
5630 |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
|
5631 |
if ((transformer = this.transformer) != null && |
|
5632 |
(reducer = this.reducer) != null) { |
|
5633 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5634 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5635 |
addToPendingCount(1); |
|
5636 |
(rights = new MapReduceValuesTask<K,V,U> |
|
5637 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5638 |
rights, transformer, reducer)).fork(); |
|
5639 |
} |
|
5640 |
U r = null; |
|
5641 |
for (Node<K,V> p; (p = advance()) != null; ) { |
|
5642 |
U u; |
|
5643 |
if ((u = transformer.apply(p.val)) != null) |
|
5644 |
r = (r == null) ? u : reducer.apply(r, u); |
|
5645 |
} |
|
5646 |
result = r; |
|
5647 |
CountedCompleter<?> c; |
|
5648 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5649 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5650 |
MapReduceValuesTask<K,V,U> |
17945 | 5651 |
t = (MapReduceValuesTask<K,V,U>)c, |
5652 |
s = t.rights; |
|
5653 |
while (s != null) { |
|
5654 |
U tr, sr; |
|
5655 |
if ((sr = s.result) != null) |
|
5656 |
t.result = (((tr = t.result) == null) ? sr : |
|
5657 |
reducer.apply(tr, sr)); |
|
5658 |
s = t.rights = s.nextRight; |
|
5659 |
} |
|
5660 |
} |
|
5661 |
} |
|
5662 |
} |
|
5663 |
} |
|
5664 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5665 |
@SuppressWarnings("serial") |
17945 | 5666 |
static final class MapReduceEntriesTask<K,V,U> |
5667 |
extends BulkTask<K,V,U> { |
|
5668 |
final Function<Map.Entry<K,V>, ? extends U> transformer; |
|
5669 |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
|
5670 |
U result; |
|
5671 |
MapReduceEntriesTask<K,V,U> rights, nextRight; |
|
5672 |
MapReduceEntriesTask |
|
5673 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5674 |
MapReduceEntriesTask<K,V,U> nextRight, |
|
5675 |
Function<Map.Entry<K,V>, ? extends U> transformer, |
|
5676 |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
|
5677 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
5678 |
this.transformer = transformer; |
|
5679 |
this.reducer = reducer; |
|
5680 |
} |
|
5681 |
public final U getRawResult() { return result; } |
|
5682 |
public final void compute() { |
|
5683 |
final Function<Map.Entry<K,V>, ? extends U> transformer; |
|
5684 |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
|
5685 |
if ((transformer = this.transformer) != null && |
|
5686 |
(reducer = this.reducer) != null) { |
|
5687 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5688 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5689 |
addToPendingCount(1); |
|
5690 |
(rights = new MapReduceEntriesTask<K,V,U> |
|
5691 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5692 |
rights, transformer, reducer)).fork(); |
|
5693 |
} |
|
5694 |
U r = null; |
|
5695 |
for (Node<K,V> p; (p = advance()) != null; ) { |
|
5696 |
U u; |
|
5697 |
if ((u = transformer.apply(p)) != null) |
|
5698 |
r = (r == null) ? u : reducer.apply(r, u); |
|
5699 |
} |
|
5700 |
result = r; |
|
5701 |
CountedCompleter<?> c; |
|
5702 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5703 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5704 |
MapReduceEntriesTask<K,V,U> |
17945 | 5705 |
t = (MapReduceEntriesTask<K,V,U>)c, |
5706 |
s = t.rights; |
|
5707 |
while (s != null) { |
|
5708 |
U tr, sr; |
|
5709 |
if ((sr = s.result) != null) |
|
5710 |
t.result = (((tr = t.result) == null) ? sr : |
|
5711 |
reducer.apply(tr, sr)); |
|
5712 |
s = t.rights = s.nextRight; |
|
5713 |
} |
|
5714 |
} |
|
5715 |
} |
|
5716 |
} |
|
5717 |
} |
|
5718 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5719 |
@SuppressWarnings("serial") |
17945 | 5720 |
static final class MapReduceMappingsTask<K,V,U> |
5721 |
extends BulkTask<K,V,U> { |
|
5722 |
final BiFunction<? super K, ? super V, ? extends U> transformer; |
|
5723 |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
|
5724 |
U result; |
|
5725 |
MapReduceMappingsTask<K,V,U> rights, nextRight; |
|
5726 |
MapReduceMappingsTask |
|
5727 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5728 |
MapReduceMappingsTask<K,V,U> nextRight, |
|
5729 |
BiFunction<? super K, ? super V, ? extends U> transformer, |
|
5730 |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
|
5731 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
5732 |
this.transformer = transformer; |
|
5733 |
this.reducer = reducer; |
|
5734 |
} |
|
5735 |
public final U getRawResult() { return result; } |
|
5736 |
public final void compute() { |
|
5737 |
final BiFunction<? super K, ? super V, ? extends U> transformer; |
|
5738 |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
|
5739 |
if ((transformer = this.transformer) != null && |
|
5740 |
(reducer = this.reducer) != null) { |
|
5741 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5742 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5743 |
addToPendingCount(1); |
|
5744 |
(rights = new MapReduceMappingsTask<K,V,U> |
|
5745 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5746 |
rights, transformer, reducer)).fork(); |
|
5747 |
} |
|
5748 |
U r = null; |
|
5749 |
for (Node<K,V> p; (p = advance()) != null; ) { |
|
5750 |
U u; |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5751 |
if ((u = transformer.apply(p.key, p.val)) != null) |
17945 | 5752 |
r = (r == null) ? u : reducer.apply(r, u); |
5753 |
} |
|
5754 |
result = r; |
|
5755 |
CountedCompleter<?> c; |
|
5756 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5757 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5758 |
MapReduceMappingsTask<K,V,U> |
17945 | 5759 |
t = (MapReduceMappingsTask<K,V,U>)c, |
5760 |
s = t.rights; |
|
5761 |
while (s != null) { |
|
5762 |
U tr, sr; |
|
5763 |
if ((sr = s.result) != null) |
|
5764 |
t.result = (((tr = t.result) == null) ? sr : |
|
5765 |
reducer.apply(tr, sr)); |
|
5766 |
s = t.rights = s.nextRight; |
|
5767 |
} |
|
5768 |
} |
|
5769 |
} |
|
5770 |
} |
|
5771 |
} |
|
5772 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5773 |
@SuppressWarnings("serial") |
17945 | 5774 |
static final class MapReduceKeysToDoubleTask<K,V> |
5775 |
extends BulkTask<K,V,Double> { |
|
5776 |
final ToDoubleFunction<? super K> transformer; |
|
5777 |
final DoubleBinaryOperator reducer; |
|
5778 |
final double basis; |
|
5779 |
double result; |
|
5780 |
MapReduceKeysToDoubleTask<K,V> rights, nextRight; |
|
5781 |
MapReduceKeysToDoubleTask |
|
5782 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5783 |
MapReduceKeysToDoubleTask<K,V> nextRight, |
|
5784 |
ToDoubleFunction<? super K> transformer, |
|
5785 |
double basis, |
|
5786 |
DoubleBinaryOperator reducer) { |
|
5787 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
5788 |
this.transformer = transformer; |
|
5789 |
this.basis = basis; this.reducer = reducer; |
|
5790 |
} |
|
5791 |
public final Double getRawResult() { return result; } |
|
5792 |
public final void compute() { |
|
5793 |
final ToDoubleFunction<? super K> transformer; |
|
5794 |
final DoubleBinaryOperator reducer; |
|
5795 |
if ((transformer = this.transformer) != null && |
|
5796 |
(reducer = this.reducer) != null) { |
|
5797 |
double r = this.basis; |
|
5798 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5799 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5800 |
addToPendingCount(1); |
|
5801 |
(rights = new MapReduceKeysToDoubleTask<K,V> |
|
5802 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5803 |
rights, transformer, r, reducer)).fork(); |
|
5804 |
} |
|
5805 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5806 |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key)); |
17945 | 5807 |
result = r; |
5808 |
CountedCompleter<?> c; |
|
5809 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5810 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5811 |
MapReduceKeysToDoubleTask<K,V> |
17945 | 5812 |
t = (MapReduceKeysToDoubleTask<K,V>)c, |
5813 |
s = t.rights; |
|
5814 |
while (s != null) { |
|
5815 |
t.result = reducer.applyAsDouble(t.result, s.result); |
|
5816 |
s = t.rights = s.nextRight; |
|
5817 |
} |
|
5818 |
} |
|
5819 |
} |
|
5820 |
} |
|
5821 |
} |
|
5822 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5823 |
@SuppressWarnings("serial") |
17945 | 5824 |
static final class MapReduceValuesToDoubleTask<K,V> |
5825 |
extends BulkTask<K,V,Double> { |
|
5826 |
final ToDoubleFunction<? super V> transformer; |
|
5827 |
final DoubleBinaryOperator reducer; |
|
5828 |
final double basis; |
|
5829 |
double result; |
|
5830 |
MapReduceValuesToDoubleTask<K,V> rights, nextRight; |
|
5831 |
MapReduceValuesToDoubleTask |
|
5832 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5833 |
MapReduceValuesToDoubleTask<K,V> nextRight, |
|
5834 |
ToDoubleFunction<? super V> transformer, |
|
5835 |
double basis, |
|
5836 |
DoubleBinaryOperator reducer) { |
|
5837 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
5838 |
this.transformer = transformer; |
|
5839 |
this.basis = basis; this.reducer = reducer; |
|
5840 |
} |
|
5841 |
public final Double getRawResult() { return result; } |
|
5842 |
public final void compute() { |
|
5843 |
final ToDoubleFunction<? super V> transformer; |
|
5844 |
final DoubleBinaryOperator reducer; |
|
5845 |
if ((transformer = this.transformer) != null && |
|
5846 |
(reducer = this.reducer) != null) { |
|
5847 |
double r = this.basis; |
|
5848 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5849 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5850 |
addToPendingCount(1); |
|
5851 |
(rights = new MapReduceValuesToDoubleTask<K,V> |
|
5852 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5853 |
rights, transformer, r, reducer)).fork(); |
|
5854 |
} |
|
5855 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
5856 |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.val)); |
|
5857 |
result = r; |
|
5858 |
CountedCompleter<?> c; |
|
5859 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5860 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5861 |
MapReduceValuesToDoubleTask<K,V> |
17945 | 5862 |
t = (MapReduceValuesToDoubleTask<K,V>)c, |
5863 |
s = t.rights; |
|
5864 |
while (s != null) { |
|
5865 |
t.result = reducer.applyAsDouble(t.result, s.result); |
|
5866 |
s = t.rights = s.nextRight; |
|
5867 |
} |
|
5868 |
} |
|
5869 |
} |
|
5870 |
} |
|
5871 |
} |
|
5872 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5873 |
@SuppressWarnings("serial") |
17945 | 5874 |
static final class MapReduceEntriesToDoubleTask<K,V> |
5875 |
extends BulkTask<K,V,Double> { |
|
5876 |
final ToDoubleFunction<Map.Entry<K,V>> transformer; |
|
5877 |
final DoubleBinaryOperator reducer; |
|
5878 |
final double basis; |
|
5879 |
double result; |
|
5880 |
MapReduceEntriesToDoubleTask<K,V> rights, nextRight; |
|
5881 |
MapReduceEntriesToDoubleTask |
|
5882 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5883 |
MapReduceEntriesToDoubleTask<K,V> nextRight, |
|
5884 |
ToDoubleFunction<Map.Entry<K,V>> transformer, |
|
5885 |
double basis, |
|
5886 |
DoubleBinaryOperator reducer) { |
|
5887 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
5888 |
this.transformer = transformer; |
|
5889 |
this.basis = basis; this.reducer = reducer; |
|
5890 |
} |
|
5891 |
public final Double getRawResult() { return result; } |
|
5892 |
public final void compute() { |
|
5893 |
final ToDoubleFunction<Map.Entry<K,V>> transformer; |
|
5894 |
final DoubleBinaryOperator reducer; |
|
5895 |
if ((transformer = this.transformer) != null && |
|
5896 |
(reducer = this.reducer) != null) { |
|
5897 |
double r = this.basis; |
|
5898 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5899 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5900 |
addToPendingCount(1); |
|
5901 |
(rights = new MapReduceEntriesToDoubleTask<K,V> |
|
5902 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5903 |
rights, transformer, r, reducer)).fork(); |
|
5904 |
} |
|
5905 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
5906 |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(p)); |
|
5907 |
result = r; |
|
5908 |
CountedCompleter<?> c; |
|
5909 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5910 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5911 |
MapReduceEntriesToDoubleTask<K,V> |
17945 | 5912 |
t = (MapReduceEntriesToDoubleTask<K,V>)c, |
5913 |
s = t.rights; |
|
5914 |
while (s != null) { |
|
5915 |
t.result = reducer.applyAsDouble(t.result, s.result); |
|
5916 |
s = t.rights = s.nextRight; |
|
5917 |
} |
|
5918 |
} |
|
5919 |
} |
|
5920 |
} |
|
5921 |
} |
|
5922 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5923 |
@SuppressWarnings("serial") |
17945 | 5924 |
static final class MapReduceMappingsToDoubleTask<K,V> |
5925 |
extends BulkTask<K,V,Double> { |
|
5926 |
final ToDoubleBiFunction<? super K, ? super V> transformer; |
|
5927 |
final DoubleBinaryOperator reducer; |
|
5928 |
final double basis; |
|
5929 |
double result; |
|
5930 |
MapReduceMappingsToDoubleTask<K,V> rights, nextRight; |
|
5931 |
MapReduceMappingsToDoubleTask |
|
5932 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5933 |
MapReduceMappingsToDoubleTask<K,V> nextRight, |
|
5934 |
ToDoubleBiFunction<? super K, ? super V> transformer, |
|
5935 |
double basis, |
|
5936 |
DoubleBinaryOperator reducer) { |
|
5937 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
5938 |
this.transformer = transformer; |
|
5939 |
this.basis = basis; this.reducer = reducer; |
|
5940 |
} |
|
5941 |
public final Double getRawResult() { return result; } |
|
5942 |
public final void compute() { |
|
5943 |
final ToDoubleBiFunction<? super K, ? super V> transformer; |
|
5944 |
final DoubleBinaryOperator reducer; |
|
5945 |
if ((transformer = this.transformer) != null && |
|
5946 |
(reducer = this.reducer) != null) { |
|
5947 |
double r = this.basis; |
|
5948 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5949 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
5950 |
addToPendingCount(1); |
|
5951 |
(rights = new MapReduceMappingsToDoubleTask<K,V> |
|
5952 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
5953 |
rights, transformer, r, reducer)).fork(); |
|
5954 |
} |
|
5955 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5956 |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val)); |
17945 | 5957 |
result = r; |
5958 |
CountedCompleter<?> c; |
|
5959 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5960 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
5961 |
MapReduceMappingsToDoubleTask<K,V> |
17945 | 5962 |
t = (MapReduceMappingsToDoubleTask<K,V>)c, |
5963 |
s = t.rights; |
|
5964 |
while (s != null) { |
|
5965 |
t.result = reducer.applyAsDouble(t.result, s.result); |
|
5966 |
s = t.rights = s.nextRight; |
|
5967 |
} |
|
5968 |
} |
|
5969 |
} |
|
5970 |
} |
|
5971 |
} |
|
5972 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
5973 |
@SuppressWarnings("serial") |
17945 | 5974 |
static final class MapReduceKeysToLongTask<K,V> |
5975 |
extends BulkTask<K,V,Long> { |
|
5976 |
final ToLongFunction<? super K> transformer; |
|
5977 |
final LongBinaryOperator reducer; |
|
5978 |
final long basis; |
|
5979 |
long result; |
|
5980 |
MapReduceKeysToLongTask<K,V> rights, nextRight; |
|
5981 |
MapReduceKeysToLongTask |
|
5982 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
5983 |
MapReduceKeysToLongTask<K,V> nextRight, |
|
5984 |
ToLongFunction<? super K> transformer, |
|
5985 |
long basis, |
|
5986 |
LongBinaryOperator reducer) { |
|
5987 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
5988 |
this.transformer = transformer; |
|
5989 |
this.basis = basis; this.reducer = reducer; |
|
5990 |
} |
|
5991 |
public final Long getRawResult() { return result; } |
|
5992 |
public final void compute() { |
|
5993 |
final ToLongFunction<? super K> transformer; |
|
5994 |
final LongBinaryOperator reducer; |
|
5995 |
if ((transformer = this.transformer) != null && |
|
5996 |
(reducer = this.reducer) != null) { |
|
5997 |
long r = this.basis; |
|
5998 |
for (int i = baseIndex, f, h; batch > 0 && |
|
5999 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
6000 |
addToPendingCount(1); |
|
6001 |
(rights = new MapReduceKeysToLongTask<K,V> |
|
6002 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
6003 |
rights, transformer, r, reducer)).fork(); |
|
6004 |
} |
|
6005 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
6006 |
r = reducer.applyAsLong(r, transformer.applyAsLong(p.key)); |
17945 | 6007 |
result = r; |
6008 |
CountedCompleter<?> c; |
|
6009 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6010 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6011 |
MapReduceKeysToLongTask<K,V> |
17945 | 6012 |
t = (MapReduceKeysToLongTask<K,V>)c, |
6013 |
s = t.rights; |
|
6014 |
while (s != null) { |
|
6015 |
t.result = reducer.applyAsLong(t.result, s.result); |
|
6016 |
s = t.rights = s.nextRight; |
|
6017 |
} |
|
6018 |
} |
|
6019 |
} |
|
6020 |
} |
|
6021 |
} |
|
6022 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
6023 |
@SuppressWarnings("serial") |
17945 | 6024 |
static final class MapReduceValuesToLongTask<K,V> |
6025 |
extends BulkTask<K,V,Long> { |
|
6026 |
final ToLongFunction<? super V> transformer; |
|
6027 |
final LongBinaryOperator reducer; |
|
6028 |
final long basis; |
|
6029 |
long result; |
|
6030 |
MapReduceValuesToLongTask<K,V> rights, nextRight; |
|
6031 |
MapReduceValuesToLongTask |
|
6032 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
6033 |
MapReduceValuesToLongTask<K,V> nextRight, |
|
6034 |
ToLongFunction<? super V> transformer, |
|
6035 |
long basis, |
|
6036 |
LongBinaryOperator reducer) { |
|
6037 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
6038 |
this.transformer = transformer; |
|
6039 |
this.basis = basis; this.reducer = reducer; |
|
6040 |
} |
|
6041 |
public final Long getRawResult() { return result; } |
|
6042 |
public final void compute() { |
|
6043 |
final ToLongFunction<? super V> transformer; |
|
6044 |
final LongBinaryOperator reducer; |
|
6045 |
if ((transformer = this.transformer) != null && |
|
6046 |
(reducer = this.reducer) != null) { |
|
6047 |
long r = this.basis; |
|
6048 |
for (int i = baseIndex, f, h; batch > 0 && |
|
6049 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
6050 |
addToPendingCount(1); |
|
6051 |
(rights = new MapReduceValuesToLongTask<K,V> |
|
6052 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
6053 |
rights, transformer, r, reducer)).fork(); |
|
6054 |
} |
|
6055 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
6056 |
r = reducer.applyAsLong(r, transformer.applyAsLong(p.val)); |
|
6057 |
result = r; |
|
6058 |
CountedCompleter<?> c; |
|
6059 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6060 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6061 |
MapReduceValuesToLongTask<K,V> |
17945 | 6062 |
t = (MapReduceValuesToLongTask<K,V>)c, |
6063 |
s = t.rights; |
|
6064 |
while (s != null) { |
|
6065 |
t.result = reducer.applyAsLong(t.result, s.result); |
|
6066 |
s = t.rights = s.nextRight; |
|
6067 |
} |
|
6068 |
} |
|
6069 |
} |
|
6070 |
} |
|
6071 |
} |
|
6072 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
6073 |
@SuppressWarnings("serial") |
17945 | 6074 |
static final class MapReduceEntriesToLongTask<K,V> |
6075 |
extends BulkTask<K,V,Long> { |
|
6076 |
final ToLongFunction<Map.Entry<K,V>> transformer; |
|
6077 |
final LongBinaryOperator reducer; |
|
6078 |
final long basis; |
|
6079 |
long result; |
|
6080 |
MapReduceEntriesToLongTask<K,V> rights, nextRight; |
|
6081 |
MapReduceEntriesToLongTask |
|
6082 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
6083 |
MapReduceEntriesToLongTask<K,V> nextRight, |
|
6084 |
ToLongFunction<Map.Entry<K,V>> transformer, |
|
6085 |
long basis, |
|
6086 |
LongBinaryOperator reducer) { |
|
6087 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
6088 |
this.transformer = transformer; |
|
6089 |
this.basis = basis; this.reducer = reducer; |
|
6090 |
} |
|
6091 |
public final Long getRawResult() { return result; } |
|
6092 |
public final void compute() { |
|
6093 |
final ToLongFunction<Map.Entry<K,V>> transformer; |
|
6094 |
final LongBinaryOperator reducer; |
|
6095 |
if ((transformer = this.transformer) != null && |
|
6096 |
(reducer = this.reducer) != null) { |
|
6097 |
long r = this.basis; |
|
6098 |
for (int i = baseIndex, f, h; batch > 0 && |
|
6099 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
6100 |
addToPendingCount(1); |
|
6101 |
(rights = new MapReduceEntriesToLongTask<K,V> |
|
6102 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
6103 |
rights, transformer, r, reducer)).fork(); |
|
6104 |
} |
|
6105 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
6106 |
r = reducer.applyAsLong(r, transformer.applyAsLong(p)); |
|
6107 |
result = r; |
|
6108 |
CountedCompleter<?> c; |
|
6109 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6110 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6111 |
MapReduceEntriesToLongTask<K,V> |
17945 | 6112 |
t = (MapReduceEntriesToLongTask<K,V>)c, |
6113 |
s = t.rights; |
|
6114 |
while (s != null) { |
|
6115 |
t.result = reducer.applyAsLong(t.result, s.result); |
|
6116 |
s = t.rights = s.nextRight; |
|
6117 |
} |
|
6118 |
} |
|
6119 |
} |
|
6120 |
} |
|
6121 |
} |
|
6122 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
6123 |
@SuppressWarnings("serial") |
17945 | 6124 |
static final class MapReduceMappingsToLongTask<K,V> |
6125 |
extends BulkTask<K,V,Long> { |
|
6126 |
final ToLongBiFunction<? super K, ? super V> transformer; |
|
6127 |
final LongBinaryOperator reducer; |
|
6128 |
final long basis; |
|
6129 |
long result; |
|
6130 |
MapReduceMappingsToLongTask<K,V> rights, nextRight; |
|
6131 |
MapReduceMappingsToLongTask |
|
6132 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
6133 |
MapReduceMappingsToLongTask<K,V> nextRight, |
|
6134 |
ToLongBiFunction<? super K, ? super V> transformer, |
|
6135 |
long basis, |
|
6136 |
LongBinaryOperator reducer) { |
|
6137 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
6138 |
this.transformer = transformer; |
|
6139 |
this.basis = basis; this.reducer = reducer; |
|
6140 |
} |
|
6141 |
public final Long getRawResult() { return result; } |
|
6142 |
public final void compute() { |
|
6143 |
final ToLongBiFunction<? super K, ? super V> transformer; |
|
6144 |
final LongBinaryOperator reducer; |
|
6145 |
if ((transformer = this.transformer) != null && |
|
6146 |
(reducer = this.reducer) != null) { |
|
6147 |
long r = this.basis; |
|
6148 |
for (int i = baseIndex, f, h; batch > 0 && |
|
6149 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
6150 |
addToPendingCount(1); |
|
6151 |
(rights = new MapReduceMappingsToLongTask<K,V> |
|
6152 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
6153 |
rights, transformer, r, reducer)).fork(); |
|
6154 |
} |
|
6155 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
6156 |
r = reducer.applyAsLong(r, transformer.applyAsLong(p.key, p.val)); |
17945 | 6157 |
result = r; |
6158 |
CountedCompleter<?> c; |
|
6159 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6160 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6161 |
MapReduceMappingsToLongTask<K,V> |
17945 | 6162 |
t = (MapReduceMappingsToLongTask<K,V>)c, |
6163 |
s = t.rights; |
|
6164 |
while (s != null) { |
|
6165 |
t.result = reducer.applyAsLong(t.result, s.result); |
|
6166 |
s = t.rights = s.nextRight; |
|
6167 |
} |
|
6168 |
} |
|
6169 |
} |
|
6170 |
} |
|
6171 |
} |
|
6172 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
6173 |
@SuppressWarnings("serial") |
17945 | 6174 |
static final class MapReduceKeysToIntTask<K,V> |
6175 |
extends BulkTask<K,V,Integer> { |
|
6176 |
final ToIntFunction<? super K> transformer; |
|
6177 |
final IntBinaryOperator reducer; |
|
6178 |
final int basis; |
|
6179 |
int result; |
|
6180 |
MapReduceKeysToIntTask<K,V> rights, nextRight; |
|
6181 |
MapReduceKeysToIntTask |
|
6182 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
6183 |
MapReduceKeysToIntTask<K,V> nextRight, |
|
6184 |
ToIntFunction<? super K> transformer, |
|
6185 |
int basis, |
|
6186 |
IntBinaryOperator reducer) { |
|
6187 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
6188 |
this.transformer = transformer; |
|
6189 |
this.basis = basis; this.reducer = reducer; |
|
6190 |
} |
|
6191 |
public final Integer getRawResult() { return result; } |
|
6192 |
public final void compute() { |
|
6193 |
final ToIntFunction<? super K> transformer; |
|
6194 |
final IntBinaryOperator reducer; |
|
6195 |
if ((transformer = this.transformer) != null && |
|
6196 |
(reducer = this.reducer) != null) { |
|
6197 |
int r = this.basis; |
|
6198 |
for (int i = baseIndex, f, h; batch > 0 && |
|
6199 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
6200 |
addToPendingCount(1); |
|
6201 |
(rights = new MapReduceKeysToIntTask<K,V> |
|
6202 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
6203 |
rights, transformer, r, reducer)).fork(); |
|
6204 |
} |
|
6205 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
6206 |
r = reducer.applyAsInt(r, transformer.applyAsInt(p.key)); |
17945 | 6207 |
result = r; |
6208 |
CountedCompleter<?> c; |
|
6209 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6210 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6211 |
MapReduceKeysToIntTask<K,V> |
17945 | 6212 |
t = (MapReduceKeysToIntTask<K,V>)c, |
6213 |
s = t.rights; |
|
6214 |
while (s != null) { |
|
6215 |
t.result = reducer.applyAsInt(t.result, s.result); |
|
6216 |
s = t.rights = s.nextRight; |
|
6217 |
} |
|
6218 |
} |
|
6219 |
} |
|
6220 |
} |
|
6221 |
} |
|
6222 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
6223 |
@SuppressWarnings("serial") |
17945 | 6224 |
static final class MapReduceValuesToIntTask<K,V> |
6225 |
extends BulkTask<K,V,Integer> { |
|
6226 |
final ToIntFunction<? super V> transformer; |
|
6227 |
final IntBinaryOperator reducer; |
|
6228 |
final int basis; |
|
6229 |
int result; |
|
6230 |
MapReduceValuesToIntTask<K,V> rights, nextRight; |
|
6231 |
MapReduceValuesToIntTask |
|
6232 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
6233 |
MapReduceValuesToIntTask<K,V> nextRight, |
|
6234 |
ToIntFunction<? super V> transformer, |
|
6235 |
int basis, |
|
6236 |
IntBinaryOperator reducer) { |
|
6237 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
6238 |
this.transformer = transformer; |
|
6239 |
this.basis = basis; this.reducer = reducer; |
|
6240 |
} |
|
6241 |
public final Integer getRawResult() { return result; } |
|
6242 |
public final void compute() { |
|
6243 |
final ToIntFunction<? super V> transformer; |
|
6244 |
final IntBinaryOperator reducer; |
|
6245 |
if ((transformer = this.transformer) != null && |
|
6246 |
(reducer = this.reducer) != null) { |
|
6247 |
int r = this.basis; |
|
6248 |
for (int i = baseIndex, f, h; batch > 0 && |
|
6249 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
6250 |
addToPendingCount(1); |
|
6251 |
(rights = new MapReduceValuesToIntTask<K,V> |
|
6252 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
6253 |
rights, transformer, r, reducer)).fork(); |
|
6254 |
} |
|
6255 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
6256 |
r = reducer.applyAsInt(r, transformer.applyAsInt(p.val)); |
|
6257 |
result = r; |
|
6258 |
CountedCompleter<?> c; |
|
6259 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6260 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6261 |
MapReduceValuesToIntTask<K,V> |
17945 | 6262 |
t = (MapReduceValuesToIntTask<K,V>)c, |
6263 |
s = t.rights; |
|
6264 |
while (s != null) { |
|
6265 |
t.result = reducer.applyAsInt(t.result, s.result); |
|
6266 |
s = t.rights = s.nextRight; |
|
6267 |
} |
|
6268 |
} |
|
6269 |
} |
|
6270 |
} |
|
6271 |
} |
|
6272 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
6273 |
@SuppressWarnings("serial") |
17945 | 6274 |
static final class MapReduceEntriesToIntTask<K,V> |
6275 |
extends BulkTask<K,V,Integer> { |
|
6276 |
final ToIntFunction<Map.Entry<K,V>> transformer; |
|
6277 |
final IntBinaryOperator reducer; |
|
6278 |
final int basis; |
|
6279 |
int result; |
|
6280 |
MapReduceEntriesToIntTask<K,V> rights, nextRight; |
|
6281 |
MapReduceEntriesToIntTask |
|
6282 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
6283 |
MapReduceEntriesToIntTask<K,V> nextRight, |
|
6284 |
ToIntFunction<Map.Entry<K,V>> transformer, |
|
6285 |
int basis, |
|
6286 |
IntBinaryOperator reducer) { |
|
6287 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
6288 |
this.transformer = transformer; |
|
6289 |
this.basis = basis; this.reducer = reducer; |
|
6290 |
} |
|
6291 |
public final Integer getRawResult() { return result; } |
|
6292 |
public final void compute() { |
|
6293 |
final ToIntFunction<Map.Entry<K,V>> transformer; |
|
6294 |
final IntBinaryOperator reducer; |
|
6295 |
if ((transformer = this.transformer) != null && |
|
6296 |
(reducer = this.reducer) != null) { |
|
6297 |
int r = this.basis; |
|
6298 |
for (int i = baseIndex, f, h; batch > 0 && |
|
6299 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
6300 |
addToPendingCount(1); |
|
6301 |
(rights = new MapReduceEntriesToIntTask<K,V> |
|
6302 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
6303 |
rights, transformer, r, reducer)).fork(); |
|
6304 |
} |
|
6305 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
6306 |
r = reducer.applyAsInt(r, transformer.applyAsInt(p)); |
|
6307 |
result = r; |
|
6308 |
CountedCompleter<?> c; |
|
6309 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6310 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6311 |
MapReduceEntriesToIntTask<K,V> |
17945 | 6312 |
t = (MapReduceEntriesToIntTask<K,V>)c, |
6313 |
s = t.rights; |
|
6314 |
while (s != null) { |
|
6315 |
t.result = reducer.applyAsInt(t.result, s.result); |
|
6316 |
s = t.rights = s.nextRight; |
|
6317 |
} |
|
6318 |
} |
|
6319 |
} |
|
6320 |
} |
|
6321 |
} |
|
6322 |
||
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
6323 |
@SuppressWarnings("serial") |
17945 | 6324 |
static final class MapReduceMappingsToIntTask<K,V> |
6325 |
extends BulkTask<K,V,Integer> { |
|
6326 |
final ToIntBiFunction<? super K, ? super V> transformer; |
|
6327 |
final IntBinaryOperator reducer; |
|
6328 |
final int basis; |
|
6329 |
int result; |
|
6330 |
MapReduceMappingsToIntTask<K,V> rights, nextRight; |
|
6331 |
MapReduceMappingsToIntTask |
|
6332 |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
|
6333 |
MapReduceMappingsToIntTask<K,V> nextRight, |
|
6334 |
ToIntBiFunction<? super K, ? super V> transformer, |
|
6335 |
int basis, |
|
6336 |
IntBinaryOperator reducer) { |
|
6337 |
super(p, b, i, f, t); this.nextRight = nextRight; |
|
6338 |
this.transformer = transformer; |
|
6339 |
this.basis = basis; this.reducer = reducer; |
|
6340 |
} |
|
6341 |
public final Integer getRawResult() { return result; } |
|
6342 |
public final void compute() { |
|
6343 |
final ToIntBiFunction<? super K, ? super V> transformer; |
|
6344 |
final IntBinaryOperator reducer; |
|
6345 |
if ((transformer = this.transformer) != null && |
|
6346 |
(reducer = this.reducer) != null) { |
|
6347 |
int r = this.basis; |
|
6348 |
for (int i = baseIndex, f, h; batch > 0 && |
|
6349 |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
|
6350 |
addToPendingCount(1); |
|
6351 |
(rights = new MapReduceMappingsToIntTask<K,V> |
|
6352 |
(this, batch >>>= 1, baseLimit = h, f, tab, |
|
6353 |
rights, transformer, r, reducer)).fork(); |
|
6354 |
} |
|
6355 |
for (Node<K,V> p; (p = advance()) != null; ) |
|
18802
68564211d16b
8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents:
17953
diff
changeset
|
6356 |
r = reducer.applyAsInt(r, transformer.applyAsInt(p.key, p.val)); |
17945 | 6357 |
result = r; |
6358 |
CountedCompleter<?> c; |
|
6359 |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
|
19862
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6360 |
@SuppressWarnings("unchecked") |
186f3a70a4f7
8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents:
19855
diff
changeset
|
6361 |
MapReduceMappingsToIntTask<K,V> |
17945 | 6362 |
t = (MapReduceMappingsToIntTask<K,V>)c, |
6363 |
s = t.rights; |
|
6364 |
while (s != null) { |
|
6365 |
t.result = reducer.applyAsInt(t.result, s.result); |
|
6366 |
s = t.rights = s.nextRight; |
|
6367 |
} |
|
6368 |
} |
|
6369 |
} |
|
2 | 6370 |
} |
6371 |
} |
|
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
6372 |
|
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
6373 |
// Unsafe mechanics |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
38551
diff
changeset
|
6374 |
private static final Unsafe U = Unsafe.getUnsafe(); |
17945 | 6375 |
private static final long SIZECTL; |
6376 |
private static final long TRANSFERINDEX; |
|
6377 |
private static final long BASECOUNT; |
|
6378 |
private static final long CELLSBUSY; |
|
6379 |
private static final long CELLVALUE; |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6380 |
private static final int ABASE; |
17945 | 6381 |
private static final int ASHIFT; |
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
6382 |
|
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
6383 |
static { |
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
6384 |
try { |
17945 | 6385 |
SIZECTL = U.objectFieldOffset |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6386 |
(ConcurrentHashMap.class.getDeclaredField("sizeCtl")); |
17945 | 6387 |
TRANSFERINDEX = U.objectFieldOffset |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6388 |
(ConcurrentHashMap.class.getDeclaredField("transferIndex")); |
17945 | 6389 |
BASECOUNT = U.objectFieldOffset |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6390 |
(ConcurrentHashMap.class.getDeclaredField("baseCount")); |
17945 | 6391 |
CELLSBUSY = U.objectFieldOffset |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6392 |
(ConcurrentHashMap.class.getDeclaredField("cellsBusy")); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6393 |
|
17945 | 6394 |
CELLVALUE = U.objectFieldOffset |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6395 |
(CounterCell.class.getDeclaredField("value")); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6396 |
|
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6397 |
ABASE = U.arrayBaseOffset(Node[].class); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6398 |
int scale = U.arrayIndexScale(Node[].class); |
17945 | 6399 |
if ((scale & (scale - 1)) != 0) |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6400 |
throw new Error("array index scale not a power of two"); |
17945 | 6401 |
ASHIFT = 31 - Integer.numberOfLeadingZeros(scale); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6402 |
} catch (ReflectiveOperationException e) { |
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
6403 |
throw new Error(e); |
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
6404 |
} |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6405 |
|
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6406 |
// Reduce the risk of rare disastrous classloading in first call to |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6407 |
// LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773 |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30655
diff
changeset
|
6408 |
Class<?> ensureLoaded = LockSupport.class; |
9279
5f5d493d30a0
7036559: ConcurrentHashMap footprint and contention improvements
dl
parents:
9242
diff
changeset
|
6409 |
} |
2 | 6410 |
} |