568 |
|
/* |
569 |
|
* Encodings for Node hash fields. See above for explanation. |
570 |
|
*/ |
571 |
< |
static final int MOVED = 0x8fffffff; // (-1) hash for forwarding nodes |
572 |
< |
static final int TREEBIN = 0x80000000; // hash for roots of trees |
573 |
< |
static final int RESERVED = 0x80000001; // hash for transient reservations |
571 |
> |
static final int MOVED = -1; // hash for forwarding nodes |
572 |
> |
static final int TREEBIN = -2; // hash for roots of trees |
573 |
> |
static final int RESERVED = -3; // hash for transient reservations |
574 |
|
static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash |
575 |
|
|
576 |
|
/** Number of CPUS, to place bounds on some sizings */ |
597 |
|
final int hash; |
598 |
|
final K key; |
599 |
|
volatile V val; |
600 |
< |
Node<K,V> next; |
600 |
> |
volatile Node<K,V> next; |
601 |
|
|
602 |
|
Node(int hash, K key, V val, Node<K,V> next) { |
603 |
|
this.hash = hash; |
722 |
|
* errors by users, these checks must operate on local variables, |
723 |
|
* which accounts for some odd-looking inline assignments below. |
724 |
|
* Note that calls to setTabAt always occur within locked regions, |
725 |
< |
* and so do not need full volatile semantics, but still require |
726 |
< |
* ordering to maintain concurrent readability. |
725 |
> |
* and so in principle require only release ordering, not need |
726 |
> |
* full volatile semantics, but are currently coded as volatile |
727 |
> |
* writes to be conservative. |
728 |
|
*/ |
729 |
|
|
730 |
|
@SuppressWarnings("unchecked") |
738 |
|
} |
739 |
|
|
740 |
|
static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) { |
741 |
< |
U.putOrderedObject(tab, ((long)i << ASHIFT) + ABASE, v); |
741 |
> |
U.putObjectVolatile(tab, ((long)i << ASHIFT) + ABASE, v); |
742 |
|
} |
743 |
|
|
744 |
|
/* ---------------- Fields -------------- */ |
2141 |
|
} |
2142 |
|
|
2143 |
|
Node<K,V> find(int h, Object k) { |
2144 |
< |
Node<K,V> e; int n; |
2145 |
< |
Node<K,V>[] tab = nextTable; |
2146 |
< |
if (k != null && tab != null && (n = tab.length) > 0 && |
2147 |
< |
(e = tabAt(tab, (n - 1) & h)) != null) { |
2148 |
< |
do { |
2144 |
> |
// loop to avoid arbitrarily deep recursion on forwarding nodes |
2145 |
> |
outer: for (Node<K,V>[] tab = nextTable;;) { |
2146 |
> |
Node<K,V> e; int n; |
2147 |
> |
if (k == null || tab == null || (n = tab.length) == 0 || |
2148 |
> |
(e = tabAt(tab, (n - 1) & h)) == null) |
2149 |
> |
return null; |
2150 |
> |
for (;;) { |
2151 |
|
int eh; K ek; |
2152 |
|
if ((eh = e.hash) == h && |
2153 |
|
((ek = e.key) == k || (ek != null && k.equals(ek)))) |
2154 |
|
return e; |
2155 |
< |
if (eh < 0) |
2156 |
< |
return e.find(h, k); |
2157 |
< |
} while ((e = e.next) != null); |
2155 |
> |
if (eh < 0) { |
2156 |
> |
if (e instanceof ForwardingNode) { |
2157 |
> |
tab = ((ForwardingNode<K,V>)e).nextTable; |
2158 |
> |
continue outer; |
2159 |
> |
} |
2160 |
> |
else |
2161 |
> |
return e.find(h, k); |
2162 |
> |
} |
2163 |
> |
if ((e = e.next) == null) |
2164 |
> |
return null; |
2165 |
> |
} |
2166 |
|
} |
2156 |
– |
return null; |
2167 |
|
} |
2168 |
|
} |
2169 |
|
|
2337 |
|
int nextn = nextTab.length; |
2338 |
|
ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab); |
2339 |
|
boolean advance = true; |
2340 |
+ |
boolean finishing = false; // to ensure sweep before committing nextTab |
2341 |
|
for (int i = 0, bound = 0;;) { |
2342 |
|
int nextIndex, nextBound, fh; Node<K,V> f; |
2343 |
|
while (advance) { |
2344 |
< |
if (--i >= bound) |
2344 |
> |
if (--i >= bound || finishing) |
2345 |
|
advance = false; |
2346 |
|
else if ((nextIndex = transferIndex) <= transferOrigin) { |
2347 |
|
i = -1; |
2357 |
|
} |
2358 |
|
} |
2359 |
|
if (i < 0 || i >= n || i + n >= nextn) { |
2360 |
+ |
if (finishing) { |
2361 |
+ |
nextTable = null; |
2362 |
+ |
table = nextTab; |
2363 |
+ |
sizeCtl = (n << 1) - (n >>> 1); |
2364 |
+ |
return; |
2365 |
+ |
} |
2366 |
|
for (int sc;;) { |
2367 |
|
if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, ++sc)) { |
2368 |
< |
if (sc == -1) { |
2369 |
< |
nextTable = null; |
2370 |
< |
table = nextTab; |
2371 |
< |
sizeCtl = (n << 1) - (n >>> 1); |
2372 |
< |
} |
2356 |
< |
return; |
2368 |
> |
if (sc != -1) |
2369 |
> |
return; |
2370 |
> |
finishing = advance = true; |
2371 |
> |
i = n; // recheck before commit |
2372 |
> |
break; |
2373 |
|
} |
2374 |
|
} |
2375 |
|
} |
2411 |
|
else |
2412 |
|
hn = new Node<K,V>(ph, pk, pv, hn); |
2413 |
|
} |
2414 |
+ |
setTabAt(nextTab, i, ln); |
2415 |
+ |
setTabAt(nextTab, i + n, hn); |
2416 |
+ |
setTabAt(tab, i, fwd); |
2417 |
+ |
advance = true; |
2418 |
|
} |
2419 |
|
else if (f instanceof TreeBin) { |
2420 |
|
TreeBin<K,V> t = (TreeBin<K,V>)f; |
2446 |
|
(hc != 0) ? new TreeBin<K,V>(lo) : t; |
2447 |
|
hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) : |
2448 |
|
(lc != 0) ? new TreeBin<K,V>(hi) : t; |
2449 |
+ |
setTabAt(nextTab, i, ln); |
2450 |
+ |
setTabAt(nextTab, i + n, hn); |
2451 |
+ |
setTabAt(tab, i, fwd); |
2452 |
+ |
advance = true; |
2453 |
|
} |
2430 |
– |
else |
2431 |
– |
ln = hn = null; |
2432 |
– |
setTabAt(nextTab, i, ln); |
2433 |
– |
setTabAt(nextTab, i + n, hn); |
2434 |
– |
setTabAt(tab, i, fwd); |
2435 |
– |
advance = true; |
2454 |
|
} |
2455 |
|
} |
2456 |
|
} |
2471 |
|
U.compareAndSwapInt(this, SIZECTL, sc, -2)) |
2472 |
|
transfer(tab, null); |
2473 |
|
} |
2474 |
< |
else if ((b = tabAt(tab, index)) != null) { |
2474 |
> |
else if ((b = tabAt(tab, index)) != null && b.hash >= 0) { |
2475 |
|
synchronized (b) { |
2476 |
|
if (tabAt(tab, index) == b) { |
2477 |
|
TreeNode<K,V> hd = null, tl = null; |