--- jsr166/src/jsr166e/StampedLock.java 2012/10/12 14:09:30 1.1 +++ jsr166/src/jsr166e/StampedLock.java 2012/10/12 17:35:29 1.6 @@ -26,7 +26,7 @@ import java.util.concurrent.TimeUnit; * in method {@link #unlockWrite} to release the lock. Untimed and * timed versions of {@code tryWriteLock} are also provided. When * the lock is held in write mode, no read locks may be obtained, - * and all observer validations will fail. + * and all optimistic read validations will fail. * *
  • Reading. Method {@link #readLock} possibly blocks * waiting for non-exclusive access, returning a stamp that can be @@ -87,7 +87,7 @@ import java.util.concurrent.TimeUnit; * *
    {@code
      * class Point {
    - *   private volatile double x, y;
    + *   private double x, y;
      *   private final StampedLock sl = new StampedLock();
      *
      *   void move(double deltaX, double deltaY) { // an exclusively locked method
    @@ -177,7 +177,7 @@ public class StampedLock implements java
          * read-locked.  The read count is ignored when validating
          * "optimistic" seqlock-reader-style stamps.  Because we must use
          * a small finite number of bits (currently 7) for readers, a
    -     * supplementatry reader overflow word is used when then number of
    +     * supplementary reader overflow word is used when then number of
          * readers exceeds the count field. We do this by treating the max
          * reader count value (RBITS) as a spinlock protecting overflow
          * updates.
    @@ -235,10 +235,10 @@ public class StampedLock implements java
         private static final int NCPU = Runtime.getRuntime().availableProcessors();
     
         /** Maximum number of retries before blocking on acquisition */
    -    private static final int SPINS = NCPU > 1 ? 1 << 6 : 1;
    +    private static final int SPINS = (NCPU > 1) ? 1 << 6 : 1;
     
    -    /** Maximum number of retries before re-blocking on write lock */
    -    private static final int MAX_HEAD_SPINS = NCPU > 1 ? 1 << 12 : 1;
    +    /** Maximum number of retries before re-blocking */
    +    private static final int MAX_HEAD_SPINS = (NCPU > 1) ? 1 << 12 : 1;
     
         /** The period for yielding when waiting for overflow spinlock */
         private static final int OVERFLOW_YIELD_RATE = 7; // must be power 2 - 1
    @@ -303,7 +303,7 @@ public class StampedLock implements java
          * Exclusively acquires the lock, blocking if necessary
          * until available.
          *
    -     * @return a stamp that can be used to unlock or convert mode.
    +     * @return a stamp that can be used to unlock or convert mode
          */
         public long writeLock() {
             long s, next;
    @@ -329,16 +329,16 @@ public class StampedLock implements java
     
         /**
          * Exclusively acquires the lock if it is available within the
    -     * given time and the current thread has not been interrupted
    +     * given time and the current thread has not been interrupted.
          *
          * @return a stamp that can be used to unlock or convert mode,
    -     * or zero if the lock is not available.
    +     * or zero if the lock is not available
          * @throws InterruptedException if the current thread is interrupted
    -     * before acquiring the lock.
    +     * before acquiring the lock
          */
         public long tryWriteLock(long time, TimeUnit unit)
             throws InterruptedException {
    -        long nanos =  unit.toNanos(time);
    +        long nanos = unit.toNanos(time);
             if (!Thread.interrupted()) {
                 long s, next, deadline;
                 if (((s = state) & ABITS) == 0L &&
    @@ -358,9 +358,9 @@ public class StampedLock implements java
          * Exclusively acquires the lock, blocking if necessary
          * until available or the current thread is interrupted.
          *
    -     * @return a stamp that can be used to unlock or convert mode.
    +     * @return a stamp that can be used to unlock or convert mode
          * @throws InterruptedException if the current thread is interrupted
    -     * before acquiring the lock.
    +     * before acquiring the lock
          */
         public long writeLockInterruptibly() throws InterruptedException {
             if (!Thread.interrupted()) {
    @@ -378,7 +378,7 @@ public class StampedLock implements java
          * Non-exclusively acquires the lock, blocking if necessary
          * until available.
          *
    -     * @return a stamp that can be used to unlock or convert mode.
    +     * @return a stamp that can be used to unlock or convert mode
          */
         public long readLock() {
             for (;;) {
    @@ -401,7 +401,7 @@ public class StampedLock implements java
          * Non-exclusively acquires the lock if it is immediately available.
          *
          * @return a stamp that can be used to unlock or convert mode,
    -     * or zero if the lock is not available.
    +     * or zero if the lock is not available
          */
         public long tryReadLock() {
             for (;;) {
    @@ -419,12 +419,12 @@ public class StampedLock implements java
     
         /**
          * Non-exclusively acquires the lock if it is available within the
    -     * given time and the current thread has not been interrupted
    +     * given time and the current thread has not been interrupted.
          *
          * @return a stamp that can be used to unlock or convert mode,
    -     * or zero if the lock is not available.
    +     * or zero if the lock is not available
          * @throws InterruptedException if the current thread is interrupted
    -     * before acquiring the lock.
    +     * before acquiring the lock
          */
         public long tryReadLock(long time, TimeUnit unit)
             throws InterruptedException {
    @@ -457,9 +457,9 @@ public class StampedLock implements java
          * Non-exclusively acquires the lock, blocking if necessary
          * until available or the current thread is interrupted.
          *
    -     * @return a stamp that can be used to unlock or convert mode.
    +     * @return a stamp that can be used to unlock or convert mode
          * @throws InterruptedException if the current thread is interrupted
    -     * before acquiring the lock.
    +     * before acquiring the lock
          */
         public long readLockInterruptibly() throws InterruptedException {
             if (!Thread.interrupted()) {
    @@ -512,7 +512,7 @@ public class StampedLock implements java
          *
          * @param stamp a stamp returned by a write-lock operation
          * @throws IllegalMonitorStateException if the stamp does
    -     * not match the current state of this lock.
    +     * not match the current state of this lock
          */
         public void unlockWrite(long stamp) {
             if (state != stamp || (stamp & WBIT) == 0L)
    @@ -527,7 +527,7 @@ public class StampedLock implements java
          *
          * @param stamp a stamp returned by a read-lock operation
          * @throws IllegalMonitorStateException if the stamp does
    -     * not match the current state of this lock.
    +     * not match the current state of this lock
          */
         public void unlockRead(long stamp) {
             long s, m;
    @@ -557,7 +557,7 @@ public class StampedLock implements java
          *
          * @param stamp a stamp returned by a lock operation
          * @throws IllegalMonitorStateException if the stamp does
    -     * not match the current state of this lock.
    +     * not match the current state of this lock
          */
         public void unlock(long stamp) {
             long a = stamp & ABITS, m, s;
    @@ -673,7 +673,7 @@ public class StampedLock implements java
          */
         public long tryConvertToOptimisticRead(long stamp) {
             long a = stamp & ABITS, m, s, next;
    -        while (((s = U.getLongVolatile(this, STATE)) & 
    +        while (((s = U.getLongVolatile(this, STATE)) &
                     SBITS) == (stamp & SBITS)) {
                 if ((m = s & ABITS) == 0L) {
                     if (a != 0L)
    @@ -707,7 +707,7 @@ public class StampedLock implements java
          * stamp value. This method may be useful for recovery after
          * errors.
          *
    -     * @return true if the lock was held, else false.
    +     * @return true if the lock was held, else false
          */
         public boolean tryUnlockWrite() {
             long s;
    @@ -724,7 +724,7 @@ public class StampedLock implements java
          * requiring a stamp value. This method may be useful for recovery
          * after errors.
          *
    -     * @return true if the read lock was held, else false.
    +     * @return true if the read lock was held, else false
          */
         public boolean tryUnlockRead() {
             long s, m;
    @@ -773,6 +773,7 @@ public class StampedLock implements java
          * Tries to increment readerOverflow by first setting state
          * access bits value to RBITS, indicating hold of spinlock,
          * then updating, then releasing.
    +     *
          * @param stamp, assumed that (stamp & ABITS) >= RFULL
          * @return new stamp on success, else zero
          */
    @@ -784,7 +785,7 @@ public class StampedLock implements java
                     return s;
                 }
             }
    -        else if ((ThreadLocalRandom.current().nextInt() & 
    +        else if ((ThreadLocalRandom.current().nextInt() &
                       OVERFLOW_YIELD_RATE) == 0)
                 Thread.yield();
             return 0L;
    @@ -792,6 +793,7 @@ public class StampedLock implements java
     
         /**
          * Tries to decrement readerOverflow.
    +     *
          * @param stamp, assumed that (stamp & ABITS) >= RFULL
          * @return new stamp on success, else zero
          */
    @@ -809,7 +811,7 @@ public class StampedLock implements java
                      return next;
                 }
             }
    -        else if ((ThreadLocalRandom.current().nextInt() & 
    +        else if ((ThreadLocalRandom.current().nextInt() &
                       OVERFLOW_YIELD_RATE) == 0)
                 Thread.yield();
             return 0L;
    @@ -911,14 +913,14 @@ public class StampedLock implements java
     
         /**
          * Possibly spins trying to obtain write lock, then enqueues and
    -     * blocks while not head of write queue or cannot aquire lock,
    +     * blocks while not head of write queue or cannot acquire lock,
          * possibly spinning when at head; cancelling on timeout or
          * interrupt.
          *
          * @param interruptible true if should check interrupts and if so
          * return INTERRUPTED
          * @param deadline if nonzero, the System.nanoTime value to timeout
    -     * at (and return zero).
    +     * at (and return zero)
          */
         private long awaitWrite(boolean interruptible, long deadline) {
             WNode node = null;
    @@ -1019,10 +1021,10 @@ public class StampedLock implements java
                 }
             }
             writerPrefSignal();
    -        return (interrupted || Thread.interrupted())? INTERRUPTED : 0L;
    +        return (interrupted || Thread.interrupted()) ? INTERRUPTED : 0L;
         }
     
    -    /*
    +    /**
          * Waits for read lock or timeout or interrupt. The form of
          * awaitRead differs from awaitWrite mainly because it must
          * restart (with a new wait node) if the thread was unqueued and
    @@ -1101,8 +1103,7 @@ public class StampedLock implements java
         /**
          * If node non-null, forces cancel status and unsplices from queue
          * if possible, by traversing entire queue looking for cancelled
    -     * nodes, cleaning out all at head, but stopping upon first
    -     * encounter otherwise.
    +     * nodes.
          */
         private long cancelReader(RNode node, boolean interrupted) {
             Thread w;
    @@ -1117,7 +1118,7 @@ public class StampedLock implements java
                         }
                         else {
                             U.compareAndSwapObject(pred, RNEXT, p, q);
    -                        break;
    +                        p = pred.next;
                         }
                     }
                     else {
    @@ -1127,7 +1128,7 @@ public class StampedLock implements java
                 }
             }
             readerPrefSignal();
    -        return (interrupted || Thread.interrupted())? INTERRUPTED : 0L;
    +        return (interrupted || Thread.interrupted()) ? INTERRUPTED : 0L;
         }
     
         // Unsafe mechanics
    @@ -1201,4 +1202,3 @@ public class StampedLock implements java
         }
     
     }
    -