1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
/* |
7 |
|
* @test |
8 |
|
* @summary basic safety and liveness of ReentrantLocks, and other locks based on them |
9 |
|
*/ |
10 |
|
|
11 |
+ |
import java.util.*; |
12 |
|
import java.util.concurrent.*; |
13 |
|
import java.util.concurrent.locks.*; |
13 |
– |
import java.util.*; |
14 |
|
|
15 |
|
public final class UncheckedLockLoops { |
16 |
|
static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); |
21 |
|
int maxThreads = 100; |
22 |
|
int iters = 10000000; |
23 |
|
|
24 |
< |
if (args.length > 0) |
24 |
> |
if (args.length > 0) |
25 |
|
maxThreads = Integer.parseInt(args[0]); |
26 |
|
|
27 |
|
rng.setSeed(3122688L); |
49 |
|
oneTest(i, iters / i); |
50 |
|
Thread.sleep(100); |
51 |
|
} |
52 |
– |
|
52 |
|
} |
53 |
|
|
54 |
|
static void oneTest(int nthreads, int iters) throws Exception { |
55 |
< |
int fairIters = (nthreads <= 1)? iters : iters/20; |
55 |
> |
int fairIters = (nthreads <= 1) ? iters : iters/20; |
56 |
|
int v = rng.next(); |
57 |
|
|
58 |
|
if (print) |
59 |
|
System.out.print("NoLock (1 thread) "); |
60 |
|
new NoLockLoop().test(v, 1, iters * nthreads); |
61 |
|
Thread.sleep(10); |
62 |
< |
|
62 |
> |
|
63 |
|
if (print) |
64 |
|
System.out.print("ReentrantLock "); |
65 |
|
new ReentrantLockLoop().test(v, nthreads, iters); |
92 |
|
System.out.print("Semaphore "); |
93 |
|
new SemaphoreLoop().test(v, nthreads, iters); |
94 |
|
Thread.sleep(10); |
95 |
< |
|
95 |
> |
|
96 |
|
if (print) |
97 |
|
System.out.print("FairSemaphore "); |
98 |
|
new FairSemaphoreLoop().test(v, nthreads, fairIters); |
107 |
|
System.out.print("FairRWriteLock "); |
108 |
|
new FairReentrantWriteLockLoop().test(v, nthreads, fairIters); |
109 |
|
Thread.sleep(10); |
110 |
< |
|
110 |
> |
|
111 |
|
if (print) |
112 |
|
System.out.print("ReentrantReadWriteLock"); |
113 |
|
new ReentrantReadWriteLockLoop().test(v, nthreads, iters); |
114 |
|
Thread.sleep(10); |
115 |
< |
|
115 |
> |
|
116 |
|
if (print) |
117 |
|
System.out.print("FairRReadWriteLock "); |
118 |
|
new FairReentrantReadWriteLockLoop().test(v, nthreads, fairIters); |
119 |
|
Thread.sleep(10); |
121 |
– |
|
120 |
|
} |
121 |
|
|
122 |
< |
static abstract class LockLoop implements Runnable { |
122 |
> |
abstract static class LockLoop implements Runnable { |
123 |
|
int value; |
124 |
|
int checkValue; |
125 |
|
int iters; |
136 |
|
|
137 |
|
final int getValue() { |
138 |
|
int v = value; |
139 |
< |
if (checkValue != ~(v ^ 0xAAAAAAAA)) |
139 |
> |
if (checkValue != ~(v ^ 0xAAAAAAAA)) |
140 |
|
++failures; |
141 |
|
return v; |
142 |
|
} |
145 |
|
setValue(initialValue); |
146 |
|
this.iters = iters; |
147 |
|
barrier = new CyclicBarrier(nthreads+1, timer); |
148 |
< |
for (int i = 0; i < nthreads; ++i) |
148 |
> |
for (int i = 0; i < nthreads; ++i) |
149 |
|
new Thread(this).start(); |
150 |
|
barrier.await(); |
151 |
|
barrier.await(); |
153 |
|
if (print) { |
154 |
|
long tpi = time / (iters * nthreads); |
155 |
|
System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update"); |
156 |
< |
// double secs = (double)(time) / 1000000000.0; |
156 |
> |
// double secs = (double) time / 1000000000.0; |
157 |
|
// System.out.print("\t " + secs + "s run time"); |
158 |
|
System.out.println(); |
159 |
|
} |
166 |
|
abstract int loop(int n); |
167 |
|
public final void run() { |
168 |
|
try { |
169 |
< |
barrier.await(); |
169 |
> |
barrier.await(); |
170 |
|
result += loop(iters); |
171 |
|
barrier.await(); |
172 |
|
} |
173 |
< |
catch (Exception ie) { |
174 |
< |
return; |
173 |
> |
catch (Exception ie) { |
174 |
> |
return; |
175 |
|
} |
176 |
|
} |
177 |
|
|
199 |
|
int sum = 0; |
200 |
|
int x = 0; |
201 |
|
while (n-- > 0) { |
202 |
< |
synchronized(this) { |
202 |
> |
synchronized (this) { |
203 |
|
x = setValue(LoopHelpers.compute1(getValue())); |
204 |
|
} |
205 |
|
sum += LoopHelpers.compute2(x); |
209 |
|
} |
210 |
|
|
211 |
|
private static class ReentrantLockLoop extends LockLoop { |
212 |
< |
final private ReentrantLock lock = new ReentrantLock(); |
212 |
> |
private final ReentrantLock lock = new ReentrantLock(); |
213 |
|
final int loop(int n) { |
214 |
|
final ReentrantLock lock = this.lock; |
215 |
|
int sum = 0; |
229 |
|
} |
230 |
|
|
231 |
|
private static class MutexLoop extends LockLoop { |
232 |
< |
final private Mutex lock = new Mutex(); |
232 |
> |
private final Mutex lock = new Mutex(); |
233 |
|
final int loop(int n) { |
234 |
|
final Mutex lock = this.lock; |
235 |
|
int sum = 0; |
249 |
|
} |
250 |
|
|
251 |
|
private static class LongMutexLoop extends LockLoop { |
252 |
< |
final private LongMutex lock = new LongMutex(); |
252 |
> |
private final LongMutex lock = new LongMutex(); |
253 |
|
final int loop(int n) { |
254 |
|
final LongMutex lock = this.lock; |
255 |
|
int sum = 0; |
269 |
|
} |
270 |
|
|
271 |
|
private static class FairReentrantLockLoop extends LockLoop { |
272 |
< |
final private ReentrantLock lock = new ReentrantLock(true); |
272 |
> |
private final ReentrantLock lock = new ReentrantLock(true); |
273 |
|
final int loop(int n) { |
274 |
|
final ReentrantLock lock = this.lock; |
275 |
|
int sum = 0; |
289 |
|
} |
290 |
|
|
291 |
|
private static class ReentrantWriteLockLoop extends LockLoop { |
292 |
< |
final private Lock lock = new ReentrantReadWriteLock().writeLock(); |
292 |
> |
private final Lock lock = new ReentrantReadWriteLock().writeLock(); |
293 |
|
final int loop(int n) { |
294 |
|
final Lock lock = this.lock; |
295 |
|
int sum = 0; |
329 |
|
} |
330 |
|
|
331 |
|
private static class SemaphoreLoop extends LockLoop { |
332 |
< |
final private Semaphore sem = new Semaphore(1, false); |
332 |
> |
private final Semaphore sem = new Semaphore(1, false); |
333 |
|
final int loop(int n) { |
334 |
|
final Semaphore sem = this.sem; |
335 |
|
int sum = 0; |
348 |
|
} |
349 |
|
} |
350 |
|
private static class FairSemaphoreLoop extends LockLoop { |
351 |
< |
final private Semaphore sem = new Semaphore(1, true); |
351 |
> |
private final Semaphore sem = new Semaphore(1, true); |
352 |
|
final int loop(int n) { |
353 |
|
final Semaphore sem = this.sem; |
354 |
|
int sum = 0; |
368 |
|
} |
369 |
|
|
370 |
|
private static class ReentrantReadWriteLockLoop extends LockLoop { |
371 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
371 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
372 |
|
final int loop(int n) { |
373 |
|
final Lock rlock = lock.readLock(); |
374 |
|
final Lock wlock = lock.writeLock(); |
401 |
|
|
402 |
|
} |
403 |
|
|
406 |
– |
|
404 |
|
private static class FairReentrantReadWriteLockLoop extends LockLoop { |
405 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
405 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
406 |
|
final int loop(int n) { |
407 |
|
final Lock rlock = lock.readLock(); |
408 |
|
final Lock wlock = lock.writeLock(); |