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.27 by jsr166, Mon Jan 14 19:00:01 2013 UTC vs.
Revision 1.28 by dl, Tue Jan 22 15:42:28 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.*;
12  
13   /**
14   * A capability-based lock with three modes for controlling read/write
# Line 80 | Line 81 | import java.util.concurrent.TimeUnit;
81   * locking.
82   *
83   * <p>The scheduling policy of StampedLock does not consistently
84 < * prefer readers over writers or vice versa.  A zero return from any
85 < * "try" method for acquiring or converting locks does not carry any
86 < * information about the state of the lock; a subsequent invocation
87 < * may succeed.
84 > * prefer readers over writers or vice versa.  All "try" methods are
85 > * best-effort and do not necessarily conform to any scheduling or
86 > * fairness policy. A zero return from any "try" method for acquiring
87 > * or converting locks does not carry any information about the state
88 > * of the lock; a subsequent invocation may succeed.
89 > *
90 > * <p>Because it supports coordinated usage across multiple lock
91 > * modes, this class does not directly implement the {@link Lock} or
92 > * {@link ReadWriteLock} interfaces. However, a StampedLock may be
93 > * viewed {@link #asReadLock()}, {@link #asWriteLock()}, or {@link
94 > * #asReadWriteLock()} in applications requiring only the associated
95 > * set of functionality.
96   *
97   * <p><b>Sample Usage.</b> The following illustrates some usage idioms
98   * in a class that maintains simple two-dimensional points. The sample
# Line 173 | Line 182 | public class StampedLock implements java
182       * http://www.lameter.com/gelato2005.pdf
183       * and elsewhere; see
184       * Boehm's http://www.hpl.hp.com/techreports/2012/HPL-2012-68.html)
185 <     * Ordered RW locks (see Shirako et al
185 >     * and Ordered RW locks (see Shirako et al
186       * http://dl.acm.org/citation.cfm?id=2312015)
178     * and Phase-Fair locks (see Brandenburg & Anderson, especially
179     * http://www.cs.unc.edu/~bbb/diss/).
187       *
188       * Conceptually, the primary state of the lock includes a sequence
189       * number that is odd when write-locked and even otherwise.
# Line 189 | Line 196 | public class StampedLock implements java
196       * reader count value (RBITS) as a spinlock protecting overflow
197       * updates.
198       *
199 <     * Waiting readers and writers use different queues. The writer
200 <     * queue is a modified form of CLH lock.  (For discussion of CLH,
201 <     * see the internal documentation of AbstractQueuedSynchronizer.)
202 <     * The reader "queue" is a form of Treiber stack, that supports
203 <     * simpler/faster operations because order within a queue doesn't
204 <     * matter and all are signalled at once.  However the sequence of
205 <     * threads within the queue vs the current stamp does matter (see
206 <     * Shirako et al) so each carries its incoming stamp value.
207 <     * Waiting writers never need to track sequence values, so they
208 <     * don't.
209 <     *
210 <     * These queue mechanics hardwire the scheduling policy.  Ignoring
211 <     * trylocks, cancellation, and spinning, they implement Phase-Fair
212 <     * preferences:
213 <     *   1. Unlocked writers prefer to signal waiting readers
214 <     *   2. Fully unlocked readers prefer to signal waiting writers
208 <     *   3. When read-locked and a waiting writer exists, the writer
209 <     *      is preferred to incoming readers
199 >     * Waiters use a modified form of CLH lock used in
200 >     * AbstractQueuedSynchronizer (see its internal documentation for
201 >     * a fuller account), where each node it tagged (field mode) as
202 >     * either a reader or writer. Sets of waiting readers are grouped
203 >     * (linked) under a common node (field cowait) so act as a single
204 >     * node with respect to most CLH mechanics.  By virtue of its
205 >     * structure, wait nodes need not actually carry sequence numbers;
206 >     * we know each is >= its predecessor.  These queue mechanics
207 >     * simplify the scheduling policy to a mainly-FIFO scheme that
208 >     * incorporates elements of Phase-Fair locks (see Brandenburg &
209 >     * Anderson, especially http://www.cs.unc.edu/~bbb/diss/).  In
210 >     * particular, we use the phase-fair anti-barging rule: If an
211 >     * incoming reader arrives while read lock is held but there is a
212 >     * queued writer, this incoming reader is queued.  (This rule is
213 >     * responsible for some of the complexity of method acquireRead,
214 >     * but without it, the lock becomes highly unfair.)
215       *
216       * These rules apply to threads actually queued. All tryLock forms
217       * opportunistically try to acquire locks regardless of preference
218 <     * rules, and so may "barge" their way in.  Additionally, initial
219 <     * phases of the await* methods (invoked from readLock() and
220 <     * writeLock()) use controlled spins that have similar effect.
221 <     * Phase-fair preferences may also be broken on cancellations due
222 <     * to timeouts and interrupts.  Rule #3 (incoming readers when a
223 <     * waiting writer) is approximated with varying precision in
224 <     * different contexts -- some checks do not account for
225 <     * in-progress spins/signals, and others do not account for
226 <     * cancellations.
227 <     *
228 <     * Controlled, randomized spinning is used in the two await
229 <     * methods to reduce (increasingly expensive) context switching
230 <     * while also avoiding sustained memory thrashing among many
231 <     * threads.  Both await methods use a similar spin strategy: If
232 <     * the associated queue appears to be empty, then the thread
233 <     * spin-waits up to SPINS times (where each iteration decreases
229 <     * spin count with 50% probability) before enqueuing, and then, if
230 <     * it is the first thread to be enqueued, spins again up to SPINS
231 <     * times before blocking. If, upon wakening it fails to obtain
232 <     * lock, and is still (or becomes) the first waiting thread (which
233 <     * indicates that some other thread barged and obtained lock), it
234 <     * escalates spins (up to MAX_HEAD_SPINS) to reduce the likelihood
235 <     * of continually losing to barging threads.
218 >     * rules, and so may "barge" their way in.  Randomized spinning is
219 >     * used in the acquire methods to reduce (increasingly expensive)
220 >     * context switching while also avoiding sustained memory
221 >     * thrashing among many threads.  We limit spins to the head of
222 >     * queue. A thread spin-waits up to SPINS times (where each
223 >     * iteration decreases spin count with 50% probability) before
224 >     * blocking. If, upon wakening it fails to obtain lock, and is
225 >     * still (or becomes) the first waiting thread (which indicates
226 >     * that some other thread barged and obtained lock), it escalates
227 >     * spins (up to MAX_HEAD_SPINS) to reduce the likelihood of
228 >     * continually losing to barging threads.
229 >     *
230 >     * Nearly all of these mechanics are carried out in methods
231 >     * acquireWrite and acquireRead, that, as typical of such code,
232 >     * sprawl out because actions and retries rely on consitent sets
233 >     * of locally cahced reads.
234       *
235       * As noted in Boehm's paper (above), sequence validation (mainly
236       * method validate()) requires stricter ordering rules than apply
# Line 258 | Line 256 | public class StampedLock implements java
256      private static final int NCPU = Runtime.getRuntime().availableProcessors();
257  
258      /** Maximum number of retries before blocking on acquisition */
259 <    private static final int SPINS = (NCPU > 1) ? 1 << 6 : 1;
259 >    private static final int SPINS = (NCPU > 1) ? 1 << 6 : 0;
260  
261      /** Maximum number of retries before re-blocking */
262 <    private static final int MAX_HEAD_SPINS = (NCPU > 1) ? 1 << 12 : 1;
262 >    private static final int MAX_HEAD_SPINS = (NCPU > 1) ? 1 << 12 : 0;
263  
264      /** The period for yielding when waiting for overflow spinlock */
265      private static final int OVERFLOW_YIELD_RATE = 7; // must be power 2 - 1
# Line 280 | Line 278 | public class StampedLock implements java
278      // Initial value for lock state; avoid failure value zero
279      private static final long ORIGIN = WBIT << 1;
280  
281 <    // Special value from cancelled await methods so caller can throw IE
281 >    // Special value from cancelled acquire methods so caller can throw IE
282      private static final long INTERRUPTED = 1L;
283  
284 <    // Values for writer status; order matters
284 >    // Values for node status; order matters
285      private static final int WAITING   = -1;
286      private static final int CANCELLED =  1;
287  
288 <    /** Wait nodes for readers */
289 <    static final class RNode {
290 <        final long seq;         // stamp value upon enqueue
293 <        volatile Thread waiter; // null if no longer waiting
294 <        volatile RNode next;
295 <        RNode(long s, Thread w) { seq = s; waiter = w; }
296 <    }
288 >    // Modes for nodes (int not boolean to allow arithmetic)
289 >    private static final int RMODE = 0;
290 >    private static final int WMODE = 1;
291  
292 <    /** Wait nodes for writers */
292 >    /** Wait nodes */
293      static final class WNode {
300        volatile int status;   // 0, WAITING, or CANCELLED
294          volatile WNode prev;
295          volatile WNode next;
296 <        volatile Thread thread;
297 <        WNode(Thread t, WNode p) { thread = t; prev = p; }
296 >        volatile WNode cowait;    // list of linked readers
297 >        volatile Thread thread;   // non-null while possibly parked
298 >        volatile int status;      // 0, WAITING, or CANCELLED
299 >        final int mode;           // RMODE or WMODE
300 >        WNode(int m, WNode p) { mode = m; prev = p; }
301      }
302  
303 <    /** Head of writer CLH queue */
303 >    /** Head of CLH queue */
304      private transient volatile WNode whead;
305 <    /** Tail (last) of writer CLH queue */
305 >    /** Tail (last) of CLH queue */
306      private transient volatile WNode wtail;
307 <    /** Head of read queue  */
308 <    private transient volatile RNode rhead;
309 <    /** The state of the lock -- high bits hold sequence, low bits read count */
307 >
308 >    // views
309 >    transient ReadLockView readLockView;
310 >    transient WriteLockView writeLockView;
311 >    transient ReadWriteLockView readWriteLockView;
312 >
313 >    /** Lock sequence/state */
314      private transient volatile long state;
315      /** extra reader count when state read count saturated */
316      private transient int readerOverflow;
# Line 329 | Line 329 | public class StampedLock implements java
329       * @return a stamp that can be used to unlock or convert mode
330       */
331      public long writeLock() {
332 <        long s, next;
333 <        if (((s = state) & ABITS) == 0L &&
334 <            U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
335 <            return next;
336 <        return awaitWrite(false, 0L);
332 >        long s, next;  // bypass acquireWrite in fully onlocked case only
333 >        return ((((s = state) & ABITS) == 0L &&
334 >                 U.compareAndSwapLong(this, STATE, s, next = s + WBIT)) ?
335 >                next : acquireWrite(false, 0L));
336      }
337  
338      /**
# Line 344 | Line 343 | public class StampedLock implements java
343       */
344      public long tryWriteLock() {
345          long s, next;
346 <        if (((s = state) & ABITS) == 0L &&
347 <            U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
348 <            return next;
350 <        return 0L;
346 >        return ((((s = state) & ABITS) == 0L &&
347 >                 U.compareAndSwapLong(this, STATE, s, next = s + WBIT)) ?
348 >                next : 0L);
349      }
350  
351      /**
352       * Exclusively acquires the lock if it is available within the
353       * given time and the current thread has not been interrupted.
354 +     * Behavior under timeout and interruption matches that specified
355 +     * for method {@link Lock#tryLock(long,TimeUnit)}.
356       *
357       * @return a stamp that can be used to unlock or convert mode,
358       * or zero if the lock is not available
# Line 363 | Line 363 | public class StampedLock implements java
363          throws InterruptedException {
364          long nanos = unit.toNanos(time);
365          if (!Thread.interrupted()) {
366 <            long s, next, deadline;
367 <            if (((s = state) & ABITS) == 0L &&
368 <                U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
366 >            long next, deadline;
367 >            if ((next = tryWriteLock()) != 0)
368                  return next;
369              if (nanos <= 0L)
370                  return 0L;
371              if ((deadline = System.nanoTime() + nanos) == 0L)
372                  deadline = 1L;
373 <            if ((next = awaitWrite(true, deadline)) != INTERRUPTED)
373 >            if ((next = acquireWrite(true, deadline)) != INTERRUPTED)
374                  return next;
375          }
376          throw new InterruptedException();
# Line 380 | Line 379 | public class StampedLock implements java
379      /**
380       * Exclusively acquires the lock, blocking if necessary
381       * until available or the current thread is interrupted.
382 +     * Behavior under interruption matches that specified
383 +     * for method {@link Lock#lockInterruptibly()}.
384       *
385       * @return a stamp that can be used to unlock or convert mode
386       * @throws InterruptedException if the current thread is interrupted
387       * before acquiring the lock
388       */
389      public long writeLockInterruptibly() throws InterruptedException {
390 <        if (!Thread.interrupted()) {
391 <            long s, next;
392 <            if (((s = state) & ABITS) == 0L &&
393 <                U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
393 <                return next;
394 <            if ((next = awaitWrite(true, 0L)) != INTERRUPTED)
395 <                return next;
396 <        }
390 >        long next;
391 >        if (!Thread.interrupted() &&
392 >            (next = acquireWrite(true, 0L)) != INTERRUPTED)
393 >            return next;
394          throw new InterruptedException();
395      }
396  
# Line 404 | Line 401 | public class StampedLock implements java
401       * @return a stamp that can be used to unlock or convert mode
402       */
403      public long readLock() {
404 <        for (;;) {
405 <            long s, m, next;
406 <            if ((m = (s = state) & ABITS) == 0L ||
407 <                (m < WBIT && whead == wtail)) {
411 <                if (m < RFULL) {
412 <                    if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT))
413 <                        return next;
414 <                }
415 <                else if ((next = tryIncReaderOverflow(s)) != 0L)
416 <                    return next;
417 <            }
418 <            else
419 <                return awaitRead(s, false, 0L);
420 <        }
404 >        long s, next;  // bypass acquireRead on fully onlocked case only
405 >        return ((((s = state) & ABITS) == 0L &&
406 >                 U.compareAndSwapLong(this, STATE, s, next = s + RUNIT)) ?
407 >                next : acquireRead(false, 0L));
408      }
409  
410      /**
# Line 443 | Line 430 | public class StampedLock implements java
430      /**
431       * Non-exclusively acquires the lock if it is available within the
432       * given time and the current thread has not been interrupted.
433 +     * Behavior under timeout and interruption matches that specified
434 +     * for method {@link Lock#tryLock(long,TimeUnit)}.
435       *
436       * @return a stamp that can be used to unlock or convert mode,
437       * or zero if the lock is not available
# Line 451 | Line 440 | public class StampedLock implements java
440       */
441      public long tryReadLock(long time, TimeUnit unit)
442          throws InterruptedException {
443 +        long next, deadline;
444          long nanos = unit.toNanos(time);
445          if (!Thread.interrupted()) {
446 <            for (;;) {
447 <                long s, m, next, deadline;
448 <                if ((m = (s = state) & ABITS) == WBIT ||
449 <                    (m != 0L && whead != wtail)) {
450 <                    if (nanos <= 0L)
451 <                        return 0L;
452 <                    if ((deadline = System.nanoTime() + nanos) == 0L)
453 <                        deadline = 1L;
464 <                    if ((next = awaitRead(s, true, deadline)) != INTERRUPTED)
465 <                        return next;
466 <                    break;
467 <                }
468 <                else if (m < RFULL) {
469 <                    if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT))
470 <                        return next;
471 <                }
472 <                else if ((next = tryIncReaderOverflow(s)) != 0L)
473 <                    return next;
474 <            }
446 >            if ((next = tryReadLock()) != 0)
447 >                return next;
448 >            if (nanos <= 0L)
449 >                return 0L;
450 >            if ((deadline = System.nanoTime() + nanos) == 0L)
451 >                deadline = 1L;
452 >            if ((next = acquireRead(true, deadline)) != INTERRUPTED)
453 >                return next;
454          }
455          throw new InterruptedException();
456      }
# Line 479 | Line 458 | public class StampedLock implements java
458      /**
459       * Non-exclusively acquires the lock, blocking if necessary
460       * until available or the current thread is interrupted.
461 +     * Behavior under interruption matches that specified
462 +     * for method {@link Lock#lockInterruptibly()}.
463       *
464       * @return a stamp that can be used to unlock or convert mode
465       * @throws InterruptedException if the current thread is interrupted
466       * before acquiring the lock
467       */
468      public long readLockInterruptibly() throws InterruptedException {
469 <        if (!Thread.interrupted()) {
470 <            for (;;) {
471 <                long s, next, m;
472 <                if ((m = (s = state) & ABITS) == WBIT ||
492 <                    (m != 0L && whead != wtail)) {
493 <                    if ((next = awaitRead(s, true, 0L)) != INTERRUPTED)
494 <                        return next;
495 <                    break;
496 <                }
497 <                else if (m < RFULL) {
498 <                    if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT))
499 <                        return next;
500 <                }
501 <                else if ((next = tryIncReaderOverflow(s)) != 0L)
502 <                    return next;
503 <            }
504 <        }
469 >        long next;
470 >        if (!Thread.interrupted() &&
471 >            (next = acquireRead(true, 0L)) != INTERRUPTED)
472 >            return next;
473          throw new InterruptedException();
474      }
475  
# Line 539 | Line 507 | public class StampedLock implements java
507       * not match the current state of this lock
508       */
509      public void unlockWrite(long stamp) {
510 +        WNode h;
511          if (state != stamp || (stamp & WBIT) == 0L)
512              throw new IllegalMonitorStateException();
513          state = (stamp += WBIT) == 0L ? ORIGIN : stamp;
514 <        readerPrefSignal();
514 >        if ((h = whead) != null && h.status != 0)
515 >            release(h);
516      }
517  
518      /**
# Line 554 | Line 524 | public class StampedLock implements java
524       * not match the current state of this lock
525       */
526      public void unlockRead(long stamp) {
527 <        long s, m;
527 >        long s, m;  WNode h;
528          if ((stamp & RBITS) != 0L) {
529              while (((s = state) & SBITS) == (stamp & SBITS)) {
530                  if ((m = s & ABITS) == 0L)
531                      break;
532                  else if (m < RFULL) {
533                      if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) {
534 <                        if (m == RUNIT)
535 <                            writerPrefSignal();
534 >                        if (m == RUNIT && (h = whead) != null && h.status != 0)
535 >                            release(h);
536                          return;
537                      }
538                  }
# Line 584 | Line 554 | public class StampedLock implements java
554       * not match the current state of this lock
555       */
556      public void unlock(long stamp) {
557 <        long a = stamp & ABITS, m, s;
557 >        long a = stamp & ABITS, m, s; WNode h;
558          while (((s = state) & SBITS) == (stamp & SBITS)) {
559              if ((m = s & ABITS) == 0L)
560                  break;
# Line 592 | Line 562 | public class StampedLock implements java
562                  if (a != m)
563                      break;
564                  state = (s += WBIT) == 0L ? ORIGIN : s;
565 <                readerPrefSignal();
565 >                if ((h = whead) != null && h.status != 0)
566 >                    release(h);
567                  return;
568              }
569              else if (a == 0L || a >= WBIT)
570                  break;
571              else if (m < RFULL) {
572                  if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) {
573 <                    if (m == RUNIT)
574 <                        writerPrefSignal();
573 >                    if (m == RUNIT && (h = whead) != null && h.status != 0)
574 >                        release(h);
575                      return;
576                  }
577              }
# Line 611 | Line 582 | public class StampedLock implements java
582      }
583  
584      /**
585 <     * If the lock state matches the given stamp then performs one of
585 >     * If the lock state matches the given stamp, performs one of
586       * the following actions. If the stamp represents holding a write
587       * lock, returns it.  Or, if a read lock, if the write lock is
588       * available, releases the read lock and returns a write stamp.
# Line 648 | Line 619 | public class StampedLock implements java
619      }
620  
621      /**
622 <     * If the lock state matches the given stamp then performs one of
622 >     * If the lock state matches the given stamp, performs one of
623       * the following actions. If the stamp represents holding a write
624       * lock, releases it and obtains a read lock.  Or, if a read lock,
625       * returns it. Or, if an optimistic read, acquires a read lock and
# Line 659 | Line 630 | public class StampedLock implements java
630       * @return a valid read stamp, or zero on failure
631       */
632      public long tryConvertToReadLock(long stamp) {
633 <        long a = stamp & ABITS, m, s, next;
633 >        long a = stamp & ABITS, m, s, next; WNode h;
634          while (((s = state) & SBITS) == (stamp & SBITS)) {
635              if ((m = s & ABITS) == 0L) {
636                  if (a != 0L)
# Line 675 | Line 646 | public class StampedLock implements java
646                  if (a != m)
647                      break;
648                  state = next = s + (WBIT + RUNIT);
649 <                readerPrefSignal();
649 >                if ((h = whead) != null && h.status != 0)
650 >                    release(h);
651                  return next;
652              }
653              else if (a != 0L && a < WBIT)
# Line 697 | Line 669 | public class StampedLock implements java
669       * @return a valid optimistic read stamp, or zero on failure
670       */
671      public long tryConvertToOptimisticRead(long stamp) {
672 <        long a = stamp & ABITS, m, s, next;
673 <        while (((s = U.getLongVolatile(this, STATE)) &
674 <                SBITS) == (stamp & SBITS)) {
672 >        long a = stamp & ABITS, m, s, next; WNode h;
673 >        for (;;) {
674 >            s = U.getLongVolatile(this, STATE); // see above
675 >            if ((s & SBITS) != (stamp & SBITS))
676 >                break;
677              if ((m = s & ABITS) == 0L) {
678                  if (a != 0L)
679                      break;
# Line 709 | Line 683 | public class StampedLock implements java
683                  if (a != m)
684                      break;
685                  state = next = (s += WBIT) == 0L ? ORIGIN : s;
686 <                readerPrefSignal();
686 >                if ((h = whead) != null && h.status != 0)
687 >                    release(h);
688                  return next;
689              }
690              else if (a == 0L || a >= WBIT)
691                  break;
692              else if (m < RFULL) {
693                  if (U.compareAndSwapLong(this, STATE, s, next = s - RUNIT)) {
694 <                    if (m == RUNIT)
695 <                        writerPrefSignal();
694 >                    if (m == RUNIT && (h = whead) != null && h.status != 0)
695 >                        release(h);
696                      return next & SBITS;
697                  }
698              }
# Line 735 | Line 710 | public class StampedLock implements java
710       * @return true if the lock was held, else false
711       */
712      public boolean tryUnlockWrite() {
713 <        long s;
713 >        long s; WNode h;
714          if (((s = state) & WBIT) != 0L) {
715              state = (s += WBIT) == 0L ? ORIGIN : s;
716 <            readerPrefSignal();
716 >            if ((h = whead) != null && h.status != 0)
717 >                release(h);
718              return true;
719          }
720          return false;
# Line 752 | Line 728 | public class StampedLock implements java
728       * @return true if the read lock was held, else false
729       */
730      public boolean tryUnlockRead() {
731 <        long s, m;
731 >        long s, m; WNode h;
732          while ((m = (s = state) & ABITS) != 0L && m < WBIT) {
733              if (m < RFULL) {
734                  if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) {
735 <                    if (m == RUNIT)
736 <                        writerPrefSignal();
735 >                    if (m == RUNIT && (h = whead) != null && h.status != 0)
736 >                        release(h);
737                      return true;
738                  }
739              }
# Line 791 | Line 767 | public class StampedLock implements java
767          state = ORIGIN; // reset to unlocked state
768      }
769  
770 +    /**
771 +     * Returns a plain {@link Lock} view of this StampedLock in which
772 +     * the {@link Lock#lock} method is mapped to {@link #readLock},
773 +     * and similarly for other methods. The returned Lock does not
774 +     * support a {@link Condition}; method {@link
775 +     * Lock#newCondition()} throws {@code
776 +     * UnsupportedOperationException}.
777 +     *
778 +     * @return the lock
779 +     */
780 +    public Lock asReadLock() {
781 +        ReadLockView v;
782 +        return ((v = readLockView) != null ? v :
783 +                (readLockView = new ReadLockView()));
784 +    }
785 +
786 +    /**
787 +     * Returns a plain {@link Lock} view of this StampedLock in which
788 +     * the {@link Lock#lock} method is mapped to {@link #writeLock},
789 +     * and similarly for other methods. The returned Lock does not
790 +     * support a {@link Condition}; method {@link
791 +     * Lock#newCondition()} throws {@code
792 +     * UnsupportedOperationException}.
793 +     *
794 +     * @return the lock
795 +     */
796 +    public Lock asWriteLock() {
797 +        WriteLockView v;
798 +        return ((v = writeLockView) != null ? v :
799 +                (writeLockView = new WriteLockView()));
800 +    }
801 +
802 +    /**
803 +     * Returns a {@link ReadWriteLock} view of this StampedLock in
804 +     * which the {@link ReadWriteLock#readLock()} method is mapped to
805 +     * {@link #asReadLock()}, and {@link ReadWriteLock#writeLock()} to
806 +     * {@link #asWriteLock()}.
807 +     *
808 +     * @return the lock
809 +     */
810 +    public ReadWriteLock asReadWriteLock() {
811 +        ReadWriteLockView v;
812 +        return ((v = readWriteLockView) != null ? v :
813 +                (readWriteLockView = new ReadWriteLockView()));
814 +    }
815 +
816 +    // view classes
817 +
818 +    final class ReadLockView implements Lock {
819 +        public void lock() { readLock(); }
820 +        public void lockInterruptibly() throws InterruptedException {
821 +            readLockInterruptibly();
822 +        }
823 +        public boolean tryLock() { return tryReadLock() != 0L; }
824 +        public boolean tryLock(long time, TimeUnit unit)
825 +            throws InterruptedException {
826 +            return tryReadLock(time, unit) != 0L;
827 +        }
828 +        // note that we give up ability to check mode so just use current state
829 +        public void unlock() { unlockRead(state); }
830 +        public Condition newCondition() {
831 +            throw new UnsupportedOperationException();
832 +        }
833 +    }
834 +
835 +    final class WriteLockView implements Lock {
836 +        public void lock() { writeLock(); }
837 +        public void lockInterruptibly() throws InterruptedException {
838 +            writeLockInterruptibly();
839 +        }
840 +        public boolean tryLock() { return tryWriteLock() != 0L; }
841 +        public boolean tryLock(long time, TimeUnit unit)
842 +            throws InterruptedException {
843 +            return tryWriteLock(time, unit) != 0L;
844 +        }
845 +        public void unlock() { unlockWrite(state); }
846 +        public Condition newCondition() {
847 +            throw new UnsupportedOperationException();
848 +        }
849 +    }
850 +
851 +    final class ReadWriteLockView implements ReadWriteLock {
852 +        public Lock readLock() { return asReadLock(); }
853 +        public Lock writeLock() { return asWriteLock(); }
854 +    }
855 +
856      // internals
857  
858      /**
# Line 842 | Line 904 | public class StampedLock implements java
904      }
905  
906      /*
907 <     * The two versions of signal implement the phase-fair policy.
908 <     * They include almost the same code, but repacked in different
909 <     * ways.  Integrating the policy with the mechanics eliminates
910 <     * state rechecks that would be needed with separate reader and
911 <     * writer signal methods.  Both methods assume that they are
912 <     * called when the lock is last known to be available, and
913 <     * continue until the lock is unavailable, or at least one thread
914 <     * is signalled, or there are no more waiting threads.  Signalling
915 <     * a reader entails popping (CASing) from rhead and unparking
916 <     * unless the thread already cancelled (indicated by a null waiter
855 <     * field). Signalling a writer requires finding the first node,
856 <     * i.e., the successor of whead. This is normally just head.next,
857 <     * but may require traversal from wtail if next pointers are
858 <     * lagging. These methods may fail to wake up an acquiring thread
859 <     * when one or more have been cancelled, but the cancel methods
860 <     * themselves provide extra safeguards to ensure liveness.
861 <     */
862 <
863 <    private void readerPrefSignal() {
864 <        boolean readers = false;
865 <        RNode p; WNode h, q; long s; Thread w;
866 <        while ((p = rhead) != null) {
867 <            if (((s = state) & WBIT) != 0L)
868 <                return;
869 <            if (p.seq == (s & SBITS))
870 <                break;
871 <            readers = true;
872 <            if (U.compareAndSwapObject(this, RHEAD, p, p.next) &&
873 <                (w = p.waiter) != null &&
874 <                U.compareAndSwapObject(p, WAITER, w, null))
875 <                U.unpark(w);
876 <        }
877 <        if (!readers && (h = whead) != null && h.status != 0 &&
878 <            (state & ABITS) == 0L) {
879 <            U.compareAndSwapInt(h, STATUS, WAITING, 0);
907 >     * Wakes up the successor of h (normally whead). This is normally
908 >     * just h.next, but may require traversal from wtail if next
909 >     * pointers are lagging. This may fail to wake up an acquiring
910 >     * thread when one or more have been cancelled, but the cancel
911 >     * methods themselves provide extra safeguards to ensure liveness.
912 >     */
913 >    private void release(WNode h) {
914 >        if (h != null) {
915 >            WNode q; Thread w;
916 >            U.compareAndSwapInt(h, WSTATUS, WAITING, 0);
917              if ((q = h.next) == null || q.status == CANCELLED) {
918                  for (WNode t = wtail; t != null && t != h; t = t.prev)
919                      if (t.status <= 0)
920                          q = t;
921              }
922 <            if (q != null && (w = q.thread) != null)
923 <                U.unpark(w);
924 <        }
925 <    }
926 <
927 <    private void writerPrefSignal() {
928 <        RNode p; WNode h, q; long s; Thread w;
929 <        if ((h = whead) != null && h.status != 0) {
930 <            U.compareAndSwapInt(h, STATUS, WAITING, 0);
931 <            if ((q = h.next) == null || q.status == CANCELLED) {
895 <                for (WNode t = wtail; t != null && t != h; t = t.prev)
896 <                    if (t.status <= 0)
897 <                        q = t;
898 <            }
899 <            if (q != null && (w = q.thread) != null)
900 <                U.unpark(w);
901 <        }
902 <        else {
903 <            while ((p = rhead) != null && ((s = state) & WBIT) == 0L &&
904 <                   p.seq != (s & SBITS)) {
905 <                if (U.compareAndSwapObject(this, RHEAD, p, p.next) &&
906 <                    (w = p.waiter) != null &&
907 <                    U.compareAndSwapObject(p, WAITER, w, null))
908 <                    U.unpark(w);
922 >            if (q != null) {
923 >                for (WNode r = q;;) {  // release co-waiters too
924 >                    if ((w = r.thread) != null) {
925 >                        r.thread = null;
926 >                        U.unpark(w);
927 >                    }
928 >                    if ((r = q.cowait) == null)
929 >                        break;
930 >                    U.compareAndSwapObject(q, WCOWAIT, r, r.cowait);
931 >                }
932              }
933          }
934      }
935  
936      /**
937 <     * RNG for local spins. The first call from await{Read,Write}
915 <     * produces a thread-local value. Unless zero, subsequent calls
916 <     * use an xorShift to further reduce memory traffic.
917 <     */
918 <    private static int nextRandom(int r) {
919 <        if (r == 0)
920 <            return ThreadLocalRandom.current().nextInt();
921 <        r ^= r << 1; // xorshift
922 <        r ^= r >>> 3;
923 <        r ^= r << 10;
924 <        return r;
925 <    }
926 <
927 <    /**
928 <     * Possibly spins trying to obtain write lock, then enqueues and
929 <     * blocks while not head of write queue or cannot acquire lock,
930 <     * possibly spinning when at head; cancelling on timeout or
931 <     * interrupt.
937 >     * See above for explanation.
938       *
939       * @param interruptible true if should check interrupts and if so
940       * return INTERRUPTED
941       * @param deadline if nonzero, the System.nanoTime value to timeout
942       * at (and return zero)
943 +     * @return next state, or INTERRUPTED
944       */
945 <    private long awaitWrite(boolean interruptible, long deadline) {
946 <        WNode node = null;
947 <        for (int r = 0, spins = -1;;) {
948 <            WNode p; long s, next;
945 >    private long acquireWrite(boolean interruptible, long deadline) {
946 >        WNode node = null, p;
947 >        for (int spins = -1;;) { // spin while enqueuing
948 >            long s, ns;
949              if (((s = state) & ABITS) == 0L) {
950 <                if (U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
951 <                    return next;
950 >                if (U.compareAndSwapLong(this, STATE, s, ns = s + WBIT))
951 >                    return ns;
952              }
946            else if (spins < 0)
947                spins = whead == wtail ? SPINS : 0;
953              else if (spins > 0) {
954 <                if ((r = nextRandom(r)) >= 0)
954 >                if (ThreadLocalRandom.current().nextInt() >= 0)
955                      --spins;
956              }
957              else if ((p = wtail) == null) { // initialize queue
958 <                if (U.compareAndSwapObject(this, WHEAD, null,
959 <                                           new WNode(null, null)))
960 <                    wtail = whead;
958 >                WNode h = new WNode(WMODE, null);
959 >                if (U.compareAndSwapObject(this, WHEAD, null, h))
960 >                    wtail = h;
961              }
962 +            else if (spins < 0)
963 +                spins = (p == whead) ? SPINS : 0;
964              else if (node == null)
965 <                node = new WNode(Thread.currentThread(), p);
965 >                node = new WNode(WMODE, p);
966              else if (node.prev != p)
967                  node.prev = p;
968              else if (U.compareAndSwapObject(this, WTAIL, p, node)) {
969                  p.next = node;
970 <                for (int headSpins = SPINS;;) {
971 <                    WNode np, pp; int ps;
972 <                    while ((np = node.prev) != p && np != null)
973 <                        (p = np).next = node; // stale
974 <                    if (p == whead) {
975 <                        for (int k = headSpins;;) {
976 <                            if (((s = state) & ABITS) == 0L) {
977 <                                if (U.compareAndSwapLong(this, STATE,
978 <                                                         s, next = s + WBIT)) {
979 <                                    whead = node;
980 <                                    node.thread = null;
981 <                                    node.prev = null;
982 <                                    return next;
983 <                                }
984 <                                break;
970 >                break;
971 >            }
972 >        }
973 >
974 >        for (int spins = SPINS;;) {
975 >            WNode np, pp; int ps; long s, ns; Thread w;
976 >            while ((np = node.prev) != p && np != null)
977 >                (p = np).next = node;   // stale
978 >            if (whead == p) {
979 >                for (int k = spins;;) { // spin at head
980 >                    if (((s = state) & ABITS) == 0L &&
981 >                        U.compareAndSwapLong(this, STATE, s, ns = s + WBIT)) {
982 >                        whead = node;
983 >                        node.prev = null;
984 >                        return ns;
985 >                    }
986 >                    else if (ThreadLocalRandom.current().nextInt() >= 0 &&
987 >                             --k <= 0)
988 >                        break;
989 >                }
990 >                if (spins < MAX_HEAD_SPINS)
991 >                    spins <<= 1;
992 >            }
993 >            if ((ps = p.status) == 0)
994 >                U.compareAndSwapInt(p, WSTATUS, 0, WAITING);
995 >            else if (ps == CANCELLED) {
996 >                if ((pp = p.prev) != null) {
997 >                    node.prev = pp;
998 >                    pp.next = node;
999 >                }
1000 >            }
1001 >            else {
1002 >                long time; // 0 argument to park means no timeout
1003 >                if (deadline == 0L)
1004 >                    time = 0L;
1005 >                else if ((time = deadline - System.nanoTime()) <= 0L)
1006 >                    return cancelWaiter(node, null, false);
1007 >                node.thread = Thread.currentThread();
1008 >                if (node.prev == p && p.status == WAITING && // recheck
1009 >                    (p != whead || (state & ABITS) != 0L)) {
1010 >                    U.park(false, time);
1011 >                    if (interruptible && Thread.interrupted())
1012 >                        return cancelWaiter(node, null, true);
1013 >                }
1014 >                node.thread = null;
1015 >            }
1016 >        }
1017 >    }
1018 >
1019 >    /**
1020 >     * See above for explanation.
1021 >     *
1022 >     * @param interruptible true if should check interrupts and if so
1023 >     * return INTERRUPTED
1024 >     * @param deadline if nonzero, the System.nanoTime value to timeout
1025 >     * at (and return zero)
1026 >     * @return next state, or INTERRUPTED
1027 >     */
1028 >    private long acquireRead(boolean interruptible, long deadline) {
1029 >        WNode node = null, group = null, p;
1030 >        for (int spins = -1;;) {
1031 >            for (;;) {
1032 >                long s, m, ns; WNode h, q; Thread w; // anti-barging guard
1033 >                if (group == null && (h = whead) != null &&
1034 >                    (q = h.next) != null && q.mode != RMODE)
1035 >                    break;
1036 >                if ((m = (s = state) & ABITS) == WBIT)
1037 >                    break;
1038 >                if (m < RFULL ?
1039 >                    U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT) :
1040 >                    (ns = tryIncReaderOverflow(s)) != 0L) {
1041 >                    if (group != null) {  // help release others
1042 >                        for (WNode r = group;;) {
1043 >                            if ((w = r.thread) != null) {
1044 >                                r.thread = null;
1045 >                                U.unpark(w);
1046                              }
1047 <                            if ((r = nextRandom(r)) >= 0 && --k <= 0)
1047 >                            if ((r = group.cowait) == null)
1048                                  break;
1049 +                            U.compareAndSwapObject(group, WCOWAIT, r, r.cowait);
1050                          }
982                        if (headSpins < MAX_HEAD_SPINS)
983                            headSpins <<= 1;
1051                      }
1052 <                    if ((ps = p.status) == 0)
1053 <                        U.compareAndSwapInt(p, STATUS, 0, WAITING);
1054 <                    else if (ps == CANCELLED) {
1055 <                        if ((pp = p.prev) != null) {
1056 <                            node.prev = pp;
1057 <                            pp.next = node;
1058 <                        }
1059 <                    }
1060 <                    else {
1061 <                        long time; // 0 argument to park means no timeout
1052 >                    return ns;
1053 >                }
1054 >            }
1055 >            if (spins > 0) {
1056 >                if (ThreadLocalRandom.current().nextInt() >= 0)
1057 >                    --spins;
1058 >            }
1059 >            else if ((p = wtail) == null) {
1060 >                WNode h = new WNode(WMODE, null);
1061 >                if (U.compareAndSwapObject(this, WHEAD, null, h))
1062 >                    wtail = h;
1063 >            }
1064 >            else if (spins < 0)
1065 >                spins = (p == whead) ? SPINS : 0;
1066 >            else if (node == null)
1067 >                node = new WNode(WMODE, p);
1068 >            else if (node.prev != p)
1069 >                node.prev = p;
1070 >            else if (p.mode == RMODE && p != whead) {
1071 >                WNode pp = p.prev;  // become co-waiter with group p
1072 >                if (pp != null && p == wtail &&
1073 >                    U.compareAndSwapObject(p, WCOWAIT,
1074 >                                           node.cowait = p.cowait, node)) {
1075 >                    node.thread = Thread.currentThread();
1076 >                    for (long time;;) {
1077                          if (deadline == 0L)
1078                              time = 0L;
1079                          else if ((time = deadline - System.nanoTime()) <= 0L)
1080 <                            return cancelWriter(node, false);
1081 <                        if (node.prev == p && p.status == WAITING &&
1082 <                            (p != whead || (state & ABITS) != 0L)) // recheck
1083 <                            U.park(false, time);
1080 >                            return cancelWaiter(node, p, false);
1081 >                        if (node.thread == null)
1082 >                            break;
1083 >                        if (p.prev != pp || p.status == CANCELLED ||
1084 >                            p == whead || p.prev != pp) {
1085 >                            node.thread = null;
1086 >                            break;
1087 >                        }
1088 >                        if (node.thread == null) // must recheck
1089 >                            break;
1090 >                        U.park(false, time);
1091                          if (interruptible && Thread.interrupted())
1092 <                            return cancelWriter(node, true);
1092 >                            return cancelWaiter(node, p, true);
1093                      }
1094 +                    group = p;
1095                  }
1096 +                node = null; // throw away
1097 +            }
1098 +            else if (U.compareAndSwapObject(this, WTAIL, p, node)) {
1099 +                p.next = node;
1100 +                break;
1101              }
1102          }
1008    }
1103  
1104 <    /**
1105 <     * If node non-null, forces cancel status and unsplices from queue
1106 <     * if possible. This is a variant of cancellation methods in
1107 <     * AbstractQueuedSynchronizer (see its detailed explanation in AQS
1108 <     * internal documentation) that more conservatively wakes up other
1109 <     * threads that may have had their links changed, so as to preserve
1110 <     * liveness in the main signalling methods.
1111 <     */
1112 <    private long cancelWriter(WNode node, boolean interrupted) {
1113 <        if (node != null) {
1114 <            node.thread = null;
1115 <            node.status = CANCELLED;
1116 <            for (WNode pred = node.prev; pred != null; ) {
1117 <                WNode succ, pp; Thread w;
1118 <                while ((succ = node.next) == null || succ.status == CANCELLED) {
1119 <                    WNode q = null;
1120 <                    for (WNode t = wtail; t != null && t != node; t = t.prev)
1121 <                        if (t.status != CANCELLED)
1122 <                            q = t;
1123 <                    if (succ == q ||
1124 <                        U.compareAndSwapObject(node, WNEXT, succ, succ = q)) {
1125 <                        if (succ == null && node == wtail)
1032 <                            U.compareAndSwapObject(this, WTAIL, node, pred);
1033 <                        break;
1104 >        for (int spins = SPINS;;) {
1105 >            WNode np, pp, r; int ps; long m, s, ns; Thread w;
1106 >            while ((np = node.prev) != p && np != null)
1107 >                (p = np).next = node;
1108 >            if (whead == p) {
1109 >                for (int k = spins;;) {
1110 >                    if ((m = (s = state) & ABITS) != WBIT) {
1111 >                        if (m < RFULL ?
1112 >                            U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT):
1113 >                            (ns = tryIncReaderOverflow(s)) != 0L) {
1114 >                            whead = node;
1115 >                            node.prev = null;
1116 >                            while ((r = node.cowait) != null) {
1117 >                                if (U.compareAndSwapObject(node, WCOWAIT,
1118 >                                                           r, r.cowait) &&
1119 >                                    (w = r.thread) != null) {
1120 >                                    r.thread = null;
1121 >                                    U.unpark(w); // release co-waiter
1122 >                                }
1123 >                            }
1124 >                            return ns;
1125 >                        }
1126                      }
1127 +                    else if (ThreadLocalRandom.current().nextInt() >= 0 &&
1128 +                             --k <= 0)
1129 +                        break;
1130                  }
1131 <                if (pred.next == node)
1132 <                    U.compareAndSwapObject(pred, WNEXT, node, succ);
1038 <                if (succ != null && (w = succ.thread) != null)
1039 <                    U.unpark(w);
1040 <                if (pred.status != CANCELLED || (pp = pred.prev) == null)
1041 <                    break;
1042 <                node.prev = pp; // repeat for new pred
1043 <                U.compareAndSwapObject(pp, WNEXT, pred, succ);
1044 <                pred = pp;
1131 >                if (spins < MAX_HEAD_SPINS)
1132 >                    spins <<= 1;
1133              }
1134 <        }
1135 <        writerPrefSignal();
1136 <        return (interrupted || Thread.interrupted()) ? INTERRUPTED : 0L;
1137 <    }
1138 <
1139 <    /**
1052 <     * Waits for read lock or timeout or interrupt. The form of
1053 <     * awaitRead differs from awaitWrite mainly because it must
1054 <     * restart (with a new wait node) if the thread was unqueued and
1055 <     * unparked but could not the obtain lock.  We also need to help
1056 <     * with preference rules by not trying to acquire the lock before
1057 <     * enqueuing if there is a known waiting writer, but also helping
1058 <     * to release those threads that are still queued from the last
1059 <     * release.
1060 <     */
1061 <    private long awaitRead(long stamp, boolean interruptible, long deadline) {
1062 <        long seq = stamp & SBITS;
1063 <        RNode node = null;
1064 <        boolean queued = false;
1065 <        for (int r = 0, headSpins = SPINS, spins = -1;;) {
1066 <            long s, m, next; RNode p; WNode wh; Thread w;
1067 <            if ((m = (s = state) & ABITS) != WBIT &&
1068 <                ((s & SBITS) != seq || (wh = whead) == null ||
1069 <                 wh.status == 0)) {
1070 <                if (m < RFULL ?
1071 <                    U.compareAndSwapLong(this, STATE, s, next = s + RUNIT) :
1072 <                    (next = tryIncReaderOverflow(s)) != 0L) {
1073 <                    if (node != null && (w = node.waiter) != null)
1074 <                        U.compareAndSwapObject(node, WAITER, w, null);
1075 <                    if ((p = rhead) != null && (s & SBITS) != p.seq &&
1076 <                        U.compareAndSwapObject(this, RHEAD, p, p.next) &&
1077 <                        (w = p.waiter) != null &&
1078 <                        U.compareAndSwapObject(p, WAITER, w, null))
1079 <                        U.unpark(w); // help signal other waiters
1080 <                    return next;
1134 >            if ((ps = p.status) == 0)
1135 >                U.compareAndSwapInt(p, WSTATUS, 0, WAITING);
1136 >            else if (ps == CANCELLED) {
1137 >                if ((pp = p.prev) != null) {
1138 >                    node.prev = pp;
1139 >                    pp.next = node;
1140                  }
1141              }
1083            else if (m != WBIT && (p = rhead) != null &&
1084                     (s & SBITS) != p.seq) { // help release old readers
1085                if (U.compareAndSwapObject(this, RHEAD, p, p.next) &&
1086                    (w = p.waiter) != null &&
1087                    U.compareAndSwapObject(p, WAITER, w, null))
1088                    U.unpark(w);
1089            }
1090            else if (queued && node != null && node.waiter == null) {
1091                node = null;    // restart
1092                queued = false;
1093                spins = -1;
1094            }
1095            else if (spins < 0) {
1096                if (rhead != node)
1097                    spins = 0;
1098                else if ((spins = headSpins) < MAX_HEAD_SPINS && node != null)
1099                    headSpins <<= 1;
1100            }
1101            else if (spins > 0) {
1102                if ((r = nextRandom(r)) >= 0)
1103                    --spins;
1104            }
1105            else if (node == null)
1106                node = new RNode(seq, Thread.currentThread());
1107            else if (!queued) {
1108                if (queued = U.compareAndSwapObject(this, RHEAD,
1109                                                    node.next = rhead, node))
1110                    spins = -1;
1111            }
1142              else {
1143                  long time;
1144                  if (deadline == 0L)
1145                      time = 0L;
1146                  else if ((time = deadline - System.nanoTime()) <= 0L)
1147 <                    return cancelReader(node, false);
1148 <                if ((state & WBIT) != 0L && node.waiter != null) // recheck
1147 >                    return cancelWaiter(node, null, false);
1148 >                node.thread = Thread.currentThread();
1149 >                if (node.prev == p && p.status == WAITING &&
1150 >                    (p != whead || (state & ABITS) != WBIT)) {
1151                      U.park(false, time);
1152 <                if (interruptible && Thread.interrupted())
1153 <                    return cancelReader(node, true);
1152 >                    if (interruptible && Thread.interrupted())
1153 >                        return cancelWaiter(node, null, true);
1154 >                }
1155 >                node.thread = null;
1156              }
1157          }
1158      }
1159  
1160      /**
1161       * If node non-null, forces cancel status and unsplices from queue
1162 <     * if possible, by traversing entire queue looking for cancelled
1163 <     * nodes.
1162 >     * if possible. This is a variant of cancellation methods in
1163 >     * AbstractQueuedSynchronizer (see its detailed explanation in AQS
1164 >     * internal documentation) that more conservatively wakes up other
1165 >     * threads that may have had their links changed, so as to preserve
1166 >     * liveness in the main signalling methods.
1167       */
1168 <    private long cancelReader(RNode node, boolean interrupted) {
1169 <        Thread w;
1170 <        if (node != null && (w = node.waiter) != null &&
1171 <            U.compareAndSwapObject(node, WAITER, w, null)) {
1172 <            for (RNode pred = null, p = rhead; p != null;) {
1173 <                RNode q = p.next;
1174 <                if (p.waiter == null) {
1175 <                    if (pred == null) {
1176 <                        U.compareAndSwapObject(this, RHEAD, p, q);
1140 <                        p = rhead;
1141 <                    }
1142 <                    else {
1143 <                        U.compareAndSwapObject(pred, RNEXT, p, q);
1144 <                        p = pred.next;
1168 >    private long cancelWaiter(WNode node, WNode group, boolean interrupted) {
1169 >        if (node != null) {
1170 >            node.thread = null;
1171 >            node.status = CANCELLED;
1172 >            if (group != null) {
1173 >                for (WNode p = group, q; p != null; p = q) {
1174 >                    if ((q = p.cowait) != null && q.status == CANCELLED) {
1175 >                        U.compareAndSwapObject(p, WCOWAIT, q, q.cowait);
1176 >                        break;
1177                      }
1178                  }
1179 <                else {
1180 <                    pred = p;
1181 <                    p = q;
1179 >            }
1180 >            else {
1181 >                for (WNode pred = node.prev; pred != null; ) {
1182 >                    WNode succ, pp; Thread w;
1183 >                    while ((succ = node.next) == null ||
1184 >                           succ.status == CANCELLED) {
1185 >                        WNode q = null;
1186 >                        for (WNode t = wtail; t != null && t != node; t = t.prev)
1187 >                            if (t.status != CANCELLED)
1188 >                                q = t;
1189 >                        if (succ == q ||
1190 >                            U.compareAndSwapObject(node, WNEXT,
1191 >                                                   succ, succ = q)) {
1192 >                            if (succ == null && node == wtail)
1193 >                                U.compareAndSwapObject(this, WTAIL, node, pred);
1194 >                            break;
1195 >                        }
1196 >                    }
1197 >                    if (pred.next == node)
1198 >                        U.compareAndSwapObject(pred, WNEXT, node, succ);
1199 >                    if (succ != null && (w = succ.thread) != null)
1200 >                        U.unpark(w);
1201 >                    if (pred.status != CANCELLED || (pp = pred.prev) == null)
1202 >                        break;
1203 >                    node.prev = pp; // repeat for new pred
1204 >                    U.compareAndSwapObject(pp, WNEXT, pred, succ);
1205 >                    pred = pp;
1206                  }
1207              }
1208          }
1209 <        readerPrefSignal();
1209 >        release(whead);
1210          return (interrupted || Thread.interrupted()) ? INTERRUPTED : 0L;
1211      }
1212  
1213      // Unsafe mechanics
1214      private static final sun.misc.Unsafe U;
1215      private static final long STATE;
1160    private static final long RHEAD;
1216      private static final long WHEAD;
1217      private static final long WTAIL;
1163    private static final long RNEXT;
1218      private static final long WNEXT;
1219 <    private static final long WPREV;
1220 <    private static final long WAITER;
1167 <    private static final long STATUS;
1219 >    private static final long WSTATUS;
1220 >    private static final long WCOWAIT;
1221  
1222      static {
1223          try {
1224              U = getUnsafe();
1225              Class<?> k = StampedLock.class;
1173            Class<?> rk = RNode.class;
1226              Class<?> wk = WNode.class;
1227              STATE = U.objectFieldOffset
1228                  (k.getDeclaredField("state"));
1177            RHEAD = U.objectFieldOffset
1178                (k.getDeclaredField("rhead"));
1229              WHEAD = U.objectFieldOffset
1230                  (k.getDeclaredField("whead"));
1231              WTAIL = U.objectFieldOffset
1232                  (k.getDeclaredField("wtail"));
1233 <            RNEXT = U.objectFieldOffset
1184 <                (rk.getDeclaredField("next"));
1185 <            WAITER = U.objectFieldOffset
1186 <                (rk.getDeclaredField("waiter"));
1187 <            STATUS = U.objectFieldOffset
1233 >            WSTATUS = U.objectFieldOffset
1234                  (wk.getDeclaredField("status"));
1235              WNEXT = U.objectFieldOffset
1236                  (wk.getDeclaredField("next"));
1237 <            WPREV = U.objectFieldOffset
1238 <                (wk.getDeclaredField("prev"));
1237 >            WCOWAIT = U.objectFieldOffset
1238 >                (wk.getDeclaredField("cowait"));
1239  
1240          } catch (Exception e) {
1241              throw new Error(e);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines