Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8214427

probable bug in logic of ConcurrentHashMap.addCount()

    XMLWordPrintable

    Details

      Backports

        Description

        A DESCRIPTION OF THE PROBLEM :
        if (check >= 0) {
                    Node<K,V>[] tab, nt; int n, sc;
                    while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
                           (n = tab.length) < MAXIMUM_CAPACITY) {
                        int rs = resizeStamp(n);
                        if (sc < 0) {
                            if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
                                sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
                                transferIndex <= 0)
                                break;
                            if (U.compareAndSetInt(this, SIZECTL, sc, sc + 1))
                                transfer(tab, nt);
                        }
                        else if (U.compareAndSetInt(this, SIZECTL, sc,
                                                     (rs << RESIZE_STAMP_SHIFT) + 2))
                            transfer(tab, null);
                        s = sumCount();
                    }
                }

        In the above code, condition of (sc == rs + 1 || sc == rs + MAX_RESIZERS ) would never be true , since the value of rs is positive and the value of sc is negative .

        The correct condition should be (sc >>> RESIZE_STAMP_SHIFT) == rs + 1 || (sc >>> RESIZE_STAMP_SHIFT) == rs + MAX_RESIZERS, which can be used to dedect if resizing process finished or resizing threads reaches maxmium limitation

        if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
                                sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
                                transferIndex <= 0)


        FREQUENCY : always


        Edited to correct apparent typo: it stated "... the value of rs is negative" but it is sc that is negative.

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                martin Martin Buchholz
                Reporter:
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                10 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved: