8 |
|
|
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
+ |
import java.util.ArrayList; |
12 |
|
import java.util.Arrays; |
13 |
|
import java.util.Collection; |
14 |
|
import java.util.HashSet; |
15 |
|
import java.util.concurrent.CountDownLatch; |
16 |
|
import java.util.concurrent.CyclicBarrier; |
17 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
18 |
|
import java.util.concurrent.locks.Condition; |
19 |
|
import java.util.concurrent.locks.ReentrantLock; |
20 |
|
|
19 |
– |
import junit.framework.AssertionFailedError; |
21 |
|
import junit.framework.Test; |
22 |
|
import junit.framework.TestSuite; |
23 |
|
|
24 |
+ |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
25 |
|
public class ReentrantLockTest extends JSR166TestCase { |
26 |
|
public static void main(String[] args) { |
27 |
|
main(suite(), args); |
87 |
|
long startTime = System.nanoTime(); |
88 |
|
while (!lock.hasQueuedThread(t)) { |
89 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
90 |
< |
throw new AssertionFailedError("timed out"); |
90 |
> |
throw new AssertionError("timed out"); |
91 |
|
Thread.yield(); |
92 |
|
} |
93 |
|
assertTrue(t.isAlive()); |
147 |
|
|
148 |
|
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
149 |
|
|
150 |
+ |
static AwaitMethod randomAwaitMethod() { |
151 |
+ |
AwaitMethod[] awaitMethods = AwaitMethod.values(); |
152 |
+ |
return awaitMethods[ThreadLocalRandom.current().nextInt(awaitMethods.length)]; |
153 |
+ |
} |
154 |
+ |
|
155 |
|
/** |
156 |
|
* Awaits condition "indefinitely" using the specified AwaitMethod. |
157 |
|
*/ |
892 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
893 |
|
public void testAwaitUninterruptibly(boolean fair) { |
894 |
|
final ReentrantLock lock = new ReentrantLock(fair); |
895 |
< |
final Condition c = lock.newCondition(); |
895 |
> |
final Condition condition = lock.newCondition(); |
896 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
897 |
|
|
898 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
901 |
|
lock.lock(); |
902 |
|
pleaseInterrupt.countDown(); |
903 |
|
Thread.currentThread().interrupt(); |
904 |
< |
c.awaitUninterruptibly(); |
904 |
> |
condition.awaitUninterruptibly(); |
905 |
|
assertTrue(Thread.interrupted()); |
906 |
|
lock.unlock(); |
907 |
|
}}); |
911 |
|
// Interrupt during awaitUninterruptibly |
912 |
|
lock.lock(); |
913 |
|
pleaseInterrupt.countDown(); |
914 |
< |
c.awaitUninterruptibly(); |
914 |
> |
condition.awaitUninterruptibly(); |
915 |
|
assertTrue(Thread.interrupted()); |
916 |
|
lock.unlock(); |
917 |
|
}}); |
918 |
|
|
919 |
|
await(pleaseInterrupt); |
920 |
+ |
t2.interrupt(); |
921 |
|
lock.lock(); |
922 |
|
lock.unlock(); |
923 |
< |
t2.interrupt(); |
924 |
< |
|
917 |
< |
assertThreadStaysAlive(t1); |
918 |
< |
assertTrue(t2.isAlive()); |
923 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
924 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
925 |
|
|
926 |
|
lock.lock(); |
927 |
< |
c.signalAll(); |
927 |
> |
condition.signalAll(); |
928 |
|
lock.unlock(); |
929 |
|
|
930 |
|
awaitTermination(t1); |
1139 |
|
lock.unlock(); |
1140 |
|
assertTrue(lock.toString().contains("Unlocked")); |
1141 |
|
} |
1142 |
+ |
|
1143 |
+ |
/** |
1144 |
+ |
* Tests scenario for JDK-8187408 |
1145 |
+ |
* AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock |
1146 |
+ |
*/ |
1147 |
+ |
public void testBug8187408() throws InterruptedException { |
1148 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
1149 |
+ |
final AwaitMethod awaitMethod = randomAwaitMethod(); |
1150 |
+ |
final int nThreads = rnd.nextInt(2, 10); |
1151 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
1152 |
+ |
final Condition cond = lock.newCondition(); |
1153 |
+ |
final CountDownLatch done = new CountDownLatch(nThreads); |
1154 |
+ |
final ArrayList<Thread> threads = new ArrayList<>(); |
1155 |
+ |
|
1156 |
+ |
Runnable rogue = () -> { |
1157 |
+ |
while (done.getCount() > 0) { |
1158 |
+ |
try { |
1159 |
+ |
// call await without holding lock?! |
1160 |
+ |
await(cond, awaitMethod); |
1161 |
+ |
throw new AssertionError("should throw"); |
1162 |
+ |
} |
1163 |
+ |
catch (IllegalMonitorStateException expected) {} |
1164 |
+ |
catch (Throwable fail) { threadUnexpectedException(fail); }}}; |
1165 |
+ |
Thread rogueThread = new Thread(rogue, "rogue"); |
1166 |
+ |
threads.add(rogueThread); |
1167 |
+ |
rogueThread.start(); |
1168 |
+ |
|
1169 |
+ |
Runnable waiter = () -> { |
1170 |
+ |
lock.lock(); |
1171 |
+ |
try { |
1172 |
+ |
done.countDown(); |
1173 |
+ |
cond.await(); |
1174 |
+ |
} catch (Throwable fail) { |
1175 |
+ |
threadUnexpectedException(fail); |
1176 |
+ |
} finally { |
1177 |
+ |
lock.unlock(); |
1178 |
+ |
}}; |
1179 |
+ |
for (int i = 0; i < nThreads; i++) { |
1180 |
+ |
Thread thread = new Thread(waiter, "waiter"); |
1181 |
+ |
threads.add(thread); |
1182 |
+ |
thread.start(); |
1183 |
+ |
} |
1184 |
+ |
|
1185 |
+ |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1186 |
+ |
lock.lock(); |
1187 |
+ |
try { |
1188 |
+ |
assertEquals(nThreads, lock.getWaitQueueLength(cond)); |
1189 |
+ |
} finally { |
1190 |
+ |
cond.signalAll(); |
1191 |
+ |
lock.unlock(); |
1192 |
+ |
} |
1193 |
+ |
for (Thread thread : threads) { |
1194 |
+ |
thread.join(LONG_DELAY_MS); |
1195 |
+ |
assertFalse(thread.isAlive()); |
1196 |
+ |
} |
1197 |
+ |
} |
1198 |
|
} |