Search code examples
javamultithreadingreentrantlockreentrantreadwritelockquasar

Quasar ReenterantReadWriteLock can't be acquired


I've got a lock:

private ReentrantReadWriteLock requestWaitingLock;

And I'm executing this block inside a Fiber:

System.out.println(requestWaitingLock.isWriteLocked());
System.out.println(requestWaitingLock.getReadHoldCount());
requestWaitingLock.writeLock().lock();
System.out.println("write lock acquired");

And that' because in docs it says:

public void lock()
Acquires the write lock if neither the read nor write lock are held by another strand and returns immediately, setting the write lock hold count to one.
If the current strand already holds the write lock then the hold count is incremented by one and the method returns immediately.
If the lock is held by another strand then the current strand becomes disabled for strand scheduling purposes and lies dormant until the write lock has been acquired, at which time the write lock hold count is set to one.

So I get this output:

false
0

This means neither read nor write lock are held by any Strand at all. So why is my Fiber getting blocked?

EDIT. I'm pretty sure that every fiber which acquires this lock will release it, so it's not possible that other fibers acquire it infinitely.


Solution

  • You seem to call incorrect method to check reader locks:

    • getReadHoldCount - for current thread only
    • getReadLockCount - for all threads in the system