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.concurrent.*; |
12 |
< |
import java.util.concurrent.locks.*; |
13 |
< |
import java.util.*; |
11 |
> |
import java.util.concurrent.CyclicBarrier; |
12 |
> |
import java.util.concurrent.Semaphore; |
13 |
> |
import java.util.concurrent.locks.Lock; |
14 |
> |
import java.util.concurrent.locks.ReentrantLock; |
15 |
> |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
16 |
|
|
17 |
|
public final class UncheckedLockLoops { |
18 |
|
static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); |
23 |
|
int maxThreads = 100; |
24 |
|
int iters = 10000000; |
25 |
|
|
26 |
< |
if (args.length > 0) |
26 |
> |
if (args.length > 0) |
27 |
|
maxThreads = Integer.parseInt(args[0]); |
28 |
|
|
29 |
|
rng.setSeed(3122688L); |
51 |
|
oneTest(i, iters / i); |
52 |
|
Thread.sleep(100); |
53 |
|
} |
52 |
– |
|
54 |
|
} |
55 |
|
|
56 |
|
static void oneTest(int nthreads, int iters) throws Exception { |
57 |
< |
int fairIters = (nthreads <= 1)? iters : iters/20; |
57 |
> |
int fairIters = (nthreads <= 1) ? iters : iters/20; |
58 |
|
int v = rng.next(); |
59 |
|
|
60 |
|
if (print) |
61 |
|
System.out.print("NoLock (1 thread) "); |
62 |
|
new NoLockLoop().test(v, 1, iters * nthreads); |
63 |
|
Thread.sleep(10); |
64 |
< |
|
64 |
> |
|
65 |
|
if (print) |
66 |
|
System.out.print("ReentrantLock "); |
67 |
|
new ReentrantLockLoop().test(v, nthreads, iters); |
94 |
|
System.out.print("Semaphore "); |
95 |
|
new SemaphoreLoop().test(v, nthreads, iters); |
96 |
|
Thread.sleep(10); |
97 |
< |
|
97 |
> |
|
98 |
|
if (print) |
99 |
|
System.out.print("FairSemaphore "); |
100 |
|
new FairSemaphoreLoop().test(v, nthreads, fairIters); |
109 |
|
System.out.print("FairRWriteLock "); |
110 |
|
new FairReentrantWriteLockLoop().test(v, nthreads, fairIters); |
111 |
|
Thread.sleep(10); |
112 |
< |
|
112 |
> |
|
113 |
|
if (print) |
114 |
|
System.out.print("ReentrantReadWriteLock"); |
115 |
|
new ReentrantReadWriteLockLoop().test(v, nthreads, iters); |
116 |
|
Thread.sleep(10); |
117 |
< |
|
117 |
> |
|
118 |
|
if (print) |
119 |
|
System.out.print("FairRReadWriteLock "); |
120 |
|
new FairReentrantReadWriteLockLoop().test(v, nthreads, fairIters); |
121 |
|
Thread.sleep(10); |
121 |
– |
|
122 |
|
} |
123 |
|
|
124 |
< |
static abstract class LockLoop implements Runnable { |
124 |
> |
abstract static class LockLoop implements Runnable { |
125 |
|
int value; |
126 |
|
int checkValue; |
127 |
|
int iters; |
138 |
|
|
139 |
|
final int getValue() { |
140 |
|
int v = value; |
141 |
< |
if (checkValue != ~(v ^ 0xAAAAAAAA)) |
141 |
> |
if (checkValue != ~(v ^ 0xAAAAAAAA)) |
142 |
|
++failures; |
143 |
|
return v; |
144 |
|
} |
147 |
|
setValue(initialValue); |
148 |
|
this.iters = iters; |
149 |
|
barrier = new CyclicBarrier(nthreads+1, timer); |
150 |
< |
for (int i = 0; i < nthreads; ++i) |
150 |
> |
for (int i = 0; i < nthreads; ++i) |
151 |
|
new Thread(this).start(); |
152 |
|
barrier.await(); |
153 |
|
barrier.await(); |
155 |
|
if (print) { |
156 |
|
long tpi = time / (iters * nthreads); |
157 |
|
System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update"); |
158 |
< |
// double secs = (double)(time) / 1000000000.0; |
158 |
> |
// double secs = (double) time / 1000000000.0; |
159 |
|
// System.out.print("\t " + secs + "s run time"); |
160 |
|
System.out.println(); |
161 |
|
} |
168 |
|
abstract int loop(int n); |
169 |
|
public final void run() { |
170 |
|
try { |
171 |
< |
barrier.await(); |
171 |
> |
barrier.await(); |
172 |
|
result += loop(iters); |
173 |
|
barrier.await(); |
174 |
|
} |
175 |
< |
catch (Exception ie) { |
176 |
< |
return; |
175 |
> |
catch (Exception ie) { |
176 |
> |
return; |
177 |
|
} |
178 |
|
} |
179 |
|
|
201 |
|
int sum = 0; |
202 |
|
int x = 0; |
203 |
|
while (n-- > 0) { |
204 |
< |
synchronized(this) { |
204 |
> |
synchronized (this) { |
205 |
|
x = setValue(LoopHelpers.compute1(getValue())); |
206 |
|
} |
207 |
|
sum += LoopHelpers.compute2(x); |
211 |
|
} |
212 |
|
|
213 |
|
private static class ReentrantLockLoop extends LockLoop { |
214 |
< |
final private ReentrantLock lock = new ReentrantLock(); |
214 |
> |
private final ReentrantLock lock = new ReentrantLock(); |
215 |
|
final int loop(int n) { |
216 |
|
final ReentrantLock lock = this.lock; |
217 |
|
int sum = 0; |
231 |
|
} |
232 |
|
|
233 |
|
private static class MutexLoop extends LockLoop { |
234 |
< |
final private Mutex lock = new Mutex(); |
234 |
> |
private final Mutex lock = new Mutex(); |
235 |
|
final int loop(int n) { |
236 |
|
final Mutex lock = this.lock; |
237 |
|
int sum = 0; |
251 |
|
} |
252 |
|
|
253 |
|
private static class LongMutexLoop extends LockLoop { |
254 |
< |
final private LongMutex lock = new LongMutex(); |
254 |
> |
private final LongMutex lock = new LongMutex(); |
255 |
|
final int loop(int n) { |
256 |
|
final LongMutex lock = this.lock; |
257 |
|
int sum = 0; |
271 |
|
} |
272 |
|
|
273 |
|
private static class FairReentrantLockLoop extends LockLoop { |
274 |
< |
final private ReentrantLock lock = new ReentrantLock(true); |
274 |
> |
private final ReentrantLock lock = new ReentrantLock(true); |
275 |
|
final int loop(int n) { |
276 |
|
final ReentrantLock lock = this.lock; |
277 |
|
int sum = 0; |
291 |
|
} |
292 |
|
|
293 |
|
private static class ReentrantWriteLockLoop extends LockLoop { |
294 |
< |
final private Lock lock = new ReentrantReadWriteLock().writeLock(); |
294 |
> |
private final Lock lock = new ReentrantReadWriteLock().writeLock(); |
295 |
|
final int loop(int n) { |
296 |
|
final Lock lock = this.lock; |
297 |
|
int sum = 0; |
331 |
|
} |
332 |
|
|
333 |
|
private static class SemaphoreLoop extends LockLoop { |
334 |
< |
final private Semaphore sem = new Semaphore(1, false); |
334 |
> |
private final Semaphore sem = new Semaphore(1, false); |
335 |
|
final int loop(int n) { |
336 |
|
final Semaphore sem = this.sem; |
337 |
|
int sum = 0; |
350 |
|
} |
351 |
|
} |
352 |
|
private static class FairSemaphoreLoop extends LockLoop { |
353 |
< |
final private Semaphore sem = new Semaphore(1, true); |
353 |
> |
private final Semaphore sem = new Semaphore(1, true); |
354 |
|
final int loop(int n) { |
355 |
|
final Semaphore sem = this.sem; |
356 |
|
int sum = 0; |
370 |
|
} |
371 |
|
|
372 |
|
private static class ReentrantReadWriteLockLoop extends LockLoop { |
373 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
373 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
374 |
|
final int loop(int n) { |
375 |
|
final Lock rlock = lock.readLock(); |
376 |
|
final Lock wlock = lock.writeLock(); |
403 |
|
|
404 |
|
} |
405 |
|
|
406 |
– |
|
406 |
|
private static class FairReentrantReadWriteLockLoop extends LockLoop { |
407 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
407 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
408 |
|
final int loop(int n) { |
409 |
|
final Lock rlock = lock.readLock(); |
410 |
|
final Lock wlock = lock.writeLock(); |