61 |
|
* help reduce some of the code bloat that otherwise occurs in |
62 |
|
* retry-based designs. |
63 |
|
* |
64 |
< |
* <p>StampedLocks are designed for use in a different (and generally |
65 |
< |
* narrower) range of contexts than most other locks: They are not |
66 |
< |
* reentrant, so locked bodies should not call other unknown methods |
67 |
< |
* that may try to re-acquire locks (although you may pass a stamp to |
68 |
< |
* other methods that can use or convert it). Unvalidated optimistic |
69 |
< |
* read sections should further not call methods that are not known to |
64 |
> |
* <p>StampedLocks are designed for use as internal utilities in the |
65 |
> |
* development of thread-safe components. Their use relies on |
66 |
> |
* knowledge of the internal properties of the data, objects, and |
67 |
> |
* methods they are protecting. They are not reentrant, so locked |
68 |
> |
* bodies should not call other unknown methods that may try to |
69 |
> |
* re-acquire locks (although you may pass a stamp to other methods |
70 |
> |
* that can use or convert it). The use of read lock modes relies on |
71 |
> |
* the associated code sections being side-effect-free. Unvalidated |
72 |
> |
* optimistic read sections cannot call methods that are not known to |
73 |
|
* tolerate potential inconsistencies. Stamps use finite |
74 |
|
* representations, and are not cryptographically secure (i.e., a |
75 |
|
* valid stamp may be guessable). Stamp values may recycle after (no |
125 |
|
* } |
126 |
|
* |
127 |
|
* double distanceFromOriginV2() { // combines code paths |
128 |
+ |
* double currentX = 0.0, currentY = 0.0; |
129 |
|
* for (long stamp = sl.tryOptimisticRead(); ; stamp = sl.readLock()) { |
126 |
– |
* double currentX, currentY; |
130 |
|
* try { |
131 |
|
* currentX = x; |
132 |
|
* currentY = y; |
133 |
|
* } finally { |
134 |
|
* if (sl.tryConvertToOptimisticRead(stamp) != 0L) // unlock or validate |
135 |
< |
* return Math.sqrt(currentX * currentX + currentY * currentY); |
135 |
> |
* break; |
136 |
|
* } |
137 |
|
* } |
138 |
+ |
* return Math.sqrt(currentX * currentX + currentY * currentY); |
139 |
|
* } |
140 |
|
* |
141 |
|
* void moveIfAtOrigin(double newX, double newY) { // upgrade |
143 |
|
* long stamp = sl.readLock(); |
144 |
|
* try { |
145 |
|
* while (x == 0.0 && y == 0.0) { |
146 |
< |
* long ws = tryConvertToWriteLock(stamp); |
146 |
> |
* long ws = sl.tryConvertToWriteLock(stamp); |
147 |
|
* if (ws != 0L) { |
148 |
|
* stamp = ws; |
149 |
|
* x = newX; |
226 |
|
* threads. Both await methods use a similar spin strategy: If |
227 |
|
* the associated queue appears to be empty, then the thread |
228 |
|
* spin-waits up to SPINS times (where each iteration decreases |
229 |
< |
* spin count with 50% probablility) before enqueing, and then, if |
229 |
> |
* spin count with 50% probability) before enqueing, and then, if |
230 |
|
* it is the first thread to be enqueued, spins again up to SPINS |
231 |
|
* times before blocking. If, upon wakening it fails to obtain |
232 |
|
* lock, and is still (or becomes) the first waiting thread (which |
314 |
|
private transient int readerOverflow; |
315 |
|
|
316 |
|
/** |
317 |
< |
* Creates a new lock initially in unlocked state. |
317 |
> |
* Creates a new lock, initially in unlocked state. |
318 |
|
*/ |
319 |
|
public StampedLock() { |
320 |
|
state = ORIGIN; |
515 |
|
} |
516 |
|
|
517 |
|
/** |
518 |
< |
* Returns true if the lock has not been exclusively held since |
519 |
< |
* issuance of the given stamp. Always returns false if the stamp |
520 |
< |
* is zero. Always returns true if the stamp represents a |
518 |
> |
* Returns true if the lock has not been exclusively acquired |
519 |
> |
* since issuance of the given stamp. Always returns false if the |
520 |
> |
* stamp is zero. Always returns true if the stamp represents a |
521 |
|
* currently held lock. |
522 |
|
* |
523 |
< |
* @return true if the lock has not been exclusively held since |
524 |
< |
* issuance of the given stamp; else false |
523 |
> |
* @return true if the lock has not been exclusively acquired |
524 |
> |
* since issuance of the given stamp; else false |
525 |
|
*/ |
526 |
|
public boolean validate(long stamp) { |
527 |
+ |
// See above about current use of getLongVolatile here |
528 |
|
return (stamp & SBITS) == (U.getLongVolatile(this, STATE) & SBITS); |
529 |
|
} |
530 |
|
|
634 |
|
break; |
635 |
|
return stamp; |
636 |
|
} |
637 |
< |
else if (m == RUNIT && a != 0L && a < WBIT) { |
637 |
> |
else if (m == RUNIT && a != 0L) { |
638 |
|
if (U.compareAndSwapLong(this, STATE, s, |
639 |
|
next = s - RUNIT + WBIT)) |
640 |
|
return next; |
672 |
|
else if (m == WBIT) { |
673 |
|
if (a != m) |
674 |
|
break; |
675 |
< |
next = state = s + (WBIT + RUNIT); |
675 |
> |
state = next = s + (WBIT + RUNIT); |
676 |
|
readerPrefSignal(); |
677 |
|
return next; |
678 |
|
} |
780 |
|
* @return true if the lock is currently held non-exclusively |
781 |
|
*/ |
782 |
|
public boolean isReadLocked() { |
783 |
< |
long m; |
779 |
< |
return (m = state & ABITS) > 0L && m < WBIT; |
783 |
> |
return (state & RBITS) != 0L; |
784 |
|
} |
785 |
|
|
786 |
|
private void readObject(java.io.ObjectInputStream s) |
796 |
|
* access bits value to RBITS, indicating hold of spinlock, |
797 |
|
* then updating, then releasing. |
798 |
|
* |
799 |
< |
* @param stamp, assumed that (stamp & ABITS) >= RFULL |
799 |
> |
* @param s, assumed that (s & ABITS) >= RFULL |
800 |
|
* @return new stamp on success, else zero |
801 |
|
*/ |
802 |
|
private long tryIncReaderOverflow(long s) { |
816 |
|
/** |
817 |
|
* Tries to decrement readerOverflow. |
818 |
|
* |
819 |
< |
* @param stamp, assumed that (stamp & ABITS) >= RFULL |
819 |
> |
* @param s, assumed that (s & ABITS) >= RFULL |
820 |
|
* @return new stamp on success, else zero |
821 |
|
*/ |
822 |
|
private long tryDecReaderOverflow(long s) { |
872 |
|
U.compareAndSwapObject(p, WAITER, w, null)) |
873 |
|
U.unpark(w); |
874 |
|
} |
875 |
< |
if (!readers && (state & ABITS) == 0L && |
876 |
< |
(h = whead) != null && h.status != 0) { |
875 |
> |
if (!readers && (h = whead) != null && h.status != 0 && |
876 |
> |
(state & ABITS) == 0L) { |
877 |
|
U.compareAndSwapInt(h, STATUS, WAITING, 0); |
878 |
|
if ((q = h.next) == null || q.status == CANCELLED) { |
875 |
– |
q = null; |
879 |
|
for (WNode t = wtail; t != null && t != h; t = t.prev) |
880 |
|
if (t.status <= 0) |
881 |
|
q = t; |
890 |
|
if ((h = whead) != null && h.status != 0) { |
891 |
|
U.compareAndSwapInt(h, STATUS, WAITING, 0); |
892 |
|
if ((q = h.next) == null || q.status == CANCELLED) { |
890 |
– |
q = null; |
893 |
|
for (WNode t = wtail; t != null && t != h; t = t.prev) |
894 |
|
if (t.status <= 0) |
895 |
|
q = t; |
959 |
|
else if (U.compareAndSwapObject(this, WTAIL, p, node)) { |
960 |
|
p.next = node; |
961 |
|
for (int headSpins = SPINS;;) { |
962 |
< |
WNode np; int ps; |
963 |
< |
if ((np = node.prev) != p && np != null && |
964 |
< |
(p = np).next != node) |
963 |
< |
p.next = node; // stale |
962 |
> |
WNode np, pp; int ps; |
963 |
> |
while ((np = node.prev) != p && np != null) |
964 |
> |
(p = np).next = node; // stale |
965 |
|
if (p == whead) { |
966 |
|
for (int k = headSpins;;) { |
967 |
|
if (((s = state) & ABITS) == 0L) { |
982 |
|
} |
983 |
|
if ((ps = p.status) == 0) |
984 |
|
U.compareAndSwapInt(p, STATUS, 0, WAITING); |
985 |
< |
else if (ps == CANCELLED) |
986 |
< |
node.prev = p.prev; |
985 |
> |
else if (ps == CANCELLED) { |
986 |
> |
if ((pp = p.prev) != null) { |
987 |
> |
node.prev = pp; |
988 |
> |
pp.next = node; |
989 |
> |
} |
990 |
> |
} |
991 |
|
else { |
992 |
|
long time; // 0 argument to park means no timeout |
993 |
|
if (deadline == 0L) |
995 |
|
else if ((time = deadline - System.nanoTime()) <= 0L) |
996 |
|
return cancelWriter(node, false); |
997 |
|
if (node.prev == p && p.status == WAITING && |
998 |
< |
(p != whead || (state & WBIT) != 0L)) { // recheck |
998 |
> |
(p != whead || (state & ABITS) != 0L)) // recheck |
999 |
|
U.park(false, time); |
1000 |
< |
if (interruptible && Thread.interrupted()) |
1001 |
< |
return cancelWriter(node, true); |
997 |
< |
} |
1000 |
> |
if (interruptible && Thread.interrupted()) |
1001 |
> |
return cancelWriter(node, true); |
1002 |
|
} |
1003 |
|
} |
1004 |
|
} |
1007 |
|
|
1008 |
|
/** |
1009 |
|
* If node non-null, forces cancel status and unsplices from queue |
1010 |
< |
* if possible. This is a streamlined variant of cancellation |
1011 |
< |
* methods in AbstractQueuedSynchronizer that includes a detailed |
1012 |
< |
* explanation. |
1010 |
> |
* if possible. This is a variant of cancellation methods in |
1011 |
> |
* AbstractQueuedSynchronizer (see its detailed explanation in AQS |
1012 |
> |
* internal documentation) that more conservatively wakes up other |
1013 |
> |
* threads that may have had their links changed, so as to preserve |
1014 |
> |
* liveness in the main signalling methods. |
1015 |
|
*/ |
1016 |
|
private long cancelWriter(WNode node, boolean interrupted) { |
1017 |
< |
WNode pred; |
1012 |
< |
if (node != null && (pred = node.prev) != null) { |
1013 |
< |
WNode pp; |
1017 |
> |
if (node != null) { |
1018 |
|
node.thread = null; |
1015 |
– |
while (pred.status == CANCELLED && (pp = pred.prev) != null) |
1016 |
– |
pred = node.prev = pp; |
1017 |
– |
WNode predNext = pred.next; |
1019 |
|
node.status = CANCELLED; |
1020 |
< |
if (predNext != null) { |
1021 |
< |
Thread w; |
1022 |
< |
WNode succ = node.next; |
1023 |
< |
if (succ == null || succ.status == CANCELLED) { |
1023 |
< |
succ = null; |
1020 |
> |
for (WNode pred = node.prev; pred != null; ) { |
1021 |
> |
WNode succ, pp; Thread w; |
1022 |
> |
while ((succ = node.next) == null || succ.status == CANCELLED) { |
1023 |
> |
WNode q = null; |
1024 |
|
for (WNode t = wtail; t != null && t != node; t = t.prev) |
1025 |
< |
if (t.status <= 0) |
1026 |
< |
succ = t; |
1027 |
< |
if (succ == null && node == wtail) |
1028 |
< |
U.compareAndSwapObject(this, WTAIL, node, pred); |
1025 |
> |
if (t.status != CANCELLED) |
1026 |
> |
q = t; |
1027 |
> |
if (succ == q || |
1028 |
> |
U.compareAndSwapObject(node, WNEXT, succ, succ = q)) { |
1029 |
> |
if (succ == null && node == wtail) |
1030 |
> |
U.compareAndSwapObject(this, WTAIL, node, pred); |
1031 |
> |
break; |
1032 |
> |
} |
1033 |
|
} |
1034 |
< |
U.compareAndSwapObject(pred, WNEXT, predNext, succ); |
1034 |
> |
if (pred.next == node) |
1035 |
> |
U.compareAndSwapObject(pred, WNEXT, node, succ); |
1036 |
|
if (succ != null && (w = succ.thread) != null) |
1037 |
|
U.unpark(w); |
1038 |
+ |
if (pred.status != CANCELLED || (pp = pred.prev) == null) |
1039 |
+ |
break; |
1040 |
+ |
node.prev = pp; // repeat for new pred |
1041 |
+ |
U.compareAndSwapObject(pp, WNEXT, pred, succ); |
1042 |
+ |
pred = pp; |
1043 |
|
} |
1044 |
|
} |
1045 |
|
writerPrefSignal(); |
1113 |
|
time = 0L; |
1114 |
|
else if ((time = deadline - System.nanoTime()) <= 0L) |
1115 |
|
return cancelReader(node, false); |
1116 |
< |
if ((state & WBIT) != 0L && node.waiter != null) { // recheck |
1116 |
> |
if ((state & WBIT) != 0L && node.waiter != null) // recheck |
1117 |
|
U.park(false, time); |
1118 |
< |
if (interruptible && Thread.interrupted()) |
1119 |
< |
return cancelReader(node, true); |
1110 |
< |
} |
1118 |
> |
if (interruptible && Thread.interrupted()) |
1119 |
> |
return cancelReader(node, true); |
1120 |
|
} |
1121 |
|
} |
1122 |
|
} |