117 |
|
} |
118 |
|
|
119 |
|
/** A constant to clarify calls to checking methods below. */ |
120 |
< |
final static Thread[] NO_THREADS = new Thread[0]; |
120 |
> |
static final Thread[] NO_THREADS = new Thread[0]; |
121 |
|
|
122 |
|
/** |
123 |
|
* Spin-waits until sync.isQueued(t) becomes true. |
944 |
|
} |
945 |
|
|
946 |
|
/** |
947 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
947 |
> |
* awaitUninterruptibly is uninterruptible |
948 |
|
*/ |
949 |
|
public void testAwaitUninterruptibly() { |
950 |
|
final Mutex sync = new Mutex(); |
951 |
|
final ConditionObject c = sync.newCondition(); |
952 |
< |
final BooleanLatch acquired = new BooleanLatch(); |
952 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
953 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
954 |
|
public void realRun() { |
955 |
|
sync.acquire(); |
956 |
< |
assertTrue(acquired.releaseShared(0)); |
956 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
957 |
|
c.awaitUninterruptibly(); |
958 |
|
assertTrue(Thread.interrupted()); |
959 |
|
assertHasWaitersLocked(sync, c, NO_THREADS); |
960 |
|
sync.release(); |
961 |
|
}}); |
962 |
|
|
963 |
< |
acquired.acquireShared(0); |
963 |
> |
pleaseInterrupt.acquireShared(0); |
964 |
|
sync.acquire(); |
965 |
|
assertHasWaitersLocked(sync, c, t); |
966 |
|
sync.release(); |
987 |
|
public void testInterruptible(final AwaitMethod awaitMethod) { |
988 |
|
final Mutex sync = new Mutex(); |
989 |
|
final ConditionObject c = sync.newCondition(); |
990 |
< |
final BooleanLatch acquired = new BooleanLatch(); |
990 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
991 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
992 |
|
public void realRun() throws InterruptedException { |
993 |
|
sync.acquire(); |
994 |
< |
assertTrue(acquired.releaseShared(0)); |
994 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
995 |
|
await(c, awaitMethod); |
996 |
|
}}); |
997 |
|
|
998 |
< |
acquired.acquireShared(0); |
998 |
> |
pleaseInterrupt.acquireShared(0); |
999 |
|
t.interrupt(); |
1000 |
|
awaitTermination(t); |
1001 |
|
} |
1187 |
|
*/ |
1188 |
|
public void testTryAcquireSharedNanos_Timeout() { |
1189 |
|
final BooleanLatch l = new BooleanLatch(); |
1190 |
+ |
final BooleanLatch observedQueued = new BooleanLatch(); |
1191 |
+ |
final long timeoutMillis = timeoutMillis(); |
1192 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1193 |
|
public void realRun() throws InterruptedException { |
1194 |
|
assertFalse(l.isSignalled()); |
1195 |
< |
long startTime = System.nanoTime(); |
1196 |
< |
long nanos = MILLISECONDS.toNanos(timeoutMillis()); |
1197 |
< |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1198 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1195 |
> |
for (long millis = timeoutMillis(); |
1196 |
> |
!observedQueued.isSignalled(); |
1197 |
> |
millis *= 2) { |
1198 |
> |
long nanos = MILLISECONDS.toNanos(millis); |
1199 |
> |
long startTime = System.nanoTime(); |
1200 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1201 |
> |
assertTrue(millisElapsedSince(startTime) >= millis); |
1202 |
> |
} |
1203 |
|
assertFalse(l.isSignalled()); |
1204 |
|
}}); |
1205 |
|
|
1206 |
|
waitForQueuedThread(l, t); |
1207 |
+ |
observedQueued.releaseShared(0); |
1208 |
|
assertFalse(l.isSignalled()); |
1209 |
|
awaitTermination(t); |
1210 |
|
assertFalse(l.isSignalled()); |