Javadoc
Base class that encapsulates non-blocking locking for different locking modes
as specified in
ExclusiveSharedLockObject by manipulation of the
internal lock state integer only. The wait queue for state has been split out
in
QueuedSynchronizer class.
Also provides helper protected methods to provide blocking lock when
explicitly provided a
QueuedSynchronizer object.
This class now extends
AtomicInteger instead of using
AtomicIntegerFieldUpdater to avoid the extra class cast and instanceof checks
in the latter when the base class does not match the actual implementation
class.
This implementation allows for providing arbitrary owner of the object for
reentrancy rather than requiring the current Thread as the owner. However,
the assignment to owner is done after CAS is done. This introduces a race
whereby reader can read a stale value of owner and mistakenly not reenter.
Even conceptually if two different threads come in concurrently with the same
owner then it is not possible to guard the underlying object against
concurrent access in any case. So users of this class must take care to not
try and reenter from two or more threads concurrently for the same lock
owner.
There is provision to atomically upgrade a lock from write-shared (EX_SH)
mode to exclusive (EX) mode. In addition users can get hold of a "weaker"
lock while holding a "stronger" lock e.g. shared (SH) lock when holding the
exclusive (EX) lock. Among other things this also allows for atomic downgrade
of locks. There is one exception: users can't acquire the write-shared
(EX_SH) lock while holding the exclusive (EX) lock. This restriction is due
to the way in which the lock counts are maintained and not an inherent design
limitation.