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

If 'h.status == 0' in cancelWaiter method of StampedLock can be deleted

    XMLWordPrintable

Details

    Description

      A DESCRIPTION OF THE PROBLEM :
      When I read the StampedLock source code, There is a code at the end of the cancelWaiter method

      if (h == whead) {
          if (q != null && h.status == 0 &&
              ((s = state) & ABITS) != WBIT &&
              (s == 0L || q.mode == RMODE))
              release(h);
          break;
       }
      I wonder when this happens?

      I think there might be a problem here, mainly with this condition 'h.status == 0', because 'q != null', so, there are only 2 cases that satisfy this condition 'h.status == 0'.

      The first is when a node is queued for initialization. Before the status is set, the status is 0, and it is in read lock state, the first three conditions can be met, but the fourth condition cannot.

      The second case is when the thread releases the lock. When the thread that acquired the lock executes the release method, status will be set to 0, so the release here is repeated, it's redundant.

      Both cases are meaningless.

      I think the author wrote this code for this scenario. There is a node in read mode, and there are some other nodes in front of it. Originally, it cannot get the lock. At a certain point, all the nodes in front of it are interrupted and cancelled, at this point, 'Stamplock' is in read lock state, so it can get the lock at this time, and then it will call release method to wake it up.

      Therefore, I think the condition 'h.status == 0' should be deleted to satisfy the scenario I mentioned.


      Attachments

        Activity

          People

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated: