[jmm-dev] bitwise RMW operators, specifically testAndSetBit/BTS

John Rose john.r.rose at oracle.com
Sat Jul 16 00:50:08 UTC 2016


On Jul 15, 2016, at 2:39 PM, Martin Buchholz <martinrb at google.com> wrote:
> 
> On Thu, Jul 14, 2016 at 7:09 PM, John Rose <john.r.rose at oracle.com <mailto:john.r.rose at oracle.com>> wrote:
> 
> The particular use case I have in mind is SeqLocks, specifically the writer-enter operation, which needs to change the lock state to "odd", unless it is already "odd", and let the processor know what happened.  An "xadd" cannot do this, but a "cmpxchg" or "bts" can, and the "bts" is preferable.
> 
> Most synchronizers have more complex state than "locked or unlocked".  StampedLock is a read-write lock, so you can only acquire the write lock if not currently read-locked.  (Did I miss something?)

The bitwise stuff allows you to acquire or release a single independent bit
in a lock word (or maybe more than one bit).  That bit doesn't have to encode
the whole state of the lock; in fact if it did we'd use getAndSet of a boolean.
The point is you can build lock state management on top of getAndBitwise*
in useful ways, when if the first interaction with the lock is to assert a setting
of that one state bit, while at the same time querying the values of the other bits.

> ReentrantLock is reentrant (!) so needs to store the lock hold count.  Perhaps ReentrantLock could benefit if you optimize for non-reentrant acquires, at the cost of doing an extra update for reentrant acquires.


It seems to me that any multi-field concurrent structure (like a StampedLock)
could be protected by a single-bit micro-lock built on top of a reserved bit taken
from one of the structure's fields.  There are often reasons not to do such
things, but when the technique is appropriate, the bitwise operators let you
lay down the bit inside the same cache line as the rest of the structure.
That seems like a win to me.

Some day we can persuade the JVM to loosen its grip on the slack bits
in pointers, allowing types like AtomicMarkableReference to be implemented
in one word.  In that case, AMR.attemptMark might use BTS/BTR.

— John


More information about the jmm-dev mailing list