[concurrency-interest] Upgrading a RL to WL inReentrantReadWriteLock

Bela Ban belaban at yahoo.com
Wed Dec 6 05:33:30 EST 2006


Hi David,





David Holmes wrote:
> Hi,
>
> This same question was asked only the other week.

Sorry for repeating the question ! To avoid repetitive FAQ questions, it 
would be *very* important to make the concurrency-interest archives 
*searchable*, which is not the case today !


That said, with changes that went in to Java 6 to track read-lock ownership,
it shouldn't be too hard for you to modify ReentrantReadWriteLock to allow
the final-reader to acquire the writeLock.

That's exactly what we do in JBossCache. However, this stuff should be 
part of j.u.c.l itself, and not reside in application code. In our case, 
the semantics are that any reader can upgrade a RL to a WL *if* there 
are no other readers or writers present. So a typical scenario for 
ReentrantReadWriteLock is:

    * TX1 acquires a RL
    * TX2 acquires a RL
    * TX2 attempts an upgrade from RL to WL and blocks for N ms
    * (before N elapses): TX1 releases its RL
    * TX2 is able to upgrade its RL to a WL

If TX2 released its RL and then tried to acquire a WL directly, we could 
run into deadlocks (with ensuing timeouts and TX rollbacks) because 
locks are not acquired in the same order by all TXs.

For now, I will look into AQS as suggested by Tim. Thanks,

P.S.: JCIP is one of the few books that are a must-read (besides Doug's 
and Joshua's books), congrats to all the authors who are lingering on 
this list !


-- 
Bela Ban
Lead JGroups / JBoss Clustering team
JBoss - a division of Red Hat


More information about the Concurrency-interest mailing list