8 |
|
|
9 |
|
import java.util.concurrent.ThreadLocalRandom; |
10 |
|
import java.util.concurrent.TimeUnit; |
11 |
< |
import java.util.concurrent.locks.*; |
11 |
> |
import java.util.concurrent.locks.Lock; |
12 |
> |
import java.util.concurrent.locks.Condition; |
13 |
> |
import java.util.concurrent.locks.ReadWriteLock; |
14 |
> |
import java.util.concurrent.locks.LockSupport; |
15 |
|
|
16 |
|
/** |
17 |
|
* A capability-based lock with three modes for controlling read/write |
40 |
|
* <li><b>Optimistic Reading.</b> Method {@link #tryOptimisticRead} |
41 |
|
* returns a non-zero stamp only if the lock is not currently held |
42 |
|
* in write mode. Method {@link #validate} returns true if the lock |
43 |
< |
* has not since been acquired in write mode. This mode can be |
44 |
< |
* thought of as an extremely weak version of a read-lock, that can |
45 |
< |
* be broken by a writer at any time. The use of optimistic mode |
46 |
< |
* for short read-only code segments often reduces contention and |
47 |
< |
* improves throughput. However, its use is inherently fragile. |
48 |
< |
* Optimistic read sections should only read fields and hold them in |
49 |
< |
* local variables for later use after validation. Fields read while |
50 |
< |
* in optimistic mode may be wildly inconsistent, so usage applies |
51 |
< |
* only when you are familiar enough with data representations to |
52 |
< |
* check consistency and/or repeatedly invoke method {@code |
53 |
< |
* validate()}. For example, such steps are typically required when |
54 |
< |
* first reading an object or array reference, and then accessing |
55 |
< |
* one of its fields, elements or methods. </li> |
43 |
> |
* has not been acquired in write mode since obtaining a given |
44 |
> |
* stamp. This mode can be thought of as an extremely weak version |
45 |
> |
* of a read-lock, that can be broken by a writer at any time. The |
46 |
> |
* use of optimistic mode for short read-only code segments often |
47 |
> |
* reduces contention and improves throughput. However, its use is |
48 |
> |
* inherently fragile. Optimistic read sections should only read |
49 |
> |
* fields and hold them in local variables for later use after |
50 |
> |
* validation. Fields read while in optimistic mode may be wildly |
51 |
> |
* inconsistent, so usage applies only when you are familiar enough |
52 |
> |
* with data representations to check consistency and/or repeatedly |
53 |
> |
* invoke method {@code validate()}. For example, such steps are |
54 |
> |
* typically required when first reading an object or array |
55 |
> |
* reference, and then accessing one of its fields, elements or |
56 |
> |
* methods. </li> |
57 |
|
* |
58 |
|
* </ul> |
59 |
|
* |
202 |
|
* |
203 |
|
* Waiters use a modified form of CLH lock used in |
204 |
|
* AbstractQueuedSynchronizer (see its internal documentation for |
205 |
< |
* a fuller account), where each node it tagged (field mode) as |
205 |
> |
* a fuller account), where each node is tagged (field mode) as |
206 |
|
* either a reader or writer. Sets of waiting readers are grouped |
207 |
|
* (linked) under a common node (field cowait) so act as a single |
208 |
< |
* node with respect to most CLH mechanics. By virtue of its |
209 |
< |
* structure, wait nodes need not actually carry sequence numbers; |
210 |
< |
* we know each is >= its predecessor. These queue mechanics |
211 |
< |
* simplify the scheduling policy to a mainly-FIFO scheme that |
208 |
> |
* node with respect to most CLH mechanics. By virtue of the |
209 |
> |
* queue structure, wait nodes need not actually carry sequence |
210 |
> |
* numbers; we know each is greater than its predecessor. This |
211 |
> |
* simplifies the scheduling policy to a mainly-FIFO scheme that |
212 |
|
* incorporates elements of Phase-Fair locks (see Brandenburg & |
213 |
|
* Anderson, especially http://www.cs.unc.edu/~bbb/diss/). In |
214 |
|
* particular, we use the phase-fair anti-barging rule: If an |
233 |
|
* |
234 |
|
* Nearly all of these mechanics are carried out in methods |
235 |
|
* acquireWrite and acquireRead, that, as typical of such code, |
236 |
< |
* sprawl out because actions and retries rely on consitent sets |
237 |
< |
* of locally cahced reads. |
236 |
> |
* sprawl out because actions and retries rely on consistent sets |
237 |
> |
* of locally cached reads. |
238 |
|
* |
239 |
|
* As noted in Boehm's paper (above), sequence validation (mainly |
240 |
|
* method validate()) requires stricter ordering rules than apply |
253 |
|
* motivation to further spread out contended locations, but might |
254 |
|
* be subject to future improvements. |
255 |
|
*/ |
252 |
– |
|
256 |
|
private static final long serialVersionUID = -6001602636862214147L; |
257 |
|
|
258 |
|
/** Number of processors, for spin control */ |
332 |
|
* @return a stamp that can be used to unlock or convert mode |
333 |
|
*/ |
334 |
|
public long writeLock() { |
335 |
< |
long s, next; // bypass acquireWrite in fully onlocked case only |
335 |
> |
long s, next; // bypass acquireWrite in fully unlocked case only |
336 |
|
return ((((s = state) & ABITS) == 0L && |
337 |
|
U.compareAndSwapLong(this, STATE, s, next = s + WBIT)) ? |
338 |
|
next : acquireWrite(false, 0L)); |
367 |
|
long nanos = unit.toNanos(time); |
368 |
|
if (!Thread.interrupted()) { |
369 |
|
long next, deadline; |
370 |
< |
if ((next = tryWriteLock()) != 0) |
370 |
> |
if ((next = tryWriteLock()) != 0L) |
371 |
|
return next; |
372 |
|
if (nanos <= 0L) |
373 |
|
return 0L; |
404 |
|
* @return a stamp that can be used to unlock or convert mode |
405 |
|
*/ |
406 |
|
public long readLock() { |
407 |
< |
long s, next; // bypass acquireRead on fully onlocked case only |
407 |
> |
long s, next; // bypass acquireRead on fully unlocked case only |
408 |
|
return ((((s = state) & ABITS) == 0L && |
409 |
|
U.compareAndSwapLong(this, STATE, s, next = s + RUNIT)) ? |
410 |
|
next : acquireRead(false, 0L)); |
446 |
|
long next, deadline; |
447 |
|
long nanos = unit.toNanos(time); |
448 |
|
if (!Thread.interrupted()) { |
449 |
< |
if ((next = tryReadLock()) != 0) |
449 |
> |
if ((next = tryReadLock()) != 0L) |
450 |
|
return next; |
451 |
|
if (nanos <= 0L) |
452 |
|
return 0L; |
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. |
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 acquired |
499 |
|
* since issuance of the given stamp; else false |
529 |
|
* not match the current state of this lock |
530 |
|
*/ |
531 |
|
public void unlockRead(long stamp) { |
532 |
< |
long s, m; WNode h; |
533 |
< |
if ((stamp & RBITS) != 0L) { |
534 |
< |
while (((s = state) & SBITS) == (stamp & SBITS)) { |
535 |
< |
if ((m = s & ABITS) == 0L) |
532 |
> |
long s, m; WNode h; |
533 |
> |
for (;;) { |
534 |
> |
if (((s = state) & SBITS) != (stamp & SBITS) || |
535 |
> |
(stamp & ABITS) == 0L || (m = s & ABITS) == 0L || m == WBIT) |
536 |
> |
throw new IllegalMonitorStateException(); |
537 |
> |
if (m < RFULL) { |
538 |
> |
if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) { |
539 |
> |
if (m == RUNIT && (h = whead) != null && h.status != 0) |
540 |
> |
release(h); |
541 |
|
break; |
532 |
– |
else if (m < RFULL) { |
533 |
– |
if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) { |
534 |
– |
if (m == RUNIT && (h = whead) != null && h.status != 0) |
535 |
– |
release(h); |
536 |
– |
return; |
537 |
– |
} |
542 |
|
} |
539 |
– |
else if (m >= WBIT) |
540 |
– |
break; |
541 |
– |
else if (tryDecReaderOverflow(s) != 0L) |
542 |
– |
return; |
543 |
|
} |
544 |
+ |
else if (tryDecReaderOverflow(s) != 0L) |
545 |
+ |
break; |
546 |
|
} |
545 |
– |
throw new IllegalMonitorStateException(); |
547 |
|
} |
548 |
|
|
549 |
|
/** |
826 |
|
throws InterruptedException { |
827 |
|
return tryReadLock(time, unit) != 0L; |
828 |
|
} |
829 |
< |
// note that we give up ability to check mode so just use current state |
829 |
< |
public void unlock() { unlockRead(state); } |
829 |
> |
public void unlock() { unstampedUnlockRead(); } |
830 |
|
public Condition newCondition() { |
831 |
|
throw new UnsupportedOperationException(); |
832 |
|
} |
842 |
|
throws InterruptedException { |
843 |
|
return tryWriteLock(time, unit) != 0L; |
844 |
|
} |
845 |
< |
public void unlock() { unlockWrite(state); } |
845 |
> |
public void unlock() { unstampedUnlockWrite(); } |
846 |
|
public Condition newCondition() { |
847 |
|
throw new UnsupportedOperationException(); |
848 |
|
} |
853 |
|
public Lock writeLock() { return asWriteLock(); } |
854 |
|
} |
855 |
|
|
856 |
+ |
// Unlock methods without stamp argument checks for view classes. |
857 |
+ |
// Needed because view-class lock methods throw away stamps. |
858 |
+ |
|
859 |
+ |
final void unstampedUnlockWrite() { |
860 |
+ |
WNode h; long s; |
861 |
+ |
if (((s = state) & WBIT) == 0L) |
862 |
+ |
throw new IllegalMonitorStateException(); |
863 |
+ |
state = (s += WBIT) == 0L ? ORIGIN : s; |
864 |
+ |
if ((h = whead) != null && h.status != 0) |
865 |
+ |
release(h); |
866 |
+ |
} |
867 |
+ |
|
868 |
+ |
final void unstampedUnlockRead() { |
869 |
+ |
for (;;) { |
870 |
+ |
long s, m; WNode h; |
871 |
+ |
if ((m = (s = state) & ABITS) == 0L || m >= WBIT) |
872 |
+ |
throw new IllegalMonitorStateException(); |
873 |
+ |
else if (m < RFULL) { |
874 |
+ |
if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) { |
875 |
+ |
if (m == RUNIT && (h = whead) != null && h.status != 0) |
876 |
+ |
release(h); |
877 |
+ |
break; |
878 |
+ |
} |
879 |
+ |
} |
880 |
+ |
else if (tryDecReaderOverflow(s) != 0L) |
881 |
+ |
break; |
882 |
+ |
} |
883 |
+ |
} |
884 |
+ |
|
885 |
|
// internals |
886 |
|
|
887 |
|
/** |
1006 |
|
(p = np).next = node; // stale |
1007 |
|
if (whead == p) { |
1008 |
|
for (int k = spins;;) { // spin at head |
1009 |
< |
if (((s = state) & ABITS) == 0L && |
1010 |
< |
U.compareAndSwapLong(this, STATE, s, ns = s + WBIT)) { |
1011 |
< |
whead = node; |
1012 |
< |
node.prev = null; |
1013 |
< |
return ns; |
1009 |
> |
if (((s = state) & ABITS) == 0L) { |
1010 |
> |
if (U.compareAndSwapLong(this, STATE, s, ns = s+WBIT)) { |
1011 |
> |
whead = node; |
1012 |
> |
node.prev = null; |
1013 |
> |
return ns; |
1014 |
> |
} |
1015 |
|
} |
1016 |
|
else if (ThreadLocalRandom.current().nextInt() >= 0 && |
1017 |
|
--k <= 0) |
1036 |
|
return cancelWaiter(node, null, false); |
1037 |
|
node.thread = Thread.currentThread(); |
1038 |
|
if (node.prev == p && p.status == WAITING && // recheck |
1039 |
< |
(p != whead || (state & ABITS) != 0L)) { |
1039 |
> |
(p != whead || (state & ABITS) != 0L)) |
1040 |
|
U.park(false, time); |
1011 |
– |
if (interruptible && Thread.interrupted()) |
1012 |
– |
return cancelWaiter(node, null, true); |
1013 |
– |
} |
1041 |
|
node.thread = null; |
1042 |
+ |
if (interruptible && Thread.interrupted()) |
1043 |
+ |
return cancelWaiter(node, null, true); |
1044 |
|
} |
1045 |
|
} |
1046 |
|
} |
1062 |
|
if (group == null && (h = whead) != null && |
1063 |
|
(q = h.next) != null && q.mode != RMODE) |
1064 |
|
break; |
1065 |
< |
if ((m = (s = state) & ABITS) == WBIT) |
1037 |
< |
break; |
1038 |
< |
if (m < RFULL ? |
1065 |
> |
if ((m = (s = state) & ABITS) < RFULL ? |
1066 |
|
U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT) : |
1067 |
< |
(ns = tryIncReaderOverflow(s)) != 0L) { |
1067 |
> |
(m < WBIT && (ns = tryIncReaderOverflow(s)) != 0L)) { |
1068 |
|
if (group != null) { // help release others |
1069 |
|
for (WNode r = group;;) { |
1070 |
|
if ((w = r.thread) != null) { |
1078 |
|
} |
1079 |
|
return ns; |
1080 |
|
} |
1081 |
+ |
if (m >= WBIT) |
1082 |
+ |
break; |
1083 |
|
} |
1084 |
|
if (spins > 0) { |
1085 |
|
if (ThreadLocalRandom.current().nextInt() >= 0) |
1103 |
|
node.cowait = p.cowait, node)) { |
1104 |
|
node.thread = Thread.currentThread(); |
1105 |
|
for (long time;;) { |
1106 |
+ |
if (interruptible && Thread.interrupted()) |
1107 |
+ |
return cancelWaiter(node, p, true); |
1108 |
|
if (deadline == 0L) |
1109 |
|
time = 0L; |
1110 |
|
else if ((time = deadline - System.nanoTime()) <= 0L) |
1119 |
|
if (node.thread == null) // must recheck |
1120 |
|
break; |
1121 |
|
U.park(false, time); |
1091 |
– |
if (interruptible && Thread.interrupted()) |
1092 |
– |
return cancelWaiter(node, p, true); |
1122 |
|
} |
1123 |
|
group = p; |
1124 |
|
} |
1176 |
|
return cancelWaiter(node, null, false); |
1177 |
|
node.thread = Thread.currentThread(); |
1178 |
|
if (node.prev == p && p.status == WAITING && |
1179 |
< |
(p != whead || (state & ABITS) != WBIT)) { |
1179 |
> |
(p != whead || (state & ABITS) != WBIT)) |
1180 |
|
U.park(false, time); |
1152 |
– |
if (interruptible && Thread.interrupted()) |
1153 |
– |
return cancelWaiter(node, null, true); |
1154 |
– |
} |
1181 |
|
node.thread = null; |
1182 |
+ |
if (interruptible && Thread.interrupted()) |
1183 |
+ |
return cancelWaiter(node, null, true); |
1184 |
|
} |
1185 |
|
} |
1186 |
|
} |
1187 |
|
|
1188 |
|
/** |
1189 |
< |
* If node non-null, forces cancel status and unsplices from queue |
1190 |
< |
* if possible. This is a variant of cancellation methods in |
1191 |
< |
* AbstractQueuedSynchronizer (see its detailed explanation in AQS |
1192 |
< |
* internal documentation) that more conservatively wakes up other |
1193 |
< |
* threads that may have had their links changed, so as to preserve |
1194 |
< |
* liveness in the main signalling methods. |
1189 |
> |
* If node non-null, forces cancel status and unsplices it from |
1190 |
> |
* queue if possible and wakes up any cowaiters. This is a variant |
1191 |
> |
* of cancellation methods in AbstractQueuedSynchronizer (see its |
1192 |
> |
* detailed explanation in AQS internal documentation) that more |
1193 |
> |
* conservatively wakes up other threads that may have had their |
1194 |
> |
* links changed, so as to preserve liveness in the main |
1195 |
> |
* signalling methods. |
1196 |
> |
* |
1197 |
> |
* @param node if nonnull, the waiter |
1198 |
> |
* @param group, if nonnull, the group current thread is cowaiting with |
1199 |
> |
* @param interrupted if already interrupted |
1200 |
> |
* @return INTERRUPTED if interrupted or Thread.interrupted, else zero |
1201 |
|
*/ |
1202 |
|
private long cancelWaiter(WNode node, WNode group, boolean interrupted) { |
1203 |
|
if (node != null) { |
1204 |
|
node.thread = null; |
1205 |
|
node.status = CANCELLED; |
1206 |
< |
if (group != null) { |
1207 |
< |
for (WNode p = group, q; p != null; p = q) { |
1208 |
< |
if ((q = p.cowait) != null && q.status == CANCELLED) { |
1209 |
< |
U.compareAndSwapObject(p, WCOWAIT, q, q.cowait); |
1210 |
< |
break; |
1211 |
< |
} |
1206 |
> |
Thread w; // wake up co-waiters; unsplice cancelled ones |
1207 |
> |
for (WNode q, p = (group != null) ? group : node; p != null; ) { |
1208 |
> |
if ((q = p.cowait) == null) |
1209 |
> |
break; |
1210 |
> |
if ((w = q.thread) != null) { |
1211 |
> |
q.thread = null; |
1212 |
> |
U.unpark(w); |
1213 |
|
} |
1214 |
+ |
if (q.status == CANCELLED) |
1215 |
+ |
U.compareAndSwapObject(p, WCOWAIT, q, q.cowait); |
1216 |
+ |
else |
1217 |
+ |
p = q; |
1218 |
|
} |
1219 |
< |
else { |
1219 |
> |
if (group == null) { // unsplice both prev and next links |
1220 |
|
for (WNode pred = node.prev; pred != null; ) { |
1221 |
< |
WNode succ, pp; Thread w; |
1221 |
> |
WNode succ, pp; // first unsplice next |
1222 |
|
while ((succ = node.next) == null || |
1223 |
|
succ.status == CANCELLED) { |
1224 |
< |
WNode q = null; |
1224 |
> |
WNode q = null; // find successor the slow way |
1225 |
|
for (WNode t = wtail; t != null && t != node; t = t.prev) |
1226 |
|
if (t.status != CANCELLED) |
1227 |
< |
q = t; |
1228 |
< |
if (succ == q || |
1227 |
> |
q = t; // don't link if succ cancelled |
1228 |
> |
if (succ == q || // ensure accurate successor |
1229 |
|
U.compareAndSwapObject(node, WNEXT, |
1230 |
|
succ, succ = q)) { |
1231 |
|
if (succ == null && node == wtail) |
1233 |
|
break; |
1234 |
|
} |
1235 |
|
} |
1236 |
< |
if (pred.next == node) |
1236 |
> |
if (pred.next == node) // unsplice pred link |
1237 |
|
U.compareAndSwapObject(pred, WNEXT, node, succ); |
1238 |
< |
if (succ != null && (w = succ.thread) != null) |
1239 |
< |
U.unpark(w); |
1238 |
> |
if (succ != null && (w = succ.thread) != null) { |
1239 |
> |
succ.thread = null; |
1240 |
> |
U.unpark(w); // conservatively wake up new succ |
1241 |
> |
} |
1242 |
|
if (pred.status != CANCELLED || (pp = pred.prev) == null) |
1243 |
|
break; |
1244 |
< |
node.prev = pp; // repeat for new pred |
1244 |
> |
node.prev = pp; // repeat in case new pred wrong/cancelled |
1245 |
|
U.compareAndSwapObject(pp, WNEXT, pred, succ); |
1246 |
|
pred = pp; |
1247 |
|
} |