21 |
|
public static void main(String[] args) throws Exception { |
22 |
|
int maxThreads = 100; |
23 |
|
int iters = 2000000; |
24 |
< |
if (args.length > 0) |
24 |
> |
if (args.length > 0) |
25 |
|
maxThreads = Integer.parseInt(args[0]); |
26 |
|
rng.setSeed(3122688L); |
27 |
|
warmup(iters); |
38 |
|
if (i == k) { |
39 |
|
k = i << 1; |
40 |
|
i = i + (i >>> 1); |
41 |
< |
} |
42 |
< |
else |
41 |
> |
} |
42 |
> |
else |
43 |
|
i = k; |
44 |
|
} |
45 |
< |
} |
45 |
> |
} |
46 |
|
|
47 |
|
static void warmup(int iters) throws Exception { |
48 |
|
print = false; |
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); |
91 |
|
System.out.print("Semaphore "); |
92 |
|
new SemaphoreLoop().test(v, nthreads, iters); |
93 |
|
Thread.sleep(10); |
94 |
< |
|
94 |
> |
|
95 |
|
if (print) |
96 |
|
System.out.print("FairSemaphore "); |
97 |
|
new FairSemaphoreLoop().test(v, nthreads, fairIters); |
106 |
|
System.out.print("FairRWriteLock "); |
107 |
|
new FairReentrantWriteLockLoop().test(v, nthreads, fairIters); |
108 |
|
Thread.sleep(10); |
109 |
< |
|
109 |
> |
|
110 |
|
if (print) |
111 |
|
System.out.print("ReentrantReadWriteLock"); |
112 |
|
new ReentrantReadWriteLockLoop().test(v, nthreads, iters); |
113 |
|
Thread.sleep(10); |
114 |
< |
|
114 |
> |
|
115 |
|
if (print) |
116 |
|
System.out.print("FairRReadWriteLock "); |
117 |
|
new FairReentrantReadWriteLockLoop().test(v, nthreads, fairIters); |
118 |
|
Thread.sleep(10); |
119 |
|
} |
120 |
|
|
121 |
< |
static abstract class LockLoop implements Runnable { |
121 |
> |
abstract static class LockLoop implements Runnable { |
122 |
|
int value; |
123 |
|
int checkValue; |
124 |
|
int iters; |
135 |
|
|
136 |
|
final int getValue() { |
137 |
|
int v = value; |
138 |
< |
if (checkValue != ~(v ^ 0xAAAAAAAA)) |
138 |
> |
if (checkValue != ~(v ^ 0xAAAAAAAA)) |
139 |
|
++failures; |
140 |
|
return v; |
141 |
|
} |
144 |
|
setValue(initialValue); |
145 |
|
this.iters = iters; |
146 |
|
barrier = new CyclicBarrier(nthreads+1, timer); |
147 |
< |
for (int i = 0; i < nthreads; ++i) |
147 |
> |
for (int i = 0; i < nthreads; ++i) |
148 |
|
pool.execute(this); |
149 |
|
barrier.await(); |
150 |
|
barrier.await(); |
164 |
|
abstract int loop(int n); |
165 |
|
public final void run() { |
166 |
|
try { |
167 |
< |
barrier.await(); |
167 |
> |
barrier.await(); |
168 |
|
result += loop(iters); |
169 |
|
barrier.await(); |
170 |
|
} |
171 |
< |
catch (Exception ie) { |
172 |
< |
return; |
171 |
> |
catch (Exception ie) { |
172 |
> |
return; |
173 |
|
} |
174 |
|
} |
175 |
|
|
197 |
|
int sum = 0; |
198 |
|
int x = 0; |
199 |
|
while (n-- > 0) { |
200 |
< |
synchronized(this) { |
200 |
> |
synchronized (this) { |
201 |
|
x = setValue(LoopHelpers.compute1(getValue())); |
202 |
|
} |
203 |
|
sum += LoopHelpers.compute2(x); |
207 |
|
} |
208 |
|
|
209 |
|
private static class ReentrantLockLoop extends LockLoop { |
210 |
< |
final private ReentrantLock lock = new ReentrantLock(); |
210 |
> |
private final ReentrantLock lock = new ReentrantLock(); |
211 |
|
final int loop(int n) { |
212 |
|
final ReentrantLock lock = this.lock; |
213 |
|
int sum = 0; |
227 |
|
} |
228 |
|
|
229 |
|
private static class MutexLoop extends LockLoop { |
230 |
< |
final private Mutex lock = new Mutex(); |
230 |
> |
private final Mutex lock = new Mutex(); |
231 |
|
final int loop(int n) { |
232 |
|
final Mutex lock = this.lock; |
233 |
|
int sum = 0; |
247 |
|
} |
248 |
|
|
249 |
|
private static class LongMutexLoop extends LockLoop { |
250 |
< |
final private LongMutex lock = new LongMutex(); |
250 |
> |
private final LongMutex lock = new LongMutex(); |
251 |
|
final int loop(int n) { |
252 |
|
final LongMutex lock = this.lock; |
253 |
|
int sum = 0; |
267 |
|
} |
268 |
|
|
269 |
|
private static class FairReentrantLockLoop extends LockLoop { |
270 |
< |
final private ReentrantLock lock = new ReentrantLock(true); |
270 |
> |
private final ReentrantLock lock = new ReentrantLock(true); |
271 |
|
final int loop(int n) { |
272 |
|
final ReentrantLock lock = this.lock; |
273 |
|
int sum = 0; |
287 |
|
} |
288 |
|
|
289 |
|
private static class ReentrantWriteLockLoop extends LockLoop { |
290 |
< |
final private Lock lock = new ReentrantReadWriteLock().writeLock(); |
290 |
> |
private final Lock lock = new ReentrantReadWriteLock().writeLock(); |
291 |
|
final int loop(int n) { |
292 |
|
final Lock lock = this.lock; |
293 |
|
int sum = 0; |
327 |
|
} |
328 |
|
|
329 |
|
private static class SemaphoreLoop extends LockLoop { |
330 |
< |
final private Semaphore sem = new Semaphore(1, false); |
330 |
> |
private final Semaphore sem = new Semaphore(1, false); |
331 |
|
final int loop(int n) { |
332 |
|
final Semaphore sem = this.sem; |
333 |
|
int sum = 0; |
346 |
|
} |
347 |
|
} |
348 |
|
private static class FairSemaphoreLoop extends LockLoop { |
349 |
< |
final private Semaphore sem = new Semaphore(1, true); |
349 |
> |
private final Semaphore sem = new Semaphore(1, true); |
350 |
|
final int loop(int n) { |
351 |
|
final Semaphore sem = this.sem; |
352 |
|
int sum = 0; |
366 |
|
} |
367 |
|
|
368 |
|
private static class ReentrantReadWriteLockLoop extends LockLoop { |
369 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
369 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
370 |
|
final int loop(int n) { |
371 |
|
final Lock rlock = lock.readLock(); |
372 |
|
final Lock wlock = lock.writeLock(); |
401 |
|
|
402 |
|
|
403 |
|
private static class FairReentrantReadWriteLockLoop extends LockLoop { |
404 |
< |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
404 |
> |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
405 |
|
final int loop(int n) { |
406 |
|
final Lock rlock = lock.readLock(); |
407 |
|
final Lock wlock = lock.writeLock(); |