-
Enhancement
-
Resolution: Fixed
-
P3
-
None
-
None
StampedLock has methods to upgrade an optimistic or read lock, but there's no way in a finally block to surely roll back any lock acquisition without extra bookkeeping with local flags or unnecessary volatile operations on the lock.
We should fix this so that users can do
* long stamp = sl.tryOptimisticRead();
* try {
* ...
* stamp = sl.tryConvertToWriteLock(stamp);
* ...
* } finally {
* if (StampedLock.isWriteLockStamp(stamp))
* sl.unlockWrite(stamp);
* }
Implementation is easy:
/**
* Tells whether a stamp represents holding a lock exclusively.
* This method may be useful in conjunction with
* {@link #tryConvertToWriteLock}, for example: <pre> {@code
* long stamp = sl.tryOptimisticRead();
* try {
* ...
* stamp = sl.tryConvertToWriteLock(stamp);
* ...
* } finally {
* if (StampedLock.isWriteLockStamp(stamp))
* sl.unlockWrite(stamp);
* }}</pre>
*
* @param stamp a stamp returned by a previous StampedLock operation
* @return {@code true} if the stamp was returned by a successful
* write-lock operation
*/
public static boolean isWriteLockStamp(long stamp) {
return (stamp & ABITS) == WBIT;
}
/**
* Tells whether a stamp represents holding a lock non-exclusively.
* This method may be useful in conjunction with
* {@link #tryConvertToReadLock}, for example: <pre> {@code
* long stamp = sl.tryOptimisticRead();
* try {
* ...
* stamp = sl.tryConvertToReadLock(stamp);
* ...
* } finally {
* if (StampedLock.isReadLockStamp(stamp))
* sl.unlockRead(stamp);
* }}</pre>
*
* @param stamp a stamp returned by a previous StampedLock operation
* @return {@code true} if the stamp was returned by a successful
* read-lock operation
*/
public static boolean isReadLockStamp(long stamp) {
return (stamp & RBITS) != 0L;
}
/**
* Tells whether a stamp represents holding a lock.
* This method may be useful in conjunction with
* {@link #tryConvertToReadLock} and {@link #tryConvertToWriteLock},
* for example: <pre> {@code
* long stamp = sl.tryOptimisticRead();
* try {
* ...
* stamp = sl.tryConvertToReadLock(stamp);
* ...
* stamp = sl.tryConvertToWriteLock(stamp);
* ...
* } finally {
* if (StampedLock.isLockStamp(stamp))
* sl.unlock(stamp);
* }}</pre>
*
* @param stamp a stamp returned by a previous StampedLock operation
* @return {@code true} if the stamp was returned by a successful
* read-lock or write-lock operation
*/
public static boolean isLockStamp(long stamp) {
return (stamp & ABITS) != 0L;
}
/**
* Tells whether a stamp represents a successful optimistic read.
*
* @param stamp a stamp returned by a previous StampedLock operation
* @return {@code true} if the stamp was returned by a successful
* optimistic read operation, that is, a non-zero return from
* {@link #tryOptimisticRead()} or
* {@link #tryConvertToOptimisticRead(long)}
*/
public static boolean isOptimisticReadStamp(long stamp) {
return (stamp & ABITS) == 0L && stamp != 0L;
}
We should fix this so that users can do
* long stamp = sl.tryOptimisticRead();
* try {
* ...
* stamp = sl.tryConvertToWriteLock(stamp);
* ...
* } finally {
* if (StampedLock.isWriteLockStamp(stamp))
* sl.unlockWrite(stamp);
* }
Implementation is easy:
/**
* Tells whether a stamp represents holding a lock exclusively.
* This method may be useful in conjunction with
* {@link #tryConvertToWriteLock}, for example: <pre> {@code
* long stamp = sl.tryOptimisticRead();
* try {
* ...
* stamp = sl.tryConvertToWriteLock(stamp);
* ...
* } finally {
* if (StampedLock.isWriteLockStamp(stamp))
* sl.unlockWrite(stamp);
* }}</pre>
*
* @param stamp a stamp returned by a previous StampedLock operation
* @return {@code true} if the stamp was returned by a successful
* write-lock operation
*/
public static boolean isWriteLockStamp(long stamp) {
return (stamp & ABITS) == WBIT;
}
/**
* Tells whether a stamp represents holding a lock non-exclusively.
* This method may be useful in conjunction with
* {@link #tryConvertToReadLock}, for example: <pre> {@code
* long stamp = sl.tryOptimisticRead();
* try {
* ...
* stamp = sl.tryConvertToReadLock(stamp);
* ...
* } finally {
* if (StampedLock.isReadLockStamp(stamp))
* sl.unlockRead(stamp);
* }}</pre>
*
* @param stamp a stamp returned by a previous StampedLock operation
* @return {@code true} if the stamp was returned by a successful
* read-lock operation
*/
public static boolean isReadLockStamp(long stamp) {
return (stamp & RBITS) != 0L;
}
/**
* Tells whether a stamp represents holding a lock.
* This method may be useful in conjunction with
* {@link #tryConvertToReadLock} and {@link #tryConvertToWriteLock},
* for example: <pre> {@code
* long stamp = sl.tryOptimisticRead();
* try {
* ...
* stamp = sl.tryConvertToReadLock(stamp);
* ...
* stamp = sl.tryConvertToWriteLock(stamp);
* ...
* } finally {
* if (StampedLock.isLockStamp(stamp))
* sl.unlock(stamp);
* }}</pre>
*
* @param stamp a stamp returned by a previous StampedLock operation
* @return {@code true} if the stamp was returned by a successful
* read-lock or write-lock operation
*/
public static boolean isLockStamp(long stamp) {
return (stamp & ABITS) != 0L;
}
/**
* Tells whether a stamp represents a successful optimistic read.
*
* @param stamp a stamp returned by a previous StampedLock operation
* @return {@code true} if the stamp was returned by a successful
* optimistic read operation, that is, a non-zero return from
* {@link #tryOptimisticRead()} or
* {@link #tryConvertToOptimisticRead(long)}
*/
public static boolean isOptimisticReadStamp(long stamp) {
return (stamp & ABITS) == 0L && stamp != 0L;
}
- csr for
-
JDK-8187942 Add StampedLock stamp inspection methods
-
- Closed
-