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.7 by dl, Fri Oct 12 23:11:14 2012 UTC vs.
Revision 1.39 by jsr166, Sun Jan 18 20:17:33 2015 UTC

# Line 6 | Line 6
6  
7   package jsr166e;
8  
9 import java.util.concurrent.ThreadLocalRandom;
9   import java.util.concurrent.TimeUnit;
10 + import java.util.concurrent.locks.Lock;
11 + import java.util.concurrent.locks.Condition;
12 + import java.util.concurrent.locks.ReadWriteLock;
13 + import java.util.concurrent.locks.LockSupport;
14  
15   /**
16   * A capability-based lock with three modes for controlling read/write
17 < * access.  The state of a StampedLock consists of a version and
18 < * mode. Lock acquisition methods return a stamp that represents and
17 > * access.  The state of a StampedLock consists of a version and mode.
18 > * Lock acquisition methods return a stamp that represents and
19   * controls access with respect to a lock state; "try" versions of
20   * these methods may instead return the special value zero to
21   * represent failure to acquire access. Lock release and conversion
# Line 36 | Line 39 | import java.util.concurrent.TimeUnit;
39   *  <li><b>Optimistic Reading.</b> Method {@link #tryOptimisticRead}
40   *   returns a non-zero stamp only if the lock is not currently held
41   *   in write mode. Method {@link #validate} returns true if the lock
42 < *   has not since been acquired in write mode. This mode can be
43 < *   thought of as an extremely weak version of a read-lock, that can
44 < *   be broken by a writer at any time.  The use of optimistic mode
45 < *   for short read-only code segments often reduces contention and
46 < *   improves throughput.  However, its use is inherently fragile.
47 < *   Optimistic read sections should only read fields and hold them in
48 < *   local variables for later use after validation. Fields read while
49 < *   in optimistic mode may be wildly inconsistent, so usage applies
50 < *   only when you are familiar enough with data representations to
51 < *   check consistency and/or repeatedly invoke method {@code
52 < *   validate()}.  For example, such steps are typically required when
53 < *   first reading an object or array reference, and then accessing
54 < *   one of its fields, elements or methods. </li>
42 > *   has not been acquired in write mode since obtaining a given
43 > *   stamp.  This mode can be thought of as an extremely weak version
44 > *   of a read-lock, that can be broken by a writer at any time.  The
45 > *   use of optimistic mode for short read-only code segments often
46 > *   reduces contention and improves throughput.  However, its use is
47 > *   inherently fragile.  Optimistic read sections should only read
48 > *   fields and hold them in local variables for later use after
49 > *   validation. Fields read while in optimistic mode may be wildly
50 > *   inconsistent, so usage applies only when you are familiar enough
51 > *   with data representations to check consistency and/or repeatedly
52 > *   invoke method {@code validate()}.  For example, such steps are
53 > *   typically required when first reading an object or array
54 > *   reference, and then accessing one of its fields, elements or
55 > *   methods. </li>
56   *
57   * </ul>
58   *
59   * <p>This class also supports methods that conditionally provide
60   * conversions across the three modes. For example, method {@link
61   * #tryConvertToWriteLock} attempts to "upgrade" a mode, returning
62 < * valid write stamp if (1) already in writing mode (2) in reading
62 > * a valid write stamp if (1) already in writing mode (2) in reading
63   * mode and there are no other readers or (3) in optimistic mode and
64   * the lock is available. The forms of these methods are designed to
65   * help reduce some of the code bloat that otherwise occurs in
66   * retry-based designs.
67   *
68 < * <p>StampedLocks are designed for use in a different (and generally
69 < * narrower) range of contexts than most other locks: They are not
70 < * reentrant, so locked bodies should not call other unknown methods
71 < * that may try to re-acquire locks (although you may pass a stamp to
72 < * other methods that can use or convert it). Unvalidated optimistic
73 < * read sections should further not call methods that are not known to
68 > * <p>StampedLocks are designed for use as internal utilities in the
69 > * development of thread-safe components. Their use relies on
70 > * knowledge of the internal properties of the data, objects, and
71 > * methods they are protecting.  They are not reentrant, so locked
72 > * bodies should not call other unknown methods that may try to
73 > * re-acquire locks (although you may pass a stamp to other methods
74 > * that can use or convert it).  The use of read lock modes relies on
75 > * the associated code sections being side-effect-free.  Unvalidated
76 > * optimistic read sections cannot call methods that are not known to
77   * tolerate potential inconsistencies.  Stamps use finite
78   * representations, and are not cryptographically secure (i.e., a
79   * valid stamp may be guessable). Stamp values may recycle after (no
# Line 77 | Line 84 | import java.util.concurrent.TimeUnit;
84   * locking.
85   *
86   * <p>The scheduling policy of StampedLock does not consistently
87 < * prefer readers over writers or vice versa.  A zero return from any
88 < * "try" method for acquiring or converting locks does carry any
89 < * information about the state of the lock; a subsequent invocation
90 < * may succeed.
87 > * prefer readers over writers or vice versa.  All "try" methods are
88 > * best-effort and do not necessarily conform to any scheduling or
89 > * fairness policy. A zero return from any "try" method for acquiring
90 > * or converting locks does not carry any information about the state
91 > * of the lock; a subsequent invocation may succeed.
92 > *
93 > * <p>Because it supports coordinated usage across multiple lock
94 > * modes, this class does not directly implement the {@link Lock} or
95 > * {@link ReadWriteLock} interfaces. However, a StampedLock may be
96 > * viewed {@link #asReadLock()}, {@link #asWriteLock()}, or {@link
97 > * #asReadWriteLock()} in applications requiring only the associated
98 > * set of functionality.
99   *
100   * <p><b>Sample Usage.</b> The following illustrates some usage idioms
101   * in a class that maintains simple two-dimensional points. The sample
# Line 103 | Line 118 | import java.util.concurrent.TimeUnit;
118   *     }
119   *   }
120   *
121 < *   double distanceFromOriginV1() { // A read-only method
122 < *     long stamp;
123 < *     if ((stamp = sl.tryOptimisticRead()) != 0L) { // optimistic
124 < *       double currentX = x;
125 < *       double currentY = y;
126 < *       if (sl.validate(stamp))
127 < *         return Math.sqrt(currentX * currentX + currentY * currentY);
128 < *     }
129 < *     stamp = sl.readLock(); // fall back to read lock
130 < *     try {
131 < *       double currentX = x;
117 < *       double currentY = y;
118 < *         return Math.sqrt(currentX * currentX + currentY * currentY);
119 < *     } finally {
120 < *       sl.unlockRead(stamp);
121 < *     }
122 < *   }
123 < *
124 < *   double distanceFromOriginV2() { // combines code paths
125 < *     for (long stamp = sl.tryOptimisticRead(); ; stamp = sl.readLock()) {
126 < *       double currentX, currentY;
127 < *       try {
128 < *         currentX = x;
129 < *         currentY = y;
130 < *       } finally {
131 < *         if (sl.tryConvertToOptimisticRead(stamp) != 0L) // unlock or validate
132 < *           return Math.sqrt(currentX * currentX + currentY * currentY);
133 < *       }
121 > *   double distanceFromOrigin() { // A read-only method
122 > *     long stamp = sl.tryOptimisticRead();
123 > *     double currentX = x, currentY = y;
124 > *     if (!sl.validate(stamp)) {
125 > *        stamp = sl.readLock();
126 > *        try {
127 > *          currentX = x;
128 > *          currentY = y;
129 > *        } finally {
130 > *           sl.unlockRead(stamp);
131 > *        }
132   *     }
133 + *     return Math.sqrt(currentX * currentX + currentY * currentY);
134   *   }
135   *
136   *   void moveIfAtOrigin(double newX, double newY) { // upgrade
# Line 139 | Line 138 | import java.util.concurrent.TimeUnit;
138   *     long stamp = sl.readLock();
139   *     try {
140   *       while (x == 0.0 && y == 0.0) {
141 < *         long ws = tryConvertToWriteLock(stamp);
141 > *         long ws = sl.tryConvertToWriteLock(stamp);
142   *         if (ws != 0L) {
143   *           stamp = ws;
144   *           x = newX;
# Line 152 | Line 151 | import java.util.concurrent.TimeUnit;
151   *         }
152   *       }
153   *     } finally {
154 < *        sl.unlock(stamp);
154 > *       sl.unlock(stamp);
155   *     }
156   *   }
157   * }}</pre>
# Line 169 | Line 168 | public class StampedLock implements java
168       * http://www.lameter.com/gelato2005.pdf
169       * and elsewhere; see
170       * Boehm's http://www.hpl.hp.com/techreports/2012/HPL-2012-68.html)
171 <     * Ordered RW locks (see Shirako et al
171 >     * and Ordered RW locks (see Shirako et al
172       * 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/).
173       *
174       * Conceptually, the primary state of the lock includes a sequence
175       * number that is odd when write-locked and even otherwise.
# Line 180 | Line 177 | public class StampedLock implements java
177       * read-locked.  The read count is ignored when validating
178       * "optimistic" seqlock-reader-style stamps.  Because we must use
179       * a small finite number of bits (currently 7) for readers, a
180 <     * supplementary reader overflow word is used when then number of
180 >     * supplementary reader overflow word is used when the number of
181       * readers exceeds the count field. We do this by treating the max
182       * reader count value (RBITS) as a spinlock protecting overflow
183       * updates.
184       *
185 <     * Waiting readers and writers use different queues. The writer
186 <     * queue is a modified form of CLH lock.  (For discussion of CLH,
187 <     * see the internal documentation of AbstractQueuedSynchronizer.)
188 <     * The reader "queue" is a form of Treiber stack, that supports
189 <     * simpler/faster operations because order within a queue doesn't
190 <     * matter and all are signalled at once.  However the sequence of
191 <     * threads within the queue vs the current stamp does matter (see
192 <     * Shirako et al) so each carries its incoming stamp value.
193 <     * Waiting writers never need to track sequence values, so they
194 <     * don't.
195 <     *
196 <     * These queue mechanics hardwire the scheduling policy.  Ignoring
197 <     * trylocks, cancellation, and spinning, they implement Phase-Fair
198 <     * preferences:
199 <     *   1. Unlocked writers prefer to signal waiting readers
200 <     *   2. Fully unlocked readers prefer to signal waiting writers
201 <     *   3. When read-locked and a waiting writer exists, the writer
202 <     *      is preferred to incoming readers
185 >     * Waiters use a modified form of CLH lock used in
186 >     * AbstractQueuedSynchronizer (see its internal documentation for
187 >     * a fuller account), where each node is tagged (field mode) as
188 >     * either a reader or writer. Sets of waiting readers are grouped
189 >     * (linked) under a common node (field cowait) so act as a single
190 >     * node with respect to most CLH mechanics.  By virtue of the
191 >     * queue structure, wait nodes need not actually carry sequence
192 >     * numbers; we know each is greater than its predecessor.  This
193 >     * simplifies the scheduling policy to a mainly-FIFO scheme that
194 >     * incorporates elements of Phase-Fair locks (see Brandenburg &
195 >     * Anderson, especially http://www.cs.unc.edu/~bbb/diss/).  In
196 >     * particular, we use the phase-fair anti-barging rule: If an
197 >     * incoming reader arrives while read lock is held but there is a
198 >     * queued writer, this incoming reader is queued.  (This rule is
199 >     * responsible for some of the complexity of method acquireRead,
200 >     * but without it, the lock becomes highly unfair.) Method release
201 >     * does not (and sometimes cannot) itself wake up cowaiters. This
202 >     * is done by the primary thread, but helped by any other threads
203 >     * with nothing better to do in methods acquireRead and
204 >     * acquireWrite.
205       *
206       * These rules apply to threads actually queued. All tryLock forms
207       * opportunistically try to acquire locks regardless of preference
208 <     * rules, and so may "barge" their way in.  Additionally, initial
209 <     * phases of the await* methods (invoked from readLock() and
210 <     * writeLock()) use controlled spins that have similar effect.
211 <     * Phase-fair preferences may also be broken on cancellations due
212 <     * to timeouts and interrupts.  Rule #3 (incoming readers when a
213 <     * waiting writer) is approximated with varying precision in
214 <     * different contexts -- some checks do not account for
215 <     * in-progress spins/signals, and others do not account for
216 <     * cancellations.
208 >     * rules, and so may "barge" their way in.  Randomized spinning is
209 >     * used in the acquire methods to reduce (increasingly expensive)
210 >     * context switching while also avoiding sustained memory
211 >     * thrashing among many threads.  We limit spins to the head of
212 >     * queue. A thread spin-waits up to SPINS times (where each
213 >     * iteration decreases spin count with 50% probability) before
214 >     * blocking. If, upon wakening it fails to obtain lock, and is
215 >     * still (or becomes) the first waiting thread (which indicates
216 >     * that some other thread barged and obtained lock), it escalates
217 >     * spins (up to MAX_HEAD_SPINS) to reduce the likelihood of
218 >     * continually losing to barging threads.
219 >     *
220 >     * Nearly all of these mechanics are carried out in methods
221 >     * acquireWrite and acquireRead, that, as typical of such code,
222 >     * sprawl out because actions and retries rely on consistent sets
223 >     * of locally cached reads.
224       *
225       * As noted in Boehm's paper (above), sequence validation (mainly
226       * method validate()) requires stricter ordering rules than apply
# Line 234 | Line 240 | public class StampedLock implements java
240       * be subject to future improvements.
241       */
242  
243 +    private static final long serialVersionUID = -6001602636862214147L;
244 +
245      /** Number of processors, for spin control */
246      private static final int NCPU = Runtime.getRuntime().availableProcessors();
247  
248 <    /** Maximum number of retries before blocking on acquisition */
249 <    private static final int SPINS = (NCPU > 1) ? 1 << 6 : 1;
248 >    /** Maximum number of retries before enqueuing on acquisition */
249 >    private static final int SPINS = (NCPU > 1) ? 1 << 6 : 0;
250 >
251 >    /** Maximum number of retries before blocking at head on acquisition */
252 >    private static final int HEAD_SPINS = (NCPU > 1) ? 1 << 10 : 0;
253  
254      /** Maximum number of retries before re-blocking */
255 <    private static final int MAX_HEAD_SPINS = (NCPU > 1) ? 1 << 12 : 1;
255 >    private static final int MAX_HEAD_SPINS = (NCPU > 1) ? 1 << 16 : 0;
256  
257      /** The period for yielding when waiting for overflow spinlock */
258      private static final int OVERFLOW_YIELD_RATE = 7; // must be power 2 - 1
259  
260      /** The number of bits to use for reader count before overflowing */
261 <    private static final int  LG_READERS = 7;
261 >    private static final int LG_READERS = 7;
262  
263      // Values for lock state and stamp operations
264      private static final long RUNIT = 1L;
# Line 260 | Line 271 | public class StampedLock implements java
271      // Initial value for lock state; avoid failure value zero
272      private static final long ORIGIN = WBIT << 1;
273  
274 <    // Special value from cancelled await methods so caller can throw IE
274 >    // Special value from cancelled acquire methods so caller can throw IE
275      private static final long INTERRUPTED = 1L;
276  
277 <    // Values for writer status; order matters
277 >    // Values for node status; order matters
278      private static final int WAITING   = -1;
279      private static final int CANCELLED =  1;
280  
281 <    /** Wait nodes for readers */
282 <    static final class RNode {
283 <        final long seq;         // stamp value upon enqueue
273 <        volatile Thread waiter; // null if no longer waiting
274 <        volatile RNode next;
275 <        RNode(long s, Thread w) { seq = s; waiter = w; }
276 <    }
281 >    // Modes for nodes (int not boolean to allow arithmetic)
282 >    private static final int RMODE = 0;
283 >    private static final int WMODE = 1;
284  
285 <    /** Wait nodes for writers */
285 >    /** Wait nodes */
286      static final class WNode {
280        volatile int status;   // 0, WAITING, or CANCELLED
287          volatile WNode prev;
288          volatile WNode next;
289 <        volatile Thread thread;
290 <        WNode(Thread t, WNode p) { thread = t; prev = p; }
289 >        volatile WNode cowait;    // list of linked readers
290 >        volatile Thread thread;   // non-null while possibly parked
291 >        volatile int status;      // 0, WAITING, or CANCELLED
292 >        final int mode;           // RMODE or WMODE
293 >        WNode(int m, WNode p) { mode = m; prev = p; }
294      }
295  
296 <    /** Head of writer CLH queue */
296 >    /** Head of CLH queue */
297      private transient volatile WNode whead;
298 <    /** Tail (last) of writer CLH queue */
298 >    /** Tail (last) of CLH queue */
299      private transient volatile WNode wtail;
300 <    /** Head of read queue  */
301 <    private transient volatile RNode rhead;
302 <    /** The state of the lock -- high bits hold sequence, low bits read count */
300 >
301 >    // views
302 >    transient ReadLockView readLockView;
303 >    transient WriteLockView writeLockView;
304 >    transient ReadWriteLockView readWriteLockView;
305 >
306 >    /** Lock sequence/state */
307      private transient volatile long state;
308      /** extra reader count when state read count saturated */
309      private transient int readerOverflow;
310  
311      /**
312 <     * Creates a new lock initially in unlocked state.
312 >     * Creates a new lock, initially in unlocked state.
313       */
314      public StampedLock() {
315          state = ORIGIN;
# Line 309 | Line 322 | public class StampedLock implements java
322       * @return a stamp that can be used to unlock or convert mode
323       */
324      public long writeLock() {
325 <        long s, next;
326 <        if (((s = state) & ABITS) == 0L &&
327 <            U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
328 <            return next;
316 <        return awaitWrite(false, 0L);
325 >        long s, next;  // bypass acquireWrite in fully unlocked case only
326 >        return ((((s = state) & ABITS) == 0L &&
327 >                 U.compareAndSwapLong(this, STATE, s, next = s + WBIT)) ?
328 >                next : acquireWrite(false, 0L));
329      }
330  
331      /**
332       * Exclusively acquires the lock if it is immediately available.
333       *
334       * @return a stamp that can be used to unlock or convert mode,
335 <     * or zero if the lock is not available.
335 >     * or zero if the lock is not available
336       */
337      public long tryWriteLock() {
338          long s, next;
339 <        if (((s = state) & ABITS) == 0L &&
340 <            U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
341 <            return next;
330 <        return 0L;
339 >        return ((((s = state) & ABITS) == 0L &&
340 >                 U.compareAndSwapLong(this, STATE, s, next = s + WBIT)) ?
341 >                next : 0L);
342      }
343  
344      /**
345       * Exclusively acquires the lock if it is available within the
346       * given time and the current thread has not been interrupted.
347 +     * Behavior under timeout and interruption matches that specified
348 +     * for method {@link Lock#tryLock(long,TimeUnit)}.
349       *
350 +     * @param time the maximum time to wait for the lock
351 +     * @param unit the time unit of the {@code time} argument
352       * @return a stamp that can be used to unlock or convert mode,
353       * or zero if the lock is not available
354       * @throws InterruptedException if the current thread is interrupted
# Line 343 | Line 358 | public class StampedLock implements java
358          throws InterruptedException {
359          long nanos = unit.toNanos(time);
360          if (!Thread.interrupted()) {
361 <            long s, next, deadline;
362 <            if (((s = state) & ABITS) == 0L &&
348 <                U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
361 >            long next, deadline;
362 >            if ((next = tryWriteLock()) != 0L)
363                  return next;
364              if (nanos <= 0L)
365                  return 0L;
366              if ((deadline = System.nanoTime() + nanos) == 0L)
367                  deadline = 1L;
368 <            if ((next = awaitWrite(true, deadline)) != INTERRUPTED)
368 >            if ((next = acquireWrite(true, deadline)) != INTERRUPTED)
369                  return next;
370          }
371          throw new InterruptedException();
# Line 360 | Line 374 | public class StampedLock implements java
374      /**
375       * Exclusively acquires the lock, blocking if necessary
376       * until available or the current thread is interrupted.
377 +     * Behavior under interruption matches that specified
378 +     * for method {@link Lock#lockInterruptibly()}.
379       *
380       * @return a stamp that can be used to unlock or convert mode
381       * @throws InterruptedException if the current thread is interrupted
382       * before acquiring the lock
383       */
384      public long writeLockInterruptibly() throws InterruptedException {
385 <        if (!Thread.interrupted()) {
386 <            long s, next;
387 <            if (((s = state) & ABITS) == 0L &&
388 <                U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
373 <                return next;
374 <            if ((next = awaitWrite(true, 0L)) != INTERRUPTED)
375 <                return next;
376 <        }
385 >        long next;
386 >        if (!Thread.interrupted() &&
387 >            (next = acquireWrite(true, 0L)) != INTERRUPTED)
388 >            return next;
389          throw new InterruptedException();
390      }
391  
# Line 384 | Line 396 | public class StampedLock implements java
396       * @return a stamp that can be used to unlock or convert mode
397       */
398      public long readLock() {
399 <        for (;;) {
400 <            long s, m, next;
401 <            if ((m = (s = state) & ABITS) == 0L ||
402 <                (m < WBIT && whead == wtail)) {
391 <                if (m < RFULL) {
392 <                    if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT))
393 <                        return next;
394 <                }
395 <                else if ((next = tryIncReaderOverflow(s)) != 0L)
396 <                    return next;
397 <            }
398 <            else
399 <                return awaitRead(s, false, 0L);
400 <        }
399 >        long s = state, next;  // bypass acquireRead on common uncontended case
400 >        return ((whead == wtail && (s & ABITS) < RFULL &&
401 >                 U.compareAndSwapLong(this, STATE, s, next = s + RUNIT)) ?
402 >                next : acquireRead(false, 0L));
403      }
404  
405      /**
# Line 423 | Line 425 | public class StampedLock implements java
425      /**
426       * Non-exclusively acquires the lock if it is available within the
427       * given time and the current thread has not been interrupted.
428 +     * Behavior under timeout and interruption matches that specified
429 +     * for method {@link Lock#tryLock(long,TimeUnit)}.
430       *
431 +     * @param time the maximum time to wait for the lock
432 +     * @param unit the time unit of the {@code time} argument
433       * @return a stamp that can be used to unlock or convert mode,
434       * or zero if the lock is not available
435       * @throws InterruptedException if the current thread is interrupted
# Line 431 | Line 437 | public class StampedLock implements java
437       */
438      public long tryReadLock(long time, TimeUnit unit)
439          throws InterruptedException {
440 +        long s, m, next, deadline;
441          long nanos = unit.toNanos(time);
442          if (!Thread.interrupted()) {
443 <            for (;;) {
444 <                long s, m, next, deadline;
438 <                if ((m = (s = state) & ABITS) == WBIT ||
439 <                    (m != 0L && whead != wtail)) {
440 <                    if (nanos <= 0L)
441 <                        return 0L;
442 <                    if ((deadline = System.nanoTime() + nanos) == 0L)
443 <                        deadline = 1L;
444 <                    if ((next = awaitRead(s, true, deadline)) != INTERRUPTED)
445 <                        return next;
446 <                    break;
447 <                }
448 <                else if (m < RFULL) {
443 >            if ((m = (s = state) & ABITS) != WBIT) {
444 >                if (m < RFULL) {
445                      if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT))
446                          return next;
447                  }
448                  else if ((next = tryIncReaderOverflow(s)) != 0L)
449                      return next;
450              }
451 +            if (nanos <= 0L)
452 +                return 0L;
453 +            if ((deadline = System.nanoTime() + nanos) == 0L)
454 +                deadline = 1L;
455 +            if ((next = acquireRead(true, deadline)) != INTERRUPTED)
456 +                return next;
457          }
458          throw new InterruptedException();
459      }
# Line 459 | Line 461 | public class StampedLock implements java
461      /**
462       * Non-exclusively acquires the lock, blocking if necessary
463       * until available or the current thread is interrupted.
464 +     * Behavior under interruption matches that specified
465 +     * for method {@link Lock#lockInterruptibly()}.
466       *
467       * @return a stamp that can be used to unlock or convert mode
468       * @throws InterruptedException if the current thread is interrupted
469       * before acquiring the lock
470       */
471      public long readLockInterruptibly() throws InterruptedException {
472 <        if (!Thread.interrupted()) {
473 <            for (;;) {
474 <                long s, next, m;
475 <                if ((m = (s = state) & ABITS) == WBIT ||
472 <                    (m != 0L && whead != wtail)) {
473 <                    if ((next = awaitRead(s, true, 0L)) != INTERRUPTED)
474 <                        return next;
475 <                    break;
476 <                }
477 <                else if (m < RFULL) {
478 <                    if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT))
479 <                        return next;
480 <                }
481 <                else if ((next = tryIncReaderOverflow(s)) != 0L)
482 <                    return next;
483 <            }
484 <        }
472 >        long next;
473 >        if (!Thread.interrupted() &&
474 >            (next = acquireRead(true, 0L)) != INTERRUPTED)
475 >            return next;
476          throw new InterruptedException();
477      }
478  
# Line 497 | Line 488 | public class StampedLock implements java
488      }
489  
490      /**
491 <     * Returns true if the lock has not been exclusively held since
492 <     * issuance of the given stamp. Always returns false if the stamp
493 <     * is zero. Always returns true if the stamp represents a
494 <     * currently held lock.
491 >     * Returns true if the lock has not been exclusively acquired
492 >     * since issuance of the given stamp. Always returns false if the
493 >     * stamp is zero. Always returns true if the stamp represents a
494 >     * currently held lock. Invoking this method with a value not
495 >     * obtained from {@link #tryOptimisticRead} or a locking method
496 >     * for this lock has no defined effect or result.
497       *
498 <     * @return true if the lock has not been exclusively held since
499 <     * issuance of the given stamp; else false
498 >     * @param stamp a stamp
499 >     * @return {@code true} if the lock has not been exclusively acquired
500 >     * since issuance of the given stamp; else false
501       */
502      public boolean validate(long stamp) {
503 +        // See above about current use of getLongVolatile here
504          return (stamp & SBITS) == (U.getLongVolatile(this, STATE) & SBITS);
505      }
506  
# Line 518 | Line 513 | public class StampedLock implements java
513       * not match the current state of this lock
514       */
515      public void unlockWrite(long stamp) {
516 +        WNode h;
517          if (state != stamp || (stamp & WBIT) == 0L)
518              throw new IllegalMonitorStateException();
519          state = (stamp += WBIT) == 0L ? ORIGIN : stamp;
520 <        readerPrefSignal();
520 >        if ((h = whead) != null && h.status != 0)
521 >            release(h);
522      }
523  
524      /**
525 <     * If the lock state matches the given stamp, releases
525 >     * If the lock state matches the given stamp, releases the
526       * non-exclusive lock.
527       *
528       * @param stamp a stamp returned by a read-lock operation
# Line 533 | Line 530 | public class StampedLock implements java
530       * not match the current state of this lock
531       */
532      public void unlockRead(long stamp) {
533 <        long s, m;
534 <        if ((stamp & RBITS) != 0L) {
535 <            while (((s = state) & SBITS) == (stamp & SBITS)) {
536 <                if ((m = s & ABITS) == 0L)
533 >        long s, m; WNode h;
534 >        for (;;) {
535 >            if (((s = state) & SBITS) != (stamp & SBITS) ||
536 >                (stamp & ABITS) == 0L || (m = s & ABITS) == 0L || m == WBIT)
537 >                throw new IllegalMonitorStateException();
538 >            if (m < RFULL) {
539 >                if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) {
540 >                    if (m == RUNIT && (h = whead) != null && h.status != 0)
541 >                        release(h);
542                      break;
541                else if (m < RFULL) {
542                    if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) {
543                        if (m == RUNIT)
544                            writerPrefSignal();
545                        return;
546                    }
543                  }
548                else if (m >= WBIT)
549                    break;
550                else if (tryDecReaderOverflow(s) != 0L)
551                    return;
544              }
545 +            else if (tryDecReaderOverflow(s) != 0L)
546 +                break;
547          }
554        throw new IllegalMonitorStateException();
548      }
549  
550      /**
# Line 563 | Line 556 | public class StampedLock implements java
556       * not match the current state of this lock
557       */
558      public void unlock(long stamp) {
559 <        long a = stamp & ABITS, m, s;
559 >        long a = stamp & ABITS, m, s; WNode h;
560          while (((s = state) & SBITS) == (stamp & SBITS)) {
561              if ((m = s & ABITS) == 0L)
562                  break;
# Line 571 | Line 564 | public class StampedLock implements java
564                  if (a != m)
565                      break;
566                  state = (s += WBIT) == 0L ? ORIGIN : s;
567 <                readerPrefSignal();
567 >                if ((h = whead) != null && h.status != 0)
568 >                    release(h);
569                  return;
570              }
571              else if (a == 0L || a >= WBIT)
572                  break;
573              else if (m < RFULL) {
574                  if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) {
575 <                    if (m == RUNIT)
576 <                        writerPrefSignal();
575 >                    if (m == RUNIT && (h = whead) != null && h.status != 0)
576 >                        release(h);
577                      return;
578                  }
579              }
# Line 590 | Line 584 | public class StampedLock implements java
584      }
585  
586      /**
587 <     * If the lock state matches the given stamp then performs one of
587 >     * If the lock state matches the given stamp, performs one of
588       * the following actions. If the stamp represents holding a write
589 <     * lock, returns it. Or, if a read lock, if the write lock is
590 <     * available, releases the read and returns a write stamp. Or, if
591 <     * an optimistic read, returns a write stamp only if immediately
592 <     * available. This method returns zero in all other cases.
589 >     * lock, returns it.  Or, if a read lock, if the write lock is
590 >     * available, releases the read lock and returns a write stamp.
591 >     * Or, if an optimistic read, returns a write stamp only if
592 >     * immediately available. This method returns zero in all other
593 >     * cases.
594       *
595       * @param stamp a stamp
596       * @return a valid write stamp, or zero on failure
# Line 614 | Line 609 | public class StampedLock implements java
609                      break;
610                  return stamp;
611              }
612 <            else if (m == RUNIT && a != 0L && a < WBIT) {
612 >            else if (m == RUNIT && a != 0L) {
613                  if (U.compareAndSwapLong(this, STATE, s,
614                                           next = s - RUNIT + WBIT))
615                      return next;
# Line 626 | Line 621 | public class StampedLock implements java
621      }
622  
623      /**
624 <     * If the lock state matches the given stamp then performs one of
624 >     * If the lock state matches the given stamp, performs one of
625       * the following actions. If the stamp represents holding a write
626       * lock, releases it and obtains a read lock.  Or, if a read lock,
627       * returns it. Or, if an optimistic read, acquires a read lock and
# Line 637 | Line 632 | public class StampedLock implements java
632       * @return a valid read stamp, or zero on failure
633       */
634      public long tryConvertToReadLock(long stamp) {
635 <        long a = stamp & ABITS, m, s, next;
635 >        long a = stamp & ABITS, m, s, next; WNode h;
636          while (((s = state) & SBITS) == (stamp & SBITS)) {
637              if ((m = s & ABITS) == 0L) {
638                  if (a != 0L)
# Line 652 | Line 647 | public class StampedLock implements java
647              else if (m == WBIT) {
648                  if (a != m)
649                      break;
650 <                next = state = s + (WBIT + RUNIT);
651 <                readerPrefSignal();
650 >                state = next = s + (WBIT + RUNIT);
651 >                if ((h = whead) != null && h.status != 0)
652 >                    release(h);
653                  return next;
654              }
655              else if (a != 0L && a < WBIT)
# Line 675 | Line 671 | public class StampedLock implements java
671       * @return a valid optimistic read stamp, or zero on failure
672       */
673      public long tryConvertToOptimisticRead(long stamp) {
674 <        long a = stamp & ABITS, m, s, next;
675 <        while (((s = U.getLongVolatile(this, STATE)) &
676 <                SBITS) == (stamp & SBITS)) {
674 >        long a = stamp & ABITS, m, s, next; WNode h;
675 >        for (;;) {
676 >            s = U.getLongVolatile(this, STATE); // see above
677 >            if (((s = state) & SBITS) != (stamp & SBITS))
678 >                break;
679              if ((m = s & ABITS) == 0L) {
680                  if (a != 0L)
681                      break;
# Line 686 | Line 684 | public class StampedLock implements java
684              else if (m == WBIT) {
685                  if (a != m)
686                      break;
687 <                next = state = (s += WBIT) == 0L ? ORIGIN : s;
688 <                readerPrefSignal();
687 >                state = next = (s += WBIT) == 0L ? ORIGIN : s;
688 >                if ((h = whead) != null && h.status != 0)
689 >                    release(h);
690                  return next;
691              }
692              else if (a == 0L || a >= WBIT)
693                  break;
694              else if (m < RFULL) {
695                  if (U.compareAndSwapLong(this, STATE, s, next = s - RUNIT)) {
696 <                    if (m == RUNIT)
697 <                        writerPrefSignal();
696 >                    if (m == RUNIT && (h = whead) != null && h.status != 0)
697 >                        release(h);
698                      return next & SBITS;
699                  }
700              }
# Line 710 | Line 709 | public class StampedLock implements java
709       * stamp value. This method may be useful for recovery after
710       * errors.
711       *
712 <     * @return true if the lock was held, else false
712 >     * @return {@code true} if the lock was held, else false
713       */
714      public boolean tryUnlockWrite() {
715 <        long s;
715 >        long s; WNode h;
716          if (((s = state) & WBIT) != 0L) {
717              state = (s += WBIT) == 0L ? ORIGIN : s;
718 <            readerPrefSignal();
718 >            if ((h = whead) != null && h.status != 0)
719 >                release(h);
720              return true;
721          }
722          return false;
# Line 727 | Line 727 | public class StampedLock implements java
727       * requiring a stamp value. This method may be useful for recovery
728       * after errors.
729       *
730 <     * @return true if the read lock was held, else false
730 >     * @return {@code true} if the read lock was held, else false
731       */
732      public boolean tryUnlockRead() {
733 <        long s, m;
733 >        long s, m; WNode h;
734          while ((m = (s = state) & ABITS) != 0L && m < WBIT) {
735              if (m < RFULL) {
736                  if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) {
737 <                    if (m == RUNIT)
738 <                        writerPrefSignal();
737 >                    if (m == RUNIT && (h = whead) != null && h.status != 0)
738 >                        release(h);
739                      return true;
740                  }
741              }
# Line 745 | Line 745 | public class StampedLock implements java
745          return false;
746      }
747  
748 +    // status monitoring methods
749 +
750 +    /**
751 +     * Returns combined state-held and overflow read count for given
752 +     * state s.
753 +     */
754 +    private int getReadLockCount(long s) {
755 +        long readers;
756 +        if ((readers = s & RBITS) >= RFULL)
757 +            readers = RFULL + readerOverflow;
758 +        return (int) readers;
759 +    }
760 +
761      /**
762 <     * Returns true if the lock is currently held exclusively.
762 >     * Returns {@code true} if the lock is currently held exclusively.
763       *
764 <     * @return true if the lock is currently held exclusively
764 >     * @return {@code true} if the lock is currently held exclusively
765       */
766      public boolean isWriteLocked() {
767          return (state & WBIT) != 0L;
768      }
769  
770      /**
771 <     * Returns true if the lock is currently held non-exclusively.
771 >     * Returns {@code true} if the lock is currently held non-exclusively.
772       *
773 <     * @return true if the lock is currently held non-exclusively
773 >     * @return {@code true} if the lock is currently held non-exclusively
774       */
775      public boolean isReadLocked() {
776 <        long m;
777 <        return (m = state & ABITS) > 0L && m < WBIT;
776 >        return (state & RBITS) != 0L;
777 >    }
778 >
779 >    /**
780 >     * Queries the number of read locks held for this lock. This
781 >     * method is designed for use in monitoring system state, not for
782 >     * synchronization control.
783 >     * @return the number of read locks held
784 >     */
785 >    public int getReadLockCount() {
786 >        return getReadLockCount(state);
787 >    }
788 >
789 >    /**
790 >     * Returns a string identifying this lock, as well as its lock
791 >     * state.  The state, in brackets, includes the String {@code
792 >     * "Unlocked"} or the String {@code "Write-locked"} or the String
793 >     * {@code "Read-locks:"} followed by the current number of
794 >     * read-locks held.
795 >     *
796 >     * @return a string identifying this lock, as well as its lock state
797 >     */
798 >    public String toString() {
799 >        long s = state;
800 >        return super.toString() +
801 >            ((s & ABITS) == 0L ? "[Unlocked]" :
802 >             (s & WBIT) != 0L ? "[Write-locked]" :
803 >             "[Read-locks:" + getReadLockCount(s) + "]");
804 >    }
805 >
806 >    // views
807 >
808 >    /**
809 >     * Returns a plain {@link Lock} view of this StampedLock in which
810 >     * the {@link Lock#lock} method is mapped to {@link #readLock},
811 >     * and similarly for other methods. The returned Lock does not
812 >     * support a {@link Condition}; method {@link
813 >     * Lock#newCondition()} throws {@code
814 >     * UnsupportedOperationException}.
815 >     *
816 >     * @return the lock
817 >     */
818 >    public Lock asReadLock() {
819 >        ReadLockView v;
820 >        return ((v = readLockView) != null ? v :
821 >                (readLockView = new ReadLockView()));
822 >    }
823 >
824 >    /**
825 >     * Returns a plain {@link Lock} view of this StampedLock in which
826 >     * the {@link Lock#lock} method is mapped to {@link #writeLock},
827 >     * and similarly for other methods. The returned Lock does not
828 >     * support a {@link Condition}; method {@link
829 >     * Lock#newCondition()} throws {@code
830 >     * UnsupportedOperationException}.
831 >     *
832 >     * @return the lock
833 >     */
834 >    public Lock asWriteLock() {
835 >        WriteLockView v;
836 >        return ((v = writeLockView) != null ? v :
837 >                (writeLockView = new WriteLockView()));
838 >    }
839 >
840 >    /**
841 >     * Returns a {@link ReadWriteLock} view of this StampedLock in
842 >     * which the {@link ReadWriteLock#readLock()} method is mapped to
843 >     * {@link #asReadLock()}, and {@link ReadWriteLock#writeLock()} to
844 >     * {@link #asWriteLock()}.
845 >     *
846 >     * @return the lock
847 >     */
848 >    public ReadWriteLock asReadWriteLock() {
849 >        ReadWriteLockView v;
850 >        return ((v = readWriteLockView) != null ? v :
851 >                (readWriteLockView = new ReadWriteLockView()));
852 >    }
853 >
854 >    // view classes
855 >
856 >    final class ReadLockView implements Lock {
857 >        public void lock() { readLock(); }
858 >        public void lockInterruptibly() throws InterruptedException {
859 >            readLockInterruptibly();
860 >        }
861 >        public boolean tryLock() { return tryReadLock() != 0L; }
862 >        public boolean tryLock(long time, TimeUnit unit)
863 >            throws InterruptedException {
864 >            return tryReadLock(time, unit) != 0L;
865 >        }
866 >        public void unlock() { unstampedUnlockRead(); }
867 >        public Condition newCondition() {
868 >            throw new UnsupportedOperationException();
869 >        }
870 >    }
871 >
872 >    final class WriteLockView implements Lock {
873 >        public void lock() { writeLock(); }
874 >        public void lockInterruptibly() throws InterruptedException {
875 >            writeLockInterruptibly();
876 >        }
877 >        public boolean tryLock() { return tryWriteLock() != 0L; }
878 >        public boolean tryLock(long time, TimeUnit unit)
879 >            throws InterruptedException {
880 >            return tryWriteLock(time, unit) != 0L;
881 >        }
882 >        public void unlock() { unstampedUnlockWrite(); }
883 >        public Condition newCondition() {
884 >            throw new UnsupportedOperationException();
885 >        }
886 >    }
887 >
888 >    final class ReadWriteLockView implements ReadWriteLock {
889 >        public Lock readLock() { return asReadLock(); }
890 >        public Lock writeLock() { return asWriteLock(); }
891 >    }
892 >
893 >    // Unlock methods without stamp argument checks for view classes.
894 >    // Needed because view-class lock methods throw away stamps.
895 >
896 >    final void unstampedUnlockWrite() {
897 >        WNode h; long s;
898 >        if (((s = state) & WBIT) == 0L)
899 >            throw new IllegalMonitorStateException();
900 >        state = (s += WBIT) == 0L ? ORIGIN : s;
901 >        if ((h = whead) != null && h.status != 0)
902 >            release(h);
903 >    }
904 >
905 >    final void unstampedUnlockRead() {
906 >        for (;;) {
907 >            long s, m; WNode h;
908 >            if ((m = (s = state) & ABITS) == 0L || m >= WBIT)
909 >                throw new IllegalMonitorStateException();
910 >            else if (m < RFULL) {
911 >                if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) {
912 >                    if (m == RUNIT && (h = whead) != null && h.status != 0)
913 >                        release(h);
914 >                    break;
915 >                }
916 >            }
917 >            else if (tryDecReaderOverflow(s) != 0L)
918 >                break;
919 >        }
920      }
921  
922      private void readObject(java.io.ObjectInputStream s)
# Line 777 | Line 932 | public class StampedLock implements java
932       * access bits value to RBITS, indicating hold of spinlock,
933       * then updating, then releasing.
934       *
935 <     * @param stamp, assumed that (stamp & ABITS) >= RFULL
935 >     * @param s a reader overflow stamp: (s & ABITS) >= RFULL
936       * @return new stamp on success, else zero
937       */
938      private long tryIncReaderOverflow(long s) {
939 +        // assert (s & ABITS) >= RFULL;
940          if ((s & ABITS) == RFULL) {
941              if (U.compareAndSwapLong(this, STATE, s, s | RBITS)) {
942                  ++readerOverflow;
# Line 797 | Line 953 | public class StampedLock implements java
953      /**
954       * Tries to decrement readerOverflow.
955       *
956 <     * @param stamp, assumed that (stamp & ABITS) >= RFULL
956 >     * @param s a reader overflow stamp: (s & ABITS) >= RFULL
957       * @return new stamp on success, else zero
958       */
959      private long tryDecReaderOverflow(long s) {
960 +        // assert (s & ABITS) >= RFULL;
961          if ((s & ABITS) == RFULL) {
962              if (U.compareAndSwapLong(this, STATE, s, s | RBITS)) {
963                  int r; long next;
# Line 820 | Line 977 | public class StampedLock implements java
977          return 0L;
978      }
979  
980 <    /*
981 <     * The two versions of signal implement the phase-fair policy.
982 <     * They include almost the same code, but repacked in different
983 <     * ways.  Integrating the policy with the mechanics eliminates
984 <     * state rechecks that would be needed with separate reader and
985 <     * writer signal methods.  Both methods assume that they are
986 <     * called when the lock is last known to be available, and
987 <     * continue until the lock is unavailable, or at least one thread
988 <     * is signalled, or there are no more waiting threads.  Signalling
989 <     * a reader entails popping (CASing) from rhead and unparking
990 <     * unless the thread already cancelled (indicated by a null waiter
834 <     * field). Signalling a writer requires finding the first node,
835 <     * i.e., the successor of whead. This is normally just head.next,
836 <     * but may require traversal from wtail if next pointers are
837 <     * lagging. These methods may fail to wake up an acquiring thread
838 <     * when one or more have been cancelled, but the cancel methods
839 <     * themselves provide extra safeguards to ensure liveness.
840 <     */
841 <
842 <    private void readerPrefSignal() {
843 <        boolean readers = false;
844 <        RNode p; WNode h, q; long s; Thread w;
845 <        while ((p = rhead) != null) {
846 <            if (((s = state) & WBIT) != 0L)
847 <                return;
848 <            if (p.seq == (s & SBITS))
849 <                break;
850 <            readers = true;
851 <            if (U.compareAndSwapObject(this, RHEAD, p, p.next) &&
852 <                (w = p.waiter) != null &&
853 <                U.compareAndSwapObject(p, WAITER, w, null))
854 <                U.unpark(w);
855 <        }
856 <        if (!readers && (state & ABITS) == 0L &&
857 <            (h = whead) != null && h.status != 0) {
858 <            U.compareAndSwapInt(h, STATUS, WAITING, 0);
859 <            if ((q = h.next) == null || q.status == CANCELLED) {
860 <                q = null;
861 <                for (WNode t = wtail; t != null && t != h; t = t.prev)
862 <                    if (t.status <= 0)
863 <                        q = t;
864 <            }
865 <            if (q != null && (w = q.thread) != null)
866 <                U.unpark(w);
867 <        }
868 <    }
869 <
870 <    private void writerPrefSignal() {
871 <        RNode p; WNode h, q; long s; Thread w;
872 <        if ((h = whead) != null && h.status != 0) {
873 <            U.compareAndSwapInt(h, STATUS, WAITING, 0);
980 >    /**
981 >     * Wakes up the successor of h (normally whead). This is normally
982 >     * just h.next, but may require traversal from wtail if next
983 >     * pointers are lagging. This may fail to wake up an acquiring
984 >     * thread when one or more have been cancelled, but the cancel
985 >     * methods themselves provide extra safeguards to ensure liveness.
986 >     */
987 >    private void release(WNode h) {
988 >        if (h != null) {
989 >            WNode q; Thread w;
990 >            U.compareAndSwapInt(h, WSTATUS, WAITING, 0);
991              if ((q = h.next) == null || q.status == CANCELLED) {
875                q = null;
992                  for (WNode t = wtail; t != null && t != h; t = t.prev)
993                      if (t.status <= 0)
994                          q = t;
# Line 880 | Line 996 | public class StampedLock implements java
996              if (q != null && (w = q.thread) != null)
997                  U.unpark(w);
998          }
883        else {
884            while ((p = rhead) != null && ((s = state) & WBIT) == 0L &&
885                   p.seq != (s & SBITS)) {
886                if (U.compareAndSwapObject(this, RHEAD, p, p.next) &&
887                    (w = p.waiter) != null &&
888                    U.compareAndSwapObject(p, WAITER, w, null))
889                    U.unpark(w);
890            }
891        }
999      }
1000  
1001      /**
1002 <     * RNG for local spins. The first call from await{Read,Write}
896 <     * produces a thread-local value. Unless zero, subsequent calls
897 <     * use an xorShift to further reduce memory traffic.  Both await
898 <     * methods use a similar spin strategy: If associated queue
899 <     * appears to be empty, then the thread spin-waits up to SPINS
900 <     * times before enqueing, and then, if the first thread to be
901 <     * enqueued, spins again up to SPINS times before blocking. If,
902 <     * upon wakening it fails to obtain lock, and is still (or
903 <     * becomes) the first waiting thread (which indicates that some
904 <     * other thread barged and obtained lock), it escalates spins (up
905 <     * to MAX_HEAD_SPINS) to reduce the likelihood of continually
906 <     * losing to barging threads.
907 <     */
908 <    private static int nextRandom(int r) {
909 <        if (r == 0)
910 <            return ThreadLocalRandom.current().nextInt();
911 <        r ^= r << 1; // xorshift
912 <        r ^= r >>> 3;
913 <        r ^= r << 10;
914 <        return r;
915 <    }
916 <
917 <    /**
918 <     * Possibly spins trying to obtain write lock, then enqueues and
919 <     * blocks while not head of write queue or cannot acquire lock,
920 <     * possibly spinning when at head; cancelling on timeout or
921 <     * interrupt.
1002 >     * See above for explanation.
1003       *
1004       * @param interruptible true if should check interrupts and if so
1005       * return INTERRUPTED
1006       * @param deadline if nonzero, the System.nanoTime value to timeout
1007       * at (and return zero)
1008 +     * @return next state, or INTERRUPTED
1009       */
1010 <    private long awaitWrite(boolean interruptible, long deadline) {
1011 <        WNode node = null;
1012 <        for (int r = 0, spins = -1;;) {
1013 <            WNode p; long s, next;
1014 <            if (((s = state) & ABITS) == 0L) {
1015 <                if (U.compareAndSwapLong(this, STATE, s, next = s + WBIT))
1016 <                    return next;
1010 >    private long acquireWrite(boolean interruptible, long deadline) {
1011 >        WNode node = null, p;
1012 >        for (int spins = -1;;) { // spin while enqueuing
1013 >            long m, s, ns;
1014 >            if ((m = (s = state) & ABITS) == 0L) {
1015 >                if (U.compareAndSwapLong(this, STATE, s, ns = s + WBIT))
1016 >                    return ns;
1017              }
1018              else if (spins < 0)
1019 <                spins = whead == wtail ? SPINS : 0;
1019 >                spins = (m == WBIT && wtail == whead) ? SPINS : 0;
1020              else if (spins > 0) {
1021 <                if ((r = nextRandom(r)) >= 0)
1021 >                if (ThreadLocalRandom.current().nextInt() >= 0)
1022                      --spins;
1023              }
1024              else if ((p = wtail) == null) { // initialize queue
1025 <                if (U.compareAndSwapObject(this, WHEAD, null,
1026 <                                           new WNode(null, null)))
1027 <                    wtail = whead;
1025 >                WNode hd = new WNode(WMODE, null);
1026 >                if (U.compareAndSwapObject(this, WHEAD, null, hd))
1027 >                    wtail = hd;
1028              }
1029              else if (node == null)
1030 <                node = new WNode(Thread.currentThread(), p);
1030 >                node = new WNode(WMODE, p);
1031              else if (node.prev != p)
1032                  node.prev = p;
1033              else if (U.compareAndSwapObject(this, WTAIL, p, node)) {
1034                  p.next = node;
1035 <                for (int headSpins = SPINS;;) {
1036 <                    WNode np; int ps;
1037 <                    if ((np = node.prev) != p && np != null)
1038 <                        (p = np).next = node; // stale
1039 <                    if (p == whead) {
1040 <                        for (int k = headSpins;;) {
1041 <                            if (((s = state) & ABITS) == 0L) {
1042 <                                if (U.compareAndSwapLong(this, STATE,
1043 <                                                         s, next = s + WBIT)) {
1044 <                                    whead = node;
1045 <                                    node.thread = null;
1046 <                                    node.prev = null;
1047 <                                    return next;
1048 <                                }
1049 <                                break;
1050 <                            }
1051 <                            if ((r = nextRandom(r)) >= 0 && --k <= 0)
1052 <                                break;
1053 <                        }
972 <                        if (headSpins < MAX_HEAD_SPINS)
973 <                            headSpins <<= 1;
974 <                    }
975 <                    if ((ps = p.status) == 0)
976 <                        U.compareAndSwapInt(p, STATUS, 0, WAITING);
977 <                    else if (ps == CANCELLED)
978 <                        node.prev = p.prev;
979 <                    else {
980 <                        long time; // 0 argument to park means no timeout
981 <                        if (deadline == 0L)
982 <                            time = 0L;
983 <                        else if ((time = deadline - System.nanoTime()) <= 0L)
984 <                            return cancelWriter(node, false);
985 <                        if (node.prev == p && p.status == WAITING &&
986 <                            (p != whead || (state & WBIT) != 0L)) { // recheck
987 <                            U.park(false, time);
988 <                            if (interruptible && Thread.interrupted())
989 <                                return cancelWriter(node, true);
1035 >                break;
1036 >            }
1037 >        }
1038 >
1039 >        for (int spins = -1;;) {
1040 >            WNode h, np, pp; int ps;
1041 >            if ((h = whead) == p) {
1042 >                if (spins < 0)
1043 >                    spins = HEAD_SPINS;
1044 >                else if (spins < MAX_HEAD_SPINS)
1045 >                    spins <<= 1;
1046 >                for (int k = spins;;) { // spin at head
1047 >                    long s, ns;
1048 >                    if (((s = state) & ABITS) == 0L) {
1049 >                        if (U.compareAndSwapLong(this, STATE, s,
1050 >                                                 ns = s + WBIT)) {
1051 >                            whead = node;
1052 >                            node.prev = null;
1053 >                            return ns;
1054                          }
1055                      }
1056 +                    else if (ThreadLocalRandom.current().nextInt() >= 0 &&
1057 +                             --k <= 0)
1058 +                        break;
1059                  }
1060              }
1061 <        }
1062 <    }
1063 <
1064 <    /**
1065 <     * If node non-null, forces cancel status and unsplices from queue
1066 <     * if possible. This is a streamlined variant of cancellation
1067 <     * methods in AbstractQueuedSynchronizer that includes a detailed
1068 <     * explanation.
1069 <     */
1070 <    private long cancelWriter(WNode node, boolean interrupted) {
1071 <        WNode pred;
1072 <        if (node != null && (pred = node.prev) != null) {
1073 <            WNode pp;
1074 <            node.thread = null;
1075 <            while (pred.status == CANCELLED && (pp = pred.prev) != null)
1076 <                pred = node.prev = pp;
1077 <            WNode predNext = pred.next;
1078 <            node.status = CANCELLED;
1079 <            if (predNext != null) {
1080 <                Thread w = null;
1081 <                WNode succ = node.next;
1082 <                while (succ != null && succ.status == CANCELLED)
1083 <                    succ = succ.next;
1084 <                if (succ != null)
1085 <                    w = succ.thread;
1086 <                else if (node == wtail)
1087 <                    U.compareAndSwapObject(this, WTAIL, node, pred);
1088 <                U.compareAndSwapObject(pred, WNEXT, predNext, succ);
1089 <                if (w != null)
1090 <                    U.unpark(w);
1061 >            else if (h != null) { // help release stale waiters
1062 >                WNode c; Thread w;
1063 >                while ((c = h.cowait) != null) {
1064 >                    if (U.compareAndSwapObject(h, WCOWAIT, c, c.cowait) &&
1065 >                        (w = c.thread) != null)
1066 >                        U.unpark(w);
1067 >                }
1068 >            }
1069 >            if (whead == h) {
1070 >                if ((np = node.prev) != p) {
1071 >                    if (np != null)
1072 >                        (p = np).next = node;   // stale
1073 >                }
1074 >                else if ((ps = p.status) == 0)
1075 >                    U.compareAndSwapInt(p, WSTATUS, 0, WAITING);
1076 >                else if (ps == CANCELLED) {
1077 >                    if ((pp = p.prev) != null) {
1078 >                        node.prev = pp;
1079 >                        pp.next = node;
1080 >                    }
1081 >                }
1082 >                else {
1083 >                    long time; // 0 argument to park means no timeout
1084 >                    if (deadline == 0L)
1085 >                        time = 0L;
1086 >                    else if ((time = deadline - System.nanoTime()) <= 0L)
1087 >                        return cancelWaiter(node, node, false);
1088 >                    Thread wt = Thread.currentThread();
1089 >                    U.putObject(wt, PARKBLOCKER, this);
1090 >                    node.thread = wt;
1091 >                    if (p.status < 0 && (p != h || (state & ABITS) != 0L) &&
1092 >                        whead == h && node.prev == p)
1093 >                        U.park(false, time);  // emulate LockSupport.park
1094 >                    node.thread = null;
1095 >                    U.putObject(wt, PARKBLOCKER, null);
1096 >                    if (interruptible && Thread.interrupted())
1097 >                        return cancelWaiter(node, node, true);
1098 >                }
1099              }
1100          }
1026        writerPrefSignal();
1027        return (interrupted || Thread.interrupted()) ? INTERRUPTED : 0L;
1101      }
1102  
1103      /**
1104 <     * Waits for read lock or timeout or interrupt. The form of
1105 <     * awaitRead differs from awaitWrite mainly because it must
1106 <     * restart (with a new wait node) if the thread was unqueued and
1107 <     * unparked but could not the obtain lock.  We also need to help
1108 <     * with preference rules by not trying to acquire the lock before
1109 <     * enqueuing if there is a known waiting writer, but also helping
1110 <     * to release those threads that are still queued from the last
1111 <     * release.
1112 <     */
1113 <    private long awaitRead(long stamp, boolean interruptible, long deadline) {
1114 <        long seq = stamp & SBITS;
1115 <        RNode node = null;
1116 <        boolean queued = false;
1117 <        for (int r = 0, headSpins = SPINS, spins = -1;;) {
1118 <            long s, m, next; RNode p; WNode wh; Thread w;
1119 <            if ((m = (s = state) & ABITS) != WBIT &&
1120 <                ((s & SBITS) != seq || (wh = whead) == null ||
1121 <                 wh.status == 0)) {
1122 <                if (m < RFULL ?
1123 <                    U.compareAndSwapLong(this, STATE, s, next = s + RUNIT) :
1124 <                    (next = tryIncReaderOverflow(s)) != 0L) {
1125 <                    if (node != null && (w = node.waiter) != null)
1126 <                        U.compareAndSwapObject(node, WAITER, w, null);
1127 <                    if ((p = rhead) != null && (s & SBITS) != p.seq &&
1128 <                        U.compareAndSwapObject(this, RHEAD, p, p.next) &&
1129 <                        (w = p.waiter) != null &&
1130 <                        U.compareAndSwapObject(p, WAITER, w, null))
1131 <                        U.unpark(w); // help signal other waiters
1132 <                    return next;
1104 >     * See above for explanation.
1105 >     *
1106 >     * @param interruptible true if should check interrupts and if so
1107 >     * return INTERRUPTED
1108 >     * @param deadline if nonzero, the System.nanoTime value to timeout
1109 >     * at (and return zero)
1110 >     * @return next state, or INTERRUPTED
1111 >     */
1112 >    private long acquireRead(boolean interruptible, long deadline) {
1113 >        WNode node = null, p;
1114 >        for (int spins = -1;;) {
1115 >            WNode h;
1116 >            if ((h = whead) == (p = wtail)) {
1117 >                for (long m, s, ns;;) {
1118 >                    if ((m = (s = state) & ABITS) < RFULL ?
1119 >                        U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT) :
1120 >                        (m < WBIT && (ns = tryIncReaderOverflow(s)) != 0L))
1121 >                        return ns;
1122 >                    else if (m >= WBIT) {
1123 >                        if (spins > 0) {
1124 >                            if (ThreadLocalRandom.current().nextInt() >= 0)
1125 >                                --spins;
1126 >                        }
1127 >                        else {
1128 >                            if (spins == 0) {
1129 >                                WNode nh = whead, np = wtail;
1130 >                                if ((nh == h && np == p) || (h = nh) != (p = np))
1131 >                                    break;
1132 >                            }
1133 >                            spins = SPINS;
1134 >                        }
1135 >                    }
1136                  }
1137              }
1138 <            else if (m != WBIT && (p = rhead) != null &&
1139 <                     (s & SBITS) != p.seq) { // help release old readers
1140 <                if (U.compareAndSwapObject(this, RHEAD, p, p.next) &&
1141 <                    (w = p.waiter) != null &&
1066 <                    U.compareAndSwapObject(p, WAITER, w, null))
1067 <                    U.unpark(w);
1068 <            }
1069 <            else if (queued && node != null && node.waiter == null) {
1070 <                node = null;    // restart
1071 <                queued = false;
1072 <                spins = -1;
1073 <            }
1074 <            else if (spins < 0) {
1075 <                if (rhead != node)
1076 <                    spins = 0;
1077 <                else if ((spins = headSpins) < MAX_HEAD_SPINS && node != null)
1078 <                    headSpins <<= 1;
1079 <            }
1080 <            else if (spins > 0) {
1081 <                if ((r = nextRandom(r)) >= 0)
1082 <                    --spins;
1138 >            if (p == null) { // initialize queue
1139 >                WNode hd = new WNode(WMODE, null);
1140 >                if (U.compareAndSwapObject(this, WHEAD, null, hd))
1141 >                    wtail = hd;
1142              }
1143              else if (node == null)
1144 <                node = new RNode(seq, Thread.currentThread());
1145 <            else if (!queued) {
1146 <                if (queued = U.compareAndSwapObject(this, RHEAD,
1147 <                                                    node.next = rhead, node))
1148 <                    spins = -1;
1144 >                node = new WNode(RMODE, p);
1145 >            else if (h == p || p.mode != RMODE) {
1146 >                if (node.prev != p)
1147 >                    node.prev = p;
1148 >                else if (U.compareAndSwapObject(this, WTAIL, p, node)) {
1149 >                    p.next = node;
1150 >                    break;
1151 >                }
1152              }
1153 +            else if (!U.compareAndSwapObject(p, WCOWAIT,
1154 +                                             node.cowait = p.cowait, node))
1155 +                node.cowait = null;
1156              else {
1157 <                long time;
1158 <                if (deadline == 0L)
1159 <                    time = 0L;
1160 <                else if ((time = deadline - System.nanoTime()) <= 0L)
1161 <                    return cancelReader(node, false);
1162 <                if ((state & WBIT) != 0L && node.waiter != null) { // recheck
1163 <                    U.park(false, time);
1164 <                    if (interruptible && Thread.interrupted())
1165 <                        return cancelReader(node, true);
1157 >                for (;;) {
1158 >                    WNode pp, c; Thread w;
1159 >                    if ((h = whead) != null && (c = h.cowait) != null &&
1160 >                        U.compareAndSwapObject(h, WCOWAIT, c, c.cowait) &&
1161 >                        (w = c.thread) != null) // help release
1162 >                        U.unpark(w);
1163 >                    if (h == (pp = p.prev) || h == p || pp == null) {
1164 >                        long m, s, ns;
1165 >                        do {
1166 >                            if ((m = (s = state) & ABITS) < RFULL ?
1167 >                                U.compareAndSwapLong(this, STATE, s,
1168 >                                                     ns = s + RUNIT) :
1169 >                                (m < WBIT &&
1170 >                                 (ns = tryIncReaderOverflow(s)) != 0L))
1171 >                                return ns;
1172 >                        } while (m < WBIT);
1173 >                    }
1174 >                    if (whead == h && p.prev == pp) {
1175 >                        long time;
1176 >                        if (pp == null || h == p || p.status > 0) {
1177 >                            node = null; // throw away
1178 >                            break;
1179 >                        }
1180 >                        if (deadline == 0L)
1181 >                            time = 0L;
1182 >                        else if ((time = deadline - System.nanoTime()) <= 0L)
1183 >                            return cancelWaiter(node, p, false);
1184 >                        Thread wt = Thread.currentThread();
1185 >                        U.putObject(wt, PARKBLOCKER, this);
1186 >                        node.thread = wt;
1187 >                        if ((h != pp || (state & ABITS) == WBIT) &&
1188 >                            whead == h && p.prev == pp)
1189 >                            U.park(false, time);
1190 >                        node.thread = null;
1191 >                        U.putObject(wt, PARKBLOCKER, null);
1192 >                        if (interruptible && Thread.interrupted())
1193 >                            return cancelWaiter(node, p, true);
1194 >                    }
1195                  }
1196              }
1197          }
1104    }
1198  
1199 <    /**
1200 <     * If node non-null, forces cancel status and unsplices from queue
1201 <     * if possible, by traversing entire queue looking for cancelled
1202 <     * nodes.
1203 <     */
1204 <    private long cancelReader(RNode node, boolean interrupted) {
1205 <        Thread w;
1206 <        if (node != null && (w = node.waiter) != null &&
1207 <            U.compareAndSwapObject(node, WAITER, w, null)) {
1208 <            for (RNode pred = null, p = rhead; p != null;) {
1209 <                RNode q = p.next;
1210 <                if (p.waiter == null) {
1211 <                    if (pred == null) {
1212 <                        U.compareAndSwapObject(this, RHEAD, p, q);
1213 <                        p = rhead;
1199 >        for (int spins = -1;;) {
1200 >            WNode h, np, pp; int ps;
1201 >            if ((h = whead) == p) {
1202 >                if (spins < 0)
1203 >                    spins = HEAD_SPINS;
1204 >                else if (spins < MAX_HEAD_SPINS)
1205 >                    spins <<= 1;
1206 >                for (int k = spins;;) { // spin at head
1207 >                    long m, s, ns;
1208 >                    if ((m = (s = state) & ABITS) < RFULL ?
1209 >                        U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT) :
1210 >                        (m < WBIT && (ns = tryIncReaderOverflow(s)) != 0L)) {
1211 >                        WNode c; Thread w;
1212 >                        whead = node;
1213 >                        node.prev = null;
1214 >                        while ((c = node.cowait) != null) {
1215 >                            if (U.compareAndSwapObject(node, WCOWAIT,
1216 >                                                       c, c.cowait) &&
1217 >                                (w = c.thread) != null)
1218 >                                U.unpark(w);
1219 >                        }
1220 >                        return ns;
1221                      }
1222 <                    else {
1223 <                        U.compareAndSwapObject(pred, RNEXT, p, q);
1224 <                        p = pred.next;
1222 >                    else if (m >= WBIT &&
1223 >                             ThreadLocalRandom.current().nextInt() >= 0 && --k <= 0)
1224 >                        break;
1225 >                }
1226 >            }
1227 >            else if (h != null) {
1228 >                WNode c; Thread w;
1229 >                while ((c = h.cowait) != null) {
1230 >                    if (U.compareAndSwapObject(h, WCOWAIT, c, c.cowait) &&
1231 >                        (w = c.thread) != null)
1232 >                        U.unpark(w);
1233 >                }
1234 >            }
1235 >            if (whead == h) {
1236 >                if ((np = node.prev) != p) {
1237 >                    if (np != null)
1238 >                        (p = np).next = node;   // stale
1239 >                }
1240 >                else if ((ps = p.status) == 0)
1241 >                    U.compareAndSwapInt(p, WSTATUS, 0, WAITING);
1242 >                else if (ps == CANCELLED) {
1243 >                    if ((pp = p.prev) != null) {
1244 >                        node.prev = pp;
1245 >                        pp.next = node;
1246                      }
1247                  }
1248                  else {
1249 <                    pred = p;
1249 >                    long time;
1250 >                    if (deadline == 0L)
1251 >                        time = 0L;
1252 >                    else if ((time = deadline - System.nanoTime()) <= 0L)
1253 >                        return cancelWaiter(node, node, false);
1254 >                    Thread wt = Thread.currentThread();
1255 >                    U.putObject(wt, PARKBLOCKER, this);
1256 >                    node.thread = wt;
1257 >                    if (p.status < 0 &&
1258 >                        (p != h || (state & ABITS) == WBIT) &&
1259 >                        whead == h && node.prev == p)
1260 >                        U.park(false, time);
1261 >                    node.thread = null;
1262 >                    U.putObject(wt, PARKBLOCKER, null);
1263 >                    if (interruptible && Thread.interrupted())
1264 >                        return cancelWaiter(node, node, true);
1265 >                }
1266 >            }
1267 >        }
1268 >    }
1269 >
1270 >    /**
1271 >     * If node non-null, forces cancel status and unsplices it from
1272 >     * queue if possible and wakes up any cowaiters (of the node, or
1273 >     * group, as applicable), and in any case helps release current
1274 >     * first waiter if lock is free. (Calling with null arguments
1275 >     * serves as a conditional form of release, which is not currently
1276 >     * needed but may be needed under possible future cancellation
1277 >     * policies). This is a variant of cancellation methods in
1278 >     * AbstractQueuedSynchronizer (see its detailed explanation in AQS
1279 >     * internal documentation).
1280 >     *
1281 >     * @param node if nonnull, the waiter
1282 >     * @param group either node or the group node is cowaiting with
1283 >     * @param interrupted if already interrupted
1284 >     * @return INTERRUPTED if interrupted or Thread.interrupted, else zero
1285 >     */
1286 >    private long cancelWaiter(WNode node, WNode group, boolean interrupted) {
1287 >        if (node != null && group != null) {
1288 >            Thread w;
1289 >            node.status = CANCELLED;
1290 >            // unsplice cancelled nodes from group
1291 >            for (WNode p = group, q; (q = p.cowait) != null;) {
1292 >                if (q.status == CANCELLED) {
1293 >                    U.compareAndSwapObject(p, WCOWAIT, q, q.cowait);
1294 >                    p = group; // restart
1295 >                }
1296 >                else
1297                      p = q;
1298 +            }
1299 +            if (group == node) {
1300 +                for (WNode r = group.cowait; r != null; r = r.cowait) {
1301 +                    if ((w = r.thread) != null)
1302 +                        U.unpark(w);       // wake up uncancelled co-waiters
1303 +                }
1304 +                for (WNode pred = node.prev; pred != null; ) { // unsplice
1305 +                    WNode succ, pp;        // find valid successor
1306 +                    while ((succ = node.next) == null ||
1307 +                           succ.status == CANCELLED) {
1308 +                        WNode q = null;    // find successor the slow way
1309 +                        for (WNode t = wtail; t != null && t != node; t = t.prev)
1310 +                            if (t.status != CANCELLED)
1311 +                                q = t;     // don't link if succ cancelled
1312 +                        if (succ == q ||   // ensure accurate successor
1313 +                            U.compareAndSwapObject(node, WNEXT,
1314 +                                                   succ, succ = q)) {
1315 +                            if (succ == null && node == wtail)
1316 +                                U.compareAndSwapObject(this, WTAIL, node, pred);
1317 +                            break;
1318 +                        }
1319 +                    }
1320 +                    if (pred.next == node) // unsplice pred link
1321 +                        U.compareAndSwapObject(pred, WNEXT, node, succ);
1322 +                    if (succ != null && (w = succ.thread) != null) {
1323 +                        succ.thread = null;
1324 +                        U.unpark(w);       // wake up succ to observe new pred
1325 +                    }
1326 +                    if (pred.status != CANCELLED || (pp = pred.prev) == null)
1327 +                        break;
1328 +                    node.prev = pp;        // repeat if new pred wrong/cancelled
1329 +                    U.compareAndSwapObject(pp, WNEXT, pred, succ);
1330 +                    pred = pp;
1331                  }
1332              }
1333          }
1334 <        readerPrefSignal();
1334 >        WNode h; // Possibly release first waiter
1335 >        while ((h = whead) != null) {
1336 >            long s; WNode q; // similar to release() but check eligibility
1337 >            if ((q = h.next) == null || q.status == CANCELLED) {
1338 >                for (WNode t = wtail; t != null && t != h; t = t.prev)
1339 >                    if (t.status <= 0)
1340 >                        q = t;
1341 >            }
1342 >            if (h == whead) {
1343 >                if (q != null && h.status == 0 &&
1344 >                    ((s = state) & ABITS) != WBIT && // waiter is eligible
1345 >                    (s == 0L || q.mode == RMODE))
1346 >                    release(h);
1347 >                break;
1348 >            }
1349 >        }
1350          return (interrupted || Thread.interrupted()) ? INTERRUPTED : 0L;
1351      }
1352  
1353      // Unsafe mechanics
1354      private static final sun.misc.Unsafe U;
1355      private static final long STATE;
1140    private static final long RHEAD;
1356      private static final long WHEAD;
1357      private static final long WTAIL;
1143    private static final long RNEXT;
1358      private static final long WNEXT;
1359 <    private static final long WPREV;
1360 <    private static final long WAITER;
1361 <    private static final long STATUS;
1359 >    private static final long WSTATUS;
1360 >    private static final long WCOWAIT;
1361 >    private static final long PARKBLOCKER;
1362  
1363      static {
1364          try {
1365              U = getUnsafe();
1366              Class<?> k = StampedLock.class;
1153            Class<?> rk = RNode.class;
1367              Class<?> wk = WNode.class;
1368              STATE = U.objectFieldOffset
1369                  (k.getDeclaredField("state"));
1157            RHEAD = U.objectFieldOffset
1158                (k.getDeclaredField("rhead"));
1370              WHEAD = U.objectFieldOffset
1371                  (k.getDeclaredField("whead"));
1372              WTAIL = U.objectFieldOffset
1373                  (k.getDeclaredField("wtail"));
1374 <            RNEXT = U.objectFieldOffset
1164 <                (rk.getDeclaredField("next"));
1165 <            WAITER = U.objectFieldOffset
1166 <                (rk.getDeclaredField("waiter"));
1167 <            STATUS = U.objectFieldOffset
1374 >            WSTATUS = U.objectFieldOffset
1375                  (wk.getDeclaredField("status"));
1376              WNEXT = U.objectFieldOffset
1377                  (wk.getDeclaredField("next"));
1378 <            WPREV = U.objectFieldOffset
1379 <                (wk.getDeclaredField("prev"));
1378 >            WCOWAIT = U.objectFieldOffset
1379 >                (wk.getDeclaredField("cowait"));
1380 >            Class<?> tk = Thread.class;
1381 >            PARKBLOCKER = U.objectFieldOffset
1382 >                (tk.getDeclaredField("parkBlocker"));
1383  
1384          } catch (Exception e) {
1385              throw new Error(e);
# Line 1186 | Line 1396 | public class StampedLock implements java
1396      private static sun.misc.Unsafe getUnsafe() {
1397          try {
1398              return sun.misc.Unsafe.getUnsafe();
1399 <        } catch (SecurityException se) {
1400 <            try {
1401 <                return java.security.AccessController.doPrivileged
1402 <                    (new java.security
1403 <                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
1404 <                        public sun.misc.Unsafe run() throws Exception {
1405 <                            java.lang.reflect.Field f = sun.misc
1406 <                                .Unsafe.class.getDeclaredField("theUnsafe");
1407 <                            f.setAccessible(true);
1408 <                            return (sun.misc.Unsafe) f.get(null);
1409 <                        }});
1410 <            } catch (java.security.PrivilegedActionException e) {
1411 <                throw new RuntimeException("Could not initialize intrinsics",
1412 <                                           e.getCause());
1413 <            }
1399 >        } catch (SecurityException tryReflectionInstead) {}
1400 >        try {
1401 >            return java.security.AccessController.doPrivileged
1402 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
1403 >                public sun.misc.Unsafe run() throws Exception {
1404 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
1405 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
1406 >                        f.setAccessible(true);
1407 >                        Object x = f.get(null);
1408 >                        if (k.isInstance(x))
1409 >                            return k.cast(x);
1410 >                    }
1411 >                    throw new NoSuchFieldError("the Unsafe");
1412 >                }});
1413 >        } catch (java.security.PrivilegedActionException e) {
1414 >            throw new RuntimeException("Could not initialize intrinsics",
1415 >                                       e.getCause());
1416          }
1417      }
1206
1418   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines