ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/ConcurrentHashMapV8.java
(Generate patch)

Comparing jsr166/src/jsr166e/ConcurrentHashMapV8.java (file contents):
Revision 1.114 by dl, Fri Aug 9 18:43:44 2013 UTC vs.
Revision 1.123 by jsr166, Fri Feb 27 21:08:53 2015 UTC

# Line 15 | Line 15 | import java.lang.reflect.Type;
15   import java.util.AbstractMap;
16   import java.util.Arrays;
17   import java.util.Collection;
18 import java.util.Comparator;
18   import java.util.ConcurrentModificationException;
19   import java.util.Enumeration;
20   import java.util.HashMap;
# Line 276 | Line 275 | public class ConcurrentHashMapV8<K,V> ex
275      /** Interface describing a function mapping two ints to an int */
276      public interface IntByIntToInt { int apply(int a, int b); }
277  
278 +
279      /*
280       * Overview:
281       *
# Line 381 | Line 381 | public class ConcurrentHashMapV8<K,V> ex
381       * The table is resized when occupancy exceeds a percentage
382       * threshold (nominally, 0.75, but see below).  Any thread
383       * noticing an overfull bin may assist in resizing after the
384 <     * initiating thread allocates and sets up the replacement
385 <     * array. However, rather than stalling, these other threads may
386 <     * proceed with insertions etc.  The use of TreeBins shields us
387 <     * from the worst case effects of overfilling while resizes are in
384 >     * initiating thread allocates and sets up the replacement array.
385 >     * However, rather than stalling, these other threads may proceed
386 >     * with insertions etc.  The use of TreeBins shields us from the
387 >     * worst case effects of overfilling while resizes are in
388       * progress.  Resizing proceeds by transferring bins, one by one,
389 <     * from the table to the next table. To enable concurrency, the
390 <     * next table must be (incrementally) prefilled with place-holders
391 <     * serving as reverse forwarders to the old table.  Because we are
389 >     * from the table to the next table. However, threads claim small
390 >     * blocks of indices to transfer (via field transferIndex) before
391 >     * doing so, reducing contention.  A generation stamp in field
392 >     * sizeCtl ensures that resizings do not overlap. Because we are
393       * using power-of-two expansion, the elements from each bin must
394       * either stay at same index, or move with a power of two
395       * offset. We eliminate unnecessary node creation by catching
# Line 409 | Line 410 | public class ConcurrentHashMapV8<K,V> ex
410       * locks, average aggregate waits become shorter as resizing
411       * progresses.  The transfer operation must also ensure that all
412       * accessible bins in both the old and new table are usable by any
413 <     * traversal.  This is arranged by proceeding from the last bin
414 <     * (table.length - 1) up towards the first.  Upon seeing a
415 <     * forwarding node, traversals (see class Traverser) arrange to
416 <     * move to the new table without revisiting nodes.  However, to
417 <     * ensure that no intervening nodes are skipped, bin splitting can
418 <     * only begin after the associated reverse-forwarders are in
419 <     * place.
413 >     * traversal.  This is arranged in part by proceeding from the
414 >     * last bin (table.length - 1) up towards the first.  Upon seeing
415 >     * a forwarding node, traversals (see class Traverser) arrange to
416 >     * move to the new table without revisiting nodes.  To ensure that
417 >     * no intervening nodes are skipped even when moved out of order,
418 >     * a stack (see class TableStack) is created on first encounter of
419 >     * a forwarding node during a traversal, to maintain its place if
420 >     * later processing the current table. The need for these
421 >     * save/restore mechanics is relatively rare, but when one
422 >     * forwarding node is encountered, typically many more will be.
423 >     * So Traversers use a simple caching scheme to avoid creating so
424 >     * many new TableStack nodes. (Thanks to Peter Levart for
425 >     * suggesting use of a stack here.)
426       *
427       * The traversal scheme also applies to partial traversals of
428       * ranges of bins (via an alternate Traverser constructor)
# Line 572 | Line 579 | public class ConcurrentHashMapV8<K,V> ex
579       */
580      private static final int MIN_TRANSFER_STRIDE = 16;
581  
582 +    /**
583 +     * The number of bits used for generation stamp in sizeCtl.
584 +     * Must be at least 6 for 32bit arrays.
585 +     */
586 +    private static int RESIZE_STAMP_BITS = 16;
587 +
588 +    /**
589 +     * The maximum number of threads that can help resize.
590 +     * Must fit in 32 - RESIZE_STAMP_BITS bits.
591 +     */
592 +    private static final int MAX_RESIZERS = (1 << (32 - RESIZE_STAMP_BITS)) - 1;
593 +
594 +    /**
595 +     * The bit shift for recording size stamp in sizeCtl.
596 +     */
597 +    private static final int RESIZE_STAMP_SHIFT = 32 - RESIZE_STAMP_BITS;
598 +
599      /*
600       * Encodings for Node hash fields. See above for explanation.
601       */
# Line 613 | Line 637 | public class ConcurrentHashMapV8<K,V> ex
637              this.next = next;
638          }
639  
640 <        public final K getKey()       { return key; }
641 <        public final V getValue()     { return val; }
642 <        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
643 <        public final String toString(){ return key + "=" + val; }
640 >        public final K getKey()     { return key; }
641 >        public final V getValue()   { return val; }
642 >        public final int hashCode() { return key.hashCode() ^ val.hashCode(); }
643 >        public final String toString() { return key + "=" + val; }
644          public final V setValue(V value) {
645              throw new UnsupportedOperationException();
646          }
# Line 729 | Line 753 | public class ConcurrentHashMapV8<K,V> ex
753       * errors by users, these checks must operate on local variables,
754       * which accounts for some odd-looking inline assignments below.
755       * Note that calls to setTabAt always occur within locked regions,
756 <     * and so in principle require only release ordering, not need
756 >     * and so in principle require only release ordering, not
757       * full volatile semantics, but are currently coded as volatile
758       * writes to be conservative.
759       */
# Line 784 | Line 808 | public class ConcurrentHashMapV8<K,V> ex
808      private transient volatile int transferIndex;
809  
810      /**
787     * The least available table index to split while resizing.
788     */
789    private transient volatile int transferOrigin;
790
791    /**
811       * Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
812       */
813      private transient volatile int cellsBusy;
# Line 1446 | Line 1465 | public class ConcurrentHashMapV8<K,V> ex
1465                  int sz = (int)size;
1466                  n = tableSizeFor(sz + (sz >>> 1) + 1);
1467              }
1468 <            @SuppressWarnings({"rawtypes","unchecked"})
1469 <                Node<K,V>[] tab = (Node<K,V>[])new Node[n];
1468 >            @SuppressWarnings("unchecked")
1469 >                Node<K,V>[] tab = (Node<K,V>[])new Node<?,?>[n];
1470              int mask = n - 1;
1471              long added = 0L;
1472              while (p != null) {
# Line 2194 | Line 2213 | public class ConcurrentHashMapV8<K,V> ex
2213      /* ---------------- Table Initialization and Resizing -------------- */
2214  
2215      /**
2216 +     * Returns the stamp bits for resizing a table of size n.
2217 +     * Must be negative when shifted left by RESIZE_STAMP_SHIFT.
2218 +     */
2219 +    static final int resizeStamp(int n) {
2220 +        return Integer.numberOfLeadingZeros(n) | (1 << (RESIZE_STAMP_BITS - 1));
2221 +    }
2222 +
2223 +    /**
2224       * Initializes table, using the size recorded in sizeCtl.
2225       */
2226      private final Node<K,V>[] initTable() {
# Line 2205 | Line 2232 | public class ConcurrentHashMapV8<K,V> ex
2232                  try {
2233                      if ((tab = table) == null || tab.length == 0) {
2234                          int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
2235 <                        @SuppressWarnings({"rawtypes","unchecked"})
2236 <                            Node<K,V>[] nt = (Node<K,V>[])new Node[n];
2235 >                        @SuppressWarnings("unchecked")
2236 >                        Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
2237                          table = tab = nt;
2238                          sc = n - (n >>> 2);
2239                      }
# Line 2248 | Line 2275 | public class ConcurrentHashMapV8<K,V> ex
2275              s = sumCount();
2276          }
2277          if (check >= 0) {
2278 <            Node<K,V>[] tab, nt; int sc;
2278 >            Node<K,V>[] tab, nt; int n, sc;
2279              while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
2280 <                   tab.length < MAXIMUM_CAPACITY) {
2280 >                   (n = tab.length) < MAXIMUM_CAPACITY) {
2281 >                int rs = resizeStamp(n);
2282                  if (sc < 0) {
2283 <                    if (sc == -1 || transferIndex <= transferOrigin ||
2284 <                        (nt = nextTable) == null)
2283 >                    if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
2284 >                        sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
2285 >                        transferIndex <= 0)
2286                          break;
2287 <                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc - 1))
2287 >                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
2288                          transfer(tab, nt);
2289                  }
2290 <                else if (U.compareAndSwapInt(this, SIZECTL, sc, -2))
2290 >                else if (U.compareAndSwapInt(this, SIZECTL, sc,
2291 >                                             (rs << RESIZE_STAMP_SHIFT) + 2))
2292                      transfer(tab, null);
2293                  s = sumCount();
2294              }
# Line 2270 | Line 2300 | public class ConcurrentHashMapV8<K,V> ex
2300       */
2301      final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
2302          Node<K,V>[] nextTab; int sc;
2303 <        if ((f instanceof ForwardingNode) &&
2303 >        if (tab != null && (f instanceof ForwardingNode) &&
2304              (nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
2305 <            if (nextTab == nextTable && tab == table &&
2306 <                transferIndex > transferOrigin && (sc = sizeCtl) < -1 &&
2307 <                U.compareAndSwapInt(this, SIZECTL, sc, sc - 1))
2308 <                transfer(tab, nextTab);
2305 >            int rs = resizeStamp(tab.length);
2306 >            while (nextTab == nextTable && table == tab &&
2307 >                   (sc = sizeCtl) < 0) {
2308 >                if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
2309 >                    sc == rs + MAX_RESIZERS || transferIndex <= 0)
2310 >                    break;
2311 >                if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) {
2312 >                    transfer(tab, nextTab);
2313 >                    break;
2314 >                }
2315 >            }
2316              return nextTab;
2317          }
2318          return table;
# Line 2297 | Line 2334 | public class ConcurrentHashMapV8<K,V> ex
2334                  if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
2335                      try {
2336                          if (table == tab) {
2337 <                            @SuppressWarnings({"rawtypes","unchecked"})
2338 <                                Node<K,V>[] nt = (Node<K,V>[])new Node[n];
2337 >                            @SuppressWarnings("unchecked")
2338 >                            Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
2339                              table = nt;
2340                              sc = n - (n >>> 2);
2341                          }
# Line 2309 | Line 2346 | public class ConcurrentHashMapV8<K,V> ex
2346              }
2347              else if (c <= sc || n >= MAXIMUM_CAPACITY)
2348                  break;
2349 <            else if (tab == table &&
2350 <                     U.compareAndSwapInt(this, SIZECTL, sc, -2))
2351 <                transfer(tab, null);
2349 >            else if (tab == table) {
2350 >                int rs = resizeStamp(n);
2351 >                if (sc < 0) {
2352 >                    Node<K,V>[] nt;
2353 >                    if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
2354 >                        sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
2355 >                        transferIndex <= 0)
2356 >                        break;
2357 >                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
2358 >                        transfer(tab, nt);
2359 >                }
2360 >                else if (U.compareAndSwapInt(this, SIZECTL, sc,
2361 >                                             (rs << RESIZE_STAMP_SHIFT) + 2))
2362 >                    transfer(tab, null);
2363 >            }
2364          }
2365      }
2366  
# Line 2325 | Line 2374 | public class ConcurrentHashMapV8<K,V> ex
2374              stride = MIN_TRANSFER_STRIDE; // subdivide range
2375          if (nextTab == null) {            // initiating
2376              try {
2377 <                @SuppressWarnings({"rawtypes","unchecked"})
2378 <                    Node<K,V>[] nt = (Node<K,V>[])new Node[n << 1];
2377 >                @SuppressWarnings("unchecked")
2378 >                Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];
2379                  nextTab = nt;
2380              } catch (Throwable ex) {      // try to cope with OOME
2381                  sizeCtl = Integer.MAX_VALUE;
2382                  return;
2383              }
2384              nextTable = nextTab;
2336            transferOrigin = n;
2385              transferIndex = n;
2338            ForwardingNode<K,V> rev = new ForwardingNode<K,V>(tab);
2339            for (int k = n; k > 0;) {    // progressively reveal ready slots
2340                int nextk = (k > stride) ? k - stride : 0;
2341                for (int m = nextk; m < k; ++m)
2342                    nextTab[m] = rev;
2343                for (int m = n + nextk; m < n + k; ++m)
2344                    nextTab[m] = rev;
2345                U.putOrderedInt(this, TRANSFERORIGIN, k = nextk);
2346            }
2386          }
2387          int nextn = nextTab.length;
2388          ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
2389          boolean advance = true;
2390          boolean finishing = false; // to ensure sweep before committing nextTab
2391          for (int i = 0, bound = 0;;) {
2392 <            int nextIndex, nextBound, fh; Node<K,V> f;
2392 >            Node<K,V> f; int fh;
2393              while (advance) {
2394 +                int nextIndex, nextBound;
2395                  if (--i >= bound || finishing)
2396                      advance = false;
2397 <                else if ((nextIndex = transferIndex) <= transferOrigin) {
2397 >                else if ((nextIndex = transferIndex) <= 0) {
2398                      i = -1;
2399                      advance = false;
2400                  }
# Line 2368 | Line 2408 | public class ConcurrentHashMapV8<K,V> ex
2408                  }
2409              }
2410              if (i < 0 || i >= n || i + n >= nextn) {
2411 +                int sc;
2412                  if (finishing) {
2413                      nextTable = null;
2414                      table = nextTab;
2415                      sizeCtl = (n << 1) - (n >>> 1);
2416                      return;
2417                  }
2418 <                for (int sc;;) {
2419 <                    if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, ++sc)) {
2420 <                        if (sc != -1)
2421 <                            return;
2422 <                        finishing = advance = true;
2382 <                        i = n; // recheck before commit
2383 <                        break;
2384 <                    }
2385 <                }
2386 <            }
2387 <            else if ((f = tabAt(tab, i)) == null) {
2388 <                if (casTabAt(tab, i, null, fwd)) {
2389 <                    setTabAt(nextTab, i, null);
2390 <                    setTabAt(nextTab, i + n, null);
2391 <                    advance = true;
2418 >                if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {
2419 >                    if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
2420 >                        return;
2421 >                    finishing = advance = true;
2422 >                    i = n; // recheck before commit
2423                  }
2424              }
2425 +            else if ((f = tabAt(tab, i)) == null)
2426 +                advance = casTabAt(tab, i, null, fwd);
2427              else if ((fh = f.hash) == MOVED)
2428                  advance = true; // already processed
2429              else {
# Line 2477 | Line 2510 | public class ConcurrentHashMapV8<K,V> ex
2510      private final void treeifyBin(Node<K,V>[] tab, int index) {
2511          Node<K,V> b; int n, sc;
2512          if (tab != null) {
2513 <            if ((n = tab.length) < MIN_TREEIFY_CAPACITY) {
2514 <                if (tab == table && (sc = sizeCtl) >= 0 &&
2482 <                    U.compareAndSwapInt(this, SIZECTL, sc, -2))
2483 <                    transfer(tab, null);
2484 <            }
2513 >            if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
2514 >                tryPresize(n << 1);
2515              else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
2516                  synchronized (b) {
2517                      if (tabAt(tab, index) == b) {
# Line 2548 | Line 2578 | public class ConcurrentHashMapV8<K,V> ex
2578          final TreeNode<K,V> findTreeNode(int h, Object k, Class<?> kc) {
2579              if (k != null) {
2580                  TreeNode<K,V> p = this;
2581 <                do  {
2581 >                do {
2582                      int ph, dir; K pk; TreeNode<K,V> q;
2583                      TreeNode<K,V> pl = p.left, pr = p.right;
2584                      if ((ph = p.hash) > h)
# Line 2679 | Line 2709 | public class ConcurrentHashMapV8<K,V> ex
2709          private final void contendedLock() {
2710              boolean waiting = false;
2711              for (int s;;) {
2712 <                if (((s = lockState) & WRITER) == 0) {
2712 >                if (((s = lockState) & ~WAITER) == 0) {
2713                      if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER)) {
2714                          if (waiting)
2715                              waiter = null;
# Line 2702 | Line 2732 | public class ConcurrentHashMapV8<K,V> ex
2732           * using tree comparisons from root, but continues linear
2733           * search when lock not available.
2734           */
2735 < final Node<K,V> find(int h, Object k) {
2735 >        final Node<K,V> find(int h, Object k) {
2736              if (k != null) {
2737 <                for (Node<K,V> e = first; e != null; e = e.next) {
2737 >                for (Node<K,V> e = first; e != null; ) {
2738                      int s; K ek;
2739                      if (((s = lockState) & (WAITER|WRITER)) != 0) {
2740                          if (e.hash == h &&
2741                              ((ek = e.key) == k || (ek != null && k.equals(ek))))
2742                              return e;
2743 +                        e = e.next;
2744                      }
2745                      else if (U.compareAndSwapInt(this, LOCKSTATE, s,
2746                                                   s + READER)) {
# Line 2996 | Line 3027 | final Node<K,V> find(int h, Object k) {
3027  
3028          static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
3029                                                     TreeNode<K,V> x) {
3030 <            for (TreeNode<K,V> xp, xpl, xpr;;)  {
3030 >            for (TreeNode<K,V> xp, xpl, xpr;;) {
3031                  if (x == null || x == root)
3032                      return root;
3033                  else if ((xp = x.parent) == null) {
# Line 3128 | Line 3159 | final Node<K,V> find(int h, Object k) {
3159      /* ----------------Table Traversal -------------- */
3160  
3161      /**
3162 +     * Records the table, its length, and current traversal index for a
3163 +     * traverser that must process a region of a forwarded table before
3164 +     * proceeding with current table.
3165 +     */
3166 +    static final class TableStack<K,V> {
3167 +        int length;
3168 +        int index;
3169 +        Node<K,V>[] tab;
3170 +        TableStack<K,V> next;
3171 +    }
3172 +
3173 +    /**
3174       * Encapsulates traversal for methods such as containsValue; also
3175       * serves as a base class for other iterators and spliterators.
3176       *
# Line 3151 | Line 3194 | final Node<K,V> find(int h, Object k) {
3194      static class Traverser<K,V> {
3195          Node<K,V>[] tab;        // current table; updated if resized
3196          Node<K,V> next;         // the next entry to use
3197 +        TableStack<K,V> stack, spare; // to save/restore on ForwardingNodes
3198          int index;              // index of bin to use next
3199          int baseIndex;          // current index of initial table
3200          int baseLimit;          // index bound for initial table
# Line 3172 | Line 3216 | final Node<K,V> find(int h, Object k) {
3216              if ((e = next) != null)
3217                  e = e.next;
3218              for (;;) {
3219 <                Node<K,V>[] t; int i, n; K ek;  // must use locals in checks
3219 >                Node<K,V>[] t; int i, n;  // must use locals in checks
3220                  if (e != null)
3221                      return next = e;
3222                  if (baseIndex >= baseLimit || (t = tab) == null ||
3223                      (n = t.length) <= (i = index) || i < 0)
3224                      return next = null;
3225 <                if ((e = tabAt(t, index)) != null && e.hash < 0) {
3225 >                if ((e = tabAt(t, i)) != null && e.hash < 0) {
3226                      if (e instanceof ForwardingNode) {
3227                          tab = ((ForwardingNode<K,V>)e).nextTable;
3228                          e = null;
3229 +                        pushState(t, i, n);
3230                          continue;
3231                      }
3232                      else if (e instanceof TreeBin)
# Line 3189 | Line 3234 | final Node<K,V> find(int h, Object k) {
3234                      else
3235                          e = null;
3236                  }
3237 <                if ((index += baseSize) >= n)
3238 <                    index = ++baseIndex;    // visit upper slots if present
3237 >                if (stack != null)
3238 >                    recoverState(n);
3239 >                else if ((index = i + baseSize) >= n)
3240 >                    index = ++baseIndex; // visit upper slots if present
3241 >            }
3242 >        }
3243 >
3244 >        /**
3245 >         * Saves traversal state upon encountering a forwarding node.
3246 >         */
3247 >        private void pushState(Node<K,V>[] t, int i, int n) {
3248 >            TableStack<K,V> s = spare;  // reuse if possible
3249 >            if (s != null)
3250 >                spare = s.next;
3251 >            else
3252 >                s = new TableStack<K,V>();
3253 >            s.tab = t;
3254 >            s.length = n;
3255 >            s.index = i;
3256 >            s.next = stack;
3257 >            stack = s;
3258 >        }
3259 >
3260 >        /**
3261 >         * Possibly pops traversal state.
3262 >         *
3263 >         * @param n length of current table
3264 >         */
3265 >        private void recoverState(int n) {
3266 >            TableStack<K,V> s; int len;
3267 >            while ((s = stack) != null && (index += (len = s.length)) >= n) {
3268 >                n = len;
3269 >                index = s.index;
3270 >                tab = s.tab;
3271 >                s.tab = null;
3272 >                TableStack<K,V> next = s.next;
3273 >                s.next = spare; // save for reuse
3274 >                stack = next;
3275 >                spare = s;
3276              }
3277 +            if (s == null && (index += baseSize) >= n)
3278 +                index = ++baseIndex;
3279          }
3280      }
3281  
# Line 6159 | Line 6243 | final Node<K,V> find(int h, Object k) {
6243      private static final sun.misc.Unsafe U;
6244      private static final long SIZECTL;
6245      private static final long TRANSFERINDEX;
6162    private static final long TRANSFERORIGIN;
6246      private static final long BASECOUNT;
6247      private static final long CELLSBUSY;
6248      private static final long CELLVALUE;
# Line 6174 | Line 6257 | final Node<K,V> find(int h, Object k) {
6257                  (k.getDeclaredField("sizeCtl"));
6258              TRANSFERINDEX = U.objectFieldOffset
6259                  (k.getDeclaredField("transferIndex"));
6177            TRANSFERORIGIN = U.objectFieldOffset
6178                (k.getDeclaredField("transferOrigin"));
6260              BASECOUNT = U.objectFieldOffset
6261                  (k.getDeclaredField("baseCount"));
6262              CELLSBUSY = U.objectFieldOffset

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines