[concurrency-interest] The atomicity of StampedLock lock conversion

Chris Vest mr.chrisvest at gmail.com
Fri Oct 10 06:12:08 EDT 2014


Hi,

One might, without further research, come to be ostensibly reasonable conclusion that a read-write lock that offers lock conversion, would be able to convert a write-lock into a read-lock atomically.

Reading the documentation for the StampedLock#tryConvertToRead, we’re told that if given a write-lock stamp, the method will first release that write-lock, and then obtain a read-lock, thus leaving a window for someone else to grab the lock ahead of us.

This ends up being a bad thing in my case. My code can, however, also use optimistic read-locks just as well as the pessimistic read-lock, so I turn to StampedLock#tryConvertToOptimisticRead. I’m told that the write-lock will be released and that I’ll get an observation stamp. I am not told whether or not this transition is atomic. That is, if it’s possible for the lock to be taken and then released again, in between me releasing my write-lock and obtaining the observation stamp, in some sense, such that the observation stamp will validate when I ask about it later.

So that’s my question: Is StampedLock#tryConvertToOptimisticRead atomic in this respect?

Cheers,
Chris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20141010/245bb928/attachment.html>


More information about the Concurrency-interest mailing list