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