ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/StampedLock.java
(Generate patch)

Comparing jsr166/src/jsr166e/StampedLock.java (file contents):
Revision 1.15 by jsr166, Sun Oct 14 16:42:07 2012 UTC vs.
Revision 1.35 by jsr166, Mon Jan 28 22:22:08 2013 UTC

# Line 8 | Line 8 | package jsr166e;
8  
9   import java.util.concurrent.ThreadLocalRandom;
10   import java.util.concurrent.TimeUnit;
11 + import java.util.concurrent.locks.Lock;
12 + import java.util.concurrent.locks.Condition;
13 + import java.util.concurrent.locks.ReadWriteLock;
14 + import java.util.concurrent.locks.LockSupport;
15  
16   /**
17   * A capability-based lock with three modes for controlling read/write
# Line 36 | Line 40 | import java.util.concurrent.TimeUnit;
40   *  <li><b>Optimistic Reading.</b> Method {@link #tryOptimisticRead}
41   *   returns a non-zero stamp only if the lock is not currently held
42   *   in write mode. Method {@link #validate} returns true if the lock
43 < *   has not since been acquired in write mode. This mode can be
44 < *   thought of as an extremely weak version of a read-lock, that can
45 < *   be broken by a writer at any time.  The use of optimistic mode
46 < *   for short read-only code segments often reduces contention and
47 < *   improves throughput.  However, its use is inherently fragile.
48 < *   Optimistic read sections should only read fields and hold them in
49 < *   local variables for later use after validation. Fields read while
50 < *   in optimistic mode may be wildly inconsistent, so usage applies
51 < *   only when you are familiar enough with data representations to
52 < *   check consistency and/or repeatedly invoke method {@code
53 < *   validate()}.  For example, such steps are typically required when
54 < *   first reading an object or array reference, and then accessing
55 < *   one of its fields, elements or methods. </li>
43 > *   has not been acquired in write mode since obtaining a given
44 > *   stamp.  This mode can be thought of as an extremely weak version
45 > *   of a read-lock, that can be broken by a writer at any time.  The
46 > *   use of optimistic mode for short read-only code segments often
47 > *   reduces contention and improves throughput.  However, its use is
48 > *   inherently fragile.  Optimistic read sections should only read
49 > *   fields and hold them in local variables for later use after
50 > *   validation. Fields read while in optimistic mode may be wildly
51 > *   inconsistent, so usage applies only when you are familiar enough
52 > *   with data representations to check consistency and/or repeatedly
53 > *   invoke method {@code validate()}.  For example, such steps are
54 > *   typically required when first reading an object or array
55 > *   reference, and then accessing one of its fields, elements or
56 > *   methods. </li>
57   *
58   * </ul>
59   *
# Line 61 | Line 66 | import java.util.concurrent.TimeUnit;
66   * help reduce some of the code bloat that otherwise occurs in
67   * retry-based designs.
68   *
69 < * <p>StampedLocks are designed for use in a different (and generally
70 < * narrower) range of contexts than most other locks: They are not
71 < * reentrant, so locked bodies should not call other unknown methods
72 < * that may try to re-acquire locks (although you may pass a stamp to
73 < * other methods that can use or convert it). Unvalidated optimistic
74 < * read sections should further not call methods that are not known to
69 > * <p>StampedLocks are designed for use as internal utilities in the
70 > * development of thread-safe components. Their use relies on
71 > * knowledge of the internal properties of the data, objects, and
72 > * methods they are protecting.  They are not reentrant, so locked
73 > * bodies should not call other unknown methods that may try to
74 > * re-acquire locks (although you may pass a stamp to other methods
75 > * that can use or convert it).  The use of read lock modes relies on
76 > * the associated code sections being side-effect-free.  Unvalidated
77 > * optimistic read sections cannot call methods that are not known to
78   * tolerate potential inconsistencies.  Stamps use finite
79   * representations, and are not cryptographically secure (i.e., a
80   * valid stamp may be guessable). Stamp values may recycle after (no
# Line 77 | Line 85 | import java.util.concurrent.TimeUnit;
85   * locking.
86   *
87   * <p>The scheduling policy of StampedLock does not consistently
88 < * prefer readers over writers or vice versa.  A zero return from any
89 < * "try" method for acquiring or converting locks does not carry any
90 < * information about the state of the lock; a subsequent invocation
91 < * may succeed.
88 > * prefer readers over writers or vice versa.  All "try" methods are
89 > * best-effort and do not necessarily conform to any scheduling or
90 > * fairness policy. A zero return from any "try" method for acquiring
91 > * or converting locks does not carry any information about the state
92 > * of the lock; a subsequent invocation may succeed.
93 > *
94 > * <p>Because it supports coordinated usage across multiple lock
95 > * modes, this class does not directly implement the {@link Lock} or
96 > * {@link ReadWriteLock} interfaces. However, a StampedLock may be
97 > * viewed {@link #asReadLock()}, {@link #asWriteLock()}, or {@link
98 > * #asReadWriteLock()} in applications requiring only the associated
99 > * set of functionality.
100   *
101   * <p><b>Sample Usage.</b> The following illustrates some usage idioms
102   * in a class that maintains simple two-dimensional points. The sample
# Line 122 | Line 138 | import java.util.concurrent.TimeUnit;
138   *   }
139   *
140   *   double distanceFromOriginV2() { // combines code paths
141 + *     double currentX = 0.0, currentY = 0.0;
142   *     for (long stamp = sl.tryOptimisticRead(); ; stamp = sl.readLock()) {
126 *       double currentX, currentY;
143   *       try {
144   *         currentX = x;
145   *         currentY = y;
146   *       } finally {
147   *         if (sl.tryConvertToOptimisticRead(stamp) != 0L) // unlock or validate
148 < *           return Math.sqrt(currentX * currentX + currentY * currentY);
148 > *           break;
149   *       }
150   *     }
151 + *     return Math.sqrt(currentX * currentX + currentY * currentY);
152   *   }
153   *
154   *   void moveIfAtOrigin(double newX, double newY) { // upgrade
# Line 139 | Line 156 | import java.util.concurrent.TimeUnit;
156   *     long stamp = sl.readLock();
157   *     try {
158   *       while (x == 0.0 && y == 0.0) {
159 < *         long ws = tryConvertToWriteLock(stamp);
159 > *         long ws = sl.tryConvertToWriteLock(stamp);
160   *         if (ws != 0L) {
161   *           stamp = ws;
162   *           x = newX;
# Line 152 | Line 169 | import java.util.concurrent.TimeUnit;
169   *         }
170   *       }
171   *     } finally {
172 < *        sl.unlock(stamp);
172 > *       sl.unlock(stamp);
173   *     }
174   *   }
175   * }}</pre>
# Line 169 | Line 186 | public class StampedLock implements java
186       * http://www.lameter.com/gelato2005.pdf
187       * and elsewhere; see
188       * Boehm's http://www.hpl.hp.com/techreports/2012/HPL-2012-68.html)
189 <     * Ordered RW locks (see Shirako et al
189 >     * and Ordered RW locks (see Shirako et al
190       * http://dl.acm.org/citation.cfm?id=2312015)
174     * and Phase-Fair locks (see Brandenburg & Anderson, especially
175     * http://www.cs.unc.edu/~bbb/diss/).
191       *
192       * Conceptually, the primary state of the lock includes a sequence
193       * number that is odd when write-locked and even otherwise.
# Line 185 | Line 200 | public class StampedLock implements java
200       * reader count value (RBITS) as a spinlock protecting overflow
201       * updates.
202       *
203 <     * Waiting readers and writers use different queues. The writer
204 <     * queue is a modified form of CLH lock.  (For discussion of CLH,
205 <     * see the internal documentation of AbstractQueuedSynchronizer.)
206 <     * The reader "queue" is a form of Treiber stack, that supports
207 <     * simpler/faster operations because order within a queue doesn't
208 <     * matter and all are signalled at once.  However the sequence of
209 <     * threads within the queue vs the current stamp does matter (see
210 <     * Shirako et al) so each carries its incoming stamp value.
211 <     * Waiting writers never need to track sequence values, so they
212 <     * don't.
213 <     *
214 <     * These queue mechanics hardwire the scheduling policy.  Ignoring
215 <     * trylocks, cancellation, and spinning, they implement Phase-Fair
216 <     * preferences:
217 <     *   1. Unlocked writers prefer to signal waiting readers
218 <     *   2. Fully unlocked readers prefer to signal waiting writers
204 <     *   3. When read-locked and a waiting writer exists, the writer
205 <     *      is preferred to incoming readers
203 >     * Waiters use a modified form of CLH lock used in
204 >     * AbstractQueuedSynchronizer (see its internal documentation for
205 >     * a fuller account), where each node is tagged (field mode) as
206 >     * either a reader or writer. Sets of waiting readers are grouped
207 >     * (linked) under a common node (field cowait) so act as a single
208 >     * node with respect to most CLH mechanics.  By virtue of the
209 >     * queue structure, wait nodes need not actually carry sequence
210 >     * numbers; we know each is greater than its predecessor.  This
211 >     * simplifies the scheduling policy to a mainly-FIFO scheme that
212 >     * incorporates elements of Phase-Fair locks (see Brandenburg &
213 >     * Anderson, especially http://www.cs.unc.edu/~bbb/diss/).  In
214 >     * particular, we use the phase-fair anti-barging rule: If an
215 >     * incoming reader arrives while read lock is held but there is a
216 >     * queued writer, this incoming reader is queued.  (This rule is
217 >     * responsible for some of the complexity of method acquireRead,
218 >     * but without it, the lock becomes highly unfair.)
219       *
220       * These rules apply to threads actually queued. All tryLock forms
221       * opportunistically try to acquire locks regardless of preference
222 <     * rules, and so may "barge" their way in.  Additionally, initial
223 <     * phases of the await* methods (invoked from readLock() and
224 <     * writeLock()) use controlled spins that have similar effect.
225 <     * Phase-fair preferences may also be broken on cancellations due
226 <     * to timeouts and interrupts.  Rule #3 (incoming readers when a
227 <     * waiting writer) is approximated with varying precision in
228 <     * different contexts -- some checks do not account for
229 <     * in-progress spins/signals, and others do not account for
230 <     * cancellations.
231 <     *
232 <     * Controlled, randomized spinning is used in the two await
233 <     * methods to reduce (increasingly expensive) context switching
234 <     * while also avoiding sustained memory thrashing among many
235 <     * threads.  Both await methods use a similar spin strategy: If
236 <     * the associated queue appears to be empty, then the thread
237 <     * spin-waits up to SPINS times (where each iteration decreases
225 <     * spin count with 50% probablility) before enqueing, and then, if
226 <     * it is the first thread to be enqueued, spins again up to SPINS
227 <     * times before blocking. If, upon wakening it fails to obtain
228 <     * lock, and is still (or becomes) the first waiting thread (which
229 <     * indicates that some other thread barged and obtained lock), it
230 <     * escalates spins (up to MAX_HEAD_SPINS) to reduce the likelihood
231 <     * of continually losing to barging threads.
222 >     * rules, and so may "barge" their way in.  Randomized spinning is
223 >     * used in the acquire methods to reduce (increasingly expensive)
224 >     * context switching while also avoiding sustained memory
225 >     * thrashing among many threads.  We limit spins to the head of
226 >     * queue. A thread spin-waits up to SPINS times (where each
227 >     * iteration decreases spin count with 50% probability) before
228 >     * blocking. If, upon wakening it fails to obtain lock, and is
229 >     * still (or becomes) the first waiting thread (which indicates
230 >     * that some other thread barged and obtained lock), it escalates
231 >     * spins (up to MAX_HEAD_SPINS) to reduce the likelihood of
232 >     * continually losing to barging threads.
233 >     *
234 >     * Nearly all of these mechanics are carried out in methods
235 >     * acquireWrite and acquireRead, that, as typical of such code,
236 >     * sprawl out because actions and retries rely on consistent sets
237 >     * of locally cached reads.
238       *
239       * As noted in Boehm's paper (above), sequence validation (mainly
240       * method validate()) requires stricter ordering rules than apply
# Line 248 | Line 254 | public class StampedLock implements java
254       * be subject to future improvements.
255       */
256  
257 +    private static final long serialVersionUID = -6001602636862214147L;
258 +
259      /** Number of processors, for spin control */
260      private static final int NCPU = Runtime.getRuntime().availableProcessors();
261  
262      /** Maximum number of retries before blocking on acquisition */
263 <    private static final int SPINS = (NCPU > 1) ? 1 << 6 : 1;
263 >    private static final int SPINS = (NCPU > 1) ? 1 << 6 : 0;
264  
265      /** Maximum number of retries before re-blocking */
266 <    private static final int MAX_HEAD_SPINS = (NCPU > 1) ? 1 << 12 : 1;
266 >    private static final int MAX_HEAD_SPINS = (NCPU > 1) ? 1 << 12 : 0;
267  
268      /** The period for yielding when waiting for overflow spinlock */
269      private static final int OVERFLOW_YIELD_RATE = 7; // must be power 2 - 1
# Line 274 | Line 282 | public class StampedLock implements java
282      // Initial value for lock state; avoid failure value zero
283      private static final long ORIGIN = WBIT << 1;
284  
285 <    // Special value from cancelled await methods so caller can throw IE
285 >    // Special value from cancelled acquire methods so caller can throw IE
286      private static final long INTERRUPTED = 1L;
287  
288 <    // Values for writer status; order matters
288 >    // Values for node status; order matters
289      private static final int WAITING   = -1;
290      private static final int CANCELLED =  1;
291  
292 <    /** Wait nodes for readers */
293 <    static final class RNode {
294 <        final long seq;         // stamp value upon enqueue
287 <        volatile Thread waiter; // null if no longer waiting
288 <        volatile RNode next;
289 <        RNode(long s, Thread w) { seq = s; waiter = w; }
290 <    }
292 >    // Modes for nodes (int not boolean to allow arithmetic)
293 >    private static final int RMODE = 0;
294 >    private static final int WMODE = 1;
295  
296 <    /** Wait nodes for writers */
296 >    /** Wait nodes */
297      static final class WNode {
294        volatile int status;   // 0, WAITING, or CANCELLED
298          volatile WNode prev;
299          volatile WNode next;
300 <        volatile Thread thread;
301 <        WNode(Thread t, WNode p) { thread = t; prev = p; }
300 >        volatile WNode cowait;    // list of linked readers
301 >        volatile Thread thread;   // non-null while possibly parked
302 >        volatile int status;      // 0, WAITING, or CANCELLED
303 >        final int mode;           // RMODE or WMODE
304 >        WNode(int m, WNode p) { mode = m; prev = p; }
305      }
306  
307 <    /** Head of writer CLH queue */
307 >    /** Head of CLH queue */
308      private transient volatile WNode whead;
309 <    /** Tail (last) of writer CLH queue */
309 >    /** Tail (last) of CLH queue */
310      private transient volatile WNode wtail;
311 <    /** Head of read queue  */
312 <    private transient volatile RNode rhead;
313 <    /** The state of the lock -- high bits hold sequence, low bits read count */
311 >
312 >    // views
313 >    transient ReadLockView readLockView;
314 >    transient WriteLockView writeLockView;
315 >    transient ReadWriteLockView readWriteLockView;
316 >
317 >    /** Lock sequence/state */
318      private transient volatile long state;
319      /** extra reader count when state read count saturated */
320      private transient int readerOverflow;
321  
322      /**
323 <     * Creates a new lock initially in unlocked state.
323 >     * Creates a new lock, initially in unlocked state.
324       */
325      public StampedLock() {
326          state = ORIGIN;
# Line 323 | Line 333 | public class StampedLock implements java
333       * @return a stamp that can be used to unlock or convert mode
334       */
335      public long writeLock() {
336 <        long s, next;
337 <        if (((s = state) & ABITS) == 0L &&
338 <            U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
339 <            return next;
330 <        return awaitWrite(false, 0L);
336 >        long s, next;  // bypass acquireWrite in fully unlocked case only
337 >        return ((((s = state) & ABITS) == 0L &&
338 >                 U.compareAndSwapLong(this, STATE, s, next = s + WBIT)) ?
339 >                next : acquireWrite(false, 0L));
340      }
341  
342      /**
# Line 338 | Line 347 | public class StampedLock implements java
347       */
348      public long tryWriteLock() {
349          long s, next;
350 <        if (((s = state) & ABITS) == 0L &&
351 <            U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
352 <            return next;
344 <        return 0L;
350 >        return ((((s = state) & ABITS) == 0L &&
351 >                 U.compareAndSwapLong(this, STATE, s, next = s + WBIT)) ?
352 >                next : 0L);
353      }
354  
355      /**
356       * Exclusively acquires the lock if it is available within the
357       * given time and the current thread has not been interrupted.
358 +     * Behavior under timeout and interruption matches that specified
359 +     * for method {@link Lock#tryLock(long,TimeUnit)}.
360       *
361       * @return a stamp that can be used to unlock or convert mode,
362       * or zero if the lock is not available
# Line 357 | Line 367 | public class StampedLock implements java
367          throws InterruptedException {
368          long nanos = unit.toNanos(time);
369          if (!Thread.interrupted()) {
370 <            long s, next, deadline;
371 <            if (((s = state) & ABITS) == 0L &&
362 <                U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
370 >            long next, deadline;
371 >            if ((next = tryWriteLock()) != 0L)
372                  return next;
373              if (nanos <= 0L)
374                  return 0L;
375              if ((deadline = System.nanoTime() + nanos) == 0L)
376                  deadline = 1L;
377 <            if ((next = awaitWrite(true, deadline)) != INTERRUPTED)
377 >            if ((next = acquireWrite(true, deadline)) != INTERRUPTED)
378                  return next;
379          }
380          throw new InterruptedException();
# Line 374 | Line 383 | public class StampedLock implements java
383      /**
384       * Exclusively acquires the lock, blocking if necessary
385       * until available or the current thread is interrupted.
386 +     * Behavior under interruption matches that specified
387 +     * for method {@link Lock#lockInterruptibly()}.
388       *
389       * @return a stamp that can be used to unlock or convert mode
390       * @throws InterruptedException if the current thread is interrupted
391       * before acquiring the lock
392       */
393      public long writeLockInterruptibly() throws InterruptedException {
394 <        if (!Thread.interrupted()) {
395 <            long s, next;
396 <            if (((s = state) & ABITS) == 0L &&
397 <                U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
387 <                return next;
388 <            if ((next = awaitWrite(true, 0L)) != INTERRUPTED)
389 <                return next;
390 <        }
394 >        long next;
395 >        if (!Thread.interrupted() &&
396 >            (next = acquireWrite(true, 0L)) != INTERRUPTED)
397 >            return next;
398          throw new InterruptedException();
399      }
400  
# Line 398 | Line 405 | public class StampedLock implements java
405       * @return a stamp that can be used to unlock or convert mode
406       */
407      public long readLock() {
408 <        for (;;) {
409 <            long s, m, next;
410 <            if ((m = (s = state) & ABITS) == 0L ||
411 <                (m < WBIT && whead == wtail)) {
405 <                if (m < RFULL) {
406 <                    if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT))
407 <                        return next;
408 <                }
409 <                else if ((next = tryIncReaderOverflow(s)) != 0L)
410 <                    return next;
411 <            }
412 <            else
413 <                return awaitRead(s, false, 0L);
414 <        }
408 >        long s, next;  // bypass acquireRead on fully unlocked case only
409 >        return ((((s = state) & ABITS) == 0L &&
410 >                 U.compareAndSwapLong(this, STATE, s, next = s + RUNIT)) ?
411 >                next : acquireRead(false, 0L));
412      }
413  
414      /**
# Line 437 | Line 434 | public class StampedLock implements java
434      /**
435       * Non-exclusively acquires the lock if it is available within the
436       * given time and the current thread has not been interrupted.
437 +     * Behavior under timeout and interruption matches that specified
438 +     * for method {@link Lock#tryLock(long,TimeUnit)}.
439       *
440       * @return a stamp that can be used to unlock or convert mode,
441       * or zero if the lock is not available
# Line 445 | Line 444 | public class StampedLock implements java
444       */
445      public long tryReadLock(long time, TimeUnit unit)
446          throws InterruptedException {
447 +        long s, m, next, deadline;
448          long nanos = unit.toNanos(time);
449          if (!Thread.interrupted()) {
450 <            for (;;) {
451 <                long s, m, next, deadline;
452 <                if ((m = (s = state) & ABITS) == WBIT ||
453 <                    (m != 0L && whead != wtail)) {
454 <                    if (nanos <= 0L)
455 <                        return 0L;
456 <                    if ((deadline = System.nanoTime() + nanos) == 0L)
457 <                        deadline = 1L;
458 <                    if ((next = awaitRead(s, true, deadline)) != INTERRUPTED)
459 <                        return next;
460 <                    break;
461 <                }
462 <                else if (m < RFULL) {
450 >            if ((m = (s = state) & ABITS) != WBIT) {
451 >                if (m < RFULL) {
452                      if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT))
453                          return next;
454                  }
455                  else if ((next = tryIncReaderOverflow(s)) != 0L)
456                      return next;
457              }
458 +            if (nanos <= 0L)
459 +                return 0L;
460 +            if ((deadline = System.nanoTime() + nanos) == 0L)
461 +                deadline = 1L;
462 +            if ((next = acquireRead(true, deadline)) != INTERRUPTED)
463 +                return next;
464          }
465          throw new InterruptedException();
466      }
# Line 473 | Line 468 | public class StampedLock implements java
468      /**
469       * Non-exclusively acquires the lock, blocking if necessary
470       * until available or the current thread is interrupted.
471 +     * Behavior under interruption matches that specified
472 +     * for method {@link Lock#lockInterruptibly()}.
473       *
474       * @return a stamp that can be used to unlock or convert mode
475       * @throws InterruptedException if the current thread is interrupted
476       * before acquiring the lock
477       */
478      public long readLockInterruptibly() throws InterruptedException {
479 <        if (!Thread.interrupted()) {
480 <            for (;;) {
481 <                long s, next, m;
482 <                if ((m = (s = state) & ABITS) == WBIT ||
486 <                    (m != 0L && whead != wtail)) {
487 <                    if ((next = awaitRead(s, true, 0L)) != INTERRUPTED)
488 <                        return next;
489 <                    break;
490 <                }
491 <                else if (m < RFULL) {
492 <                    if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT))
493 <                        return next;
494 <                }
495 <                else if ((next = tryIncReaderOverflow(s)) != 0L)
496 <                    return next;
497 <            }
498 <        }
479 >        long next;
480 >        if (!Thread.interrupted() &&
481 >            (next = acquireRead(true, 0L)) != INTERRUPTED)
482 >            return next;
483          throw new InterruptedException();
484      }
485  
# Line 511 | Line 495 | public class StampedLock implements java
495      }
496  
497      /**
498 <     * Returns true if the lock has not been exclusively held since
499 <     * issuance of the given stamp. Always returns false if the stamp
500 <     * is zero. Always returns true if the stamp represents a
501 <     * currently held lock.
498 >     * Returns true if the lock has not been exclusively acquired
499 >     * since issuance of the given stamp. Always returns false if the
500 >     * stamp is zero. Always returns true if the stamp represents a
501 >     * currently held lock. Invoking this method with a value not
502 >     * obtained from {@link #tryOptimisticRead} or a locking method
503 >     * for this lock has no defined effect or result.
504       *
505 <     * @return true if the lock has not been exclusively held since
506 <     * issuance of the given stamp; else false
505 >     * @return true if the lock has not been exclusively acquired
506 >     * since issuance of the given stamp; else false
507       */
508      public boolean validate(long stamp) {
509 +        // See above about current use of getLongVolatile here
510          return (stamp & SBITS) == (U.getLongVolatile(this, STATE) & SBITS);
511      }
512  
# Line 532 | Line 519 | public class StampedLock implements java
519       * not match the current state of this lock
520       */
521      public void unlockWrite(long stamp) {
522 +        WNode h;
523          if (state != stamp || (stamp & WBIT) == 0L)
524              throw new IllegalMonitorStateException();
525          state = (stamp += WBIT) == 0L ? ORIGIN : stamp;
526 <        readerPrefSignal();
526 >        if ((h = whead) != null && h.status != 0)
527 >            release(h);
528      }
529  
530      /**
# Line 547 | Line 536 | public class StampedLock implements java
536       * not match the current state of this lock
537       */
538      public void unlockRead(long stamp) {
539 <        long s, m;
540 <        if ((stamp & RBITS) != 0L) {
541 <            while (((s = state) & SBITS) == (stamp & SBITS)) {
542 <                if ((m = s & ABITS) == 0L)
539 >        long s, m; WNode h;
540 >        for (;;) {
541 >            if (((s = state) & SBITS) != (stamp & SBITS) ||
542 >                (stamp & ABITS) == 0L || (m = s & ABITS) == 0L || m == WBIT)
543 >                throw new IllegalMonitorStateException();
544 >            if (m < RFULL) {
545 >                if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) {
546 >                    if (m == RUNIT && (h = whead) != null && h.status != 0)
547 >                        release(h);
548                      break;
555                else if (m < RFULL) {
556                    if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) {
557                        if (m == RUNIT)
558                            writerPrefSignal();
559                        return;
560                    }
549                  }
562                else if (m >= WBIT)
563                    break;
564                else if (tryDecReaderOverflow(s) != 0L)
565                    return;
550              }
551 +            else if (tryDecReaderOverflow(s) != 0L)
552 +                break;
553          }
568        throw new IllegalMonitorStateException();
554      }
555  
556      /**
# Line 577 | Line 562 | public class StampedLock implements java
562       * not match the current state of this lock
563       */
564      public void unlock(long stamp) {
565 <        long a = stamp & ABITS, m, s;
565 >        long a = stamp & ABITS, m, s; WNode h;
566          while (((s = state) & SBITS) == (stamp & SBITS)) {
567              if ((m = s & ABITS) == 0L)
568                  break;
# Line 585 | Line 570 | public class StampedLock implements java
570                  if (a != m)
571                      break;
572                  state = (s += WBIT) == 0L ? ORIGIN : s;
573 <                readerPrefSignal();
573 >                if ((h = whead) != null && h.status != 0)
574 >                    release(h);
575                  return;
576              }
577              else if (a == 0L || a >= WBIT)
578                  break;
579              else if (m < RFULL) {
580                  if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) {
581 <                    if (m == RUNIT)
582 <                        writerPrefSignal();
581 >                    if (m == RUNIT && (h = whead) != null && h.status != 0)
582 >                        release(h);
583                      return;
584                  }
585              }
# Line 604 | Line 590 | public class StampedLock implements java
590      }
591  
592      /**
593 <     * If the lock state matches the given stamp then performs one of
593 >     * If the lock state matches the given stamp, performs one of
594       * the following actions. If the stamp represents holding a write
595       * lock, returns it.  Or, if a read lock, if the write lock is
596       * available, releases the read lock and returns a write stamp.
# Line 629 | Line 615 | public class StampedLock implements java
615                      break;
616                  return stamp;
617              }
618 <            else if (m == RUNIT && a != 0L && a < WBIT) {
618 >            else if (m == RUNIT && a != 0L) {
619                  if (U.compareAndSwapLong(this, STATE, s,
620                                           next = s - RUNIT + WBIT))
621                      return next;
# Line 641 | Line 627 | public class StampedLock implements java
627      }
628  
629      /**
630 <     * If the lock state matches the given stamp then performs one of
630 >     * If the lock state matches the given stamp, performs one of
631       * the following actions. If the stamp represents holding a write
632       * lock, releases it and obtains a read lock.  Or, if a read lock,
633       * returns it. Or, if an optimistic read, acquires a read lock and
# Line 652 | Line 638 | public class StampedLock implements java
638       * @return a valid read stamp, or zero on failure
639       */
640      public long tryConvertToReadLock(long stamp) {
641 <        long a = stamp & ABITS, m, s, next;
641 >        long a = stamp & ABITS, m, s, next; WNode h;
642          while (((s = state) & SBITS) == (stamp & SBITS)) {
643              if ((m = s & ABITS) == 0L) {
644                  if (a != 0L)
# Line 667 | Line 653 | public class StampedLock implements java
653              else if (m == WBIT) {
654                  if (a != m)
655                      break;
656 <                next = state = s + (WBIT + RUNIT);
657 <                readerPrefSignal();
656 >                state = next = s + (WBIT + RUNIT);
657 >                if ((h = whead) != null && h.status != 0)
658 >                    release(h);
659                  return next;
660              }
661              else if (a != 0L && a < WBIT)
# Line 690 | Line 677 | public class StampedLock implements java
677       * @return a valid optimistic read stamp, or zero on failure
678       */
679      public long tryConvertToOptimisticRead(long stamp) {
680 <        long a = stamp & ABITS, m, s, next;
681 <        while (((s = U.getLongVolatile(this, STATE)) &
682 <                SBITS) == (stamp & SBITS)) {
680 >        long a = stamp & ABITS, m, s, next; WNode h;
681 >        for (;;) {
682 >            s = U.getLongVolatile(this, STATE); // see above
683 >            if ((s & SBITS) != (stamp & SBITS))
684 >                break;
685              if ((m = s & ABITS) == 0L) {
686                  if (a != 0L)
687                      break;
# Line 701 | Line 690 | public class StampedLock implements java
690              else if (m == WBIT) {
691                  if (a != m)
692                      break;
693 <                next = state = (s += WBIT) == 0L ? ORIGIN : s;
694 <                readerPrefSignal();
693 >                state = next = (s += WBIT) == 0L ? ORIGIN : s;
694 >                if ((h = whead) != null && h.status != 0)
695 >                    release(h);
696                  return next;
697              }
698              else if (a == 0L || a >= WBIT)
699                  break;
700              else if (m < RFULL) {
701                  if (U.compareAndSwapLong(this, STATE, s, next = s - RUNIT)) {
702 <                    if (m == RUNIT)
703 <                        writerPrefSignal();
702 >                    if (m == RUNIT && (h = whead) != null && h.status != 0)
703 >                        release(h);
704                      return next & SBITS;
705                  }
706              }
# Line 728 | Line 718 | public class StampedLock implements java
718       * @return true if the lock was held, else false
719       */
720      public boolean tryUnlockWrite() {
721 <        long s;
721 >        long s; WNode h;
722          if (((s = state) & WBIT) != 0L) {
723              state = (s += WBIT) == 0L ? ORIGIN : s;
724 <            readerPrefSignal();
724 >            if ((h = whead) != null && h.status != 0)
725 >                release(h);
726              return true;
727          }
728          return false;
# Line 745 | Line 736 | public class StampedLock implements java
736       * @return true if the read lock was held, else false
737       */
738      public boolean tryUnlockRead() {
739 <        long s, m;
739 >        long s, m; WNode h;
740          while ((m = (s = state) & ABITS) != 0L && m < WBIT) {
741              if (m < RFULL) {
742                  if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) {
743 <                    if (m == RUNIT)
744 <                        writerPrefSignal();
743 >                    if (m == RUNIT && (h = whead) != null && h.status != 0)
744 >                        release(h);
745                      return true;
746                  }
747              }
# Line 775 | Line 766 | public class StampedLock implements java
766       * @return true if the lock is currently held non-exclusively
767       */
768      public boolean isReadLocked() {
769 <        long m;
779 <        return (m = state & ABITS) > 0L && m < WBIT;
769 >        return (state & RBITS) != 0L;
770      }
771  
772      private void readObject(java.io.ObjectInputStream s)
# Line 785 | Line 775 | public class StampedLock implements java
775          state = ORIGIN; // reset to unlocked state
776      }
777  
778 +    /**
779 +     * Returns a plain {@link Lock} view of this StampedLock in which
780 +     * the {@link Lock#lock} method is mapped to {@link #readLock},
781 +     * and similarly for other methods. The returned Lock does not
782 +     * support a {@link Condition}; method {@link
783 +     * Lock#newCondition()} throws {@code
784 +     * UnsupportedOperationException}.
785 +     *
786 +     * @return the lock
787 +     */
788 +    public Lock asReadLock() {
789 +        ReadLockView v;
790 +        return ((v = readLockView) != null ? v :
791 +                (readLockView = new ReadLockView()));
792 +    }
793 +
794 +    /**
795 +     * Returns a plain {@link Lock} view of this StampedLock in which
796 +     * the {@link Lock#lock} method is mapped to {@link #writeLock},
797 +     * and similarly for other methods. The returned Lock does not
798 +     * support a {@link Condition}; method {@link
799 +     * Lock#newCondition()} throws {@code
800 +     * UnsupportedOperationException}.
801 +     *
802 +     * @return the lock
803 +     */
804 +    public Lock asWriteLock() {
805 +        WriteLockView v;
806 +        return ((v = writeLockView) != null ? v :
807 +                (writeLockView = new WriteLockView()));
808 +    }
809 +
810 +    /**
811 +     * Returns a {@link ReadWriteLock} view of this StampedLock in
812 +     * which the {@link ReadWriteLock#readLock()} method is mapped to
813 +     * {@link #asReadLock()}, and {@link ReadWriteLock#writeLock()} to
814 +     * {@link #asWriteLock()}.
815 +     *
816 +     * @return the lock
817 +     */
818 +    public ReadWriteLock asReadWriteLock() {
819 +        ReadWriteLockView v;
820 +        return ((v = readWriteLockView) != null ? v :
821 +                (readWriteLockView = new ReadWriteLockView()));
822 +    }
823 +
824 +    // view classes
825 +
826 +    final class ReadLockView implements Lock {
827 +        public void lock() { readLock(); }
828 +        public void lockInterruptibly() throws InterruptedException {
829 +            readLockInterruptibly();
830 +        }
831 +        public boolean tryLock() { return tryReadLock() != 0L; }
832 +        public boolean tryLock(long time, TimeUnit unit)
833 +            throws InterruptedException {
834 +            return tryReadLock(time, unit) != 0L;
835 +        }
836 +        public void unlock() { unstampedUnlockRead(); }
837 +        public Condition newCondition() {
838 +            throw new UnsupportedOperationException();
839 +        }
840 +    }
841 +
842 +    final class WriteLockView implements Lock {
843 +        public void lock() { writeLock(); }
844 +        public void lockInterruptibly() throws InterruptedException {
845 +            writeLockInterruptibly();
846 +        }
847 +        public boolean tryLock() { return tryWriteLock() != 0L; }
848 +        public boolean tryLock(long time, TimeUnit unit)
849 +            throws InterruptedException {
850 +            return tryWriteLock(time, unit) != 0L;
851 +        }
852 +        public void unlock() { unstampedUnlockWrite(); }
853 +        public Condition newCondition() {
854 +            throw new UnsupportedOperationException();
855 +        }
856 +    }
857 +
858 +    final class ReadWriteLockView implements ReadWriteLock {
859 +        public Lock readLock() { return asReadLock(); }
860 +        public Lock writeLock() { return asWriteLock(); }
861 +    }
862 +
863 +    // Unlock methods without stamp argument checks for view classes.
864 +    // Needed because view-class lock methods throw away stamps.
865 +
866 +    final void unstampedUnlockWrite() {
867 +        WNode h; long s;
868 +        if (((s = state) & WBIT) == 0L)
869 +            throw new IllegalMonitorStateException();
870 +        state = (s += WBIT) == 0L ? ORIGIN : s;
871 +        if ((h = whead) != null && h.status != 0)
872 +            release(h);
873 +    }
874 +
875 +    final void unstampedUnlockRead() {
876 +        for (;;) {
877 +            long s, m; WNode h;
878 +            if ((m = (s = state) & ABITS) == 0L || m >= WBIT)
879 +                throw new IllegalMonitorStateException();
880 +            else if (m < RFULL) {
881 +                if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) {
882 +                    if (m == RUNIT && (h = whead) != null && h.status != 0)
883 +                        release(h);
884 +                    break;
885 +                }
886 +            }
887 +            else if (tryDecReaderOverflow(s) != 0L)
888 +                break;
889 +        }
890 +    }
891 +
892      // internals
893  
894      /**
# Line 792 | Line 896 | public class StampedLock implements java
896       * access bits value to RBITS, indicating hold of spinlock,
897       * then updating, then releasing.
898       *
899 <     * @param stamp, assumed that (stamp & ABITS) >= RFULL
899 >     * @param s a reader overflow stamp: (s & ABITS) >= RFULL
900       * @return new stamp on success, else zero
901       */
902      private long tryIncReaderOverflow(long s) {
903 +        // assert (s & ABITS) >= RFULL
904          if ((s & ABITS) == RFULL) {
905              if (U.compareAndSwapLong(this, STATE, s, s | RBITS)) {
906                  ++readerOverflow;
# Line 812 | Line 917 | public class StampedLock implements java
917      /**
918       * Tries to decrement readerOverflow.
919       *
920 <     * @param stamp, assumed that (stamp & ABITS) >= RFULL
920 >     * @param s a reader overflow stamp: (s & ABITS) >= RFULL
921       * @return new stamp on success, else zero
922       */
923      private long tryDecReaderOverflow(long s) {
924 +        // assert (s & ABITS) >= RFULL
925          if ((s & ABITS) == RFULL) {
926              if (U.compareAndSwapLong(this, STATE, s, s | RBITS)) {
927                  int r; long next;
# Line 835 | Line 941 | public class StampedLock implements java
941          return 0L;
942      }
943  
944 <    /*
945 <     * The two versions of signal implement the phase-fair policy.
946 <     * They include almost the same code, but repacked in different
947 <     * ways.  Integrating the policy with the mechanics eliminates
948 <     * state rechecks that would be needed with separate reader and
949 <     * writer signal methods.  Both methods assume that they are
950 <     * called when the lock is last known to be available, and
951 <     * continue until the lock is unavailable, or at least one thread
952 <     * is signalled, or there are no more waiting threads.  Signalling
953 <     * a reader entails popping (CASing) from rhead and unparking
954 <     * unless the thread already cancelled (indicated by a null waiter
849 <     * field). Signalling a writer requires finding the first node,
850 <     * i.e., the successor of whead. This is normally just head.next,
851 <     * but may require traversal from wtail if next pointers are
852 <     * lagging. These methods may fail to wake up an acquiring thread
853 <     * when one or more have been cancelled, but the cancel methods
854 <     * themselves provide extra safeguards to ensure liveness.
855 <     */
856 <
857 <    private void readerPrefSignal() {
858 <        boolean readers = false;
859 <        RNode p; WNode h, q; long s; Thread w;
860 <        while ((p = rhead) != null) {
861 <            if (((s = state) & WBIT) != 0L)
862 <                return;
863 <            if (p.seq == (s & SBITS))
864 <                break;
865 <            readers = true;
866 <            if (U.compareAndSwapObject(this, RHEAD, p, p.next) &&
867 <                (w = p.waiter) != null &&
868 <                U.compareAndSwapObject(p, WAITER, w, null))
869 <                U.unpark(w);
870 <        }
871 <        if (!readers && (state & ABITS) == 0L &&
872 <            (h = whead) != null && h.status != 0) {
873 <            U.compareAndSwapInt(h, STATUS, WAITING, 0);
874 <            if ((q = h.next) == null || q.status == CANCELLED) {
875 <                q = null;
876 <                for (WNode t = wtail; t != null && t != h; t = t.prev)
877 <                    if (t.status <= 0)
878 <                        q = t;
879 <            }
880 <            if (q != null && (w = q.thread) != null)
881 <                U.unpark(w);
882 <        }
883 <    }
884 <
885 <    private void writerPrefSignal() {
886 <        RNode p; WNode h, q; long s; Thread w;
887 <        if ((h = whead) != null && h.status != 0) {
888 <            U.compareAndSwapInt(h, STATUS, WAITING, 0);
944 >    /**
945 >     * Wakes up the successor of h (normally whead). This is normally
946 >     * just h.next, but may require traversal from wtail if next
947 >     * pointers are lagging. This may fail to wake up an acquiring
948 >     * thread when one or more have been cancelled, but the cancel
949 >     * methods themselves provide extra safeguards to ensure liveness.
950 >     */
951 >    private void release(WNode h) {
952 >        if (h != null) {
953 >            WNode q; Thread w;
954 >            U.compareAndSwapInt(h, WSTATUS, WAITING, 0);
955              if ((q = h.next) == null || q.status == CANCELLED) {
890                q = null;
956                  for (WNode t = wtail; t != null && t != h; t = t.prev)
957                      if (t.status <= 0)
958                          q = t;
959              }
960 <            if (q != null && (w = q.thread) != null)
961 <                U.unpark(w);
962 <        }
963 <        else {
964 <            while ((p = rhead) != null && ((s = state) & WBIT) == 0L &&
965 <                   p.seq != (s & SBITS)) {
966 <                if (U.compareAndSwapObject(this, RHEAD, p, p.next) &&
967 <                    (w = p.waiter) != null &&
968 <                    U.compareAndSwapObject(p, WAITER, w, null))
969 <                    U.unpark(w);
960 >            if (q != null) {
961 >                for (WNode r = q;;) {  // release co-waiters too
962 >                    if ((w = r.thread) != null) {
963 >                        r.thread = null;
964 >                        U.unpark(w);
965 >                    }
966 >                    if ((r = q.cowait) == null)
967 >                        break;
968 >                    U.compareAndSwapObject(q, WCOWAIT, r, r.cowait);
969 >                }
970              }
971          }
972      }
973  
974      /**
975 <     * RNG for local spins. The first call from await{Read,Write}
911 <     * produces a thread-local value. Unless zero, subsequent calls
912 <     * use an xorShift to further reduce memory traffic.
913 <     */
914 <    private static int nextRandom(int r) {
915 <        if (r == 0)
916 <            return ThreadLocalRandom.current().nextInt();
917 <        r ^= r << 1; // xorshift
918 <        r ^= r >>> 3;
919 <        r ^= r << 10;
920 <        return r;
921 <    }
922 <
923 <    /**
924 <     * Possibly spins trying to obtain write lock, then enqueues and
925 <     * blocks while not head of write queue or cannot acquire lock,
926 <     * possibly spinning when at head; cancelling on timeout or
927 <     * interrupt.
975 >     * See above for explanation.
976       *
977       * @param interruptible true if should check interrupts and if so
978       * return INTERRUPTED
979       * @param deadline if nonzero, the System.nanoTime value to timeout
980       * at (and return zero)
981 +     * @return next state, or INTERRUPTED
982       */
983 <    private long awaitWrite(boolean interruptible, long deadline) {
984 <        WNode node = null;
985 <        for (int r = 0, spins = -1;;) {
986 <            WNode p; long s, next;
983 >    private long acquireWrite(boolean interruptible, long deadline) {
984 >        WNode node = null, p;
985 >        for (int spins = -1;;) { // spin while enqueuing
986 >            long s, ns;
987              if (((s = state) & ABITS) == 0L) {
988 <                if (U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
989 <                    return next;
988 >                if (U.compareAndSwapLong(this, STATE, s, ns = s + WBIT))
989 >                    return ns;
990              }
942            else if (spins < 0)
943                spins = whead == wtail ? SPINS : 0;
991              else if (spins > 0) {
992 <                if ((r = nextRandom(r)) >= 0)
992 >                if (ThreadLocalRandom.current().nextInt() >= 0)
993                      --spins;
994              }
995              else if ((p = wtail) == null) { // initialize queue
996 <                if (U.compareAndSwapObject(this, WHEAD, null,
997 <                                           new WNode(null, null)))
998 <                    wtail = whead;
996 >                WNode h = new WNode(WMODE, null);
997 >                if (U.compareAndSwapObject(this, WHEAD, null, h))
998 >                    wtail = h;
999              }
1000 +            else if (spins < 0)
1001 +                spins = (p == whead) ? SPINS : 0;
1002              else if (node == null)
1003 <                node = new WNode(Thread.currentThread(), p);
1003 >                node = new WNode(WMODE, p);
1004              else if (node.prev != p)
1005                  node.prev = p;
1006              else if (U.compareAndSwapObject(this, WTAIL, p, node)) {
1007                  p.next = node;
1008 <                for (int headSpins = SPINS;;) {
1009 <                    WNode np; int ps;
1010 <                    if ((np = node.prev) != p && np != null &&
1011 <                        (p = np).next != node)
1012 <                        p.next = node; // stale
1013 <                    if (p == whead) {
1014 <                        for (int k = headSpins;;) {
1015 <                            if (((s = state) & ABITS) == 0L) {
1016 <                                if (U.compareAndSwapLong(this, STATE,
1017 <                                                         s, next = s + WBIT)) {
1018 <                                    whead = node;
1019 <                                    node.thread = null;
1020 <                                    node.prev = null;
1021 <                                    return next;
1022 <                                }
1023 <                                break;
1008 >                break;
1009 >            }
1010 >        }
1011 >
1012 >        for (int spins = SPINS;;) {
1013 >            WNode np, pp; int ps; long s, ns; Thread w;
1014 >            while ((np = node.prev) != p && np != null)
1015 >                (p = np).next = node;   // stale
1016 >            if (whead == p) {
1017 >                for (int k = spins;;) { // spin at head
1018 >                    if (((s = state) & ABITS) == 0L) {
1019 >                        if (U.compareAndSwapLong(this, STATE, s, ns = s+WBIT)) {
1020 >                            whead = node;
1021 >                            node.prev = null;
1022 >                            return ns;
1023 >                        }
1024 >                    }
1025 >                    else if (ThreadLocalRandom.current().nextInt() >= 0 &&
1026 >                             --k <= 0)
1027 >                        break;
1028 >                }
1029 >                if (spins < MAX_HEAD_SPINS)
1030 >                    spins <<= 1;
1031 >            }
1032 >            if ((ps = p.status) == 0)
1033 >                U.compareAndSwapInt(p, WSTATUS, 0, WAITING);
1034 >            else if (ps == CANCELLED) {
1035 >                if ((pp = p.prev) != null) {
1036 >                    node.prev = pp;
1037 >                    pp.next = node;
1038 >                }
1039 >            }
1040 >            else {
1041 >                long time; // 0 argument to park means no timeout
1042 >                if (deadline == 0L)
1043 >                    time = 0L;
1044 >                else if ((time = deadline - System.nanoTime()) <= 0L)
1045 >                    return cancelWaiter(node, node, false);
1046 >                node.thread = Thread.currentThread();
1047 >                if (node.prev == p && p.status == WAITING && // recheck
1048 >                    (p != whead || (state & ABITS) != 0L))
1049 >                    U.park(false, time);
1050 >                node.thread = null;
1051 >                if (interruptible && Thread.interrupted())
1052 >                    return cancelWaiter(node, node, true);
1053 >            }
1054 >        }
1055 >    }
1056 >
1057 >    /**
1058 >     * See above for explanation.
1059 >     *
1060 >     * @param interruptible true if should check interrupts and if so
1061 >     * return INTERRUPTED
1062 >     * @param deadline if nonzero, the System.nanoTime value to timeout
1063 >     * at (and return zero)
1064 >     * @return next state, or INTERRUPTED
1065 >     */
1066 >    private long acquireRead(boolean interruptible, long deadline) {
1067 >        WNode node = null, group = null, p;
1068 >        for (int spins = -1;;) {
1069 >            for (;;) {
1070 >                long s, m, ns; WNode h, q; Thread w; // anti-barging guard
1071 >                if (group == null && (h = whead) != null &&
1072 >                    (q = h.next) != null && q.mode != RMODE)
1073 >                    break;
1074 >                if ((m = (s = state) & ABITS) < RFULL ?
1075 >                    U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT) :
1076 >                    (m < WBIT && (ns = tryIncReaderOverflow(s)) != 0L)) {
1077 >                    if (group != null) {  // help release others
1078 >                        for (WNode r = group;;) {
1079 >                            if ((w = r.thread) != null) {
1080 >                                r.thread = null;
1081 >                                U.unpark(w);
1082                              }
1083 <                            if ((r = nextRandom(r)) >= 0 && --k <= 0)
1083 >                            if ((r = group.cowait) == null)
1084                                  break;
1085 +                            U.compareAndSwapObject(group, WCOWAIT, r, r.cowait);
1086                          }
979                        if (headSpins < MAX_HEAD_SPINS)
980                            headSpins <<= 1;
1087                      }
1088 <                    if ((ps = p.status) == 0)
1089 <                        U.compareAndSwapInt(p, STATUS, 0, WAITING);
1090 <                    else if (ps == CANCELLED)
1091 <                        node.prev = p.prev;
1092 <                    else {
1093 <                        long time; // 0 argument to park means no timeout
1088 >                    return ns;
1089 >                }
1090 >                if (m >= WBIT)
1091 >                    break;
1092 >            }
1093 >            if (spins > 0) {
1094 >                if (ThreadLocalRandom.current().nextInt() >= 0)
1095 >                    --spins;
1096 >            }
1097 >            else if ((p = wtail) == null) {
1098 >                WNode h = new WNode(WMODE, null);
1099 >                if (U.compareAndSwapObject(this, WHEAD, null, h))
1100 >                    wtail = h;
1101 >            }
1102 >            else if (spins < 0)
1103 >                spins = (p == whead) ? SPINS : 0;
1104 >            else if (node == null)
1105 >                node = new WNode(WMODE, p);
1106 >            else if (node.prev != p)
1107 >                node.prev = p;
1108 >            else if (p.mode == RMODE && p != whead) {
1109 >                WNode pp = p.prev;  // become co-waiter with group p
1110 >                if (pp != null && p == wtail &&
1111 >                    U.compareAndSwapObject(p, WCOWAIT,
1112 >                                           node.cowait = p.cowait, node)) {
1113 >                    node.thread = Thread.currentThread();
1114 >                    for (long time;;) {
1115 >                        if (interruptible && Thread.interrupted())
1116 >                            return cancelWaiter(node, p, true);
1117                          if (deadline == 0L)
1118                              time = 0L;
1119                          else if ((time = deadline - System.nanoTime()) <= 0L)
1120 <                            return cancelWriter(node, false);
1121 <                        if (node.prev == p && p.status == WAITING &&
1122 <                            (p != whead || (state & WBIT) != 0L)) { // recheck
1123 <                            U.park(false, time);
1124 <                            if (interruptible && Thread.interrupted())
1125 <                                return cancelWriter(node, true);
1120 >                            return cancelWaiter(node, p, false);
1121 >                        if (node.thread == null)
1122 >                            break;
1123 >                        if (p.prev != pp || p.status == CANCELLED ||
1124 >                            p == whead || p.prev != pp) {
1125 >                            node.thread = null;
1126 >                            break;
1127                          }
1128 +                        if (node.thread == null) // must recheck
1129 +                            break;
1130 +                        U.park(false, time);
1131                      }
1132 +                    group = p;
1133                  }
1134 +                node = null; // throw away
1135              }
1136 <        }
1137 <    }
1138 <
1004 <    /**
1005 <     * If node non-null, forces cancel status and unsplices from queue
1006 <     * if possible. This is a streamlined variant of cancellation
1007 <     * methods in AbstractQueuedSynchronizer that includes a detailed
1008 <     * explanation.
1009 <     */
1010 <    private long cancelWriter(WNode node, boolean interrupted) {
1011 <        WNode pred;
1012 <        if (node != null && (pred = node.prev) != null) {
1013 <            WNode pp;
1014 <            node.thread = null;
1015 <            while (pred.status == CANCELLED && (pp = pred.prev) != null)
1016 <                pred = node.prev = pp;
1017 <            WNode predNext = pred.next;
1018 <            node.status = CANCELLED;
1019 <            if (predNext != null) {
1020 <                Thread w;
1021 <                WNode succ = node.next;
1022 <                if (succ == null || succ.status == CANCELLED) {
1023 <                    succ = null;
1024 <                    for (WNode t = wtail; t != null && t != node; t = t.prev)
1025 <                        if (t.status <= 0)
1026 <                            succ = t;
1027 <                    if (succ == null && node == wtail)
1028 <                        U.compareAndSwapObject(this, WTAIL, node, pred);
1029 <                }
1030 <                U.compareAndSwapObject(pred, WNEXT, predNext, succ);
1031 <                if (succ != null && (w = succ.thread) != null)
1032 <                    U.unpark(w);
1136 >            else if (U.compareAndSwapObject(this, WTAIL, p, node)) {
1137 >                p.next = node;
1138 >                break;
1139              }
1140          }
1035        writerPrefSignal();
1036        return (interrupted || Thread.interrupted()) ? INTERRUPTED : 0L;
1037    }
1141  
1142 <    /**
1143 <     * Waits for read lock or timeout or interrupt. The form of
1144 <     * awaitRead differs from awaitWrite mainly because it must
1145 <     * restart (with a new wait node) if the thread was unqueued and
1146 <     * unparked but could not the obtain lock.  We also need to help
1147 <     * with preference rules by not trying to acquire the lock before
1148 <     * enqueuing if there is a known waiting writer, but also helping
1149 <     * to release those threads that are still queued from the last
1150 <     * release.
1151 <     */
1152 <    private long awaitRead(long stamp, boolean interruptible, long deadline) {
1153 <        long seq = stamp & SBITS;
1154 <        RNode node = null;
1155 <        boolean queued = false;
1156 <        for (int r = 0, headSpins = SPINS, spins = -1;;) {
1157 <            long s, m, next; RNode p; WNode wh; Thread w;
1158 <            if ((m = (s = state) & ABITS) != WBIT &&
1159 <                ((s & SBITS) != seq || (wh = whead) == null ||
1160 <                 wh.status == 0)) {
1161 <                if (m < RFULL ?
1162 <                    U.compareAndSwapLong(this, STATE, s, next = s + RUNIT) :
1163 <                    (next = tryIncReaderOverflow(s)) != 0L) {
1164 <                    if (node != null && (w = node.waiter) != null)
1165 <                        U.compareAndSwapObject(node, WAITER, w, null);
1166 <                    if ((p = rhead) != null && (s & SBITS) != p.seq &&
1167 <                        U.compareAndSwapObject(this, RHEAD, p, p.next) &&
1065 <                        (w = p.waiter) != null &&
1066 <                        U.compareAndSwapObject(p, WAITER, w, null))
1067 <                        U.unpark(w); // help signal other waiters
1068 <                    return next;
1142 >        for (int spins = SPINS;;) {
1143 >            WNode np, pp, r; int ps; long m, s, ns; Thread w;
1144 >            while ((np = node.prev) != p && np != null)
1145 >                (p = np).next = node;
1146 >            if (whead == p) {
1147 >                for (int k = spins;;) {
1148 >                    if ((m = (s = state) & ABITS) != WBIT) {
1149 >                        if (m < RFULL ?
1150 >                            U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT):
1151 >                            (ns = tryIncReaderOverflow(s)) != 0L) {
1152 >                            whead = node;
1153 >                            node.prev = null;
1154 >                            while ((r = node.cowait) != null) {
1155 >                                if (U.compareAndSwapObject(node, WCOWAIT,
1156 >                                                           r, r.cowait) &&
1157 >                                    (w = r.thread) != null) {
1158 >                                    r.thread = null;
1159 >                                    U.unpark(w); // release co-waiter
1160 >                                }
1161 >                            }
1162 >                            return ns;
1163 >                        }
1164 >                    }
1165 >                    else if (ThreadLocalRandom.current().nextInt() >= 0 &&
1166 >                             --k <= 0)
1167 >                        break;
1168                  }
1169 +                if (spins < MAX_HEAD_SPINS)
1170 +                    spins <<= 1;
1171              }
1172 <            else if (m != WBIT && (p = rhead) != null &&
1173 <                     (s & SBITS) != p.seq) { // help release old readers
1174 <                if (U.compareAndSwapObject(this, RHEAD, p, p.next) &&
1175 <                    (w = p.waiter) != null &&
1176 <                    U.compareAndSwapObject(p, WAITER, w, null))
1177 <                    U.unpark(w);
1178 <            }
1078 <            else if (queued && node != null && node.waiter == null) {
1079 <                node = null;    // restart
1080 <                queued = false;
1081 <                spins = -1;
1082 <            }
1083 <            else if (spins < 0) {
1084 <                if (rhead != node)
1085 <                    spins = 0;
1086 <                else if ((spins = headSpins) < MAX_HEAD_SPINS && node != null)
1087 <                    headSpins <<= 1;
1088 <            }
1089 <            else if (spins > 0) {
1090 <                if ((r = nextRandom(r)) >= 0)
1091 <                    --spins;
1092 <            }
1093 <            else if (node == null)
1094 <                node = new RNode(seq, Thread.currentThread());
1095 <            else if (!queued) {
1096 <                if (queued = U.compareAndSwapObject(this, RHEAD,
1097 <                                                    node.next = rhead, node))
1098 <                    spins = -1;
1172 >            if ((ps = p.status) == 0)
1173 >                U.compareAndSwapInt(p, WSTATUS, 0, WAITING);
1174 >            else if (ps == CANCELLED) {
1175 >                if ((pp = p.prev) != null) {
1176 >                    node.prev = pp;
1177 >                    pp.next = node;
1178 >                }
1179              }
1180              else {
1181                  long time;
1182                  if (deadline == 0L)
1183                      time = 0L;
1184                  else if ((time = deadline - System.nanoTime()) <= 0L)
1185 <                    return cancelReader(node, false);
1186 <                if ((state & WBIT) != 0L && node.waiter != null) { // recheck
1185 >                    return cancelWaiter(node, node, false);
1186 >                node.thread = Thread.currentThread();
1187 >                if (node.prev == p && p.status == WAITING &&
1188 >                    (p != whead || (state & ABITS) != WBIT))
1189                      U.park(false, time);
1190 <                    if (interruptible && Thread.interrupted())
1191 <                        return cancelReader(node, true);
1192 <                }
1190 >                node.thread = null;
1191 >                if (interruptible && Thread.interrupted())
1192 >                    return cancelWaiter(node, node, true);
1193              }
1194          }
1195      }
1196  
1197      /**
1198 <     * If node non-null, forces cancel status and unsplices from queue
1199 <     * if possible, by traversing entire queue looking for cancelled
1200 <     * nodes.
1201 <     */
1202 <    private long cancelReader(RNode node, boolean interrupted) {
1203 <        Thread w;
1204 <        if (node != null && (w = node.waiter) != null &&
1205 <            U.compareAndSwapObject(node, WAITER, w, null)) {
1206 <            for (RNode pred = null, p = rhead; p != null;) {
1207 <                RNode q = p.next;
1208 <                if (p.waiter == null) {
1209 <                    if (pred == null) {
1210 <                        U.compareAndSwapObject(this, RHEAD, p, q);
1211 <                        p = rhead;
1212 <                    }
1213 <                    else {
1214 <                        U.compareAndSwapObject(pred, RNEXT, p, q);
1215 <                        p = pred.next;
1198 >     * If node non-null, forces cancel status and unsplices it from
1199 >     * queue if possible and wakes up any cowaiters (of the node, or
1200 >     * group, as applicable), and in any case helps release current
1201 >     * first waiter if lock is free. (Calling with null arguments
1202 >     * serves as a conditional form of release, which is not currently
1203 >     * needed but may be needed under possible future cancellation
1204 >     * policies). This is a variant of cancellation methods in
1205 >     * AbstractQueuedSynchronizer (see its detailed explanation in AQS
1206 >     * internal documentation).
1207 >     *
1208 >     * @param node if nonnull, the waiter
1209 >     * @param group either node or the group node is cowaiting with
1210 >     * @param interrupted if already interrupted
1211 >     * @return INTERRUPTED if interrupted or Thread.interrupted, else zero
1212 >     */
1213 >    private long cancelWaiter(WNode node, WNode group, boolean interrupted) {
1214 >        if (node != null && group != null) {
1215 >            Thread w;
1216 >            node.status = CANCELLED;
1217 >            node.thread = null;
1218 >            // unsplice cancelled nodes from group
1219 >            for (WNode p = group, q; (q = p.cowait) != null;) {
1220 >                if (q.status == CANCELLED)
1221 >                    U.compareAndSwapObject(p, WNEXT, q, q.next);
1222 >                else
1223 >                    p = q;
1224 >            }
1225 >            if (group == node) {
1226 >                WNode r; // detach and wake up uncancelled co-waiters
1227 >                while ((r = node.cowait) != null) {
1228 >                    if (U.compareAndSwapObject(node, WCOWAIT, r, r.cowait) &&
1229 >                        (w = r.thread) != null) {
1230 >                        r.thread = null;
1231 >                        U.unpark(w);
1232                      }
1233                  }
1234 <                else {
1235 <                    pred = p;
1236 <                    p = q;
1234 >                for (WNode pred = node.prev; pred != null; ) { // unsplice
1235 >                    WNode succ, pp;        // find valid successor
1236 >                    while ((succ = node.next) == null ||
1237 >                           succ.status == CANCELLED) {
1238 >                        WNode q = null;    // find successor the slow way
1239 >                        for (WNode t = wtail; t != null && t != node; t = t.prev)
1240 >                            if (t.status != CANCELLED)
1241 >                                q = t;     // don't link if succ cancelled
1242 >                        if (succ == q ||   // ensure accurate successor
1243 >                            U.compareAndSwapObject(node, WNEXT,
1244 >                                                   succ, succ = q)) {
1245 >                            if (succ == null && node == wtail)
1246 >                                U.compareAndSwapObject(this, WTAIL, node, pred);
1247 >                            break;
1248 >                        }
1249 >                    }
1250 >                    if (pred.next == node) // unsplice pred link
1251 >                        U.compareAndSwapObject(pred, WNEXT, node, succ);
1252 >                    if (succ != null && (w = succ.thread) != null) {
1253 >                        succ.thread = null;
1254 >                        U.unpark(w);       // wake up succ to observe new pred
1255 >                    }
1256 >                    if (pred.status != CANCELLED || (pp = pred.prev) == null)
1257 >                        break;
1258 >                    node.prev = pp;        // repeat if new pred wrong/cancelled
1259 >                    U.compareAndSwapObject(pp, WNEXT, pred, succ);
1260 >                    pred = pp;
1261                  }
1262              }
1263          }
1264 <        readerPrefSignal();
1264 >        WNode h; // Possibly release first waiter
1265 >        while ((h = whead) != null) {
1266 >            long s; WNode q; // similar to release() but check eligibility
1267 >            if ((q = h.next) == null || q.status == CANCELLED) {
1268 >                for (WNode t = wtail; t != null && t != h; t = t.prev)
1269 >                    if (t.status <= 0)
1270 >                        q = t;
1271 >            }
1272 >            if (h == whead) {
1273 >                if (q != null && h.status == 0 &&
1274 >                    ((s = state) & ABITS) != WBIT && // waiter is eligible
1275 >                    (s == 0L || q.mode == RMODE))
1276 >                    release(h);
1277 >                break;
1278 >            }
1279 >        }
1280          return (interrupted || Thread.interrupted()) ? INTERRUPTED : 0L;
1281      }
1282  
1283      // Unsafe mechanics
1284      private static final sun.misc.Unsafe U;
1285      private static final long STATE;
1149    private static final long RHEAD;
1286      private static final long WHEAD;
1287      private static final long WTAIL;
1152    private static final long RNEXT;
1288      private static final long WNEXT;
1289 <    private static final long WPREV;
1290 <    private static final long WAITER;
1156 <    private static final long STATUS;
1289 >    private static final long WSTATUS;
1290 >    private static final long WCOWAIT;
1291  
1292      static {
1293          try {
1294              U = getUnsafe();
1295              Class<?> k = StampedLock.class;
1162            Class<?> rk = RNode.class;
1296              Class<?> wk = WNode.class;
1297              STATE = U.objectFieldOffset
1298                  (k.getDeclaredField("state"));
1166            RHEAD = U.objectFieldOffset
1167                (k.getDeclaredField("rhead"));
1299              WHEAD = U.objectFieldOffset
1300                  (k.getDeclaredField("whead"));
1301              WTAIL = U.objectFieldOffset
1302                  (k.getDeclaredField("wtail"));
1303 <            RNEXT = U.objectFieldOffset
1173 <                (rk.getDeclaredField("next"));
1174 <            WAITER = U.objectFieldOffset
1175 <                (rk.getDeclaredField("waiter"));
1176 <            STATUS = U.objectFieldOffset
1303 >            WSTATUS = U.objectFieldOffset
1304                  (wk.getDeclaredField("status"));
1305              WNEXT = U.objectFieldOffset
1306                  (wk.getDeclaredField("next"));
1307 <            WPREV = U.objectFieldOffset
1308 <                (wk.getDeclaredField("prev"));
1307 >            WCOWAIT = U.objectFieldOffset
1308 >                (wk.getDeclaredField("cowait"));
1309  
1310          } catch (Exception e) {
1311              throw new Error(e);
# Line 1195 | Line 1322 | public class StampedLock implements java
1322      private static sun.misc.Unsafe getUnsafe() {
1323          try {
1324              return sun.misc.Unsafe.getUnsafe();
1325 <        } catch (SecurityException se) {
1326 <            try {
1327 <                return java.security.AccessController.doPrivileged
1328 <                    (new java.security
1329 <                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
1330 <                        public sun.misc.Unsafe run() throws Exception {
1331 <                            java.lang.reflect.Field f = sun.misc
1332 <                                .Unsafe.class.getDeclaredField("theUnsafe");
1333 <                            f.setAccessible(true);
1334 <                            return (sun.misc.Unsafe) f.get(null);
1335 <                        }});
1336 <            } catch (java.security.PrivilegedActionException e) {
1337 <                throw new RuntimeException("Could not initialize intrinsics",
1338 <                                           e.getCause());
1339 <            }
1325 >        } catch (SecurityException tryReflectionInstead) {}
1326 >        try {
1327 >            return java.security.AccessController.doPrivileged
1328 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
1329 >                public sun.misc.Unsafe run() throws Exception {
1330 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
1331 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
1332 >                        f.setAccessible(true);
1333 >                        Object x = f.get(null);
1334 >                        if (k.isInstance(x))
1335 >                            return k.cast(x);
1336 >                    }
1337 >                    throw new NoSuchFieldError("the Unsafe");
1338 >                }});
1339 >        } catch (java.security.PrivilegedActionException e) {
1340 >            throw new RuntimeException("Could not initialize intrinsics",
1341 >                                       e.getCause());
1342          }
1343      }
1215
1344   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines