views:

2418

answers:

7

I am using in my code at the moment a ReentrantReadWriteLock to synchronize access over a tree-like structure. This structure is large, and read by many threads at once with occasional modifications to small parts of it - so it seems to fit the read-write idiom well. I understand that with this particular class, one cannot elevate a read lock to a write lock, so as per the Javadocs one must release the read lock before obtaining the write lock. I've used this pattern successfully in non-reentrant contexts before.

What I'm finding however is that I cannot reliably acquire the write lock without blocking forever. Since the read lock is reentrant and I am actually using it as such, the simple code

lock.getReadLock().unlock();
lock.getWriteLock().lock()

can block if I have acquired the readlock reentrantly. Each call to unlock just reduces the hold count, and the lock is only actually released when the hold count hits zero.

EDIT to clarify this, as I don't think I explained it too well initially - I am aware that there is no built-in lock escalation in this class, and that I have to simply release the read lock and obtain the write lock. My problem is/was that regardless of what other threads are doing, calling getReadLock().unlock() may not actually release this thread's hold on the lock if it acquired it reentrantly, in which case the call to getWriteLock().lock() will block forever as this thread still has a hold on the read lock and thus blocks itself.

For example, this code snippet will never reach the println statement, even when run singlethreaded with no other threads accessing the lock:

final ReadWriteLock lock = new ReentrantReadWriteLock();
lock.getReadLock().lock();

// In real code we would go call other methods that end up calling back and
// thus locking again
lock.getReadLock().lock();

// Now we do some stuff and realise we need to write so try to escalate the
// lock as per the Javadocs and the above description
lock.getReadLock().unlock(); // Does not actually release the lock
lock.getWriteLock().lock();  // Blocks as some thread (this one!) holds read lock

System.out.println("Will never get here");

So I ask, is there a nice idiom to handle this situation? Specifically, when a thread that holds a read lock (possibly reentrantly) discovers that it needs to do some writing, and thus wants to "suspend" its own read lock in order to pick up the write lock (blocking as required on other threads to release their holds on the read lock), and then "pick up" its hold on the read lock in the same state afterwards?

Since this ReadWriteLock implementation was specifically designed to be reentrant, surely there is some sensible way to elevate a read lock to a write lock when the locks may be acquired reentrantly? This is the critical part that means the naive approach does not work.

+2  A: 

I have made a little progress on this. By declaring the lock variable explicitly as a ReentrantReadWriteLock instead of simply a ReadWriteLock (less than ideal, but probably a necessary evil in this case) I can call the getReadHoldCount() method. This lets me obtain the number of holds for the current thread, and thus I can release the readlock this many times (and reacquire it the same number afterwards). So this works, as shown by a quick-and-dirty test:

final int holdCount = lock.getReadHoldCount();
for (int i = 0; i < holdCount; i++)
{
   lock.getReadLock().unlock;
}
lock.getWriteLock().lock()
try
{
   // Perform modifications
}
finally
{
   // Downgrade by reacquiring read lock before releasing write lock
   for (int i = 0; i < holdCount; i++)
   {
      lock.getReadLock().lock();
   }
   lock.getWriteLock().unlock();
}

Still, is this going to be the best I can do? It doesn't feel very elegant, and I'm still hoping that there's a way to handle this in a less "manual" fashion.

Andrzej Doyle
Note that getReadHoldCount's javadoc has the following advice: "This method is designed for use in monitoring system state, not for synchronization control", which seems to imply that it's not reliable.
Olivier
Furthermore, I think getReadHoldCount returns the count for all threads (although the doc doesn't say it clearly). While RRWL permits releasing other threads' read locks, your second for loop is reacquiring them all with the current thread, which is more than you want.
Olivier
I think you're looking at getRead*LOCK*Count*(), rather than getRead*HOLD*Count*(). The latter has no such warning in the Javadocs and does return the number of holds for the current thread (e.g. lock twice in thread A, then 3 times in thread B, it will return 2 in thread A and not 5).
Andrzej Doyle
Exact! That pretty much voids my two comments :-)
Olivier
A: 

Use the "fair" flag on the ReentrantReadWriteLock. "fair" means that lock requests are served on first come, first served. You could experience performance depredation since when you'll issue a "write" request, all of the subsequent "read" requests will be locked, even if they could have been served while the pre-existing read locks are still locked.

Ran Biron
I don't think you understand my problem, starvation is not an issue here.Try this in a single thread:ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);lock.readLock().lock();lock.readLock().lock();lock.readLock().unlock();lock.writeLock().lock();It still blocks forever.
Andrzej Doyle
+2  A: 

What you're looking for is a lock upgrade, and is not possible (at least not atomically) using the standard java.concurrent ReentrantReadWriteLock. Your best shot is unlock/lock, and then check that noone made modifications inbetween.

What you're attempting to do, forcing all read locks out of the way is not a very good idea. Read locks are there for a reason, that you shouldn't write. :)

EDIT:
As Ran Biron pointed out, if your problem is starvation (read locks are being set and released all the time, never dropping to zero) you could try using fair queueing. But your question didn't sound like this was your problem?

EDIT 2:
I now see your problem, you've actually acquired multiple read-locks on the stack, and you'd like to convert them to a write-lock (upgrade). This is in fact impossible with the JDK-implementation, as it doesn't keep track of the owners of the read-lock. There could be others holding read-locks that you wouldn't see, and it has no idea how many of the read-locks belong to your thread, not to mention your current call-stack (i.e. your loop is killing all read locks, not just your own, so your write lock won't wait for any concurrent readers to finish, and you'll end up with a mess on your hands)

I've actually had a similar problem, and I ended up writing my own lock keeping track of who's got what read-locks and upgrading these to write-locks. Although this was also a Copy-on-Write kind of read/write lock (allowing one writer along the readers), so it was a little different still.

roe
Right, that's the issue. I'm not trying to force ALL read locks out of the way, just the ones held by the current thread. I'm happy for the write lock acquisition to wait for *other* threads to release their read locks.
Andrzej Doyle
In fact the JDK implementation does keep track of who locked the readlock to some extent; getReadHoldCount() returns the count for the current thread only (via a ThreadLocal) so exactly the right number of unlocks() can be called in this situation. The code I put in my own answer does in fact work.
Andrzej Doyle
interesting, then this has been changed in jdk1.6, jdk1.5 does not use thread locals at all.
roe
Apparently so - http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6364793
Andrzej Doyle
There is no way to upgrade a read lock to a write lock that is guaranteed to succeed. A variant of "unlock/lock, and then check that no one made modifications in between" worked for me.
Seun Osewa
A: 

I suppose the ReentrantLock is motivated by a recursive traversal of the tree:

public void doSomething(Node node) {
  // Acquire reentrant lock
  ... // Do something, possibly acquire write lock
  for (Node child : node.childs) {
    doSomething(child);
  }
  // Release reentrant lock
}

Can't you refactor your code to move the lock handling outside of the recursion ?

public void doSomething(Node node) {
  // Acquire NON-reentrant read lock
  recurseDoSomething(node);
  // Release NON-reentrant read lock
}

private void recurseDoSomething(Node node) {
  ... // Do something, possibly acquire write lock
  for (Node child : node.childs) {
    recurseDoSomething(child);
  }
}
Olivier
Not easily. There's a visitor pattern element to this meaning we lock the node for reading and then call out to other classes whose logic may or may not call back in pointing at the same node. Besides (in the more abstract sense) why make the lock reentrant if you can't use it safely that way? :-)
Andrzej Doyle
The idea is to acquire the read lock at client-invocation level. Your other classes could point back to the "internal" method (would probably become protected), knowing they are operating in the context of the read lock.
Olivier
True, I could do that, but then these methods are not inherently thread-safe. So far as possible I don't want to depend on the client needing to obtain a lock with subtly incorrect (yet often apparently correct) behaviour if they don't.
Andrzej Doyle
A: 

to OP: just unlock as many times as you have entered the lock, simple as that:

boolean needWrite = false;
readLock.lock()
try{
  needWrite = checkState();
}finally{
  readLock().unlock()
}

//the state is free to change right here, but not likely
//see who has handled it under the write lock, if need be
if (needWrite){
  writeLock().lock();
  try{
    if (checkState()){//check again under the exclusive write lock
   //modify state
    }
  }finally{
    writeLock.unlock()
  }
}

in the write lock as any self-respect concurrent program check the state needed.

HoldCount shan't be used beyond debug/monitor/fast-fail detect. It'

xss
A: 

What you are trying to do is simply not possible this way.

You cannot have a read/write lock that you can upgrade from read to write without problems. Example:

void test() {
    lock.readLock().lock();
    ...
    if ( ... ) {
        lock.writeLock.lock();
        ...
        lock.writeLock.unlock();
    }
    lock.readLock().unlock();
}

Now suppose, two threads would enter that function. (And you are assuming concurrency, right? Otherwise you would not care about locks in the first place....)

Assume both threads would start at the same time and run equally fast. That would mean, both would acquire a read lock, which is perfectly legal. However, then both would eventually try to acquire the write lock, which NONE of them will ever get: The respective other threads hold a read lock!

Locks that allow upgrading of read locks to write locks are prone to deadlocks by definition. Sorry, but you need to modify your approach.

Steffen Heil