|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
/* |
|
* This file is available under and governed by the GNU General Public |
|
* License version 2 only, as published by the Free Software Foundation. |
|
* However, the following notice accompanied the original version of this |
|
* file: |
|
* |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
* Expert Group and released to the public domain, as explained at |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
*/ |
|
|
|
package java.util.concurrent.locks; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
import java.util.concurrent.locks.Lock; |
|
import java.util.concurrent.locks.Condition; |
|
import java.util.concurrent.locks.ReadWriteLock; |
|
import java.util.concurrent.locks.LockSupport; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public class StampedLock implements java.io.Serializable { |
|
/* |
|
* Algorithmic notes: |
|
* |
|
* The design employs elements of Sequence locks |
|
* (as used in linux kernels; see Lameter's |
|
* http://www.lameter.com/gelato2005.pdf |
|
* and elsewhere; see |
|
* Boehm's http://www.hpl.hp.com/techreports/2012/HPL-2012-68.html) |
|
* and Ordered RW locks (see Shirako et al |
|
* http://dl.acm.org/citation.cfm?id=2312015) |
|
* |
|
* Conceptually, the primary state of the lock includes a sequence |
|
* number that is odd when write-locked and even otherwise. |
|
* However, this is offset by a reader count that is non-zero when |
|
* read-locked. The read count is ignored when validating |
|
* "optimistic" seqlock-reader-style stamps. Because we must use |
|
* a small finite number of bits (currently 7) for readers, a |
|
* supplementary reader overflow word is used when the number of |
|
* readers exceeds the count field. We do this by treating the max |
|
* reader count value (RBITS) as a spinlock protecting overflow |
|
* updates. |
|
* |
|
* Waiters use a modified form of CLH lock used in |
|
* AbstractQueuedSynchronizer (see its internal documentation for |
|
* a fuller account), where each node is tagged (field mode) as |
|
* either a reader or writer. Sets of waiting readers are grouped |
|
* (linked) under a common node (field cowait) so act as a single |
|
* node with respect to most CLH mechanics. By virtue of the |
|
* queue structure, wait nodes need not actually carry sequence |
|
* numbers; we know each is greater than its predecessor. This |
|
* simplifies the scheduling policy to a mainly-FIFO scheme that |
|
* incorporates elements of Phase-Fair locks (see Brandenburg & |
|
* Anderson, especially http://www.cs.unc.edu/~bbb/diss/). In |
|
* particular, we use the phase-fair anti-barging rule: If an |
|
* incoming reader arrives while read lock is held but there is a |
|
* queued writer, this incoming reader is queued. (This rule is |
|
* responsible for some of the complexity of method acquireRead, |
|
* but without it, the lock becomes highly unfair.) Method release |
|
* does not (and sometimes cannot) itself wake up cowaiters. This |
|
* is done by the primary thread, but helped by any other threads |
|
* with nothing better to do in methods acquireRead and |
|
* acquireWrite. |
|
* |
|
* These rules apply to threads actually queued. All tryLock forms |
|
* opportunistically try to acquire locks regardless of preference |
|
* rules, and so may "barge" their way in. Randomized spinning is |
|
* used in the acquire methods to reduce (increasingly expensive) |
|
* context switching while also avoiding sustained memory |
|
* thrashing among many threads. We limit spins to the head of |
|
* queue. A thread spin-waits up to SPINS times (where each |
|
* iteration decreases spin count with 50% probability) before |
|
* blocking. If, upon wakening it fails to obtain lock, and is |
|
* still (or becomes) the first waiting thread (which indicates |
|
* that some other thread barged and obtained lock), it escalates |
|
* spins (up to MAX_HEAD_SPINS) to reduce the likelihood of |
|
* continually losing to barging threads. |
|
* |
|
* Nearly all of these mechanics are carried out in methods |
|
* acquireWrite and acquireRead, that, as typical of such code, |
|
* sprawl out because actions and retries rely on consistent sets |
|
* of locally cached reads. |
|
* |
|
* As noted in Boehm's paper (above), sequence validation (mainly |
|
* method validate()) requires stricter ordering rules than apply |
|
* to normal volatile reads (of "state"). To force orderings of |
|
* reads before a validation and the validation itself in those |
|
* cases where this is not already forced, we use |
|
* Unsafe.loadFence. |
|
* |
|
* The memory layout keeps lock state and queue pointers together |
|
* (normally on the same cache line). This usually works well for |
|
* read-mostly loads. In most other cases, the natural tendency of |
|
* adaptive-spin CLH locks to reduce memory contention lessens |
|
* motivation to further spread out contended locations, but might |
|
* be subject to future improvements. |
|
*/ |
|
|
|
private static final long serialVersionUID = -6001602636862214147L; |
|
|
|
|
|
private static final int NCPU = Runtime.getRuntime().availableProcessors(); |
|
|
|
|
|
private static final int SPINS = (NCPU > 1) ? 1 << 6 : 0; |
|
|
|
|
|
private static final int HEAD_SPINS = (NCPU > 1) ? 1 << 10 : 0; |
|
|
|
|
|
private static final int MAX_HEAD_SPINS = (NCPU > 1) ? 1 << 16 : 0; |
|
|
|
/** The period for yielding when waiting for overflow spinlock */ |
|
private static final int OVERFLOW_YIELD_RATE = 7; |
|
|
|
|
|
private static final int LG_READERS = 7; |
|
|
|
|
|
private static final long RUNIT = 1L; |
|
private static final long WBIT = 1L << LG_READERS; |
|
private static final long RBITS = WBIT - 1L; |
|
private static final long RFULL = RBITS - 1L; |
|
private static final long ABITS = RBITS | WBIT; |
|
private static final long SBITS = ~RBITS; |
|
|
|
|
|
private static final long ORIGIN = WBIT << 1; |
|
|
|
|
|
private static final long INTERRUPTED = 1L; |
|
|
|
|
|
private static final int WAITING = -1; |
|
private static final int CANCELLED = 1; |
|
|
|
|
|
private static final int RMODE = 0; |
|
private static final int WMODE = 1; |
|
|
|
|
|
static final class WNode { |
|
volatile WNode prev; |
|
volatile WNode next; |
|
volatile WNode cowait; |
|
volatile Thread thread; |
|
volatile int status; |
|
final int mode; |
|
WNode(int m, WNode p) { mode = m; prev = p; } |
|
} |
|
|
|
|
|
private transient volatile WNode whead; |
|
|
|
private transient volatile WNode wtail; |
|
|
|
|
|
transient ReadLockView readLockView; |
|
transient WriteLockView writeLockView; |
|
transient ReadWriteLockView readWriteLockView; |
|
|
|
|
|
private transient volatile long state; |
|
|
|
private transient int readerOverflow; |
|
|
|
|
|
|
|
*/ |
|
public StampedLock() { |
|
state = ORIGIN; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public long writeLock() { |
|
long s, next; |
|
return ((((s = state) & ABITS) == 0L && |
|
U.compareAndSwapLong(this, STATE, s, next = s + WBIT)) ? |
|
next : acquireWrite(false, 0L)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public long tryWriteLock() { |
|
long s, next; |
|
return ((((s = state) & ABITS) == 0L && |
|
U.compareAndSwapLong(this, STATE, s, next = s + WBIT)) ? |
|
next : 0L); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public long tryWriteLock(long time, TimeUnit unit) |
|
throws InterruptedException { |
|
long nanos = unit.toNanos(time); |
|
if (!Thread.interrupted()) { |
|
long next, deadline; |
|
if ((next = tryWriteLock()) != 0L) |
|
return next; |
|
if (nanos <= 0L) |
|
return 0L; |
|
if ((deadline = System.nanoTime() + nanos) == 0L) |
|
deadline = 1L; |
|
if ((next = acquireWrite(true, deadline)) != INTERRUPTED) |
|
return next; |
|
} |
|
throw new InterruptedException(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public long writeLockInterruptibly() throws InterruptedException { |
|
long next; |
|
if (!Thread.interrupted() && |
|
(next = acquireWrite(true, 0L)) != INTERRUPTED) |
|
return next; |
|
throw new InterruptedException(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public long readLock() { |
|
long s = state, next; |
|
return ((whead == wtail && (s & ABITS) < RFULL && |
|
U.compareAndSwapLong(this, STATE, s, next = s + RUNIT)) ? |
|
next : acquireRead(false, 0L)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public long tryReadLock() { |
|
for (;;) { |
|
long s, m, next; |
|
if ((m = (s = state) & ABITS) == WBIT) |
|
return 0L; |
|
else if (m < RFULL) { |
|
if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT)) |
|
return next; |
|
} |
|
else if ((next = tryIncReaderOverflow(s)) != 0L) |
|
return next; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public long tryReadLock(long time, TimeUnit unit) |
|
throws InterruptedException { |
|
long s, m, next, deadline; |
|
long nanos = unit.toNanos(time); |
|
if (!Thread.interrupted()) { |
|
if ((m = (s = state) & ABITS) != WBIT) { |
|
if (m < RFULL) { |
|
if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT)) |
|
return next; |
|
} |
|
else if ((next = tryIncReaderOverflow(s)) != 0L) |
|
return next; |
|
} |
|
if (nanos <= 0L) |
|
return 0L; |
|
if ((deadline = System.nanoTime() + nanos) == 0L) |
|
deadline = 1L; |
|
if ((next = acquireRead(true, deadline)) != INTERRUPTED) |
|
return next; |
|
} |
|
throw new InterruptedException(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public long readLockInterruptibly() throws InterruptedException { |
|
long next; |
|
if (!Thread.interrupted() && |
|
(next = acquireRead(true, 0L)) != INTERRUPTED) |
|
return next; |
|
throw new InterruptedException(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public long tryOptimisticRead() { |
|
long s; |
|
return (((s = state) & WBIT) == 0L) ? (s & SBITS) : 0L; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean validate(long stamp) { |
|
U.loadFence(); |
|
return (stamp & SBITS) == (state & SBITS); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void unlockWrite(long stamp) { |
|
WNode h; |
|
if (state != stamp || (stamp & WBIT) == 0L) |
|
throw new IllegalMonitorStateException(); |
|
state = (stamp += WBIT) == 0L ? ORIGIN : stamp; |
|
if ((h = whead) != null && h.status != 0) |
|
release(h); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void unlockRead(long stamp) { |
|
long s, m; WNode h; |
|
for (;;) { |
|
if (((s = state) & SBITS) != (stamp & SBITS) || |
|
(stamp & ABITS) == 0L || (m = s & ABITS) == 0L || m == WBIT) |
|
throw new IllegalMonitorStateException(); |
|
if (m < RFULL) { |
|
if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) { |
|
if (m == RUNIT && (h = whead) != null && h.status != 0) |
|
release(h); |
|
break; |
|
} |
|
} |
|
else if (tryDecReaderOverflow(s) != 0L) |
|
break; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void unlock(long stamp) { |
|
long a = stamp & ABITS, m, s; WNode h; |
|
while (((s = state) & SBITS) == (stamp & SBITS)) { |
|
if ((m = s & ABITS) == 0L) |
|
break; |
|
else if (m == WBIT) { |
|
if (a != m) |
|
break; |
|
state = (s += WBIT) == 0L ? ORIGIN : s; |
|
if ((h = whead) != null && h.status != 0) |
|
release(h); |
|
return; |
|
} |
|
else if (a == 0L || a >= WBIT) |
|
break; |
|
else if (m < RFULL) { |
|
if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) { |
|
if (m == RUNIT && (h = whead) != null && h.status != 0) |
|
release(h); |
|
return; |
|
} |
|
} |
|
else if (tryDecReaderOverflow(s) != 0L) |
|
return; |
|
} |
|
throw new IllegalMonitorStateException(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public long tryConvertToWriteLock(long stamp) { |
|
long a = stamp & ABITS, m, s, next; |
|
while (((s = state) & SBITS) == (stamp & SBITS)) { |
|
if ((m = s & ABITS) == 0L) { |
|
if (a != 0L) |
|
break; |
|
if (U.compareAndSwapLong(this, STATE, s, next = s + WBIT)) |
|
return next; |
|
} |
|
else if (m == WBIT) { |
|
if (a != m) |
|
break; |
|
return stamp; |
|
} |
|
else if (m == RUNIT && a != 0L) { |
|
if (U.compareAndSwapLong(this, STATE, s, |
|
next = s - RUNIT + WBIT)) |
|
return next; |
|
} |
|
else |
|
break; |
|
} |
|
return 0L; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public long tryConvertToReadLock(long stamp) { |
|
long a = stamp & ABITS, m, s, next; WNode h; |
|
while (((s = state) & SBITS) == (stamp & SBITS)) { |
|
if ((m = s & ABITS) == 0L) { |
|
if (a != 0L) |
|
break; |
|
else if (m < RFULL) { |
|
if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT)) |
|
return next; |
|
} |
|
else if ((next = tryIncReaderOverflow(s)) != 0L) |
|
return next; |
|
} |
|
else if (m == WBIT) { |
|
if (a != m) |
|
break; |
|
state = next = s + (WBIT + RUNIT); |
|
if ((h = whead) != null && h.status != 0) |
|
release(h); |
|
return next; |
|
} |
|
else if (a != 0L && a < WBIT) |
|
return stamp; |
|
else |
|
break; |
|
} |
|
return 0L; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public long tryConvertToOptimisticRead(long stamp) { |
|
long a = stamp & ABITS, m, s, next; WNode h; |
|
U.loadFence(); |
|
for (;;) { |
|
if (((s = state) & SBITS) != (stamp & SBITS)) |
|
break; |
|
if ((m = s & ABITS) == 0L) { |
|
if (a != 0L) |
|
break; |
|
return s; |
|
} |
|
else if (m == WBIT) { |
|
if (a != m) |
|
break; |
|
state = next = (s += WBIT) == 0L ? ORIGIN : s; |
|
if ((h = whead) != null && h.status != 0) |
|
release(h); |
|
return next; |
|
} |
|
else if (a == 0L || a >= WBIT) |
|
break; |
|
else if (m < RFULL) { |
|
if (U.compareAndSwapLong(this, STATE, s, next = s - RUNIT)) { |
|
if (m == RUNIT && (h = whead) != null && h.status != 0) |
|
release(h); |
|
return next & SBITS; |
|
} |
|
} |
|
else if ((next = tryDecReaderOverflow(s)) != 0L) |
|
return next & SBITS; |
|
} |
|
return 0L; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean tryUnlockWrite() { |
|
long s; WNode h; |
|
if (((s = state) & WBIT) != 0L) { |
|
state = (s += WBIT) == 0L ? ORIGIN : s; |
|
if ((h = whead) != null && h.status != 0) |
|
release(h); |
|
return true; |
|
} |
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean tryUnlockRead() { |
|
long s, m; WNode h; |
|
while ((m = (s = state) & ABITS) != 0L && m < WBIT) { |
|
if (m < RFULL) { |
|
if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) { |
|
if (m == RUNIT && (h = whead) != null && h.status != 0) |
|
release(h); |
|
return true; |
|
} |
|
} |
|
else if (tryDecReaderOverflow(s) != 0L) |
|
return true; |
|
} |
|
return false; |
|
} |
|
|
|
// status monitoring methods |
|
|
|
|
|
|
|
|
|
*/ |
|
private int getReadLockCount(long s) { |
|
long readers; |
|
if ((readers = s & RBITS) >= RFULL) |
|
readers = RFULL + readerOverflow; |
|
return (int) readers; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean isWriteLocked() { |
|
return (state & WBIT) != 0L; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean isReadLocked() { |
|
return (state & RBITS) != 0L; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public int getReadLockCount() { |
|
return getReadLockCount(state); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String toString() { |
|
long s = state; |
|
return super.toString() + |
|
((s & ABITS) == 0L ? "[Unlocked]" : |
|
(s & WBIT) != 0L ? "[Write-locked]" : |
|
"[Read-locks:" + getReadLockCount(s) + "]"); |
|
} |
|
|
|
// views |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Lock asReadLock() { |
|
ReadLockView v; |
|
return ((v = readLockView) != null ? v : |
|
(readLockView = new ReadLockView())); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Lock asWriteLock() { |
|
WriteLockView v; |
|
return ((v = writeLockView) != null ? v : |
|
(writeLockView = new WriteLockView())); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public ReadWriteLock asReadWriteLock() { |
|
ReadWriteLockView v; |
|
return ((v = readWriteLockView) != null ? v : |
|
(readWriteLockView = new ReadWriteLockView())); |
|
} |
|
|
|
// view classes |
|
|
|
final class ReadLockView implements Lock { |
|
public void lock() { readLock(); } |
|
public void lockInterruptibly() throws InterruptedException { |
|
readLockInterruptibly(); |
|
} |
|
public boolean tryLock() { return tryReadLock() != 0L; } |
|
public boolean tryLock(long time, TimeUnit unit) |
|
throws InterruptedException { |
|
return tryReadLock(time, unit) != 0L; |
|
} |
|
public void unlock() { unstampedUnlockRead(); } |
|
public Condition newCondition() { |
|
throw new UnsupportedOperationException(); |
|
} |
|
} |
|
|
|
final class WriteLockView implements Lock { |
|
public void lock() { writeLock(); } |
|
public void lockInterruptibly() throws InterruptedException { |
|
writeLockInterruptibly(); |
|
} |
|
public boolean tryLock() { return tryWriteLock() != 0L; } |
|
public boolean tryLock(long time, TimeUnit unit) |
|
throws InterruptedException { |
|
return tryWriteLock(time, unit) != 0L; |
|
} |
|
public void unlock() { unstampedUnlockWrite(); } |
|
public Condition newCondition() { |
|
throw new UnsupportedOperationException(); |
|
} |
|
} |
|
|
|
final class ReadWriteLockView implements ReadWriteLock { |
|
public Lock readLock() { return asReadLock(); } |
|
public Lock writeLock() { return asWriteLock(); } |
|
} |
|
|
|
// Unlock methods without stamp argument checks for view classes. |
|
// Needed because view-class lock methods throw away stamps. |
|
|
|
final void unstampedUnlockWrite() { |
|
WNode h; long s; |
|
if (((s = state) & WBIT) == 0L) |
|
throw new IllegalMonitorStateException(); |
|
state = (s += WBIT) == 0L ? ORIGIN : s; |
|
if ((h = whead) != null && h.status != 0) |
|
release(h); |
|
} |
|
|
|
final void unstampedUnlockRead() { |
|
for (;;) { |
|
long s, m; WNode h; |
|
if ((m = (s = state) & ABITS) == 0L || m >= WBIT) |
|
throw new IllegalMonitorStateException(); |
|
else if (m < RFULL) { |
|
if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) { |
|
if (m == RUNIT && (h = whead) != null && h.status != 0) |
|
release(h); |
|
break; |
|
} |
|
} |
|
else if (tryDecReaderOverflow(s) != 0L) |
|
break; |
|
} |
|
} |
|
|
|
private void readObject(java.io.ObjectInputStream s) |
|
throws java.io.IOException, ClassNotFoundException { |
|
s.defaultReadObject(); |
|
state = ORIGIN; |
|
} |
|
|
|
// internals |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private long tryIncReaderOverflow(long s) { |
|
|
|
if ((s & ABITS) == RFULL) { |
|
if (U.compareAndSwapLong(this, STATE, s, s | RBITS)) { |
|
++readerOverflow; |
|
state = s; |
|
return s; |
|
} |
|
} |
|
else if ((LockSupport.nextSecondarySeed() & |
|
OVERFLOW_YIELD_RATE) == 0) |
|
Thread.yield(); |
|
return 0L; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private long tryDecReaderOverflow(long s) { |
|
|
|
if ((s & ABITS) == RFULL) { |
|
if (U.compareAndSwapLong(this, STATE, s, s | RBITS)) { |
|
int r; long next; |
|
if ((r = readerOverflow) > 0) { |
|
readerOverflow = r - 1; |
|
next = s; |
|
} |
|
else |
|
next = s - RUNIT; |
|
state = next; |
|
return next; |
|
} |
|
} |
|
else if ((LockSupport.nextSecondarySeed() & |
|
OVERFLOW_YIELD_RATE) == 0) |
|
Thread.yield(); |
|
return 0L; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private void release(WNode h) { |
|
if (h != null) { |
|
WNode q; Thread w; |
|
U.compareAndSwapInt(h, WSTATUS, WAITING, 0); |
|
if ((q = h.next) == null || q.status == CANCELLED) { |
|
for (WNode t = wtail; t != null && t != h; t = t.prev) |
|
if (t.status <= 0) |
|
q = t; |
|
} |
|
if (q != null && (w = q.thread) != null) |
|
U.unpark(w); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private long acquireWrite(boolean interruptible, long deadline) { |
|
WNode node = null, p; |
|
for (int spins = -1;;) { |
|
long m, s, ns; |
|
if ((m = (s = state) & ABITS) == 0L) { |
|
if (U.compareAndSwapLong(this, STATE, s, ns = s + WBIT)) |
|
return ns; |
|
} |
|
else if (spins < 0) |
|
spins = (m == WBIT && wtail == whead) ? SPINS : 0; |
|
else if (spins > 0) { |
|
if (LockSupport.nextSecondarySeed() >= 0) |
|
--spins; |
|
} |
|
else if ((p = wtail) == null) { |
|
WNode hd = new WNode(WMODE, null); |
|
if (U.compareAndSwapObject(this, WHEAD, null, hd)) |
|
wtail = hd; |
|
} |
|
else if (node == null) |
|
node = new WNode(WMODE, p); |
|
else if (node.prev != p) |
|
node.prev = p; |
|
else if (U.compareAndSwapObject(this, WTAIL, p, node)) { |
|
p.next = node; |
|
break; |
|
} |
|
} |
|
|
|
for (int spins = -1;;) { |
|
WNode h, np, pp; int ps; |
|
if ((h = whead) == p) { |
|
if (spins < 0) |
|
spins = HEAD_SPINS; |
|
else if (spins < MAX_HEAD_SPINS) |
|
spins <<= 1; |
|
for (int k = spins;;) { |
|
long s, ns; |
|
if (((s = state) & ABITS) == 0L) { |
|
if (U.compareAndSwapLong(this, STATE, s, |
|
ns = s + WBIT)) { |
|
whead = node; |
|
node.prev = null; |
|
return ns; |
|
} |
|
} |
|
else if (LockSupport.nextSecondarySeed() >= 0 && |
|
--k <= 0) |
|
break; |
|
} |
|
} |
|
else if (h != null) { |
|
WNode c; Thread w; |
|
while ((c = h.cowait) != null) { |
|
if (U.compareAndSwapObject(h, WCOWAIT, c, c.cowait) && |
|
(w = c.thread) != null) |
|
U.unpark(w); |
|
} |
|
} |
|
if (whead == h) { |
|
if ((np = node.prev) != p) { |
|
if (np != null) |
|
(p = np).next = node; |
|
} |
|
else if ((ps = p.status) == 0) |
|
U.compareAndSwapInt(p, WSTATUS, 0, WAITING); |
|
else if (ps == CANCELLED) { |
|
if ((pp = p.prev) != null) { |
|
node.prev = pp; |
|
pp.next = node; |
|
} |
|
} |
|
else { |
|
long time; |
|
if (deadline == 0L) |
|
time = 0L; |
|
else if ((time = deadline - System.nanoTime()) <= 0L) |
|
return cancelWaiter(node, node, false); |
|
Thread wt = Thread.currentThread(); |
|
U.putObject(wt, PARKBLOCKER, this); |
|
node.thread = wt; |
|
if (p.status < 0 && (p != h || (state & ABITS) != 0L) && |
|
whead == h && node.prev == p) |
|
U.park(false, time); |
|
node.thread = null; |
|
U.putObject(wt, PARKBLOCKER, null); |
|
if (interruptible && Thread.interrupted()) |
|
return cancelWaiter(node, node, true); |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private long acquireRead(boolean interruptible, long deadline) { |
|
WNode node = null, p; |
|
for (int spins = -1;;) { |
|
WNode h; |
|
if ((h = whead) == (p = wtail)) { |
|
for (long m, s, ns;;) { |
|
if ((m = (s = state) & ABITS) < RFULL ? |
|
U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT) : |
|
(m < WBIT && (ns = tryIncReaderOverflow(s)) != 0L)) |
|
return ns; |
|
else if (m >= WBIT) { |
|
if (spins > 0) { |
|
if (LockSupport.nextSecondarySeed() >= 0) |
|
--spins; |
|
} |
|
else { |
|
if (spins == 0) { |
|
WNode nh = whead, np = wtail; |
|
if ((nh == h && np == p) || (h = nh) != (p = np)) |
|
break; |
|
} |
|
spins = SPINS; |
|
} |
|
} |
|
} |
|
} |
|
if (p == null) { |
|
WNode hd = new WNode(WMODE, null); |
|
if (U.compareAndSwapObject(this, WHEAD, null, hd)) |
|
wtail = hd; |
|
} |
|
else if (node == null) |
|
node = new WNode(RMODE, p); |
|
else if (h == p || p.mode != RMODE) { |
|
if (node.prev != p) |
|
node.prev = p; |
|
else if (U.compareAndSwapObject(this, WTAIL, p, node)) { |
|
p.next = node; |
|
break; |
|
} |
|
} |
|
else if (!U.compareAndSwapObject(p, WCOWAIT, |
|
node.cowait = p.cowait, node)) |
|
node.cowait = null; |
|
else { |
|
for (;;) { |
|
WNode pp, c; Thread w; |
|
if ((h = whead) != null && (c = h.cowait) != null && |
|
U.compareAndSwapObject(h, WCOWAIT, c, c.cowait) && |
|
(w = c.thread) != null) |
|
U.unpark(w); |
|
if (h == (pp = p.prev) || h == p || pp == null) { |
|
long m, s, ns; |
|
do { |
|
if ((m = (s = state) & ABITS) < RFULL ? |
|
U.compareAndSwapLong(this, STATE, s, |
|
ns = s + RUNIT) : |
|
(m < WBIT && |
|
(ns = tryIncReaderOverflow(s)) != 0L)) |
|
return ns; |
|
} while (m < WBIT); |
|
} |
|
if (whead == h && p.prev == pp) { |
|
long time; |
|
if (pp == null || h == p || p.status > 0) { |
|
node = null; |
|
break; |
|
} |
|
if (deadline == 0L) |
|
time = 0L; |
|
else if ((time = deadline - System.nanoTime()) <= 0L) |
|
return cancelWaiter(node, p, false); |
|
Thread wt = Thread.currentThread(); |
|
U.putObject(wt, PARKBLOCKER, this); |
|
node.thread = wt; |
|
if ((h != pp || (state & ABITS) == WBIT) && |
|
whead == h && p.prev == pp) |
|
U.park(false, time); |
|
node.thread = null; |
|
U.putObject(wt, PARKBLOCKER, null); |
|
if (interruptible && Thread.interrupted()) |
|
return cancelWaiter(node, p, true); |
|
} |
|
} |
|
} |
|
} |
|
|
|
for (int spins = -1;;) { |
|
WNode h, np, pp; int ps; |
|
if ((h = whead) == p) { |
|
if (spins < 0) |
|
spins = HEAD_SPINS; |
|
else if (spins < MAX_HEAD_SPINS) |
|
spins <<= 1; |
|
for (int k = spins;;) { |
|
long m, s, ns; |
|
if ((m = (s = state) & ABITS) < RFULL ? |
|
U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT) : |
|
(m < WBIT && (ns = tryIncReaderOverflow(s)) != 0L)) { |
|
WNode c; Thread w; |
|
whead = node; |
|
node.prev = null; |
|
while ((c = node.cowait) != null) { |
|
if (U.compareAndSwapObject(node, WCOWAIT, |
|
c, c.cowait) && |
|
(w = c.thread) != null) |
|
U.unpark(w); |
|
} |
|
return ns; |
|
} |
|
else if (m >= WBIT && |
|
LockSupport.nextSecondarySeed() >= 0 && --k <= 0) |
|
break; |
|
} |
|
} |
|
else if (h != null) { |
|
WNode c; Thread w; |
|
while ((c = h.cowait) != null) { |
|
if (U.compareAndSwapObject(h, WCOWAIT, c, c.cowait) && |
|
(w = c.thread) != null) |
|
U.unpark(w); |
|
} |
|
} |
|
if (whead == h) { |
|
if ((np = node.prev) != p) { |
|
if (np != null) |
|
(p = np).next = node; |
|
} |
|
else if ((ps = p.status) == 0) |
|
U.compareAndSwapInt(p, WSTATUS, 0, WAITING); |
|
else if (ps == CANCELLED) { |
|
if ((pp = p.prev) != null) { |
|
node.prev = pp; |
|
pp.next = node; |
|
} |
|
} |
|
else { |
|
long time; |
|
if (deadline == 0L) |
|
time = 0L; |
|
else if ((time = deadline - System.nanoTime()) <= 0L) |
|
return cancelWaiter(node, node, false); |
|
Thread wt = Thread.currentThread(); |
|
U.putObject(wt, PARKBLOCKER, this); |
|
node.thread = wt; |
|
if (p.status < 0 && |
|
(p != h || (state & ABITS) == WBIT) && |
|
whead == h && node.prev == p) |
|
U.park(false, time); |
|
node.thread = null; |
|
U.putObject(wt, PARKBLOCKER, null); |
|
if (interruptible && Thread.interrupted()) |
|
return cancelWaiter(node, node, true); |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private long cancelWaiter(WNode node, WNode group, boolean interrupted) { |
|
if (node != null && group != null) { |
|
Thread w; |
|
node.status = CANCELLED; |
|
|
|
for (WNode p = group, q; (q = p.cowait) != null;) { |
|
if (q.status == CANCELLED) { |
|
U.compareAndSwapObject(p, WCOWAIT, q, q.cowait); |
|
p = group; |
|
} |
|
else |
|
p = q; |
|
} |
|
if (group == node) { |
|
for (WNode r = group.cowait; r != null; r = r.cowait) { |
|
if ((w = r.thread) != null) |
|
U.unpark(w); |
|
} |
|
for (WNode pred = node.prev; pred != null; ) { // unsplice |
|
WNode succ, pp; |
|
while ((succ = node.next) == null || |
|
succ.status == CANCELLED) { |
|
WNode q = null; |
|
for (WNode t = wtail; t != null && t != node; t = t.prev) |
|
if (t.status != CANCELLED) |
|
q = t; |
|
if (succ == q || |
|
U.compareAndSwapObject(node, WNEXT, |
|
succ, succ = q)) { |
|
if (succ == null && node == wtail) |
|
U.compareAndSwapObject(this, WTAIL, node, pred); |
|
break; |
|
} |
|
} |
|
if (pred.next == node) |
|
U.compareAndSwapObject(pred, WNEXT, node, succ); |
|
if (succ != null && (w = succ.thread) != null) { |
|
succ.thread = null; |
|
U.unpark(w); |
|
} |
|
if (pred.status != CANCELLED || (pp = pred.prev) == null) |
|
break; |
|
node.prev = pp; |
|
U.compareAndSwapObject(pp, WNEXT, pred, succ); |
|
pred = pp; |
|
} |
|
} |
|
} |
|
WNode h; |
|
while ((h = whead) != null) { |
|
long s; WNode q; |
|
if ((q = h.next) == null || q.status == CANCELLED) { |
|
for (WNode t = wtail; t != null && t != h; t = t.prev) |
|
if (t.status <= 0) |
|
q = t; |
|
} |
|
if (h == whead) { |
|
if (q != null && h.status == 0 && |
|
((s = state) & ABITS) != WBIT && |
|
(s == 0L || q.mode == RMODE)) |
|
release(h); |
|
break; |
|
} |
|
} |
|
return (interrupted || Thread.interrupted()) ? INTERRUPTED : 0L; |
|
} |
|
|
|
|
|
private static final sun.misc.Unsafe U; |
|
private static final long STATE; |
|
private static final long WHEAD; |
|
private static final long WTAIL; |
|
private static final long WNEXT; |
|
private static final long WSTATUS; |
|
private static final long WCOWAIT; |
|
private static final long PARKBLOCKER; |
|
|
|
static { |
|
try { |
|
U = sun.misc.Unsafe.getUnsafe(); |
|
Class<?> k = StampedLock.class; |
|
Class<?> wk = WNode.class; |
|
STATE = U.objectFieldOffset |
|
(k.getDeclaredField("state")); |
|
WHEAD = U.objectFieldOffset |
|
(k.getDeclaredField("whead")); |
|
WTAIL = U.objectFieldOffset |
|
(k.getDeclaredField("wtail")); |
|
WSTATUS = U.objectFieldOffset |
|
(wk.getDeclaredField("status")); |
|
WNEXT = U.objectFieldOffset |
|
(wk.getDeclaredField("next")); |
|
WCOWAIT = U.objectFieldOffset |
|
(wk.getDeclaredField("cowait")); |
|
Class<?> tk = Thread.class; |
|
PARKBLOCKER = U.objectFieldOffset |
|
(tk.getDeclaredField("parkBlocker")); |
|
|
|
} catch (Exception e) { |
|
throw new Error(e); |
|
} |
|
} |
|
} |