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/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.ExecutorService; |
13 |
> |
import java.util.concurrent.Executors; |
14 |
> |
import java.util.concurrent.Semaphore; |
15 |
> |
import java.util.concurrent.locks.Lock; |
16 |
> |
import java.util.concurrent.locks.ReentrantLock; |
17 |
> |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
18 |
|
|
19 |
|
public final class CheckedLockLoops { |
20 |
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
25 |
|
public static void main(String[] args) throws Exception { |
26 |
|
int maxThreads = 100; |
27 |
|
int iters = 2000000; |
28 |
< |
if (args.length > 0) |
28 |
> |
if (args.length > 0) |
29 |
|
maxThreads = Integer.parseInt(args[0]); |
30 |
|
rng.setSeed(3122688L); |
31 |
|
warmup(iters); |
35 |
|
|
36 |
|
static void runTest(int maxThreads, int iters) throws Exception { |
37 |
|
print = true; |
38 |
< |
int k = 1; |
30 |
< |
for (int i = 1; i <= maxThreads;) { |
38 |
> |
for (int k = 1, i = 1; i <= maxThreads;) { |
39 |
|
System.out.println("Threads:" + i); |
40 |
|
oneTest(i, iters / i); |
41 |
|
if (i == k) { |
42 |
|
k = i << 1; |
43 |
|
i = i + (i >>> 1); |
44 |
< |
} |
45 |
< |
else |
44 |
> |
} |
45 |
> |
else |
46 |
|
i = k; |
47 |
|
} |
48 |
< |
} |
48 |
> |
} |
49 |
|
|
50 |
|
static void warmup(int iters) throws Exception { |
51 |
|
print = false; |
55 |
|
} |
56 |
|
|
57 |
|
static void oneTest(int nthreads, int iters) throws Exception { |
58 |
< |
int fairIters = (nthreads <= 1)? iters : iters/20; |
58 |
> |
int fairIters = (nthreads <= 1) ? iters : iters/20; |
59 |
|
int v = rng.next(); |
60 |
|
|
61 |
|
if (print) |
62 |
|
System.out.print("NoLock (1 thread) "); |
63 |
|
new NoLockLoop().test(v, 1, iters * nthreads); |
64 |
|
Thread.sleep(10); |
65 |
< |
|
65 |
> |
|
66 |
|
if (print) |
67 |
|
System.out.print("ReentrantLock "); |
68 |
|
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); |
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 |
|
pool.execute(this); |
152 |
|
barrier.await(); |
153 |
|
barrier.await(); |
167 |
|
abstract int loop(int n); |
168 |
|
public final void run() { |
169 |
|
try { |
170 |
< |
barrier.await(); |
170 |
> |
barrier.await(); |
171 |
|
result += loop(iters); |
172 |
|
barrier.await(); |
173 |
|
} |
174 |
< |
catch (Exception ie) { |
175 |
< |
return; |
174 |
> |
catch (Exception ie) { |
175 |
> |
return; |
176 |
|
} |
177 |
|
} |
178 |
|
|
182 |
|
private volatile int readBarrier; |
183 |
|
final int loop(int n) { |
184 |
|
int sum = 0; |
185 |
< |
int x = 0;; |
185 |
> |
int x = 0; |
186 |
|
while (n-- > 0) { |
187 |
|
int r1 = readBarrier; |
188 |
|
x = setValue(LoopHelpers.compute1(getValue())); |
198 |
|
private static class BuiltinLockLoop extends LockLoop { |
199 |
|
final int loop(int n) { |
200 |
|
int sum = 0; |
201 |
< |
int x = 0;; |
201 |
> |
int x = 0; |
202 |
|
while (n-- > 0) { |
203 |
< |
synchronized(this) { |
203 |
> |
synchronized (this) { |
204 |
|
x = setValue(LoopHelpers.compute1(getValue())); |
205 |
|
} |
206 |
|
sum += LoopHelpers.compute2(x); |
210 |
|
} |
211 |
|
|
212 |
|
private static class ReentrantLockLoop extends LockLoop { |
213 |
< |
final private ReentrantLock lock = new ReentrantLock(); |
213 |
> |
private final ReentrantLock lock = new ReentrantLock(); |
214 |
|
final int loop(int n) { |
215 |
|
final ReentrantLock lock = this.lock; |
216 |
|
int sum = 0; |
230 |
|
} |
231 |
|
|
232 |
|
private static class MutexLoop extends LockLoop { |
233 |
< |
final private Mutex lock = new Mutex(); |
233 |
> |
private final Mutex lock = new Mutex(); |
234 |
|
final int loop(int n) { |
235 |
|
final Mutex lock = this.lock; |
236 |
|
int sum = 0; |
250 |
|
} |
251 |
|
|
252 |
|
private static class LongMutexLoop extends LockLoop { |
253 |
< |
final private LongMutex lock = new LongMutex(); |
253 |
> |
private final LongMutex lock = new LongMutex(); |
254 |
|
final int loop(int n) { |
255 |
|
final LongMutex lock = this.lock; |
256 |
|
int sum = 0; |
270 |
|
} |
271 |
|
|
272 |
|
private static class FairReentrantLockLoop extends LockLoop { |
273 |
< |
final private ReentrantLock lock = new ReentrantLock(true); |
273 |
> |
private final ReentrantLock lock = new ReentrantLock(true); |
274 |
|
final int loop(int n) { |
275 |
|
final ReentrantLock lock = this.lock; |
276 |
|
int sum = 0; |
290 |
|
} |
291 |
|
|
292 |
|
private static class ReentrantWriteLockLoop extends LockLoop { |
293 |
< |
final private Lock lock = new ReentrantReadWriteLock().writeLock(); |
293 |
> |
private final Lock lock = new ReentrantReadWriteLock().writeLock(); |
294 |
|
final int loop(int n) { |
295 |
|
final Lock lock = this.lock; |
296 |
|
int sum = 0; |
330 |
|
} |
331 |
|
|
332 |
|
private static class SemaphoreLoop extends LockLoop { |
333 |
< |
final private Semaphore sem = new Semaphore(1, false); |
333 |
> |
private final Semaphore sem = new Semaphore(1, false); |
334 |
|
final int loop(int n) { |
335 |
|
final Semaphore sem = this.sem; |
336 |
|
int sum = 0; |
349 |
|
} |
350 |
|
} |
351 |
|
private static class FairSemaphoreLoop extends LockLoop { |
352 |
< |
final private Semaphore sem = new Semaphore(1, true); |
352 |
> |
private final Semaphore sem = new Semaphore(1, true); |
353 |
|
final int loop(int n) { |
354 |
|
final Semaphore sem = this.sem; |
355 |
|
int sum = 0; |
369 |
|
} |
370 |
|
|
371 |
|
private static class ReentrantReadWriteLockLoop extends LockLoop { |
372 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
372 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
373 |
|
final int loop(int n) { |
374 |
|
final Lock rlock = lock.readLock(); |
375 |
|
final Lock wlock = lock.writeLock(); |
402 |
|
|
403 |
|
} |
404 |
|
|
397 |
– |
|
405 |
|
private static class FairReentrantReadWriteLockLoop extends LockLoop { |
406 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
406 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
407 |
|
final int loop(int n) { |
408 |
|
final Lock rlock = lock.readLock(); |
409 |
|
final Lock wlock = lock.writeLock(); |