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 |
|
|
22 |
|
import junit.framework.Test; |
23 |
|
import junit.framework.TestSuite; |
24 |
|
|
25 |
+ |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
26 |
|
public class ReentrantLockTest extends JSR166TestCase { |
27 |
|
public static void main(String[] args) { |
28 |
|
main(suite(), args); |
148 |
|
|
149 |
|
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
150 |
|
|
151 |
+ |
static AwaitMethod randomAwaitMethod() { |
152 |
+ |
AwaitMethod[] awaitMethods = AwaitMethod.values(); |
153 |
+ |
return awaitMethods[ThreadLocalRandom.current().nextInt(awaitMethods.length)]; |
154 |
+ |
} |
155 |
+ |
|
156 |
|
/** |
157 |
|
* Awaits condition "indefinitely" using the specified AwaitMethod. |
158 |
|
*/ |
1140 |
|
lock.unlock(); |
1141 |
|
assertTrue(lock.toString().contains("Unlocked")); |
1142 |
|
} |
1143 |
+ |
|
1144 |
+ |
/** |
1145 |
+ |
* Tests scenario for JDK-8187408 |
1146 |
+ |
* AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock |
1147 |
+ |
*/ |
1148 |
+ |
public void testBug8187408() throws InterruptedException { |
1149 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
1150 |
+ |
final AwaitMethod awaitMethod = randomAwaitMethod(); |
1151 |
+ |
final int nThreads = rnd.nextInt(2, 10); |
1152 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
1153 |
+ |
final Condition cond = lock.newCondition(); |
1154 |
+ |
final CountDownLatch done = new CountDownLatch(nThreads); |
1155 |
+ |
final ArrayList<Thread> threads = new ArrayList<>(); |
1156 |
+ |
|
1157 |
+ |
Runnable rogue = () -> { |
1158 |
+ |
while (done.getCount() > 0) { |
1159 |
+ |
try { |
1160 |
+ |
// call await without holding lock?! |
1161 |
+ |
await(cond, awaitMethod); |
1162 |
+ |
throw new AssertionError("should throw"); |
1163 |
+ |
} |
1164 |
+ |
catch (IllegalMonitorStateException expected) {} |
1165 |
+ |
catch (Throwable fail) { threadUnexpectedException(fail); }}}; |
1166 |
+ |
Thread rogueThread = new Thread(rogue, "rogue"); |
1167 |
+ |
threads.add(rogueThread); |
1168 |
+ |
rogueThread.start(); |
1169 |
+ |
|
1170 |
+ |
Runnable waiter = () -> { |
1171 |
+ |
lock.lock(); |
1172 |
+ |
try { |
1173 |
+ |
done.countDown(); |
1174 |
+ |
cond.await(); |
1175 |
+ |
} catch (Throwable fail) { |
1176 |
+ |
threadUnexpectedException(fail); |
1177 |
+ |
} finally { |
1178 |
+ |
lock.unlock(); |
1179 |
+ |
}}; |
1180 |
+ |
for (int i = 0; i < nThreads; i++) { |
1181 |
+ |
Thread thread = new Thread(waiter, "waiter"); |
1182 |
+ |
threads.add(thread); |
1183 |
+ |
thread.start(); |
1184 |
+ |
} |
1185 |
+ |
|
1186 |
+ |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1187 |
+ |
lock.lock(); |
1188 |
+ |
try { |
1189 |
+ |
assertEquals(nThreads, lock.getWaitQueueLength(cond)); |
1190 |
+ |
} finally { |
1191 |
+ |
cond.signalAll(); |
1192 |
+ |
lock.unlock(); |
1193 |
+ |
} |
1194 |
+ |
for (Thread thread : threads) { |
1195 |
+ |
thread.join(LONG_DELAY_MS); |
1196 |
+ |
assertFalse(thread.isAlive()); |
1197 |
+ |
} |
1198 |
+ |
} |
1199 |
|
} |