public final boolean isWriteLocked() { return getState() == WRITE_MASK; }
public final int numReaders() { return getState() & READ_MASK; }
/** * Returns a string identifying this synchronizer's state. The state, in * brackets, includes the String {@code "State="} followed by the current * value of {@link #getState}. * * @return a string identifying the state of this synchronizer */ @Override public String toString() { final StringBuilder sb = new StringBuilder(this.getClass().getSimpleName()) .append('@').append(Integer.toHexString(System.identityHashCode(this))) .append(','); return this.sync.toObjectString(sb).append("(lockState=0x") .append(Integer.toHexString(getState())).append(')').toString(); }
final int currentState = getState();
/** * Attempts to set the state to reflect a release in shared mode. * * <p> * This method is always invoked by the thread performing release. * * @throws IllegalMonitorStateException * if releasing would place this synchronizer in an illegal state. * This exception must be thrown in a consistent fashion for * synchronization to work correctly. */ private final void tryReleaseShared() { for (;;) { final int currentState = getState(); if (currentState <= 0) { // if system is going down then this can happen in some rare cases getCancelCriterion().checkCancelInProgress(null); throw new IllegalMonitorStateException( "read lock count is <= zero in release = " + currentState); } if (compareAndSet(currentState, currentState - 1)) { return; } } } }