4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
+ |
// Snapshot Tue Jun 5 14:56:09 2012 Doug Lea (dl at altair) |
8 |
+ |
|
9 |
|
package jsr166e; |
10 |
|
import jsr166e.LongAdder; |
11 |
|
import java.util.Arrays; |
22 |
|
import java.util.ConcurrentModificationException; |
23 |
|
import java.util.NoSuchElementException; |
24 |
|
import java.util.concurrent.ConcurrentMap; |
25 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
26 |
|
import java.util.concurrent.locks.LockSupport; |
27 |
+ |
import java.util.concurrent.locks.AbstractQueuedSynchronizer; |
28 |
|
import java.io.Serializable; |
29 |
|
|
30 |
|
/** |
151 |
|
* supplying null-checks and casts as needed. This also allows |
152 |
|
* many of the public methods to be factored into a smaller number |
153 |
|
* of internal methods (although sadly not so for the five |
154 |
< |
* sprawling variants of put-related operations). |
154 |
> |
* variants of put-related operations). The validation-based |
155 |
> |
* approach explained below leads to a lot of code sprawl because |
156 |
> |
* retry-control precludes factoring into smaller methods. |
157 |
|
* |
158 |
|
* The table is lazily initialized to a power-of-two size upon the |
159 |
< |
* first insertion. Each bin in the table contains a list of |
160 |
< |
* Nodes (most often, the list has only zero or one Node). Table |
161 |
< |
* accesses require volatile/atomic reads, writes, and CASes. |
162 |
< |
* Because there is no other way to arrange this without adding |
163 |
< |
* further indirections, we use intrinsics (sun.misc.Unsafe) |
164 |
< |
* operations. The lists of nodes within bins are always |
165 |
< |
* accurately traversable under volatile reads, so long as lookups |
166 |
< |
* check hash code and non-nullness of value before checking key |
167 |
< |
* equality. |
159 |
> |
* first insertion. Each bin in the table normally contains a |
160 |
> |
* list of Nodes (most often, the list has only zero or one Node). |
161 |
> |
* Table accesses require volatile/atomic reads, writes, and |
162 |
> |
* CASes. Because there is no other way to arrange this without |
163 |
> |
* adding further indirections, we use intrinsics |
164 |
> |
* (sun.misc.Unsafe) operations. The lists of nodes within bins |
165 |
> |
* are always accurately traversable under volatile reads, so long |
166 |
> |
* as lookups check hash code and non-nullness of value before |
167 |
> |
* checking key equality. |
168 |
|
* |
169 |
|
* We use the top two bits of Node hash fields for control |
170 |
|
* purposes -- they are available anyway because of addressing |
176 |
|
* 10 - Node is a forwarding node |
177 |
|
* |
178 |
|
* The lower 30 bits of each Node's hash field contain a |
179 |
< |
* transformation (for better randomization -- method "spread") of |
180 |
< |
* the key's hash code, except for forwarding nodes, for which the |
181 |
< |
* lower bits are zero (and so always have hash field == MOVED). |
179 |
> |
* transformation of the key's hash code, except for forwarding |
180 |
> |
* nodes, for which the lower bits are zero (and so always have |
181 |
> |
* hash field == MOVED). |
182 |
|
* |
183 |
|
* Insertion (via put or its variants) of the first node in an |
184 |
|
* empty bin is performed by just CASing it to the bin. This is |
185 |
< |
* by far the most common case for put operations. Other update |
186 |
< |
* operations (insert, delete, and replace) require locks. We do |
187 |
< |
* not want to waste the space required to associate a distinct |
188 |
< |
* lock object with each bin, so instead use the first node of a |
189 |
< |
* bin list itself as a lock. Blocking support for these locks |
190 |
< |
* relies on the builtin "synchronized" monitors. However, we |
191 |
< |
* also need a tryLock construction, so we overlay these by using |
192 |
< |
* bits of the Node hash field for lock control (see above), and |
193 |
< |
* so normally use builtin monitors only for blocking and |
194 |
< |
* signalling using wait/notifyAll constructions. See |
195 |
< |
* Node.tryAwaitLock. |
185 |
> |
* by far the most common case for put operations under most |
186 |
> |
* key/hash distributions. Other update operations (insert, |
187 |
> |
* delete, and replace) require locks. We do not want to waste |
188 |
> |
* the space required to associate a distinct lock object with |
189 |
> |
* each bin, so instead use the first node of a bin list itself as |
190 |
> |
* a lock. Blocking support for these locks relies on the builtin |
191 |
> |
* "synchronized" monitors. However, we also need a tryLock |
192 |
> |
* construction, so we overlay these by using bits of the Node |
193 |
> |
* hash field for lock control (see above), and so normally use |
194 |
> |
* builtin monitors only for blocking and signalling using |
195 |
> |
* wait/notifyAll constructions. See Node.tryAwaitLock. |
196 |
|
* |
197 |
|
* Using the first node of a list as a lock does not by itself |
198 |
|
* suffice though: When a node is locked, any update must first |
207 |
|
* The main disadvantage of per-bin locks is that other update |
208 |
|
* operations on other nodes in a bin list protected by the same |
209 |
|
* lock can stall, for example when user equals() or mapping |
210 |
< |
* functions take a long time. However, statistically, this is |
211 |
< |
* not a common enough problem to outweigh the time/space overhead |
212 |
< |
* of alternatives: Under random hash codes, the frequency of |
207 |
< |
* nodes in bins follows a Poisson distribution |
210 |
> |
* functions take a long time. However, statistically, under |
211 |
> |
* random hash codes, this is not a common problem. Ideally, the |
212 |
> |
* frequency of nodes in bins follows a Poisson distribution |
213 |
|
* (http://en.wikipedia.org/wiki/Poisson_distribution) with a |
214 |
|
* parameter of about 0.5 on average, given the resizing threshold |
215 |
|
* of 0.75, although with a large variance because of resizing |
216 |
|
* granularity. Ignoring variance, the expected occurrences of |
217 |
|
* list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The |
218 |
< |
* first few values are: |
218 |
> |
* first values are: |
219 |
|
* |
220 |
< |
* 0: 0.607 |
221 |
< |
* 1: 0.303 |
222 |
< |
* 2: 0.076 |
223 |
< |
* 3: 0.012 |
224 |
< |
* more: 0.002 |
220 |
> |
* 0: 0.60653066 |
221 |
> |
* 1: 0.30326533 |
222 |
> |
* 2: 0.07581633 |
223 |
> |
* 3: 0.01263606 |
224 |
> |
* 4: 0.00157952 |
225 |
> |
* 5: 0.00015795 |
226 |
> |
* 6: 0.00001316 |
227 |
> |
* 7: 0.00000094 |
228 |
> |
* 8: 0.00000006 |
229 |
> |
* more: less than 1 in ten million |
230 |
|
* |
231 |
|
* Lock contention probability for two threads accessing distinct |
232 |
< |
* elements is roughly 1 / (8 * #elements). Function "spread" |
233 |
< |
* performs hashCode randomization that improves the likelihood |
234 |
< |
* that these assumptions hold unless users define exactly the |
235 |
< |
* same value for too many hashCodes. |
232 |
> |
* elements is roughly 1 / (8 * #elements) under random hashes. |
233 |
> |
* |
234 |
> |
* Actual hash code distributions encountered in practice |
235 |
> |
* sometimes deviate significantly from uniform randomness. This |
236 |
> |
* includes the case when N > (1<<30), so some keys MUST collide. |
237 |
> |
* Similarly for dumb or hostile usages in which multiple keys are |
238 |
> |
* designed to have identical hash codes. Also, although we guard |
239 |
> |
* against the worst effects of this (see method spread), sets of |
240 |
> |
* hashes may differ only in bits that do not impact their bin |
241 |
> |
* index for a given power-of-two mask. So we use a secondary |
242 |
> |
* strategy that applies when the number of nodes in a bin exceeds |
243 |
> |
* a threshold, and at least one of the keys implements |
244 |
> |
* Comparable. These TreeBins use a balanced tree to hold nodes |
245 |
> |
* (a specialized form of red-black trees), bounding search time |
246 |
> |
* to O(log N). Each search step in a TreeBin is around twice as |
247 |
> |
* slow as in a regular list, but given that N cannot exceed |
248 |
> |
* (1<<64) (before running out of addresses) this bounds search |
249 |
> |
* steps, lock hold times, etc, to reasonable constants (roughly |
250 |
> |
* 100 nodes inspected per operation worst case) so long as keys |
251 |
> |
* are Comparable (which is very common -- String, Long, etc). |
252 |
> |
* TreeBin nodes (TreeNodes) also maintain the same "next" |
253 |
> |
* traversal pointers as regular nodes, so can be traversed in |
254 |
> |
* iterators in the same way. |
255 |
|
* |
256 |
< |
* The table is resized when occupancy exceeds an occupancy |
256 |
> |
* The table is resized when occupancy exceeds a percentage |
257 |
|
* threshold (nominally, 0.75, but see below). Only a single |
258 |
|
* thread performs the resize (using field "sizeCtl", to arrange |
259 |
|
* exclusion), but the table otherwise remains usable for reads |
274 |
|
* |
275 |
|
* Each bin transfer requires its bin lock. However, unlike other |
276 |
|
* cases, a transfer can skip a bin if it fails to acquire its |
277 |
< |
* lock, and revisit it later. Method rebuild maintains a buffer |
278 |
< |
* of TRANSFER_BUFFER_SIZE bins that have been skipped because of |
279 |
< |
* failure to acquire a lock, and blocks only if none are |
280 |
< |
* available (i.e., only very rarely). The transfer operation |
281 |
< |
* must also ensure that all accessible bins in both the old and |
282 |
< |
* new table are usable by any traversal. When there are no lock |
283 |
< |
* acquisition failures, this is arranged simply by proceeding |
284 |
< |
* from the last bin (table.length - 1) up towards the first. |
285 |
< |
* Upon seeing a forwarding node, traversals (see class |
286 |
< |
* InternalIterator) arrange to move to the new table without |
287 |
< |
* revisiting nodes. However, when any node is skipped during a |
288 |
< |
* transfer, all earlier table bins may have become visible, so |
289 |
< |
* are initialized with a reverse-forwarding node back to the old |
290 |
< |
* table until the new ones are established. (This sometimes |
291 |
< |
* requires transiently locking a forwarding node, which is |
292 |
< |
* possible under the above encoding.) These more expensive |
277 |
> |
* lock, and revisit it later (unless it is a TreeBin). Method |
278 |
> |
* rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that |
279 |
> |
* have been skipped because of failure to acquire a lock, and |
280 |
> |
* blocks only if none are available (i.e., only very rarely). |
281 |
> |
* The transfer operation must also ensure that all accessible |
282 |
> |
* bins in both the old and new table are usable by any traversal. |
283 |
> |
* When there are no lock acquisition failures, this is arranged |
284 |
> |
* simply by proceeding from the last bin (table.length - 1) up |
285 |
> |
* towards the first. Upon seeing a forwarding node, traversals |
286 |
> |
* (see class InternalIterator) arrange to move to the new table |
287 |
> |
* without revisiting nodes. However, when any node is skipped |
288 |
> |
* during a transfer, all earlier table bins may have become |
289 |
> |
* visible, so are initialized with a reverse-forwarding node back |
290 |
> |
* to the old table until the new ones are established. (This |
291 |
> |
* sometimes requires transiently locking a forwarding node, which |
292 |
> |
* is possible under the above encoding.) These more expensive |
293 |
|
* mechanics trigger only when necessary. |
294 |
|
* |
295 |
|
* The traversal scheme also applies to partial traversals of |
376 |
|
*/ |
377 |
|
private static final int TRANSFER_BUFFER_SIZE = 32; |
378 |
|
|
379 |
+ |
/** |
380 |
+ |
* The bin count threshold for using a tree rather than list for a |
381 |
+ |
* bin. The value reflects the approximate break-even point for |
382 |
+ |
* using tree-based operations. |
383 |
+ |
*/ |
384 |
+ |
private static final int TREE_THRESHOLD = 8; |
385 |
+ |
|
386 |
|
/* |
387 |
|
* Encodings for special uses of Node hash fields. See above for |
388 |
|
* explanation. |
422 |
|
/** For serialization compatibility. Null unless serialized; see below */ |
423 |
|
private Segment<K,V>[] segments; |
424 |
|
|
425 |
+ |
/* ---------------- Table element access -------------- */ |
426 |
+ |
|
427 |
+ |
/* |
428 |
+ |
* Volatile access methods are used for table elements as well as |
429 |
+ |
* elements of in-progress next table while resizing. Uses are |
430 |
+ |
* null checked by callers, and implicitly bounds-checked, relying |
431 |
+ |
* on the invariants that tab arrays have non-zero size, and all |
432 |
+ |
* indices are masked with (tab.length - 1) which is never |
433 |
+ |
* negative and always less than length. Note that, to be correct |
434 |
+ |
* wrt arbitrary concurrency errors by users, bounds checks must |
435 |
+ |
* operate on local variables, which accounts for some odd-looking |
436 |
+ |
* inline assignments below. |
437 |
+ |
*/ |
438 |
+ |
|
439 |
+ |
static final Node tabAt(Node[] tab, int i) { // used by InternalIterator |
440 |
+ |
return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE); |
441 |
+ |
} |
442 |
+ |
|
443 |
+ |
private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) { |
444 |
+ |
return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v); |
445 |
+ |
} |
446 |
+ |
|
447 |
+ |
private static final void setTabAt(Node[] tab, int i, Node v) { |
448 |
+ |
UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v); |
449 |
+ |
} |
450 |
+ |
|
451 |
|
/* ---------------- Nodes -------------- */ |
452 |
|
|
453 |
|
/** |
460 |
|
* access, a key may be read before a val, but can only be used |
461 |
|
* after checking val to be non-null. |
462 |
|
*/ |
463 |
< |
static final class Node { |
463 |
> |
static class Node { |
464 |
|
volatile int hash; |
465 |
|
final Object key; |
466 |
|
volatile Object val; |
497 |
|
*/ |
498 |
|
final void tryAwaitLock(Node[] tab, int i) { |
499 |
|
if (tab != null && i >= 0 && i < tab.length) { // bounds check |
500 |
+ |
int r = ThreadLocalRandom.current().nextInt(); // randomize spins |
501 |
|
int spins = MAX_SPINS, h; |
502 |
|
while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) { |
503 |
|
if (spins >= 0) { |
504 |
< |
if (--spins == MAX_SPINS >>> 1) |
505 |
< |
Thread.yield(); // heuristically yield mid-way |
504 |
> |
r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift |
505 |
> |
if (r >= 0 && --spins == 0) |
506 |
> |
Thread.yield(); // yield before block |
507 |
|
} |
508 |
|
else if (casHash(h, h | WAITING)) { |
509 |
|
synchronized (this) { |
540 |
|
} |
541 |
|
} |
542 |
|
|
543 |
< |
/* ---------------- Table element access -------------- */ |
543 |
> |
/* ---------------- TreeBins -------------- */ |
544 |
|
|
545 |
< |
/* |
546 |
< |
* Volatile access methods are used for table elements as well as |
483 |
< |
* elements of in-progress next table while resizing. Uses are |
484 |
< |
* null checked by callers, and implicitly bounds-checked, relying |
485 |
< |
* on the invariants that tab arrays have non-zero size, and all |
486 |
< |
* indices are masked with (tab.length - 1) which is never |
487 |
< |
* negative and always less than length. Note that, to be correct |
488 |
< |
* wrt arbitrary concurrency errors by users, bounds checks must |
489 |
< |
* operate on local variables, which accounts for some odd-looking |
490 |
< |
* inline assignments below. |
545 |
> |
/** |
546 |
> |
* Nodes for use in TreeBins |
547 |
|
*/ |
548 |
< |
|
549 |
< |
static final Node tabAt(Node[] tab, int i) { // used by InternalIterator |
550 |
< |
return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE); |
548 |
> |
static final class TreeNode extends Node { |
549 |
> |
TreeNode parent; // red-black tree links |
550 |
> |
TreeNode left; |
551 |
> |
TreeNode right; |
552 |
> |
TreeNode prev; // needed to unlink next upon deletion |
553 |
> |
boolean red; |
554 |
> |
|
555 |
> |
TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) { |
556 |
> |
super(hash, key, val, next); |
557 |
> |
this.parent = parent; |
558 |
> |
} |
559 |
|
} |
560 |
|
|
561 |
< |
private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) { |
562 |
< |
return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v); |
563 |
< |
} |
561 |
> |
/** |
562 |
> |
* A specialized form of red-black tree for use in bins |
563 |
> |
* whose size exceeds a threshold. |
564 |
> |
* |
565 |
> |
* TreeBins use a special form of comparison for search and |
566 |
> |
* related operations (which is the main reason we cannot use |
567 |
> |
* existing collections such as TreeMaps). TreeBins contain |
568 |
> |
* Comparable elements, but may contain others, as well as |
569 |
> |
* elements that are Comparable but not necessarily Comparable<T> |
570 |
> |
* for the same T, so we cannot invoke compareTo among them. To |
571 |
> |
* handle this, the tree is ordered primarily by hash value, then |
572 |
> |
* by getClass().getName() order, and then by Comparator order |
573 |
> |
* among elements of the same class. On lookup at a node, if |
574 |
> |
* non-Comparable, both left and right children may need to be |
575 |
> |
* searched in the case of tied hash values. (This corresponds to |
576 |
> |
* the full list search that would be necessary if all elements |
577 |
> |
* were non-Comparable and had tied hashes.) |
578 |
> |
* |
579 |
> |
* TreeBins also maintain a separate locking discipline than |
580 |
> |
* regular bins. Because they are forwarded via special MOVED |
581 |
> |
* nodes at bin heads (which can never change once established), |
582 |
> |
* we cannot use use those nodes as locks. Instead, TreeBin |
583 |
> |
* extends AbstractQueuedSynchronizer to support a simple form of |
584 |
> |
* read-write lock. For update operations and table validation, |
585 |
> |
* the exclusive form of lock behaves in the same way as bin-head |
586 |
> |
* locks. However, lookups use shared read-lock mechanics to allow |
587 |
> |
* multiple readers in the absence of writers. Additionally, |
588 |
> |
* these lookups do not ever block: While the lock is not |
589 |
> |
* available, they proceed along the slow traversal path (via |
590 |
> |
* next-pointers) until the lock becomes available or the list is |
591 |
> |
* exhausted, whichever comes first. (These cases are not fast, |
592 |
> |
* but maximize aggregate expected throughput.) The AQS mechanics |
593 |
> |
* for doing this are straightforward. The lock state is held as |
594 |
> |
* AQS getState(). Read counts are negative; the write count (1) |
595 |
> |
* is positive. There are no signalling preferences among readers |
596 |
> |
* and writers. Since we don't need to export full Lock API, we |
597 |
> |
* just override the minimal AQS methods and use them directly. |
598 |
> |
*/ |
599 |
> |
static final class TreeBin extends AbstractQueuedSynchronizer { |
600 |
> |
private static final long serialVersionUID = 2249069246763182397L; |
601 |
> |
TreeNode root; // root of tree |
602 |
> |
TreeNode first; // head of next-pointer list |
603 |
|
|
604 |
< |
private static final void setTabAt(Node[] tab, int i, Node v) { |
605 |
< |
UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v); |
604 |
> |
/* AQS overrides */ |
605 |
> |
public final boolean isHeldExclusively() { return getState() > 0; } |
606 |
> |
public final boolean tryAcquire(int ignore) { |
607 |
> |
if (compareAndSetState(0, 1)) { |
608 |
> |
setExclusiveOwnerThread(Thread.currentThread()); |
609 |
> |
return true; |
610 |
> |
} |
611 |
> |
return false; |
612 |
> |
} |
613 |
> |
public final boolean tryRelease(int ignore) { |
614 |
> |
setExclusiveOwnerThread(null); |
615 |
> |
setState(0); |
616 |
> |
return true; |
617 |
> |
} |
618 |
> |
public final int tryAcquireShared(int ignore) { |
619 |
> |
for (int c;;) { |
620 |
> |
if ((c = getState()) > 0) |
621 |
> |
return -1; |
622 |
> |
if (compareAndSetState(c, c -1)) |
623 |
> |
return 1; |
624 |
> |
} |
625 |
> |
} |
626 |
> |
public final boolean tryReleaseShared(int ignore) { |
627 |
> |
int c; |
628 |
> |
do {} while (!compareAndSetState(c = getState(), c + 1)); |
629 |
> |
return c == -1; |
630 |
> |
} |
631 |
> |
|
632 |
> |
/** |
633 |
> |
* Return the TreeNode (or null if not found) for the given key |
634 |
> |
* starting at given root. |
635 |
> |
*/ |
636 |
> |
@SuppressWarnings("unchecked") // suppress Comparable cast warning |
637 |
> |
final TreeNode getTreeNode(int h, Object k, TreeNode p) { |
638 |
> |
Class<?> c = k.getClass(); |
639 |
> |
while (p != null) { |
640 |
> |
int dir, ph; Object pk; Class<?> pc; TreeNode r; |
641 |
> |
if (h < (ph = p.hash)) |
642 |
> |
dir = -1; |
643 |
> |
else if (h > ph) |
644 |
> |
dir = 1; |
645 |
> |
else if ((pk = p.key) == k || k.equals(pk)) |
646 |
> |
return p; |
647 |
> |
else if (c != (pc = pk.getClass())) |
648 |
> |
dir = c.getName().compareTo(pc.getName()); |
649 |
> |
else if (k instanceof Comparable) |
650 |
> |
dir = ((Comparable)k).compareTo((Comparable)pk); |
651 |
> |
else |
652 |
> |
dir = 0; |
653 |
> |
TreeNode pr = p.right; |
654 |
> |
if (dir > 0) |
655 |
> |
p = pr; |
656 |
> |
else if (dir == 0 && pr != null && h >= pr.hash && |
657 |
> |
(r = getTreeNode(h, k, pr)) != null) |
658 |
> |
return r; |
659 |
> |
else |
660 |
> |
p = p.left; |
661 |
> |
} |
662 |
> |
return null; |
663 |
> |
} |
664 |
> |
|
665 |
> |
/** |
666 |
> |
* Wrapper for getTreeNode used by CHM.get. Tries to obtain |
667 |
> |
* read-lock to call getTreeNode, but during failure to get |
668 |
> |
* lock, searches along next links. |
669 |
> |
*/ |
670 |
> |
final Object getValue(int h, Object k) { |
671 |
> |
Node r = null; |
672 |
> |
int c = getState(); // Must read lock state first |
673 |
> |
for (Node e = first; e != null; e = e.next) { |
674 |
> |
if (c <= 0 && compareAndSetState(c, c - 1)) { |
675 |
> |
try { |
676 |
> |
r = getTreeNode(h, k, root); |
677 |
> |
} finally { |
678 |
> |
releaseShared(0); |
679 |
> |
} |
680 |
> |
break; |
681 |
> |
} |
682 |
> |
else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) { |
683 |
> |
r = e; |
684 |
> |
break; |
685 |
> |
} |
686 |
> |
else |
687 |
> |
c = getState(); |
688 |
> |
} |
689 |
> |
return r == null ? null : r.val; |
690 |
> |
} |
691 |
> |
|
692 |
> |
/** |
693 |
> |
* Find or add a node |
694 |
> |
* @return null if added |
695 |
> |
*/ |
696 |
> |
@SuppressWarnings("unchecked") // suppress Comparable cast warning |
697 |
> |
final TreeNode putTreeNode(int h, Object k, Object v) { |
698 |
> |
Class<?> c = k.getClass(); |
699 |
> |
TreeNode p = root; |
700 |
> |
int dir = 0; |
701 |
> |
if (p != null) { |
702 |
> |
for (;;) { |
703 |
> |
int ph; Object pk; Class<?> pc; TreeNode r; |
704 |
> |
if (h < (ph = p.hash)) |
705 |
> |
dir = -1; |
706 |
> |
else if (h > ph) |
707 |
> |
dir = 1; |
708 |
> |
else if ((pk = p.key) == k || k.equals(pk)) |
709 |
> |
return p; |
710 |
> |
else if (c != (pc = (pk = p.key).getClass())) |
711 |
> |
dir = c.getName().compareTo(pc.getName()); |
712 |
> |
else if (k instanceof Comparable) |
713 |
> |
dir = ((Comparable)k).compareTo((Comparable)pk); |
714 |
> |
else |
715 |
> |
dir = 0; |
716 |
> |
TreeNode pr = p.right, pl; |
717 |
> |
if (dir > 0) { |
718 |
> |
if (pr == null) |
719 |
> |
break; |
720 |
> |
p = pr; |
721 |
> |
} |
722 |
> |
else if (dir == 0 && pr != null && h >= pr.hash && |
723 |
> |
(r = getTreeNode(h, k, pr)) != null) |
724 |
> |
return r; |
725 |
> |
else if ((pl = p.left) == null) |
726 |
> |
break; |
727 |
> |
else |
728 |
> |
p = pl; |
729 |
> |
} |
730 |
> |
} |
731 |
> |
TreeNode f = first; |
732 |
> |
TreeNode r = first = new TreeNode(h, k, v, f, p); |
733 |
> |
if (p == null) |
734 |
> |
root = r; |
735 |
> |
else { |
736 |
> |
if (dir <= 0) |
737 |
> |
p.left = r; |
738 |
> |
else |
739 |
> |
p.right = r; |
740 |
> |
if (f != null) |
741 |
> |
f.prev = r; |
742 |
> |
fixAfterInsertion(r); |
743 |
> |
} |
744 |
> |
return null; |
745 |
> |
} |
746 |
> |
|
747 |
> |
/** |
748 |
> |
* Removes the given node, that must be present before this |
749 |
> |
* call. This is messier than typical red-black deletion code |
750 |
> |
* because we cannot swap the contents of an interior node |
751 |
> |
* with a leaf successor that is pinned by "next" pointers |
752 |
> |
* that are accessible independently of lock. So instead we |
753 |
> |
* swap the tree linkages. |
754 |
> |
*/ |
755 |
> |
final void deleteTreeNode(TreeNode p) { |
756 |
> |
TreeNode next = (TreeNode)p.next; // unlink traversal pointers |
757 |
> |
TreeNode pred = p.prev; |
758 |
> |
if (pred == null) |
759 |
> |
first = next; |
760 |
> |
else |
761 |
> |
pred.next = next; |
762 |
> |
if (next != null) |
763 |
> |
next.prev = pred; |
764 |
> |
TreeNode replacement; |
765 |
> |
TreeNode pl = p.left; |
766 |
> |
TreeNode pr = p.right; |
767 |
> |
if (pl != null && pr != null) { |
768 |
> |
TreeNode s = pr; |
769 |
> |
while (s.left != null) // find successor |
770 |
> |
s = s.left; |
771 |
> |
boolean c = s.red; s.red = p.red; p.red = c; // swap colors |
772 |
> |
TreeNode sr = s.right; |
773 |
> |
TreeNode pp = p.parent; |
774 |
> |
if (s == pr) { // p was s's direct parent |
775 |
> |
p.parent = s; |
776 |
> |
s.right = p; |
777 |
> |
} |
778 |
> |
else { |
779 |
> |
TreeNode sp = s.parent; |
780 |
> |
if ((p.parent = sp) != null) { |
781 |
> |
if (s == sp.left) |
782 |
> |
sp.left = p; |
783 |
> |
else |
784 |
> |
sp.right = p; |
785 |
> |
} |
786 |
> |
if ((s.right = pr) != null) |
787 |
> |
pr.parent = s; |
788 |
> |
} |
789 |
> |
p.left = null; |
790 |
> |
if ((p.right = sr) != null) |
791 |
> |
sr.parent = p; |
792 |
> |
if ((s.left = pl) != null) |
793 |
> |
pl.parent = s; |
794 |
> |
if ((s.parent = pp) == null) |
795 |
> |
root = s; |
796 |
> |
else if (p == pp.left) |
797 |
> |
pp.left = s; |
798 |
> |
else |
799 |
> |
pp.right = s; |
800 |
> |
replacement = sr; |
801 |
> |
} |
802 |
> |
else |
803 |
> |
replacement = (pl != null) ? pl : pr; |
804 |
> |
TreeNode pp = p.parent; |
805 |
> |
if (replacement == null) { |
806 |
> |
if (pp == null) { |
807 |
> |
root = null; |
808 |
> |
return; |
809 |
> |
} |
810 |
> |
replacement = p; |
811 |
> |
} |
812 |
> |
else { |
813 |
> |
replacement.parent = pp; |
814 |
> |
if (pp == null) |
815 |
> |
root = replacement; |
816 |
> |
else if (p == pp.left) |
817 |
> |
pp.left = replacement; |
818 |
> |
else |
819 |
> |
pp.right = replacement; |
820 |
> |
p.left = p.right = p.parent = null; |
821 |
> |
} |
822 |
> |
if (!p.red) |
823 |
> |
fixAfterDeletion(replacement); |
824 |
> |
if (p == replacement && (pp = p.parent) != null) { |
825 |
> |
if (p == pp.left) // detach pointers |
826 |
> |
pp.left = null; |
827 |
> |
else if (p == pp.right) |
828 |
> |
pp.right = null; |
829 |
> |
p.parent = null; |
830 |
> |
} |
831 |
> |
} |
832 |
> |
|
833 |
> |
// CLR code updated from pre-jdk-collections version at |
834 |
> |
// http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java |
835 |
> |
|
836 |
> |
/** From CLR */ |
837 |
> |
private void rotateLeft(TreeNode p) { |
838 |
> |
if (p != null) { |
839 |
> |
TreeNode r = p.right, pp, rl; |
840 |
> |
if ((rl = p.right = r.left) != null) |
841 |
> |
rl.parent = p; |
842 |
> |
if ((pp = r.parent = p.parent) == null) |
843 |
> |
root = r; |
844 |
> |
else if (pp.left == p) |
845 |
> |
pp.left = r; |
846 |
> |
else |
847 |
> |
pp.right = r; |
848 |
> |
r.left = p; |
849 |
> |
p.parent = r; |
850 |
> |
} |
851 |
> |
} |
852 |
> |
|
853 |
> |
/** From CLR */ |
854 |
> |
private void rotateRight(TreeNode p) { |
855 |
> |
if (p != null) { |
856 |
> |
TreeNode l = p.left, pp, lr; |
857 |
> |
if ((lr = p.left = l.right) != null) |
858 |
> |
lr.parent = p; |
859 |
> |
if ((pp = l.parent = p.parent) == null) |
860 |
> |
root = l; |
861 |
> |
else if (pp.right == p) |
862 |
> |
pp.right = l; |
863 |
> |
else |
864 |
> |
pp.left = l; |
865 |
> |
l.right = p; |
866 |
> |
p.parent = l; |
867 |
> |
} |
868 |
> |
} |
869 |
> |
|
870 |
> |
/** From CLR */ |
871 |
> |
private void fixAfterInsertion(TreeNode x) { |
872 |
> |
x.red = true; |
873 |
> |
TreeNode xp, xpp; |
874 |
> |
while (x != null && (xp = x.parent) != null && xp.red && |
875 |
> |
(xpp = xp.parent) != null) { |
876 |
> |
TreeNode xppl = xpp.left; |
877 |
> |
if (xp == xppl) { |
878 |
> |
TreeNode y = xpp.right; |
879 |
> |
if (y != null && y.red) { |
880 |
> |
y.red = false; |
881 |
> |
xp.red = false; |
882 |
> |
xpp.red = true; |
883 |
> |
x = xpp; |
884 |
> |
} |
885 |
> |
else { |
886 |
> |
if (x == xp.right) { |
887 |
> |
x = xp; |
888 |
> |
rotateLeft(x); |
889 |
> |
xpp = (xp = x.parent) == null ? null : xp.parent; |
890 |
> |
} |
891 |
> |
if (xp != null) { |
892 |
> |
xp.red = false; |
893 |
> |
if (xpp != null) { |
894 |
> |
xpp.red = true; |
895 |
> |
rotateRight(xpp); |
896 |
> |
} |
897 |
> |
} |
898 |
> |
} |
899 |
> |
} |
900 |
> |
else { |
901 |
> |
TreeNode y = xppl; |
902 |
> |
if (y != null && y.red) { |
903 |
> |
y.red = false; |
904 |
> |
xp.red = false; |
905 |
> |
xpp.red = true; |
906 |
> |
x = xpp; |
907 |
> |
} |
908 |
> |
else { |
909 |
> |
if (x == xp.left) { |
910 |
> |
x = xp; |
911 |
> |
rotateRight(x); |
912 |
> |
xpp = (xp = x.parent) == null ? null : xp.parent; |
913 |
> |
} |
914 |
> |
if (xp != null) { |
915 |
> |
xp.red = false; |
916 |
> |
if (xpp != null) { |
917 |
> |
xpp.red = true; |
918 |
> |
rotateLeft(xpp); |
919 |
> |
} |
920 |
> |
} |
921 |
> |
} |
922 |
> |
} |
923 |
> |
} |
924 |
> |
TreeNode r = root; |
925 |
> |
if (r != null && r.red) |
926 |
> |
r.red = false; |
927 |
> |
} |
928 |
> |
|
929 |
> |
/** From CLR */ |
930 |
> |
private void fixAfterDeletion(TreeNode x) { |
931 |
> |
while (x != null) { |
932 |
> |
TreeNode xp, xpl; |
933 |
> |
if (x.red || (xp = x.parent) == null) { |
934 |
> |
x.red = false; |
935 |
> |
break; |
936 |
> |
} |
937 |
> |
if (x == (xpl = xp.left)) { |
938 |
> |
TreeNode sib = xp.right; |
939 |
> |
if (sib != null && sib.red) { |
940 |
> |
sib.red = false; |
941 |
> |
xp.red = true; |
942 |
> |
rotateLeft(xp); |
943 |
> |
sib = (xp = x.parent) == null ? null : xp.right; |
944 |
> |
} |
945 |
> |
if (sib == null) |
946 |
> |
x = xp; |
947 |
> |
else { |
948 |
> |
TreeNode sl = sib.left, sr = sib.right; |
949 |
> |
if ((sr == null || !sr.red) && |
950 |
> |
(sl == null || !sl.red)) { |
951 |
> |
sib.red = true; |
952 |
> |
x = xp; |
953 |
> |
} |
954 |
> |
else { |
955 |
> |
if (sr == null || !sr.red) { |
956 |
> |
if (sl != null) |
957 |
> |
sl.red = false; |
958 |
> |
sib.red = true; |
959 |
> |
rotateRight(sib); |
960 |
> |
sib = (xp = x.parent) == null ? null : xp.right; |
961 |
> |
} |
962 |
> |
if (sib != null) { |
963 |
> |
sib.red = (xp == null)? false : xp.red; |
964 |
> |
if ((sr = sib.right) != null) |
965 |
> |
sr.red = false; |
966 |
> |
} |
967 |
> |
if (xp != null) { |
968 |
> |
xp.red = false; |
969 |
> |
rotateLeft(xp); |
970 |
> |
} |
971 |
> |
x = root; |
972 |
> |
} |
973 |
> |
} |
974 |
> |
} |
975 |
> |
else { // symmetric |
976 |
> |
TreeNode sib = xpl; |
977 |
> |
if (sib != null && sib.red) { |
978 |
> |
sib.red = false; |
979 |
> |
xp.red = true; |
980 |
> |
rotateRight(xp); |
981 |
> |
sib = (xp = x.parent) == null ? null : xp.left; |
982 |
> |
} |
983 |
> |
if (sib == null) |
984 |
> |
x = xp; |
985 |
> |
else { |
986 |
> |
TreeNode sl = sib.left, sr = sib.right; |
987 |
> |
if ((sl == null || !sl.red) && |
988 |
> |
(sr == null || !sr.red)) { |
989 |
> |
sib.red = true; |
990 |
> |
x = xp; |
991 |
> |
} |
992 |
> |
else { |
993 |
> |
if (sl == null || !sl.red) { |
994 |
> |
if (sr != null) |
995 |
> |
sr.red = false; |
996 |
> |
sib.red = true; |
997 |
> |
rotateLeft(sib); |
998 |
> |
sib = (xp = x.parent) == null ? null : xp.left; |
999 |
> |
} |
1000 |
> |
if (sib != null) { |
1001 |
> |
sib.red = (xp == null)? false : xp.red; |
1002 |
> |
if ((sl = sib.left) != null) |
1003 |
> |
sl.red = false; |
1004 |
> |
} |
1005 |
> |
if (xp != null) { |
1006 |
> |
xp.red = false; |
1007 |
> |
rotateRight(xp); |
1008 |
> |
} |
1009 |
> |
x = root; |
1010 |
> |
} |
1011 |
> |
} |
1012 |
> |
} |
1013 |
> |
} |
1014 |
> |
} |
1015 |
|
} |
1016 |
|
|
1017 |
< |
/* ---------------- Internal access and update methods -------------- */ |
1017 |
> |
/* ---------------- Collision reduction methods -------------- */ |
1018 |
|
|
1019 |
|
/** |
1020 |
< |
* Applies a supplemental hash function to a given hashCode, which |
1021 |
< |
* defends against poor quality hash functions. The result must |
1022 |
< |
* be have the top 2 bits clear. For reasonable performance, this |
1023 |
< |
* function must have good avalanche properties; i.e., that each |
1024 |
< |
* bit of the argument affects each bit of the result. (Although |
1025 |
< |
* we don't care about the unused top 2 bits.) |
1020 |
> |
* Spreads higher bits to lower, and also forces top 2 bits to 0. |
1021 |
> |
* Because the table uses power-of-two masking, sets of hashes |
1022 |
> |
* that vary only in bits above the current mask will always |
1023 |
> |
* collide. (Among known examples are sets of Float keys holding |
1024 |
> |
* consecutive whole numbers in small tables.) To counter this, |
1025 |
> |
* we apply a transform that spreads the impact of higher bits |
1026 |
> |
* downward. There is a tradeoff between speed, utility, and |
1027 |
> |
* quality of bit-spreading. Because many common sets of hashes |
1028 |
> |
* are already reaonably distributed across bits (so don't benefit |
1029 |
> |
* from spreading), and because we use trees to handle large sets |
1030 |
> |
* of collisions in bins, we don't need excessively high quality. |
1031 |
|
*/ |
1032 |
|
private static final int spread(int h) { |
1033 |
< |
// Apply base step of MurmurHash; see http://code.google.com/p/smhasher/ |
1034 |
< |
// Despite two multiplies, this is often faster than others |
1035 |
< |
// with comparable bit-spread properties. |
1036 |
< |
h ^= h >>> 16; |
1037 |
< |
h *= 0x85ebca6b; |
1038 |
< |
h ^= h >>> 13; |
1039 |
< |
h *= 0xc2b2ae35; |
1040 |
< |
return ((h >>> 16) ^ h) & HASH_BITS; // mask out top bits |
1033 |
> |
h ^= (h >>> 18) ^ (h >>> 12); |
1034 |
> |
return (h ^ (h >>> 10)) & HASH_BITS; |
1035 |
> |
} |
1036 |
> |
|
1037 |
> |
/** |
1038 |
> |
* Replaces a list bin with a tree bin. Call only when locked. |
1039 |
> |
* Fails to replace if the given key is non-comparable or table |
1040 |
> |
* is, or needs, resizing. |
1041 |
> |
*/ |
1042 |
> |
private final void replaceWithTreeBin(Node[] tab, int index, Object key) { |
1043 |
> |
if ((key instanceof Comparable) && |
1044 |
> |
(tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) { |
1045 |
> |
TreeBin t = new TreeBin(); |
1046 |
> |
for (Node e = tabAt(tab, index); e != null; e = e.next) |
1047 |
> |
t.putTreeNode(e.hash & HASH_BITS, e.key, e.val); |
1048 |
> |
setTabAt(tab, index, new Node(MOVED, t, null, null)); |
1049 |
> |
} |
1050 |
|
} |
1051 |
|
|
1052 |
+ |
/* ---------------- Internal access and update methods -------------- */ |
1053 |
+ |
|
1054 |
|
/** Implementation for get and containsKey */ |
1055 |
|
private final Object internalGet(Object k) { |
1056 |
|
int h = spread(k.hashCode()); |
1057 |
|
retry: for (Node[] tab = table; tab != null;) { |
1058 |
< |
Node e; Object ek, ev; int eh; // locals to read fields once |
1058 |
> |
Node e, p; Object ek, ev; int eh; // locals to read fields once |
1059 |
|
for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) { |
1060 |
|
if ((eh = e.hash) == MOVED) { |
1061 |
< |
tab = (Node[])e.key; // restart with new table |
1062 |
< |
continue retry; |
1061 |
> |
if ((ek = e.key) instanceof TreeBin) // search TreeBin |
1062 |
> |
return ((TreeBin)ek).getValue(h, k); |
1063 |
> |
else { // restart with new table |
1064 |
> |
tab = (Node[])ek; |
1065 |
> |
continue retry; |
1066 |
> |
} |
1067 |
|
} |
1068 |
< |
if ((eh & HASH_BITS) == h && (ev = e.val) != null && |
1069 |
< |
((ek = e.key) == k || k.equals(ek))) |
1068 |
> |
else if ((eh & HASH_BITS) == h && (ev = e.val) != null && |
1069 |
> |
((ek = e.key) == k || k.equals(ek))) |
1070 |
|
return ev; |
1071 |
|
} |
1072 |
|
break; |
1083 |
|
int h = spread(k.hashCode()); |
1084 |
|
Object oldVal = null; |
1085 |
|
for (Node[] tab = table;;) { |
1086 |
< |
Node f; int i, fh; |
1086 |
> |
Node f; int i, fh; Object fk; |
1087 |
|
if (tab == null || |
1088 |
|
(f = tabAt(tab, i = (tab.length - 1) & h)) == null) |
1089 |
|
break; |
1090 |
< |
else if ((fh = f.hash) == MOVED) |
1091 |
< |
tab = (Node[])f.key; |
1090 |
> |
else if ((fh = f.hash) == MOVED) { |
1091 |
> |
if ((fk = f.key) instanceof TreeBin) { |
1092 |
> |
TreeBin t = (TreeBin)fk; |
1093 |
> |
boolean validated = false; |
1094 |
> |
boolean deleted = false; |
1095 |
> |
t.acquire(0); |
1096 |
> |
try { |
1097 |
> |
if (tabAt(tab, i) == f) { |
1098 |
> |
validated = true; |
1099 |
> |
TreeNode p = t.getTreeNode(h, k, t.root); |
1100 |
> |
if (p != null) { |
1101 |
> |
Object pv = p.val; |
1102 |
> |
if (cv == null || cv == pv || cv.equals(pv)) { |
1103 |
> |
oldVal = pv; |
1104 |
> |
if ((p.val = v) == null) { |
1105 |
> |
deleted = true; |
1106 |
> |
t.deleteTreeNode(p); |
1107 |
> |
} |
1108 |
> |
} |
1109 |
> |
} |
1110 |
> |
} |
1111 |
> |
} finally { |
1112 |
> |
t.release(0); |
1113 |
> |
} |
1114 |
> |
if (validated) { |
1115 |
> |
if (deleted) |
1116 |
> |
counter.add(-1L); |
1117 |
> |
break; |
1118 |
> |
} |
1119 |
> |
} |
1120 |
> |
else |
1121 |
> |
tab = (Node[])fk; |
1122 |
> |
} |
1123 |
|
else if ((fh & HASH_BITS) != h && f.next == null) // precheck |
1124 |
|
break; // rules out possible existence |
1125 |
|
else if ((fh & LOCKED) != 0) { |
1178 |
|
* 1. If table uninitialized, create |
1179 |
|
* 2. If bin empty, try to CAS new node |
1180 |
|
* 3. If bin stale, use new table |
1181 |
< |
* 4. Lock and validate; if valid, scan and add or update |
1181 |
> |
* 4. if bin converted to TreeBin, validate and relay to TreeBin methods |
1182 |
> |
* 5. Lock and validate; if valid, scan and add or update |
1183 |
|
* |
1184 |
|
* The others interweave other checks and/or alternative actions: |
1185 |
|
* * Plain put checks for and performs resize after insertion. |
1200 |
|
/** Implementation for put */ |
1201 |
|
private final Object internalPut(Object k, Object v) { |
1202 |
|
int h = spread(k.hashCode()); |
1203 |
< |
boolean checkSize = false; |
1203 |
> |
int count = 0; |
1204 |
|
for (Node[] tab = table;;) { |
1205 |
< |
int i; Node f; int fh; |
1205 |
> |
int i; Node f; int fh; Object fk; |
1206 |
|
if (tab == null) |
1207 |
|
tab = initTable(); |
1208 |
|
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1209 |
|
if (casTabAt(tab, i, null, new Node(h, k, v, null))) |
1210 |
|
break; // no lock when adding to empty bin |
1211 |
|
} |
1212 |
< |
else if ((fh = f.hash) == MOVED) |
1213 |
< |
tab = (Node[])f.key; |
1212 |
> |
else if ((fh = f.hash) == MOVED) { |
1213 |
> |
if ((fk = f.key) instanceof TreeBin) { |
1214 |
> |
TreeBin t = (TreeBin)fk; |
1215 |
> |
Object oldVal = null; |
1216 |
> |
t.acquire(0); |
1217 |
> |
try { |
1218 |
> |
if (tabAt(tab, i) == f) { |
1219 |
> |
count = 2; |
1220 |
> |
TreeNode p = t.putTreeNode(h, k, v); |
1221 |
> |
if (p != null) { |
1222 |
> |
oldVal = p.val; |
1223 |
> |
p.val = v; |
1224 |
> |
} |
1225 |
> |
} |
1226 |
> |
} finally { |
1227 |
> |
t.release(0); |
1228 |
> |
} |
1229 |
> |
if (count != 0) { |
1230 |
> |
if (oldVal != null) |
1231 |
> |
return oldVal; |
1232 |
> |
break; |
1233 |
> |
} |
1234 |
> |
} |
1235 |
> |
else |
1236 |
> |
tab = (Node[])fk; |
1237 |
> |
} |
1238 |
|
else if ((fh & LOCKED) != 0) { |
1239 |
|
checkForResize(); |
1240 |
|
f.tryAwaitLock(tab, i); |
1241 |
|
} |
1242 |
|
else if (f.casHash(fh, fh | LOCKED)) { |
1243 |
|
Object oldVal = null; |
656 |
– |
boolean validated = false; |
1244 |
|
try { // needed in case equals() throws |
1245 |
|
if (tabAt(tab, i) == f) { |
1246 |
< |
validated = true; // retry if 1st already deleted |
1247 |
< |
for (Node e = f;;) { |
1246 |
> |
count = 1; |
1247 |
> |
for (Node e = f;; ++count) { |
1248 |
|
Object ek, ev; |
1249 |
|
if ((e.hash & HASH_BITS) == h && |
1250 |
|
(ev = e.val) != null && |
1256 |
|
Node last = e; |
1257 |
|
if ((e = e.next) == null) { |
1258 |
|
last.next = new Node(h, k, v, null); |
1259 |
< |
if (last != f || tab.length <= 64) |
1260 |
< |
checkSize = true; |
1259 |
> |
if (count >= TREE_THRESHOLD) |
1260 |
> |
replaceWithTreeBin(tab, i, k); |
1261 |
|
break; |
1262 |
|
} |
1263 |
|
} |
1268 |
|
synchronized (f) { f.notifyAll(); }; |
1269 |
|
} |
1270 |
|
} |
1271 |
< |
if (validated) { |
1271 |
> |
if (count != 0) { |
1272 |
|
if (oldVal != null) |
1273 |
|
return oldVal; |
1274 |
+ |
if (tab.length <= 64) |
1275 |
+ |
count = 2; |
1276 |
|
break; |
1277 |
|
} |
1278 |
|
} |
1279 |
|
} |
1280 |
|
counter.add(1L); |
1281 |
< |
if (checkSize) |
1281 |
> |
if (count > 1) |
1282 |
|
checkForResize(); |
1283 |
|
return null; |
1284 |
|
} |
1286 |
|
/** Implementation for putIfAbsent */ |
1287 |
|
private final Object internalPutIfAbsent(Object k, Object v) { |
1288 |
|
int h = spread(k.hashCode()); |
1289 |
+ |
int count = 0; |
1290 |
|
for (Node[] tab = table;;) { |
1291 |
|
int i; Node f; int fh; Object fk, fv; |
1292 |
|
if (tab == null) |
1295 |
|
if (casTabAt(tab, i, null, new Node(h, k, v, null))) |
1296 |
|
break; |
1297 |
|
} |
1298 |
< |
else if ((fh = f.hash) == MOVED) |
1299 |
< |
tab = (Node[])f.key; |
1298 |
> |
else if ((fh = f.hash) == MOVED) { |
1299 |
> |
if ((fk = f.key) instanceof TreeBin) { |
1300 |
> |
TreeBin t = (TreeBin)fk; |
1301 |
> |
Object oldVal = null; |
1302 |
> |
t.acquire(0); |
1303 |
> |
try { |
1304 |
> |
if (tabAt(tab, i) == f) { |
1305 |
> |
count = 2; |
1306 |
> |
TreeNode p = t.putTreeNode(h, k, v); |
1307 |
> |
if (p != null) |
1308 |
> |
oldVal = p.val; |
1309 |
> |
} |
1310 |
> |
} finally { |
1311 |
> |
t.release(0); |
1312 |
> |
} |
1313 |
> |
if (count != 0) { |
1314 |
> |
if (oldVal != null) |
1315 |
> |
return oldVal; |
1316 |
> |
break; |
1317 |
> |
} |
1318 |
> |
} |
1319 |
> |
else |
1320 |
> |
tab = (Node[])fk; |
1321 |
> |
} |
1322 |
|
else if ((fh & HASH_BITS) == h && (fv = f.val) != null && |
1323 |
|
((fk = f.key) == k || k.equals(fk))) |
1324 |
|
return fv; |
1342 |
|
} |
1343 |
|
else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) { |
1344 |
|
Object oldVal = null; |
733 |
– |
boolean validated = false; |
1345 |
|
try { |
1346 |
|
if (tabAt(tab, i) == f) { |
1347 |
< |
validated = true; |
1348 |
< |
for (Node e = f;;) { |
1347 |
> |
count = 1; |
1348 |
> |
for (Node e = f;; ++count) { |
1349 |
|
Object ek, ev; |
1350 |
|
if ((e.hash & HASH_BITS) == h && |
1351 |
|
(ev = e.val) != null && |
1356 |
|
Node last = e; |
1357 |
|
if ((e = e.next) == null) { |
1358 |
|
last.next = new Node(h, k, v, null); |
1359 |
+ |
if (count >= TREE_THRESHOLD) |
1360 |
+ |
replaceWithTreeBin(tab, i, k); |
1361 |
|
break; |
1362 |
|
} |
1363 |
|
} |
1368 |
|
synchronized (f) { f.notifyAll(); }; |
1369 |
|
} |
1370 |
|
} |
1371 |
< |
if (validated) { |
1371 |
> |
if (count != 0) { |
1372 |
|
if (oldVal != null) |
1373 |
|
return oldVal; |
1374 |
+ |
if (tab.length <= 64) |
1375 |
+ |
count = 2; |
1376 |
|
break; |
1377 |
|
} |
1378 |
|
} |
1379 |
|
} |
1380 |
|
} |
1381 |
|
counter.add(1L); |
1382 |
+ |
if (count > 1) |
1383 |
+ |
checkForResize(); |
1384 |
|
return null; |
1385 |
|
} |
1386 |
|
|
1389 |
|
MappingFunction<? super K, ?> mf) { |
1390 |
|
int h = spread(k.hashCode()); |
1391 |
|
Object val = null; |
1392 |
+ |
int count = 0; |
1393 |
|
for (Node[] tab = table;;) { |
1394 |
|
Node f; int i, fh; Object fk, fv; |
1395 |
|
if (tab == null) |
1396 |
|
tab = initTable(); |
1397 |
|
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1398 |
|
Node node = new Node(fh = h | LOCKED, k, null, null); |
781 |
– |
boolean validated = false; |
1399 |
|
if (casTabAt(tab, i, null, node)) { |
1400 |
< |
validated = true; |
1400 |
> |
count = 1; |
1401 |
|
try { |
1402 |
|
if ((val = mf.map(k)) != null) |
1403 |
|
node.val = val; |
1410 |
|
} |
1411 |
|
} |
1412 |
|
} |
1413 |
< |
if (validated) |
1413 |
> |
if (count != 0) |
1414 |
|
break; |
1415 |
|
} |
1416 |
< |
else if ((fh = f.hash) == MOVED) |
1417 |
< |
tab = (Node[])f.key; |
1416 |
> |
else if ((fh = f.hash) == MOVED) { |
1417 |
> |
if ((fk = f.key) instanceof TreeBin) { |
1418 |
> |
TreeBin t = (TreeBin)fk; |
1419 |
> |
boolean added = false; |
1420 |
> |
t.acquire(0); |
1421 |
> |
try { |
1422 |
> |
if (tabAt(tab, i) == f) { |
1423 |
> |
count = 1; |
1424 |
> |
TreeNode p = t.getTreeNode(h, k, t.root); |
1425 |
> |
if (p != null) |
1426 |
> |
val = p.val; |
1427 |
> |
else if ((val = mf.map(k)) != null) { |
1428 |
> |
added = true; |
1429 |
> |
count = 2; |
1430 |
> |
t.putTreeNode(h, k, val); |
1431 |
> |
} |
1432 |
> |
} |
1433 |
> |
} finally { |
1434 |
> |
t.release(0); |
1435 |
> |
} |
1436 |
> |
if (count != 0) { |
1437 |
> |
if (!added) |
1438 |
> |
return val; |
1439 |
> |
break; |
1440 |
> |
} |
1441 |
> |
} |
1442 |
> |
else |
1443 |
> |
tab = (Node[])fk; |
1444 |
> |
} |
1445 |
|
else if ((fh & HASH_BITS) == h && (fv = f.val) != null && |
1446 |
|
((fk = f.key) == k || k.equals(fk))) |
1447 |
|
return fv; |
1464 |
|
f.tryAwaitLock(tab, i); |
1465 |
|
} |
1466 |
|
else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) { |
1467 |
< |
boolean validated = false; |
1467 |
> |
boolean added = false; |
1468 |
|
try { |
1469 |
|
if (tabAt(tab, i) == f) { |
1470 |
< |
validated = true; |
1471 |
< |
for (Node e = f;;) { |
1470 |
> |
count = 1; |
1471 |
> |
for (Node e = f;; ++count) { |
1472 |
|
Object ek, ev; |
1473 |
|
if ((e.hash & HASH_BITS) == h && |
1474 |
|
(ev = e.val) != null && |
1478 |
|
} |
1479 |
|
Node last = e; |
1480 |
|
if ((e = e.next) == null) { |
1481 |
< |
if ((val = mf.map(k)) != null) |
1481 |
> |
if ((val = mf.map(k)) != null) { |
1482 |
> |
added = true; |
1483 |
|
last.next = new Node(h, k, val, null); |
1484 |
+ |
if (count >= TREE_THRESHOLD) |
1485 |
+ |
replaceWithTreeBin(tab, i, k); |
1486 |
+ |
} |
1487 |
|
break; |
1488 |
|
} |
1489 |
|
} |
1494 |
|
synchronized (f) { f.notifyAll(); }; |
1495 |
|
} |
1496 |
|
} |
1497 |
< |
if (validated) |
1497 |
> |
if (count != 0) { |
1498 |
> |
if (!added) |
1499 |
> |
return val; |
1500 |
> |
if (tab.length <= 64) |
1501 |
> |
count = 2; |
1502 |
|
break; |
1503 |
+ |
} |
1504 |
|
} |
1505 |
|
} |
1506 |
|
} |
1507 |
|
if (val == null) |
1508 |
|
throw new NullPointerException(); |
1509 |
|
counter.add(1L); |
1510 |
+ |
if (count > 1) |
1511 |
+ |
checkForResize(); |
1512 |
|
return val; |
1513 |
|
} |
1514 |
|
|
1519 |
|
int h = spread(k.hashCode()); |
1520 |
|
Object val = null; |
1521 |
|
boolean added = false; |
1522 |
< |
boolean checkSize = false; |
1522 |
> |
int count = 0; |
1523 |
|
for (Node[] tab = table;;) { |
1524 |
< |
Node f; int i, fh; |
1524 |
> |
Node f; int i, fh; Object fk; |
1525 |
|
if (tab == null) |
1526 |
|
tab = initTable(); |
1527 |
|
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1528 |
|
Node node = new Node(fh = h | LOCKED, k, null, null); |
874 |
– |
boolean validated = false; |
1529 |
|
if (casTabAt(tab, i, null, node)) { |
876 |
– |
validated = true; |
1530 |
|
try { |
1531 |
+ |
count = 1; |
1532 |
|
if ((val = mf.remap(k, null)) != null) { |
1533 |
|
node.val = val; |
1534 |
|
added = true; |
1542 |
|
} |
1543 |
|
} |
1544 |
|
} |
1545 |
< |
if (validated) |
1545 |
> |
if (count != 0) |
1546 |
|
break; |
1547 |
|
} |
1548 |
< |
else if ((fh = f.hash) == MOVED) |
1549 |
< |
tab = (Node[])f.key; |
1548 |
> |
else if ((fh = f.hash) == MOVED) { |
1549 |
> |
if ((fk = f.key) instanceof TreeBin) { |
1550 |
> |
TreeBin t = (TreeBin)fk; |
1551 |
> |
t.acquire(0); |
1552 |
> |
try { |
1553 |
> |
if (tabAt(tab, i) == f) { |
1554 |
> |
count = 1; |
1555 |
> |
TreeNode p = t.getTreeNode(h, k, t.root); |
1556 |
> |
Object pv = (p == null)? null : p.val; |
1557 |
> |
if ((val = mf.remap(k, (V)pv)) != null) { |
1558 |
> |
if (p != null) |
1559 |
> |
p.val = val; |
1560 |
> |
else { |
1561 |
> |
count = 2; |
1562 |
> |
added = true; |
1563 |
> |
t.putTreeNode(h, k, val); |
1564 |
> |
} |
1565 |
> |
} |
1566 |
> |
} |
1567 |
> |
} finally { |
1568 |
> |
t.release(0); |
1569 |
> |
} |
1570 |
> |
if (count != 0) |
1571 |
> |
break; |
1572 |
> |
} |
1573 |
> |
else |
1574 |
> |
tab = (Node[])fk; |
1575 |
> |
} |
1576 |
|
else if ((fh & LOCKED) != 0) { |
1577 |
|
checkForResize(); |
1578 |
|
f.tryAwaitLock(tab, i); |
1579 |
|
} |
1580 |
|
else if (f.casHash(fh, fh | LOCKED)) { |
901 |
– |
boolean validated = false; |
1581 |
|
try { |
1582 |
|
if (tabAt(tab, i) == f) { |
1583 |
< |
validated = true; |
1584 |
< |
for (Node e = f;;) { |
1583 |
> |
count = 1; |
1584 |
> |
for (Node e = f;; ++count) { |
1585 |
|
Object ek, ev; |
1586 |
|
if ((e.hash & HASH_BITS) == h && |
1587 |
|
(ev = e.val) != null && |
1596 |
|
if ((val = mf.remap(k, null)) != null) { |
1597 |
|
last.next = new Node(h, k, val, null); |
1598 |
|
added = true; |
1599 |
< |
if (last != f || tab.length <= 64) |
1600 |
< |
checkSize = true; |
1599 |
> |
if (count >= TREE_THRESHOLD) |
1600 |
> |
replaceWithTreeBin(tab, i, k); |
1601 |
|
} |
1602 |
|
break; |
1603 |
|
} |
1609 |
|
synchronized (f) { f.notifyAll(); }; |
1610 |
|
} |
1611 |
|
} |
1612 |
< |
if (validated) |
1612 |
> |
if (count != 0) { |
1613 |
> |
if (tab.length <= 64) |
1614 |
> |
count = 2; |
1615 |
|
break; |
1616 |
+ |
} |
1617 |
|
} |
1618 |
|
} |
1619 |
|
if (val == null) |
1620 |
|
throw new NullPointerException(); |
1621 |
|
if (added) { |
1622 |
|
counter.add(1L); |
1623 |
< |
if (checkSize) |
1623 |
> |
if (count > 1) |
1624 |
|
checkForResize(); |
1625 |
|
} |
1626 |
|
return val; |
1641 |
|
} |
1642 |
|
int h = spread(k.hashCode()); |
1643 |
|
for (Node[] tab = table;;) { |
1644 |
< |
int i; Node f; int fh; |
1644 |
> |
int i; Node f; int fh; Object fk; |
1645 |
|
if (tab == null) |
1646 |
|
tab = initTable(); |
1647 |
|
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){ |
1650 |
|
break; |
1651 |
|
} |
1652 |
|
} |
1653 |
< |
else if ((fh = f.hash) == MOVED) |
1654 |
< |
tab = (Node[])f.key; |
1653 |
> |
else if ((fh = f.hash) == MOVED) { |
1654 |
> |
if ((fk = f.key) instanceof TreeBin) { |
1655 |
> |
TreeBin t = (TreeBin)fk; |
1656 |
> |
boolean validated = false; |
1657 |
> |
t.acquire(0); |
1658 |
> |
try { |
1659 |
> |
if (tabAt(tab, i) == f) { |
1660 |
> |
validated = true; |
1661 |
> |
TreeNode p = t.getTreeNode(h, k, t.root); |
1662 |
> |
if (p != null) |
1663 |
> |
p.val = v; |
1664 |
> |
else { |
1665 |
> |
t.putTreeNode(h, k, v); |
1666 |
> |
++delta; |
1667 |
> |
} |
1668 |
> |
} |
1669 |
> |
} finally { |
1670 |
> |
t.release(0); |
1671 |
> |
} |
1672 |
> |
if (validated) |
1673 |
> |
break; |
1674 |
> |
} |
1675 |
> |
else |
1676 |
> |
tab = (Node[])fk; |
1677 |
> |
} |
1678 |
|
else if ((fh & LOCKED) != 0) { |
1679 |
|
counter.add(delta); |
1680 |
|
delta = 0L; |
1682 |
|
f.tryAwaitLock(tab, i); |
1683 |
|
} |
1684 |
|
else if (f.casHash(fh, fh | LOCKED)) { |
1685 |
< |
boolean validated = false; |
981 |
< |
boolean tooLong = false; |
1685 |
> |
int count = 0; |
1686 |
|
try { |
1687 |
|
if (tabAt(tab, i) == f) { |
1688 |
< |
validated = true; |
1689 |
< |
for (Node e = f;;) { |
1688 |
> |
count = 1; |
1689 |
> |
for (Node e = f;; ++count) { |
1690 |
|
Object ek, ev; |
1691 |
|
if ((e.hash & HASH_BITS) == h && |
1692 |
|
(ev = e.val) != null && |
1698 |
|
if ((e = e.next) == null) { |
1699 |
|
++delta; |
1700 |
|
last.next = new Node(h, k, v, null); |
1701 |
+ |
if (count >= TREE_THRESHOLD) |
1702 |
+ |
replaceWithTreeBin(tab, i, k); |
1703 |
|
break; |
1704 |
|
} |
999 |
– |
tooLong = true; |
1705 |
|
} |
1706 |
|
} |
1707 |
|
} finally { |
1710 |
|
synchronized (f) { f.notifyAll(); }; |
1711 |
|
} |
1712 |
|
} |
1713 |
< |
if (validated) { |
1714 |
< |
if (tooLong) { |
1713 |
> |
if (count != 0) { |
1714 |
> |
if (count > 1) { |
1715 |
|
counter.add(delta); |
1716 |
|
delta = 0L; |
1717 |
|
checkForResize(); |
1867 |
|
} |
1868 |
|
} |
1869 |
|
} |
1870 |
< |
else if (((fh = f.hash) & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) { |
1870 |
> |
else if ((fh = f.hash) == MOVED) { |
1871 |
> |
Object fk = f.key; |
1872 |
> |
if (fk instanceof TreeBin) { |
1873 |
> |
TreeBin t = (TreeBin)fk; |
1874 |
> |
boolean validated = false; |
1875 |
> |
t.acquire(0); |
1876 |
> |
try { |
1877 |
> |
if (tabAt(tab, i) == f) { |
1878 |
> |
validated = true; |
1879 |
> |
splitTreeBin(nextTab, i, t); |
1880 |
> |
setTabAt(tab, i, fwd); |
1881 |
> |
} |
1882 |
> |
} finally { |
1883 |
> |
t.release(0); |
1884 |
> |
} |
1885 |
> |
if (!validated) |
1886 |
> |
continue; |
1887 |
> |
} |
1888 |
> |
} |
1889 |
> |
else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) { |
1890 |
|
boolean validated = false; |
1891 |
|
try { // split to lo and hi lists; copying as needed |
1892 |
|
if (tabAt(tab, i) == f) { |
1893 |
|
validated = true; |
1894 |
< |
Node e = f, lastRun = f; |
1171 |
< |
Node lo = null, hi = null; |
1172 |
< |
int runBit = e.hash & n; |
1173 |
< |
for (Node p = e.next; p != null; p = p.next) { |
1174 |
< |
int b = p.hash & n; |
1175 |
< |
if (b != runBit) { |
1176 |
< |
runBit = b; |
1177 |
< |
lastRun = p; |
1178 |
< |
} |
1179 |
< |
} |
1180 |
< |
if (runBit == 0) |
1181 |
< |
lo = lastRun; |
1182 |
< |
else |
1183 |
< |
hi = lastRun; |
1184 |
< |
for (Node p = e; p != lastRun; p = p.next) { |
1185 |
< |
int ph = p.hash & HASH_BITS; |
1186 |
< |
Object pk = p.key, pv = p.val; |
1187 |
< |
if ((ph & n) == 0) |
1188 |
< |
lo = new Node(ph, pk, pv, lo); |
1189 |
< |
else |
1190 |
< |
hi = new Node(ph, pk, pv, hi); |
1191 |
< |
} |
1192 |
< |
setTabAt(nextTab, i, lo); |
1193 |
< |
setTabAt(nextTab, i + n, hi); |
1894 |
> |
splitBin(nextTab, i, f); |
1895 |
|
setTabAt(tab, i, fwd); |
1896 |
|
} |
1897 |
|
} finally { |
1937 |
|
} |
1938 |
|
|
1939 |
|
/** |
1940 |
+ |
* Split a normal bin with list headed by e into lo and hi parts; |
1941 |
+ |
* install in given table |
1942 |
+ |
*/ |
1943 |
+ |
private static void splitBin(Node[] nextTab, int i, Node e) { |
1944 |
+ |
int bit = nextTab.length >>> 1; // bit to split on |
1945 |
+ |
int runBit = e.hash & bit; |
1946 |
+ |
Node lastRun = e, lo = null, hi = null; |
1947 |
+ |
for (Node p = e.next; p != null; p = p.next) { |
1948 |
+ |
int b = p.hash & bit; |
1949 |
+ |
if (b != runBit) { |
1950 |
+ |
runBit = b; |
1951 |
+ |
lastRun = p; |
1952 |
+ |
} |
1953 |
+ |
} |
1954 |
+ |
if (runBit == 0) |
1955 |
+ |
lo = lastRun; |
1956 |
+ |
else |
1957 |
+ |
hi = lastRun; |
1958 |
+ |
for (Node p = e; p != lastRun; p = p.next) { |
1959 |
+ |
int ph = p.hash & HASH_BITS; |
1960 |
+ |
Object pk = p.key, pv = p.val; |
1961 |
+ |
if ((ph & bit) == 0) |
1962 |
+ |
lo = new Node(ph, pk, pv, lo); |
1963 |
+ |
else |
1964 |
+ |
hi = new Node(ph, pk, pv, hi); |
1965 |
+ |
} |
1966 |
+ |
setTabAt(nextTab, i, lo); |
1967 |
+ |
setTabAt(nextTab, i + bit, hi); |
1968 |
+ |
} |
1969 |
+ |
|
1970 |
+ |
/** |
1971 |
+ |
* Split a tree bin into lo and hi parts; install in given table |
1972 |
+ |
*/ |
1973 |
+ |
private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) { |
1974 |
+ |
int bit = nextTab.length >>> 1; |
1975 |
+ |
TreeBin lt = new TreeBin(); |
1976 |
+ |
TreeBin ht = new TreeBin(); |
1977 |
+ |
int lc = 0, hc = 0; |
1978 |
+ |
for (Node e = t.first; e != null; e = e.next) { |
1979 |
+ |
int h = e.hash & HASH_BITS; |
1980 |
+ |
Object k = e.key, v = e.val; |
1981 |
+ |
if ((h & bit) == 0) { |
1982 |
+ |
++lc; |
1983 |
+ |
lt.putTreeNode(h, k, v); |
1984 |
+ |
} |
1985 |
+ |
else { |
1986 |
+ |
++hc; |
1987 |
+ |
ht.putTreeNode(h, k, v); |
1988 |
+ |
} |
1989 |
+ |
} |
1990 |
+ |
Node ln, hn; // throw away trees if too small |
1991 |
+ |
if (lc <= (TREE_THRESHOLD >>> 1)) { |
1992 |
+ |
ln = null; |
1993 |
+ |
for (Node p = lt.first; p != null; p = p.next) |
1994 |
+ |
ln = new Node(p.hash, p.key, p.val, ln); |
1995 |
+ |
} |
1996 |
+ |
else |
1997 |
+ |
ln = new Node(MOVED, lt, null, null); |
1998 |
+ |
setTabAt(nextTab, i, ln); |
1999 |
+ |
if (hc <= (TREE_THRESHOLD >>> 1)) { |
2000 |
+ |
hn = null; |
2001 |
+ |
for (Node p = ht.first; p != null; p = p.next) |
2002 |
+ |
hn = new Node(p.hash, p.key, p.val, hn); |
2003 |
+ |
} |
2004 |
+ |
else |
2005 |
+ |
hn = new Node(MOVED, ht, null, null); |
2006 |
+ |
setTabAt(nextTab, i + bit, hn); |
2007 |
+ |
} |
2008 |
+ |
|
2009 |
+ |
/** |
2010 |
|
* Implementation for clear. Steps through each bin, removing all |
2011 |
|
* nodes. |
2012 |
|
*/ |
2015 |
|
int i = 0; |
2016 |
|
Node[] tab = table; |
2017 |
|
while (tab != null && i < tab.length) { |
2018 |
< |
int fh; |
2018 |
> |
int fh; Object fk; |
2019 |
|
Node f = tabAt(tab, i); |
2020 |
|
if (f == null) |
2021 |
|
++i; |
2022 |
< |
else if ((fh = f.hash) == MOVED) |
2023 |
< |
tab = (Node[])f.key; |
2022 |
> |
else if ((fh = f.hash) == MOVED) { |
2023 |
> |
if ((fk = f.key) instanceof TreeBin) { |
2024 |
> |
TreeBin t = (TreeBin)fk; |
2025 |
> |
t.acquire(0); |
2026 |
> |
try { |
2027 |
> |
if (tabAt(tab, i) == f) { |
2028 |
> |
for (Node p = t.first; p != null; p = p.next) { |
2029 |
> |
p.val = null; |
2030 |
> |
--delta; |
2031 |
> |
} |
2032 |
> |
t.first = null; |
2033 |
> |
t.root = null; |
2034 |
> |
++i; |
2035 |
> |
} |
2036 |
> |
} finally { |
2037 |
> |
t.release(0); |
2038 |
> |
} |
2039 |
> |
} |
2040 |
> |
else |
2041 |
> |
tab = (Node[])fk; |
2042 |
> |
} |
2043 |
|
else if ((fh & LOCKED) != 0) { |
2044 |
|
counter.add(delta); // opportunistically update count |
2045 |
|
delta = 0L; |
2046 |
|
f.tryAwaitLock(tab, i); |
2047 |
|
} |
2048 |
|
else if (f.casHash(fh, fh | LOCKED)) { |
1259 |
– |
boolean validated = false; |
2049 |
|
try { |
2050 |
|
if (tabAt(tab, i) == f) { |
1262 |
– |
validated = true; |
2051 |
|
for (Node e = f; e != null; e = e.next) { |
2052 |
< |
if (e.val != null) { // currently always true |
2053 |
< |
e.val = null; |
1266 |
< |
--delta; |
1267 |
< |
} |
2052 |
> |
e.val = null; |
2053 |
> |
--delta; |
2054 |
|
} |
2055 |
|
setTabAt(tab, i, null); |
2056 |
+ |
++i; |
2057 |
|
} |
2058 |
|
} finally { |
2059 |
|
if (!f.casHash(fh | LOCKED, fh)) { |
2061 |
|
synchronized (f) { f.notifyAll(); }; |
2062 |
|
} |
2063 |
|
} |
1277 |
– |
if (validated) |
1278 |
– |
++i; |
2064 |
|
} |
2065 |
|
} |
2066 |
|
if (delta != 0) |
2067 |
|
counter.add(delta); |
2068 |
|
} |
2069 |
|
|
1285 |
– |
|
2070 |
|
/* ----------------Table Traversal -------------- */ |
2071 |
|
|
2072 |
|
/** |
2075 |
|
* |
2076 |
|
* At each step, the iterator snapshots the key ("nextKey") and |
2077 |
|
* value ("nextVal") of a valid node (i.e., one that, at point of |
2078 |
< |
* snapshot, has a nonnull user value). Because val fields can |
2078 |
> |
* snapshot, has a non-null user value). Because val fields can |
2079 |
|
* change (including to null, indicating deletion), field nextVal |
2080 |
|
* might not be accurate at point of use, but still maintains the |
2081 |
|
* weak consistency property of holding a value that was once |
2145 |
|
if (e != null) // advance past used/skipped node |
2146 |
|
e = e.next; |
2147 |
|
while (e == null) { // get to next non-null bin |
2148 |
< |
Node[] t; int b, i, n; // checks must use locals |
2148 |
> |
Node[] t; int b, i, n; Object ek; // checks must use locals |
2149 |
|
if ((b = baseIndex) >= baseLimit || (i = index) < 0 || |
2150 |
|
(t = tab) == null || i >= (n = t.length)) |
2151 |
|
break outer; |
2152 |
< |
else if ((e = tabAt(t, i)) != null && e.hash == MOVED) |
2153 |
< |
tab = (Node[])e.key; // restarts due to null val |
2154 |
< |
else // visit upper slots if present |
2155 |
< |
index = (i += baseSize) < n ? i : (baseIndex = b + 1); |
2152 |
> |
else if ((e = tabAt(t, i)) != null && e.hash == MOVED) { |
2153 |
> |
if ((ek = e.key) instanceof TreeBin) |
2154 |
> |
e = ((TreeBin)ek).first; |
2155 |
> |
else { |
2156 |
> |
tab = (Node[])ek; |
2157 |
> |
continue; // restarts due to null val |
2158 |
> |
} |
2159 |
> |
} // visit upper slots if present |
2160 |
> |
index = (i += baseSize) < n ? i : (baseIndex = b + 1); |
2161 |
|
} |
2162 |
|
nextKey = e.key; |
2163 |
|
} while ((nextVal = e.val) == null);// skip deleted or special nodes |
3158 |
|
K k = (K) s.readObject(); |
3159 |
|
V v = (V) s.readObject(); |
3160 |
|
if (k != null && v != null) { |
3161 |
< |
p = new Node(spread(k.hashCode()), k, v, p); |
3161 |
> |
int h = spread(k.hashCode()); |
3162 |
> |
p = new Node(h, k, v, p); |
3163 |
|
++size; |
3164 |
|
} |
3165 |
|
else |
3175 |
|
n = tableSizeFor(sz + (sz >>> 1) + 1); |
3176 |
|
} |
3177 |
|
int sc = sizeCtl; |
3178 |
+ |
boolean collide = false; |
3179 |
|
if (n > sc && |
3180 |
|
UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) { |
3181 |
|
try { |
3186 |
|
while (p != null) { |
3187 |
|
int j = p.hash & mask; |
3188 |
|
Node next = p.next; |
3189 |
< |
p.next = tabAt(tab, j); |
3189 |
> |
Node q = p.next = tabAt(tab, j); |
3190 |
|
setTabAt(tab, j, p); |
3191 |
+ |
if (!collide && q != null && q.hash == p.hash) |
3192 |
+ |
collide = true; |
3193 |
|
p = next; |
3194 |
|
} |
3195 |
|
table = tab; |
3199 |
|
} finally { |
3200 |
|
sizeCtl = sc; |
3201 |
|
} |
3202 |
+ |
if (collide) { // rescan and convert to TreeBins |
3203 |
+ |
Node[] tab = table; |
3204 |
+ |
for (int i = 0; i < tab.length; ++i) { |
3205 |
+ |
int c = 0; |
3206 |
+ |
for (Node e = tabAt(tab, i); e != null; e = e.next) { |
3207 |
+ |
if (++c > TREE_THRESHOLD && |
3208 |
+ |
(e.key instanceof Comparable)) { |
3209 |
+ |
replaceWithTreeBin(tab, i, e.key); |
3210 |
+ |
break; |
3211 |
+ |
} |
3212 |
+ |
} |
3213 |
+ |
} |
3214 |
+ |
} |
3215 |
|
} |
3216 |
|
if (!init) { // Can only happen if unsafely published. |
3217 |
|
while (p != null) { |
3219 |
|
p = p.next; |
3220 |
|
} |
3221 |
|
} |
3222 |
+ |
|
3223 |
|
} |
3224 |
|
} |
3225 |
|
|