6 |
|
|
7 |
|
package jsr166e; |
8 |
|
|
9 |
– |
import java.util.concurrent.ThreadLocalRandom; |
9 |
|
import java.util.concurrent.TimeUnit; |
10 |
< |
import java.util.concurrent.locks.*; |
10 |
> |
import java.util.concurrent.locks.Lock; |
11 |
> |
import java.util.concurrent.locks.Condition; |
12 |
> |
import java.util.concurrent.locks.ReadWriteLock; |
13 |
|
|
14 |
|
/** |
15 |
|
* A capability-based lock with three modes for controlling read/write |
28 |
|
* in method {@link #unlockWrite} to release the lock. Untimed and |
29 |
|
* timed versions of {@code tryWriteLock} are also provided. When |
30 |
|
* the lock is held in write mode, no read locks may be obtained, |
31 |
< |
* and all optimistic read validations will fail. </li> |
31 |
> |
* and all optimistic read validations will fail. |
32 |
|
* |
33 |
|
* <li><b>Reading.</b> Method {@link #readLock} possibly blocks |
34 |
|
* waiting for non-exclusive access, returning a stamp that can be |
35 |
|
* used in method {@link #unlockRead} to release the lock. Untimed |
36 |
< |
* and timed versions of {@code tryReadLock} are also provided. </li> |
36 |
> |
* and timed versions of {@code tryReadLock} are also provided. |
37 |
|
* |
38 |
|
* <li><b>Optimistic Reading.</b> Method {@link #tryOptimisticRead} |
39 |
|
* returns a non-zero stamp only if the lock is not currently held |
40 |
|
* in write mode. Method {@link #validate} returns true if the lock |
41 |
< |
* has not since been acquired in write mode. This mode can be |
42 |
< |
* thought of as an extremely weak version of a read-lock, that can |
43 |
< |
* be broken by a writer at any time. The use of optimistic mode |
44 |
< |
* for short read-only code segments often reduces contention and |
45 |
< |
* improves throughput. However, its use is inherently fragile. |
46 |
< |
* Optimistic read sections should only read fields and hold them in |
47 |
< |
* local variables for later use after validation. Fields read while |
48 |
< |
* in optimistic mode may be wildly inconsistent, so usage applies |
49 |
< |
* only when you are familiar enough with data representations to |
50 |
< |
* check consistency and/or repeatedly invoke method {@code |
51 |
< |
* validate()}. For example, such steps are typically required when |
52 |
< |
* first reading an object or array reference, and then accessing |
53 |
< |
* one of its fields, elements or methods. </li> |
41 |
> |
* has not been acquired in write mode since obtaining a given |
42 |
> |
* stamp. This mode can be thought of as an extremely weak version |
43 |
> |
* of a read-lock, that can be broken by a writer at any time. The |
44 |
> |
* use of optimistic mode for short read-only code segments often |
45 |
> |
* reduces contention and improves throughput. However, its use is |
46 |
> |
* inherently fragile. Optimistic read sections should only read |
47 |
> |
* fields and hold them in local variables for later use after |
48 |
> |
* validation. Fields read while in optimistic mode may be wildly |
49 |
> |
* inconsistent, so usage applies only when you are familiar enough |
50 |
> |
* with data representations to check consistency and/or repeatedly |
51 |
> |
* invoke method {@code validate()}. For example, such steps are |
52 |
> |
* typically required when first reading an object or array |
53 |
> |
* reference, and then accessing one of its fields, elements or |
54 |
> |
* methods. |
55 |
|
* |
56 |
|
* </ul> |
57 |
|
* |
117 |
|
* } |
118 |
|
* } |
119 |
|
* |
120 |
< |
* double distanceFromOriginV1() { // A read-only method |
121 |
< |
* long stamp; |
122 |
< |
* if ((stamp = sl.tryOptimisticRead()) != 0L) { // optimistic |
123 |
< |
* double currentX = x; |
124 |
< |
* double currentY = y; |
125 |
< |
* if (sl.validate(stamp)) |
126 |
< |
* return Math.sqrt(currentX * currentX + currentY * currentY); |
127 |
< |
* } |
128 |
< |
* stamp = sl.readLock(); // fall back to read lock |
129 |
< |
* try { |
130 |
< |
* double currentX = x; |
129 |
< |
* double currentY = y; |
130 |
< |
* return Math.sqrt(currentX * currentX + currentY * currentY); |
131 |
< |
* } finally { |
132 |
< |
* sl.unlockRead(stamp); |
133 |
< |
* } |
134 |
< |
* } |
135 |
< |
* |
136 |
< |
* double distanceFromOriginV2() { // combines code paths |
137 |
< |
* double currentX = 0.0, currentY = 0.0; |
138 |
< |
* for (long stamp = sl.tryOptimisticRead(); ; stamp = sl.readLock()) { |
139 |
< |
* try { |
140 |
< |
* currentX = x; |
141 |
< |
* currentY = y; |
142 |
< |
* } finally { |
143 |
< |
* if (sl.tryConvertToOptimisticRead(stamp) != 0L) // unlock or validate |
144 |
< |
* break; |
145 |
< |
* } |
120 |
> |
* double distanceFromOrigin() { // A read-only method |
121 |
> |
* long stamp = sl.tryOptimisticRead(); |
122 |
> |
* double currentX = x, currentY = y; |
123 |
> |
* if (!sl.validate(stamp)) { |
124 |
> |
* stamp = sl.readLock(); |
125 |
> |
* try { |
126 |
> |
* currentX = x; |
127 |
> |
* currentY = y; |
128 |
> |
* } finally { |
129 |
> |
* sl.unlockRead(stamp); |
130 |
> |
* } |
131 |
|
* } |
132 |
|
* return Math.sqrt(currentX * currentX + currentY * currentY); |
133 |
|
* } |
183 |
|
* |
184 |
|
* Waiters use a modified form of CLH lock used in |
185 |
|
* AbstractQueuedSynchronizer (see its internal documentation for |
186 |
< |
* a fuller account), where each node it tagged (field mode) as |
186 |
> |
* a fuller account), where each node is tagged (field mode) as |
187 |
|
* either a reader or writer. Sets of waiting readers are grouped |
188 |
|
* (linked) under a common node (field cowait) so act as a single |
189 |
< |
* node with respect to most CLH mechanics. By virtue of its |
190 |
< |
* structure, wait nodes need not actually carry sequence numbers; |
191 |
< |
* we know each is >= its predecessor. These queue mechanics |
192 |
< |
* simplify the scheduling policy to a mainly-FIFO scheme that |
189 |
> |
* node with respect to most CLH mechanics. By virtue of the |
190 |
> |
* queue structure, wait nodes need not actually carry sequence |
191 |
> |
* numbers; we know each is greater than its predecessor. This |
192 |
> |
* simplifies the scheduling policy to a mainly-FIFO scheme that |
193 |
|
* incorporates elements of Phase-Fair locks (see Brandenburg & |
194 |
|
* Anderson, especially http://www.cs.unc.edu/~bbb/diss/). In |
195 |
|
* particular, we use the phase-fair anti-barging rule: If an |
196 |
|
* incoming reader arrives while read lock is held but there is a |
197 |
|
* queued writer, this incoming reader is queued. (This rule is |
198 |
|
* responsible for some of the complexity of method acquireRead, |
199 |
< |
* but without it, the lock becomes highly unfair.) |
199 |
> |
* but without it, the lock becomes highly unfair.) Method release |
200 |
> |
* does not (and sometimes cannot) itself wake up cowaiters. This |
201 |
> |
* is done by the primary thread, but helped by any other threads |
202 |
> |
* with nothing better to do in methods acquireRead and |
203 |
> |
* acquireWrite. |
204 |
|
* |
205 |
|
* These rules apply to threads actually queued. All tryLock forms |
206 |
|
* opportunistically try to acquire locks regardless of preference |
218 |
|
* |
219 |
|
* Nearly all of these mechanics are carried out in methods |
220 |
|
* acquireWrite and acquireRead, that, as typical of such code, |
221 |
< |
* sprawl out because actions and retries rely on consitent sets |
222 |
< |
* of locally cahced reads. |
221 |
> |
* sprawl out because actions and retries rely on consistent sets |
222 |
> |
* of locally cached reads. |
223 |
|
* |
224 |
|
* As noted in Boehm's paper (above), sequence validation (mainly |
225 |
|
* method validate()) requires stricter ordering rules than apply |
244 |
|
/** Number of processors, for spin control */ |
245 |
|
private static final int NCPU = Runtime.getRuntime().availableProcessors(); |
246 |
|
|
247 |
< |
/** Maximum number of retries before blocking on acquisition */ |
247 |
> |
/** Maximum number of retries before enqueuing on acquisition */ |
248 |
|
private static final int SPINS = (NCPU > 1) ? 1 << 6 : 0; |
249 |
|
|
250 |
+ |
/** Maximum number of retries before blocking at head on acquisition */ |
251 |
+ |
private static final int HEAD_SPINS = (NCPU > 1) ? 1 << 10 : 0; |
252 |
+ |
|
253 |
|
/** Maximum number of retries before re-blocking */ |
254 |
< |
private static final int MAX_HEAD_SPINS = (NCPU > 1) ? 1 << 12 : 0; |
254 |
> |
private static final int MAX_HEAD_SPINS = (NCPU > 1) ? 1 << 16 : 0; |
255 |
|
|
256 |
|
/** The period for yielding when waiting for overflow spinlock */ |
257 |
|
private static final int OVERFLOW_YIELD_RATE = 7; // must be power 2 - 1 |
321 |
|
* @return a stamp that can be used to unlock or convert mode |
322 |
|
*/ |
323 |
|
public long writeLock() { |
324 |
< |
long s, next; // bypass acquireWrite in fully onlocked case only |
324 |
> |
long s, next; // bypass acquireWrite in fully unlocked case only |
325 |
|
return ((((s = state) & ABITS) == 0L && |
326 |
|
U.compareAndSwapLong(this, STATE, s, next = s + WBIT)) ? |
327 |
|
next : acquireWrite(false, 0L)); |
346 |
|
* Behavior under timeout and interruption matches that specified |
347 |
|
* for method {@link Lock#tryLock(long,TimeUnit)}. |
348 |
|
* |
349 |
+ |
* @param time the maximum time to wait for the lock |
350 |
+ |
* @param unit the time unit of the {@code time} argument |
351 |
|
* @return a stamp that can be used to unlock or convert mode, |
352 |
|
* or zero if the lock is not available |
353 |
|
* @throws InterruptedException if the current thread is interrupted |
358 |
|
long nanos = unit.toNanos(time); |
359 |
|
if (!Thread.interrupted()) { |
360 |
|
long next, deadline; |
361 |
< |
if ((next = tryWriteLock()) != 0) |
361 |
> |
if ((next = tryWriteLock()) != 0L) |
362 |
|
return next; |
363 |
|
if (nanos <= 0L) |
364 |
|
return 0L; |
395 |
|
* @return a stamp that can be used to unlock or convert mode |
396 |
|
*/ |
397 |
|
public long readLock() { |
398 |
< |
long s, next; // bypass acquireRead on fully onlocked case only |
399 |
< |
return ((((s = state) & ABITS) == 0L && |
398 |
> |
long s = state, next; // bypass acquireRead on common uncontended case |
399 |
> |
return ((whead == wtail && (s & ABITS) < RFULL && |
400 |
|
U.compareAndSwapLong(this, STATE, s, next = s + RUNIT)) ? |
401 |
|
next : acquireRead(false, 0L)); |
402 |
|
} |
427 |
|
* Behavior under timeout and interruption matches that specified |
428 |
|
* for method {@link Lock#tryLock(long,TimeUnit)}. |
429 |
|
* |
430 |
+ |
* @param time the maximum time to wait for the lock |
431 |
+ |
* @param unit the time unit of the {@code time} argument |
432 |
|
* @return a stamp that can be used to unlock or convert mode, |
433 |
|
* or zero if the lock is not available |
434 |
|
* @throws InterruptedException if the current thread is interrupted |
436 |
|
*/ |
437 |
|
public long tryReadLock(long time, TimeUnit unit) |
438 |
|
throws InterruptedException { |
439 |
< |
long next, deadline; |
439 |
> |
long s, m, next, deadline; |
440 |
|
long nanos = unit.toNanos(time); |
441 |
|
if (!Thread.interrupted()) { |
442 |
< |
if ((next = tryReadLock()) != 0) |
443 |
< |
return next; |
442 |
> |
if ((m = (s = state) & ABITS) != WBIT) { |
443 |
> |
if (m < RFULL) { |
444 |
> |
if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT)) |
445 |
> |
return next; |
446 |
> |
} |
447 |
> |
else if ((next = tryIncReaderOverflow(s)) != 0L) |
448 |
> |
return next; |
449 |
> |
} |
450 |
|
if (nanos <= 0L) |
451 |
|
return 0L; |
452 |
|
if ((deadline = System.nanoTime() + nanos) == 0L) |
490 |
|
* Returns true if the lock has not been exclusively acquired |
491 |
|
* since issuance of the given stamp. Always returns false if the |
492 |
|
* stamp is zero. Always returns true if the stamp represents a |
493 |
< |
* currently held lock. |
493 |
> |
* currently held lock. Invoking this method with a value not |
494 |
> |
* obtained from {@link #tryOptimisticRead} or a locking method |
495 |
> |
* for this lock has no defined effect or result. |
496 |
|
* |
497 |
< |
* @return true if the lock has not been exclusively acquired |
497 |
> |
* @param stamp a stamp |
498 |
> |
* @return {@code true} if the lock has not been exclusively acquired |
499 |
|
* since issuance of the given stamp; else false |
500 |
|
*/ |
501 |
|
public boolean validate(long stamp) { |
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 |
|
/** |
673 |
|
long a = stamp & ABITS, m, s, next; WNode h; |
674 |
|
for (;;) { |
675 |
|
s = U.getLongVolatile(this, STATE); // see above |
676 |
< |
if ((s & SBITS) != (stamp & SBITS)) |
676 |
> |
if (((s = state) & SBITS) != (stamp & SBITS)) |
677 |
|
break; |
678 |
|
if ((m = s & ABITS) == 0L) { |
679 |
|
if (a != 0L) |
708 |
|
* stamp value. This method may be useful for recovery after |
709 |
|
* errors. |
710 |
|
* |
711 |
< |
* @return true if the lock was held, else false |
711 |
> |
* @return {@code true} if the lock was held, else false |
712 |
|
*/ |
713 |
|
public boolean tryUnlockWrite() { |
714 |
|
long s; WNode h; |
726 |
|
* requiring a stamp value. This method may be useful for recovery |
727 |
|
* after errors. |
728 |
|
* |
729 |
< |
* @return true if the read lock was held, else false |
729 |
> |
* @return {@code true} if the read lock was held, else false |
730 |
|
*/ |
731 |
|
public boolean tryUnlockRead() { |
732 |
|
long s, m; WNode h; |
744 |
|
return false; |
745 |
|
} |
746 |
|
|
747 |
+ |
// status monitoring methods |
748 |
+ |
|
749 |
+ |
/** |
750 |
+ |
* Returns combined state-held and overflow read count for given |
751 |
+ |
* state s. |
752 |
+ |
*/ |
753 |
+ |
private int getReadLockCount(long s) { |
754 |
+ |
long readers; |
755 |
+ |
if ((readers = s & RBITS) >= RFULL) |
756 |
+ |
readers = RFULL + readerOverflow; |
757 |
+ |
return (int) readers; |
758 |
+ |
} |
759 |
+ |
|
760 |
|
/** |
761 |
< |
* Returns true if the lock is currently held exclusively. |
761 |
> |
* Returns {@code true} if the lock is currently held exclusively. |
762 |
|
* |
763 |
< |
* @return true if the lock is currently held exclusively |
763 |
> |
* @return {@code true} if the lock is currently held exclusively |
764 |
|
*/ |
765 |
|
public boolean isWriteLocked() { |
766 |
|
return (state & WBIT) != 0L; |
767 |
|
} |
768 |
|
|
769 |
|
/** |
770 |
< |
* Returns true if the lock is currently held non-exclusively. |
770 |
> |
* Returns {@code true} if the lock is currently held non-exclusively. |
771 |
|
* |
772 |
< |
* @return true if the lock is currently held non-exclusively |
772 |
> |
* @return {@code true} if the lock is currently held non-exclusively |
773 |
|
*/ |
774 |
|
public boolean isReadLocked() { |
775 |
|
return (state & RBITS) != 0L; |
776 |
|
} |
777 |
|
|
778 |
< |
private void readObject(java.io.ObjectInputStream s) |
779 |
< |
throws java.io.IOException, ClassNotFoundException { |
780 |
< |
s.defaultReadObject(); |
781 |
< |
state = ORIGIN; // reset to unlocked state |
778 |
> |
/** |
779 |
> |
* Queries the number of read locks held for this lock. This |
780 |
> |
* method is designed for use in monitoring system state, not for |
781 |
> |
* synchronization control. |
782 |
> |
* @return the number of read locks held |
783 |
> |
*/ |
784 |
> |
public int getReadLockCount() { |
785 |
> |
return getReadLockCount(state); |
786 |
> |
} |
787 |
> |
|
788 |
> |
/** |
789 |
> |
* Returns a string identifying this lock, as well as its lock |
790 |
> |
* state. The state, in brackets, includes the String {@code |
791 |
> |
* "Unlocked"} or the String {@code "Write-locked"} or the String |
792 |
> |
* {@code "Read-locks:"} followed by the current number of |
793 |
> |
* read-locks held. |
794 |
> |
* |
795 |
> |
* @return a string identifying this lock, as well as its lock state |
796 |
> |
*/ |
797 |
> |
public String toString() { |
798 |
> |
long s = state; |
799 |
> |
return super.toString() + |
800 |
> |
((s & ABITS) == 0L ? "[Unlocked]" : |
801 |
> |
(s & WBIT) != 0L ? "[Write-locked]" : |
802 |
> |
"[Read-locks:" + getReadLockCount(s) + "]"); |
803 |
|
} |
804 |
|
|
805 |
+ |
// views |
806 |
+ |
|
807 |
|
/** |
808 |
|
* Returns a plain {@link Lock} view of this StampedLock in which |
809 |
|
* the {@link Lock#lock} method is mapped to {@link #readLock}, |
862 |
|
throws InterruptedException { |
863 |
|
return tryReadLock(time, unit) != 0L; |
864 |
|
} |
865 |
< |
// note that we give up ability to check mode so just use current state |
829 |
< |
public void unlock() { unlockRead(state); } |
865 |
> |
public void unlock() { unstampedUnlockRead(); } |
866 |
|
public Condition newCondition() { |
867 |
|
throw new UnsupportedOperationException(); |
868 |
|
} |
878 |
|
throws InterruptedException { |
879 |
|
return tryWriteLock(time, unit) != 0L; |
880 |
|
} |
881 |
< |
public void unlock() { unlockWrite(state); } |
881 |
> |
public void unlock() { unstampedUnlockWrite(); } |
882 |
|
public Condition newCondition() { |
883 |
|
throw new UnsupportedOperationException(); |
884 |
|
} |
889 |
|
public Lock writeLock() { return asWriteLock(); } |
890 |
|
} |
891 |
|
|
892 |
+ |
// Unlock methods without stamp argument checks for view classes. |
893 |
+ |
// Needed because view-class lock methods throw away stamps. |
894 |
+ |
|
895 |
+ |
final void unstampedUnlockWrite() { |
896 |
+ |
WNode h; long s; |
897 |
+ |
if (((s = state) & WBIT) == 0L) |
898 |
+ |
throw new IllegalMonitorStateException(); |
899 |
+ |
state = (s += WBIT) == 0L ? ORIGIN : s; |
900 |
+ |
if ((h = whead) != null && h.status != 0) |
901 |
+ |
release(h); |
902 |
+ |
} |
903 |
+ |
|
904 |
+ |
final void unstampedUnlockRead() { |
905 |
+ |
for (;;) { |
906 |
+ |
long s, m; WNode h; |
907 |
+ |
if ((m = (s = state) & ABITS) == 0L || m >= WBIT) |
908 |
+ |
throw new IllegalMonitorStateException(); |
909 |
+ |
else if (m < RFULL) { |
910 |
+ |
if (U.compareAndSwapLong(this, STATE, s, s - RUNIT)) { |
911 |
+ |
if (m == RUNIT && (h = whead) != null && h.status != 0) |
912 |
+ |
release(h); |
913 |
+ |
break; |
914 |
+ |
} |
915 |
+ |
} |
916 |
+ |
else if (tryDecReaderOverflow(s) != 0L) |
917 |
+ |
break; |
918 |
+ |
} |
919 |
+ |
} |
920 |
+ |
|
921 |
+ |
private void readObject(java.io.ObjectInputStream s) |
922 |
+ |
throws java.io.IOException, ClassNotFoundException { |
923 |
+ |
s.defaultReadObject(); |
924 |
+ |
state = ORIGIN; // reset to unlocked state |
925 |
+ |
} |
926 |
+ |
|
927 |
|
// internals |
928 |
|
|
929 |
|
/** |
931 |
|
* access bits value to RBITS, indicating hold of spinlock, |
932 |
|
* then updating, then releasing. |
933 |
|
* |
934 |
< |
* @param s, assumed that (s & ABITS) >= RFULL |
934 |
> |
* @param s a reader overflow stamp: (s & ABITS) >= RFULL |
935 |
|
* @return new stamp on success, else zero |
936 |
|
*/ |
937 |
|
private long tryIncReaderOverflow(long s) { |
938 |
+ |
// assert (s & ABITS) >= RFULL; |
939 |
|
if ((s & ABITS) == RFULL) { |
940 |
|
if (U.compareAndSwapLong(this, STATE, s, s | RBITS)) { |
941 |
|
++readerOverflow; |
952 |
|
/** |
953 |
|
* Tries to decrement readerOverflow. |
954 |
|
* |
955 |
< |
* @param s, assumed that (s & ABITS) >= RFULL |
955 |
> |
* @param s a reader overflow stamp: (s & ABITS) >= RFULL |
956 |
|
* @return new stamp on success, else zero |
957 |
|
*/ |
958 |
|
private long tryDecReaderOverflow(long s) { |
959 |
+ |
// assert (s & ABITS) >= RFULL; |
960 |
|
if ((s & ABITS) == RFULL) { |
961 |
|
if (U.compareAndSwapLong(this, STATE, s, s | RBITS)) { |
962 |
|
int r; long next; |
976 |
|
return 0L; |
977 |
|
} |
978 |
|
|
979 |
< |
/* |
979 |
> |
/** |
980 |
|
* Wakes up the successor of h (normally whead). This is normally |
981 |
|
* just h.next, but may require traversal from wtail if next |
982 |
|
* pointers are lagging. This may fail to wake up an acquiring |
992 |
|
if (t.status <= 0) |
993 |
|
q = t; |
994 |
|
} |
995 |
< |
if (q != null) { |
996 |
< |
for (WNode r = q;;) { // release co-waiters too |
924 |
< |
if ((w = r.thread) != null) { |
925 |
< |
r.thread = null; |
926 |
< |
U.unpark(w); |
927 |
< |
} |
928 |
< |
if ((r = q.cowait) == null) |
929 |
< |
break; |
930 |
< |
U.compareAndSwapObject(q, WCOWAIT, r, r.cowait); |
931 |
< |
} |
932 |
< |
} |
995 |
> |
if (q != null && (w = q.thread) != null) |
996 |
> |
U.unpark(w); |
997 |
|
} |
998 |
|
} |
999 |
|
|
1009 |
|
private long acquireWrite(boolean interruptible, long deadline) { |
1010 |
|
WNode node = null, p; |
1011 |
|
for (int spins = -1;;) { // spin while enqueuing |
1012 |
< |
long s, ns; |
1013 |
< |
if (((s = state) & ABITS) == 0L) { |
1012 |
> |
long m, s, ns; |
1013 |
> |
if ((m = (s = state) & ABITS) == 0L) { |
1014 |
|
if (U.compareAndSwapLong(this, STATE, s, ns = s + WBIT)) |
1015 |
|
return ns; |
1016 |
|
} |
1017 |
+ |
else if (spins < 0) |
1018 |
+ |
spins = (m == WBIT && wtail == whead) ? SPINS : 0; |
1019 |
|
else if (spins > 0) { |
1020 |
|
if (ThreadLocalRandom.current().nextInt() >= 0) |
1021 |
|
--spins; |
1022 |
|
} |
1023 |
|
else if ((p = wtail) == null) { // initialize queue |
1024 |
< |
WNode h = new WNode(WMODE, null); |
1025 |
< |
if (U.compareAndSwapObject(this, WHEAD, null, h)) |
1026 |
< |
wtail = h; |
1024 |
> |
WNode hd = new WNode(WMODE, null); |
1025 |
> |
if (U.compareAndSwapObject(this, WHEAD, null, hd)) |
1026 |
> |
wtail = hd; |
1027 |
|
} |
962 |
– |
else if (spins < 0) |
963 |
– |
spins = (p == whead) ? SPINS : 0; |
1028 |
|
else if (node == null) |
1029 |
|
node = new WNode(WMODE, p); |
1030 |
|
else if (node.prev != p) |
1035 |
|
} |
1036 |
|
} |
1037 |
|
|
1038 |
< |
for (int spins = SPINS;;) { |
1039 |
< |
WNode np, pp; int ps; long s, ns; Thread w; |
1040 |
< |
while ((np = node.prev) != p && np != null) |
1041 |
< |
(p = np).next = node; // stale |
1042 |
< |
if (whead == p) { |
1038 |
> |
for (int spins = -1;;) { |
1039 |
> |
WNode h, np, pp; int ps; |
1040 |
> |
if ((h = whead) == p) { |
1041 |
> |
if (spins < 0) |
1042 |
> |
spins = HEAD_SPINS; |
1043 |
> |
else if (spins < MAX_HEAD_SPINS) |
1044 |
> |
spins <<= 1; |
1045 |
|
for (int k = spins;;) { // spin at head |
1046 |
< |
if (((s = state) & ABITS) == 0L && |
1047 |
< |
U.compareAndSwapLong(this, STATE, s, ns = s + WBIT)) { |
1048 |
< |
whead = node; |
1049 |
< |
node.prev = null; |
1050 |
< |
return ns; |
1046 |
> |
long s, ns; |
1047 |
> |
if (((s = state) & ABITS) == 0L) { |
1048 |
> |
if (U.compareAndSwapLong(this, STATE, s, |
1049 |
> |
ns = s + WBIT)) { |
1050 |
> |
whead = node; |
1051 |
> |
node.prev = null; |
1052 |
> |
return ns; |
1053 |
> |
} |
1054 |
|
} |
1055 |
|
else if (ThreadLocalRandom.current().nextInt() >= 0 && |
1056 |
|
--k <= 0) |
1057 |
|
break; |
1058 |
|
} |
990 |
– |
if (spins < MAX_HEAD_SPINS) |
991 |
– |
spins <<= 1; |
1059 |
|
} |
1060 |
< |
if ((ps = p.status) == 0) |
1061 |
< |
U.compareAndSwapInt(p, WSTATUS, 0, WAITING); |
1062 |
< |
else if (ps == CANCELLED) { |
1063 |
< |
if ((pp = p.prev) != null) { |
1064 |
< |
node.prev = pp; |
1065 |
< |
pp.next = node; |
1060 |
> |
else if (h != null) { // help release stale waiters |
1061 |
> |
WNode c; Thread w; |
1062 |
> |
while ((c = h.cowait) != null) { |
1063 |
> |
if (U.compareAndSwapObject(h, WCOWAIT, c, c.cowait) && |
1064 |
> |
(w = c.thread) != null) |
1065 |
> |
U.unpark(w); |
1066 |
|
} |
1067 |
|
} |
1068 |
< |
else { |
1069 |
< |
long time; // 0 argument to park means no timeout |
1070 |
< |
if (deadline == 0L) |
1071 |
< |
time = 0L; |
1072 |
< |
else if ((time = deadline - System.nanoTime()) <= 0L) |
1073 |
< |
return cancelWaiter(node, null, false); |
1074 |
< |
node.thread = Thread.currentThread(); |
1075 |
< |
if (node.prev == p && p.status == WAITING && // recheck |
1076 |
< |
(p != whead || (state & ABITS) != 0L)) { |
1077 |
< |
U.park(false, time); |
1068 |
> |
if (whead == h) { |
1069 |
> |
if ((np = node.prev) != p) { |
1070 |
> |
if (np != null) |
1071 |
> |
(p = np).next = node; // stale |
1072 |
> |
} |
1073 |
> |
else if ((ps = p.status) == 0) |
1074 |
> |
U.compareAndSwapInt(p, WSTATUS, 0, WAITING); |
1075 |
> |
else if (ps == CANCELLED) { |
1076 |
> |
if ((pp = p.prev) != null) { |
1077 |
> |
node.prev = pp; |
1078 |
> |
pp.next = node; |
1079 |
> |
} |
1080 |
> |
} |
1081 |
> |
else { |
1082 |
> |
long time; // 0 argument to park means no timeout |
1083 |
> |
if (deadline == 0L) |
1084 |
> |
time = 0L; |
1085 |
> |
else if ((time = deadline - System.nanoTime()) <= 0L) |
1086 |
> |
return cancelWaiter(node, node, false); |
1087 |
> |
Thread wt = Thread.currentThread(); |
1088 |
> |
U.putObject(wt, PARKBLOCKER, this); |
1089 |
> |
node.thread = wt; |
1090 |
> |
if (p.status < 0 && (p != h || (state & ABITS) != 0L) && |
1091 |
> |
whead == h && node.prev == p) |
1092 |
> |
U.park(false, time); // emulate LockSupport.park |
1093 |
> |
node.thread = null; |
1094 |
> |
U.putObject(wt, PARKBLOCKER, null); |
1095 |
|
if (interruptible && Thread.interrupted()) |
1096 |
< |
return cancelWaiter(node, null, true); |
1096 |
> |
return cancelWaiter(node, node, true); |
1097 |
|
} |
1014 |
– |
node.thread = null; |
1098 |
|
} |
1099 |
|
} |
1100 |
|
} |
1109 |
|
* @return next state, or INTERRUPTED |
1110 |
|
*/ |
1111 |
|
private long acquireRead(boolean interruptible, long deadline) { |
1112 |
< |
WNode node = null, group = null, p; |
1112 |
> |
WNode node = null, p; |
1113 |
|
for (int spins = -1;;) { |
1114 |
< |
for (;;) { |
1115 |
< |
long s, m, ns; WNode h, q; Thread w; // anti-barging guard |
1116 |
< |
if (group == null && (h = whead) != null && |
1117 |
< |
(q = h.next) != null && q.mode != RMODE) |
1118 |
< |
break; |
1119 |
< |
if ((m = (s = state) & ABITS) == WBIT) |
1120 |
< |
break; |
1121 |
< |
if (m < RFULL ? |
1122 |
< |
U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT) : |
1123 |
< |
(ns = tryIncReaderOverflow(s)) != 0L) { |
1124 |
< |
if (group != null) { // help release others |
1125 |
< |
for (WNode r = group;;) { |
1126 |
< |
if ((w = r.thread) != null) { |
1127 |
< |
r.thread = null; |
1128 |
< |
U.unpark(w); |
1114 |
> |
WNode h; |
1115 |
> |
if ((h = whead) == (p = wtail)) { |
1116 |
> |
for (long m, s, ns;;) { |
1117 |
> |
if ((m = (s = state) & ABITS) < RFULL ? |
1118 |
> |
U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT) : |
1119 |
> |
(m < WBIT && (ns = tryIncReaderOverflow(s)) != 0L)) |
1120 |
> |
return ns; |
1121 |
> |
else if (m >= WBIT) { |
1122 |
> |
if (spins > 0) { |
1123 |
> |
if (ThreadLocalRandom.current().nextInt() >= 0) |
1124 |
> |
--spins; |
1125 |
> |
} |
1126 |
> |
else { |
1127 |
> |
if (spins == 0) { |
1128 |
> |
WNode nh = whead, np = wtail; |
1129 |
> |
if ((nh == h && np == p) || (h = nh) != (p = np)) |
1130 |
> |
break; |
1131 |
|
} |
1132 |
< |
if ((r = group.cowait) == null) |
1048 |
< |
break; |
1049 |
< |
U.compareAndSwapObject(group, WCOWAIT, r, r.cowait); |
1132 |
> |
spins = SPINS; |
1133 |
|
} |
1134 |
|
} |
1052 |
– |
return ns; |
1135 |
|
} |
1136 |
|
} |
1137 |
< |
if (spins > 0) { |
1138 |
< |
if (ThreadLocalRandom.current().nextInt() >= 0) |
1139 |
< |
--spins; |
1140 |
< |
} |
1059 |
< |
else if ((p = wtail) == null) { |
1060 |
< |
WNode h = new WNode(WMODE, null); |
1061 |
< |
if (U.compareAndSwapObject(this, WHEAD, null, h)) |
1062 |
< |
wtail = h; |
1137 |
> |
if (p == null) { // initialize queue |
1138 |
> |
WNode hd = new WNode(WMODE, null); |
1139 |
> |
if (U.compareAndSwapObject(this, WHEAD, null, hd)) |
1140 |
> |
wtail = hd; |
1141 |
|
} |
1064 |
– |
else if (spins < 0) |
1065 |
– |
spins = (p == whead) ? SPINS : 0; |
1142 |
|
else if (node == null) |
1143 |
< |
node = new WNode(WMODE, p); |
1144 |
< |
else if (node.prev != p) |
1145 |
< |
node.prev = p; |
1146 |
< |
else if (p.mode == RMODE && p != whead) { |
1147 |
< |
WNode pp = p.prev; // become co-waiter with group p |
1148 |
< |
if (pp != null && p == wtail && |
1149 |
< |
U.compareAndSwapObject(p, WCOWAIT, |
1150 |
< |
node.cowait = p.cowait, node)) { |
1151 |
< |
node.thread = Thread.currentThread(); |
1152 |
< |
for (long time;;) { |
1143 |
> |
node = new WNode(RMODE, p); |
1144 |
> |
else if (h == p || p.mode != RMODE) { |
1145 |
> |
if (node.prev != p) |
1146 |
> |
node.prev = p; |
1147 |
> |
else if (U.compareAndSwapObject(this, WTAIL, p, node)) { |
1148 |
> |
p.next = node; |
1149 |
> |
break; |
1150 |
> |
} |
1151 |
> |
} |
1152 |
> |
else if (!U.compareAndSwapObject(p, WCOWAIT, |
1153 |
> |
node.cowait = p.cowait, node)) |
1154 |
> |
node.cowait = null; |
1155 |
> |
else { |
1156 |
> |
for (;;) { |
1157 |
> |
WNode pp, c; Thread w; |
1158 |
> |
if ((h = whead) != null && (c = h.cowait) != null && |
1159 |
> |
U.compareAndSwapObject(h, WCOWAIT, c, c.cowait) && |
1160 |
> |
(w = c.thread) != null) // help release |
1161 |
> |
U.unpark(w); |
1162 |
> |
if (h == (pp = p.prev) || h == p || pp == null) { |
1163 |
> |
long m, s, ns; |
1164 |
> |
do { |
1165 |
> |
if ((m = (s = state) & ABITS) < RFULL ? |
1166 |
> |
U.compareAndSwapLong(this, STATE, s, |
1167 |
> |
ns = s + RUNIT) : |
1168 |
> |
(m < WBIT && |
1169 |
> |
(ns = tryIncReaderOverflow(s)) != 0L)) |
1170 |
> |
return ns; |
1171 |
> |
} while (m < WBIT); |
1172 |
> |
} |
1173 |
> |
if (whead == h && p.prev == pp) { |
1174 |
> |
long time; |
1175 |
> |
if (pp == null || h == p || p.status > 0) { |
1176 |
> |
node = null; // throw away |
1177 |
> |
break; |
1178 |
> |
} |
1179 |
|
if (deadline == 0L) |
1180 |
|
time = 0L; |
1181 |
|
else if ((time = deadline - System.nanoTime()) <= 0L) |
1182 |
|
return cancelWaiter(node, p, false); |
1183 |
< |
if (node.thread == null) |
1184 |
< |
break; |
1185 |
< |
if (p.prev != pp || p.status == CANCELLED || |
1186 |
< |
p == whead || p.prev != pp) { |
1187 |
< |
node.thread = null; |
1188 |
< |
break; |
1189 |
< |
} |
1190 |
< |
if (node.thread == null) // must recheck |
1089 |
< |
break; |
1090 |
< |
U.park(false, time); |
1183 |
> |
Thread wt = Thread.currentThread(); |
1184 |
> |
U.putObject(wt, PARKBLOCKER, this); |
1185 |
> |
node.thread = wt; |
1186 |
> |
if ((h != pp || (state & ABITS) == WBIT) && |
1187 |
> |
whead == h && p.prev == pp) |
1188 |
> |
U.park(false, time); |
1189 |
> |
node.thread = null; |
1190 |
> |
U.putObject(wt, PARKBLOCKER, null); |
1191 |
|
if (interruptible && Thread.interrupted()) |
1192 |
|
return cancelWaiter(node, p, true); |
1193 |
|
} |
1094 |
– |
group = p; |
1194 |
|
} |
1096 |
– |
node = null; // throw away |
1097 |
– |
} |
1098 |
– |
else if (U.compareAndSwapObject(this, WTAIL, p, node)) { |
1099 |
– |
p.next = node; |
1100 |
– |
break; |
1195 |
|
} |
1196 |
|
} |
1197 |
|
|
1198 |
< |
for (int spins = SPINS;;) { |
1199 |
< |
WNode np, pp, r; int ps; long m, s, ns; Thread w; |
1200 |
< |
while ((np = node.prev) != p && np != null) |
1201 |
< |
(p = np).next = node; |
1202 |
< |
if (whead == p) { |
1203 |
< |
for (int k = spins;;) { |
1204 |
< |
if ((m = (s = state) & ABITS) != WBIT) { |
1205 |
< |
if (m < RFULL ? |
1206 |
< |
U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT): |
1207 |
< |
(ns = tryIncReaderOverflow(s)) != 0L) { |
1208 |
< |
whead = node; |
1209 |
< |
node.prev = null; |
1210 |
< |
while ((r = node.cowait) != null) { |
1211 |
< |
if (U.compareAndSwapObject(node, WCOWAIT, |
1212 |
< |
r, r.cowait) && |
1213 |
< |
(w = r.thread) != null) { |
1214 |
< |
r.thread = null; |
1215 |
< |
U.unpark(w); // release co-waiter |
1216 |
< |
} |
1217 |
< |
} |
1124 |
< |
return ns; |
1198 |
> |
for (int spins = -1;;) { |
1199 |
> |
WNode h, np, pp; int ps; |
1200 |
> |
if ((h = whead) == p) { |
1201 |
> |
if (spins < 0) |
1202 |
> |
spins = HEAD_SPINS; |
1203 |
> |
else if (spins < MAX_HEAD_SPINS) |
1204 |
> |
spins <<= 1; |
1205 |
> |
for (int k = spins;;) { // spin at head |
1206 |
> |
long m, s, ns; |
1207 |
> |
if ((m = (s = state) & ABITS) < RFULL ? |
1208 |
> |
U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT) : |
1209 |
> |
(m < WBIT && (ns = tryIncReaderOverflow(s)) != 0L)) { |
1210 |
> |
WNode c; Thread w; |
1211 |
> |
whead = node; |
1212 |
> |
node.prev = null; |
1213 |
> |
while ((c = node.cowait) != null) { |
1214 |
> |
if (U.compareAndSwapObject(node, WCOWAIT, |
1215 |
> |
c, c.cowait) && |
1216 |
> |
(w = c.thread) != null) |
1217 |
> |
U.unpark(w); |
1218 |
|
} |
1219 |
+ |
return ns; |
1220 |
|
} |
1221 |
< |
else if (ThreadLocalRandom.current().nextInt() >= 0 && |
1222 |
< |
--k <= 0) |
1221 |
> |
else if (m >= WBIT && |
1222 |
> |
ThreadLocalRandom.current().nextInt() >= 0 && --k <= 0) |
1223 |
|
break; |
1224 |
|
} |
1131 |
– |
if (spins < MAX_HEAD_SPINS) |
1132 |
– |
spins <<= 1; |
1225 |
|
} |
1226 |
< |
if ((ps = p.status) == 0) |
1227 |
< |
U.compareAndSwapInt(p, WSTATUS, 0, WAITING); |
1228 |
< |
else if (ps == CANCELLED) { |
1229 |
< |
if ((pp = p.prev) != null) { |
1230 |
< |
node.prev = pp; |
1231 |
< |
pp.next = node; |
1226 |
> |
else if (h != null) { |
1227 |
> |
WNode c; Thread w; |
1228 |
> |
while ((c = h.cowait) != null) { |
1229 |
> |
if (U.compareAndSwapObject(h, WCOWAIT, c, c.cowait) && |
1230 |
> |
(w = c.thread) != null) |
1231 |
> |
U.unpark(w); |
1232 |
|
} |
1233 |
|
} |
1234 |
< |
else { |
1235 |
< |
long time; |
1236 |
< |
if (deadline == 0L) |
1237 |
< |
time = 0L; |
1238 |
< |
else if ((time = deadline - System.nanoTime()) <= 0L) |
1239 |
< |
return cancelWaiter(node, null, false); |
1240 |
< |
node.thread = Thread.currentThread(); |
1241 |
< |
if (node.prev == p && p.status == WAITING && |
1242 |
< |
(p != whead || (state & ABITS) != WBIT)) { |
1243 |
< |
U.park(false, time); |
1234 |
> |
if (whead == h) { |
1235 |
> |
if ((np = node.prev) != p) { |
1236 |
> |
if (np != null) |
1237 |
> |
(p = np).next = node; // stale |
1238 |
> |
} |
1239 |
> |
else if ((ps = p.status) == 0) |
1240 |
> |
U.compareAndSwapInt(p, WSTATUS, 0, WAITING); |
1241 |
> |
else if (ps == CANCELLED) { |
1242 |
> |
if ((pp = p.prev) != null) { |
1243 |
> |
node.prev = pp; |
1244 |
> |
pp.next = node; |
1245 |
> |
} |
1246 |
> |
} |
1247 |
> |
else { |
1248 |
> |
long time; |
1249 |
> |
if (deadline == 0L) |
1250 |
> |
time = 0L; |
1251 |
> |
else if ((time = deadline - System.nanoTime()) <= 0L) |
1252 |
> |
return cancelWaiter(node, node, false); |
1253 |
> |
Thread wt = Thread.currentThread(); |
1254 |
> |
U.putObject(wt, PARKBLOCKER, this); |
1255 |
> |
node.thread = wt; |
1256 |
> |
if (p.status < 0 && |
1257 |
> |
(p != h || (state & ABITS) == WBIT) && |
1258 |
> |
whead == h && node.prev == p) |
1259 |
> |
U.park(false, time); |
1260 |
> |
node.thread = null; |
1261 |
> |
U.putObject(wt, PARKBLOCKER, null); |
1262 |
|
if (interruptible && Thread.interrupted()) |
1263 |
< |
return cancelWaiter(node, null, true); |
1263 |
> |
return cancelWaiter(node, node, true); |
1264 |
|
} |
1155 |
– |
node.thread = null; |
1265 |
|
} |
1266 |
|
} |
1267 |
|
} |
1268 |
|
|
1269 |
|
/** |
1270 |
< |
* If node non-null, forces cancel status and unsplices from queue |
1271 |
< |
* if possible. This is a variant of cancellation methods in |
1270 |
> |
* If node non-null, forces cancel status and unsplices it from |
1271 |
> |
* queue if possible and wakes up any cowaiters (of the node, or |
1272 |
> |
* group, as applicable), and in any case helps release current |
1273 |
> |
* first waiter if lock is free. (Calling with null arguments |
1274 |
> |
* serves as a conditional form of release, which is not currently |
1275 |
> |
* needed but may be needed under possible future cancellation |
1276 |
> |
* policies). This is a variant of cancellation methods in |
1277 |
|
* AbstractQueuedSynchronizer (see its detailed explanation in AQS |
1278 |
< |
* internal documentation) that more conservatively wakes up other |
1279 |
< |
* threads that may have had their links changed, so as to preserve |
1280 |
< |
* liveness in the main signalling methods. |
1278 |
> |
* internal documentation). |
1279 |
> |
* |
1280 |
> |
* @param node if non-null, the waiter |
1281 |
> |
* @param group either node or the group node is cowaiting with |
1282 |
> |
* @param interrupted if already interrupted |
1283 |
> |
* @return INTERRUPTED if interrupted or Thread.interrupted, else zero |
1284 |
|
*/ |
1285 |
|
private long cancelWaiter(WNode node, WNode group, boolean interrupted) { |
1286 |
< |
if (node != null) { |
1287 |
< |
node.thread = null; |
1286 |
> |
if (node != null && group != null) { |
1287 |
> |
Thread w; |
1288 |
|
node.status = CANCELLED; |
1289 |
< |
if (group != null) { |
1290 |
< |
for (WNode p = group, q; p != null; p = q) { |
1291 |
< |
if ((q = p.cowait) != null && q.status == CANCELLED) { |
1292 |
< |
U.compareAndSwapObject(p, WCOWAIT, q, q.cowait); |
1293 |
< |
break; |
1177 |
< |
} |
1289 |
> |
// unsplice cancelled nodes from group |
1290 |
> |
for (WNode p = group, q; (q = p.cowait) != null;) { |
1291 |
> |
if (q.status == CANCELLED) { |
1292 |
> |
U.compareAndSwapObject(p, WCOWAIT, q, q.cowait); |
1293 |
> |
p = group; // restart |
1294 |
|
} |
1295 |
+ |
else |
1296 |
+ |
p = q; |
1297 |
|
} |
1298 |
< |
else { |
1299 |
< |
for (WNode pred = node.prev; pred != null; ) { |
1300 |
< |
WNode succ, pp; Thread w; |
1298 |
> |
if (group == node) { |
1299 |
> |
for (WNode r = group.cowait; r != null; r = r.cowait) { |
1300 |
> |
if ((w = r.thread) != null) |
1301 |
> |
U.unpark(w); // wake up uncancelled co-waiters |
1302 |
> |
} |
1303 |
> |
for (WNode pred = node.prev; pred != null; ) { // unsplice |
1304 |
> |
WNode succ, pp; // find valid successor |
1305 |
|
while ((succ = node.next) == null || |
1306 |
|
succ.status == CANCELLED) { |
1307 |
< |
WNode q = null; |
1307 |
> |
WNode q = null; // find successor the slow way |
1308 |
|
for (WNode t = wtail; t != null && t != node; t = t.prev) |
1309 |
|
if (t.status != CANCELLED) |
1310 |
< |
q = t; |
1311 |
< |
if (succ == q || |
1310 |
> |
q = t; // don't link if succ cancelled |
1311 |
> |
if (succ == q || // ensure accurate successor |
1312 |
|
U.compareAndSwapObject(node, WNEXT, |
1313 |
|
succ, succ = q)) { |
1314 |
|
if (succ == null && node == wtail) |
1316 |
|
break; |
1317 |
|
} |
1318 |
|
} |
1319 |
< |
if (pred.next == node) |
1319 |
> |
if (pred.next == node) // unsplice pred link |
1320 |
|
U.compareAndSwapObject(pred, WNEXT, node, succ); |
1321 |
< |
if (succ != null && (w = succ.thread) != null) |
1322 |
< |
U.unpark(w); |
1321 |
> |
if (succ != null && (w = succ.thread) != null) { |
1322 |
> |
succ.thread = null; |
1323 |
> |
U.unpark(w); // wake up succ to observe new pred |
1324 |
> |
} |
1325 |
|
if (pred.status != CANCELLED || (pp = pred.prev) == null) |
1326 |
|
break; |
1327 |
< |
node.prev = pp; // repeat for new pred |
1327 |
> |
node.prev = pp; // repeat if new pred wrong/cancelled |
1328 |
|
U.compareAndSwapObject(pp, WNEXT, pred, succ); |
1329 |
|
pred = pp; |
1330 |
|
} |
1331 |
|
} |
1332 |
|
} |
1333 |
< |
release(whead); |
1333 |
> |
WNode h; // Possibly release first waiter |
1334 |
> |
while ((h = whead) != null) { |
1335 |
> |
long s; WNode q; // similar to release() but check eligibility |
1336 |
> |
if ((q = h.next) == null || q.status == CANCELLED) { |
1337 |
> |
for (WNode t = wtail; t != null && t != h; t = t.prev) |
1338 |
> |
if (t.status <= 0) |
1339 |
> |
q = t; |
1340 |
> |
} |
1341 |
> |
if (h == whead) { |
1342 |
> |
if (q != null && h.status == 0 && |
1343 |
> |
((s = state) & ABITS) != WBIT && // waiter is eligible |
1344 |
> |
(s == 0L || q.mode == RMODE)) |
1345 |
> |
release(h); |
1346 |
> |
break; |
1347 |
> |
} |
1348 |
> |
} |
1349 |
|
return (interrupted || Thread.interrupted()) ? INTERRUPTED : 0L; |
1350 |
|
} |
1351 |
|
|
1357 |
|
private static final long WNEXT; |
1358 |
|
private static final long WSTATUS; |
1359 |
|
private static final long WCOWAIT; |
1360 |
+ |
private static final long PARKBLOCKER; |
1361 |
|
|
1362 |
|
static { |
1363 |
|
try { |
1376 |
|
(wk.getDeclaredField("next")); |
1377 |
|
WCOWAIT = U.objectFieldOffset |
1378 |
|
(wk.getDeclaredField("cowait")); |
1379 |
+ |
Class<?> tk = Thread.class; |
1380 |
+ |
PARKBLOCKER = U.objectFieldOffset |
1381 |
+ |
(tk.getDeclaredField("parkBlocker")); |
1382 |
|
|
1383 |
|
} catch (Exception e) { |
1384 |
|
throw new Error(e); |