The same thread of Java ReentrantWriteLock first acquires the read lock, but no longer acquires the write lock.

problem description

ReentrantWriteLock the same thread first acquires a write lock and then acquires a read lock (lock degradation) and a write lock, but after acquiring a read lock, it can only acquire a read lock. What is the reason for this?

related codes

protected final int tryAcquireShared(int unused) {
            /*
             * Walkthrough:
             * 1. If write lock held by another thread, fail.
             * 2. Otherwise, this thread is eligible for
             *    lock wrt state, so ask if it should block
             *    because of queue policy. If not, try
             *    to grant by CASing state and updating count.
             *    Note that step does not check for reentrant
             *    acquires, which is postponed to full version
             *    to avoid having to check hold count in
             *    the more typical non-reentrant case.
             * 3. If step 2 fails either because thread
             *    apparently not eligible or CAS fails or count
             *    saturated, chain to version with full retry loop.
             */
            Thread current = Thread.currentThread();
            int c = getState();
            if (exclusiveCount(c) != 0 &&
                getExclusiveOwnerThread() != current)
                return -1;
            int r = sharedCount(c);
            if (!readerShouldBlock() &&
                r < MAX_COUNT &&
                compareAndSetState(c, c + SHARED_UNIT)) {
                if (r == 0) {
                    firstReader = current;
                    firstReaderHoldCount = 1;
                } else if (firstReader == current) {
                    firstReaderHoldCountPP;
                } else {
                    HoldCounter rh = cachedHoldCounter;
                    if (rh == null || rh.tid != getThreadId(current))
                        cachedHoldCounter = rh = readHolds.get();
                    else if (rh.count == 0)
                        readHolds.set(rh);
                    rh.countPP;
                }
                return 1;
            }
            return fullTryAcquireShared(current);
        }
Nov.23,2021

isn't that what read-write locks are for? A write lock is an exclusive lock, and when a thread holds a write lock, it can guarantee that no other thread holds the lock, which is equivalent to monopoly, so it can hold the read lock again. But not the other way around, because the read lock is not exclusive, and other threads may be holding read or write locks, so they cannot be upgraded at will

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b37471-2b8c9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b37471-2b8c9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?