9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
13 |
|
import java.util.concurrent.locks.AbstractQueuedSynchronizer; |
14 |
|
import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject; |
15 |
|
|
122 |
|
} |
123 |
|
|
124 |
|
/** A constant to clarify calls to checking methods below. */ |
125 |
< |
final static Thread[] NO_THREADS = new Thread[0]; |
125 |
> |
static final Thread[] NO_THREADS = new Thread[0]; |
126 |
|
|
127 |
|
/** |
128 |
|
* Spin-waits until sync.isQueued(t) becomes true. |
948 |
|
} |
949 |
|
|
950 |
|
/** |
951 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
951 |
> |
* awaitUninterruptibly is uninterruptible |
952 |
|
*/ |
953 |
|
public void testAwaitUninterruptibly() { |
954 |
|
final Mutex sync = new Mutex(); |
955 |
|
final ConditionObject c = sync.newCondition(); |
956 |
< |
final BooleanLatch acquired = new BooleanLatch(); |
956 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
957 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
958 |
|
public void realRun() { |
959 |
|
sync.acquire(); |
960 |
< |
assertTrue(acquired.releaseShared(0)); |
960 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
961 |
|
c.awaitUninterruptibly(); |
962 |
|
assertTrue(Thread.interrupted()); |
963 |
|
assertHasWaitersLocked(sync, c, NO_THREADS); |
964 |
|
sync.release(); |
965 |
|
}}); |
966 |
|
|
967 |
< |
acquired.acquireShared(0); |
967 |
> |
pleaseInterrupt.acquireShared(0); |
968 |
|
sync.acquire(); |
969 |
|
assertHasWaitersLocked(sync, c, t); |
970 |
|
sync.release(); |
991 |
|
public void testInterruptible(final AwaitMethod awaitMethod) { |
992 |
|
final Mutex sync = new Mutex(); |
993 |
|
final ConditionObject c = sync.newCondition(); |
994 |
< |
final BooleanLatch acquired = new BooleanLatch(); |
994 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
995 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
996 |
|
public void realRun() throws InterruptedException { |
997 |
|
sync.acquire(); |
998 |
< |
assertTrue(acquired.releaseShared(0)); |
998 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
999 |
|
await(c, awaitMethod); |
1000 |
|
}}); |
1001 |
|
|
1002 |
< |
acquired.acquireShared(0); |
1002 |
> |
pleaseInterrupt.acquireShared(0); |
1003 |
|
t.interrupt(); |
1004 |
|
awaitTermination(t); |
1005 |
|
} |
1191 |
|
*/ |
1192 |
|
public void testTryAcquireSharedNanos_Timeout() { |
1193 |
|
final BooleanLatch l = new BooleanLatch(); |
1194 |
+ |
final BooleanLatch observedQueued = new BooleanLatch(); |
1195 |
+ |
final long timeoutMillis = timeoutMillis(); |
1196 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1197 |
|
public void realRun() throws InterruptedException { |
1198 |
|
assertFalse(l.isSignalled()); |
1199 |
< |
long startTime = System.nanoTime(); |
1200 |
< |
long nanos = MILLISECONDS.toNanos(timeoutMillis()); |
1201 |
< |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1202 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1199 |
> |
for (long millis = timeoutMillis(); |
1200 |
> |
!observedQueued.isSignalled(); |
1201 |
> |
millis *= 2) { |
1202 |
> |
long nanos = MILLISECONDS.toNanos(millis); |
1203 |
> |
long startTime = System.nanoTime(); |
1204 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1205 |
> |
assertTrue(millisElapsedSince(startTime) >= millis); |
1206 |
> |
} |
1207 |
|
assertFalse(l.isSignalled()); |
1208 |
|
}}); |
1209 |
|
|
1210 |
|
waitForQueuedThread(l, t); |
1211 |
+ |
observedQueued.releaseShared(0); |
1212 |
|
assertFalse(l.isSignalled()); |
1213 |
|
awaitTermination(t); |
1214 |
|
assertFalse(l.isSignalled()); |
1215 |
|
} |
1216 |
|
|
1217 |
+ |
/** |
1218 |
+ |
* awaitNanos/timed await with 0 wait times out immediately |
1219 |
+ |
*/ |
1220 |
+ |
public void testAwait_Zero() throws InterruptedException { |
1221 |
+ |
final Mutex sync = new Mutex(); |
1222 |
+ |
final ConditionObject c = sync.newCondition(); |
1223 |
+ |
sync.acquire(); |
1224 |
+ |
assertTrue(c.awaitNanos(0L) <= 0); |
1225 |
+ |
assertFalse(c.await(0L, NANOSECONDS)); |
1226 |
+ |
sync.release(); |
1227 |
+ |
} |
1228 |
+ |
|
1229 |
+ |
/** |
1230 |
+ |
* awaitNanos/timed await with maximum negative wait times does not underflow |
1231 |
+ |
*/ |
1232 |
+ |
public void testAwait_NegativeInfinity() throws InterruptedException { |
1233 |
+ |
final Mutex sync = new Mutex(); |
1234 |
+ |
final ConditionObject c = sync.newCondition(); |
1235 |
+ |
sync.acquire(); |
1236 |
+ |
assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0); |
1237 |
+ |
assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS)); |
1238 |
+ |
sync.release(); |
1239 |
+ |
} |
1240 |
+ |
|
1241 |
|
} |