Try upgrades and downgrades for POSIX rwlocks

Alfred Perlstein alfred at
Tue Jan 25 01:33:47 UTC 2011

* John Baldwin <jhb at> [110124 13:05] wrote:
> Does anyone know if there is a de facto or proposed standard for supporting 
> upgrades and downgrades in POSIX rwlocks?  IBM seems to support something 
> rather gross where a wrlock() will succeed if the only shared lock is held by 
> the current thread.  But then the thread holds both a read and write lock, and 
> it has to call unlock twice, the first to drop the write lock, the second to 
> drop the read lock.  If we were to add support for upgrades and downgrades I 
> would prefer something more along the lines of our in-kernel APIs where there 
> are try_upgrade() and downgrade() operations that convert a given lock between 
> states.

There may a be a tricky problem here.

In order to avoid writer starvation (in the kernel) we give writers
preference over readers so that many concurrent readers can't keep
the lock in a shared-only state forever.  (Or at least that's how I
left it when I touched it last. :D )

There is a problem here.

To conserve that behavior we need to look at the situation of an upgrade:
1) we have to put the upgrader to the front of the writer queue,
this can starve other threads looking for only a writer lock by
giving an unfair advantage to those that take a share lock first.

2) we don't even look at this issue and we wind up with some deadlock.

At a glance, I think we have to have some kind of "try_upgrade"
that doesn't give preference to the thread already holding the lock.

We should probably strongly encourage try_upgrades to have some sort
of fault injection so that a potentially infrequently "missed upgrade"
path can be exercised easily.  Perhaps with a kernel config option or
that fault injection stuff?

just some ideas.

Maybe there's someone that can explain how IBM does the rwlocks.

Why do we want to have our own method.  And if our methods are different,
does it break the semantics that may become standard across the board?  Does it
help us to diverge?  What about OS X and Solaris?


More information about the freebsd-threads mailing list