ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/UncheckedLockLoops.java
Revision: 1.9
Committed: Tue Mar 15 19:47:06 2011 UTC (13 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.8: +1 -1 lines
Log Message:
Update Creative Commons license URL in legal notices

File Contents

# Content
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.*;
14
15 public final class UncheckedLockLoops {
16 static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
17 static boolean print = false;
18 static boolean doBuiltin = true;
19
20 public static void main(String[] args) throws Exception {
21 int maxThreads = 100;
22 int iters = 10000000;
23
24 if (args.length > 0)
25 maxThreads = Integer.parseInt(args[0]);
26
27 rng.setSeed(3122688L);
28
29 print = false;
30 System.out.println("Warmup...");
31 oneTest(1, 100000);
32 Thread.sleep(1000);
33 oneTest(3, 10000);
34 Thread.sleep(1000);
35 oneTest(2, 10000);
36 Thread.sleep(100);
37 oneTest(1, 100000);
38 Thread.sleep(100);
39 oneTest(1, 100000);
40 Thread.sleep(1000);
41 print = true;
42
43 System.out.println("Threads:" + 1);
44 oneTest(1, iters / 1);
45 Thread.sleep(100);
46
47 for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) {
48 System.out.println("Threads:" + i);
49 oneTest(i, iters / i);
50 Thread.sleep(100);
51 }
52
53 }
54
55 static void oneTest(int nthreads, int iters) throws Exception {
56 int fairIters = (nthreads <= 1) ? iters : iters/20;
57 int v = rng.next();
58
59 if (print)
60 System.out.print("NoLock (1 thread) ");
61 new NoLockLoop().test(v, 1, iters * nthreads);
62 Thread.sleep(10);
63
64 if (print)
65 System.out.print("ReentrantLock ");
66 new ReentrantLockLoop().test(v, nthreads, iters);
67 Thread.sleep(10);
68
69 if (false) {
70 if (print)
71 System.out.print("FairReentrantLock ");
72 new FairReentrantLockLoop().test(v, nthreads, fairIters);
73 Thread.sleep(10);
74 }
75 if (doBuiltin) {
76 if (print)
77 System.out.print("builtin lock ");
78 new BuiltinLockLoop().test(v, nthreads, fairIters);
79 Thread.sleep(10);
80 }
81
82 if (print)
83 System.out.print("Mutex ");
84 new MutexLoop().test(v, nthreads, iters);
85 Thread.sleep(10);
86
87 if (print)
88 System.out.print("LongMutex ");
89 new LongMutexLoop().test(v, nthreads, iters);
90 Thread.sleep(10);
91
92 if (print)
93 System.out.print("Semaphore ");
94 new SemaphoreLoop().test(v, nthreads, iters);
95 Thread.sleep(10);
96
97 if (print)
98 System.out.print("FairSemaphore ");
99 new FairSemaphoreLoop().test(v, nthreads, fairIters);
100 Thread.sleep(10);
101
102 if (print)
103 System.out.print("ReentrantWriteLock ");
104 new ReentrantWriteLockLoop().test(v, nthreads, iters);
105 Thread.sleep(10);
106
107 if (print)
108 System.out.print("FairRWriteLock ");
109 new FairReentrantWriteLockLoop().test(v, nthreads, fairIters);
110 Thread.sleep(10);
111
112 if (print)
113 System.out.print("ReentrantReadWriteLock");
114 new ReentrantReadWriteLockLoop().test(v, nthreads, iters);
115 Thread.sleep(10);
116
117 if (print)
118 System.out.print("FairRReadWriteLock ");
119 new FairReentrantReadWriteLockLoop().test(v, nthreads, fairIters);
120 Thread.sleep(10);
121
122 }
123
124 abstract static class LockLoop implements Runnable {
125 int value;
126 int checkValue;
127 int iters;
128 volatile int result;
129 volatile int failures;
130 final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
131 CyclicBarrier barrier;
132
133 final int setValue(int v) {
134 checkValue = v ^ 0x55555555;
135 value = v;
136 return v;
137 }
138
139 final int getValue() {
140 int v = value;
141 if (checkValue != ~(v ^ 0xAAAAAAAA))
142 ++failures;
143 return v;
144 }
145
146 final void test(int initialValue, int nthreads, int iters) throws Exception {
147 setValue(initialValue);
148 this.iters = iters;
149 barrier = new CyclicBarrier(nthreads+1, timer);
150 for (int i = 0; i < nthreads; ++i)
151 new Thread(this).start();
152 barrier.await();
153 barrier.await();
154 long time = timer.getTime();
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;
159 // System.out.print("\t " + secs + "s run time");
160 System.out.println();
161 }
162
163 if (result == 0) // avoid overoptimization
164 System.out.println("useless result: " + result);
165 if (failures != 0)
166 throw new Error("protection failure?");
167 }
168 abstract int loop(int n);
169 public final void run() {
170 try {
171 barrier.await();
172 result += loop(iters);
173 barrier.await();
174 }
175 catch (Exception ie) {
176 return;
177 }
178 }
179
180 }
181
182 private static class NoLockLoop extends LockLoop {
183 private volatile int readBarrier;
184 final int loop(int n) {
185 int sum = 0;
186 int x = 0;
187 while (n-- > 0) {
188 int r1 = readBarrier;
189 x = setValue(LoopHelpers.compute1(getValue()));
190 int r2 = readBarrier;
191 if (r1 == r2 && (x & 255) == 0)
192 ++readBarrier;
193 sum += LoopHelpers.compute2(x);
194 }
195 return sum;
196 }
197 }
198
199 private static class BuiltinLockLoop extends LockLoop {
200 final int loop(int n) {
201 int sum = 0;
202 int x = 0;
203 while (n-- > 0) {
204 synchronized (this) {
205 x = setValue(LoopHelpers.compute1(getValue()));
206 }
207 sum += LoopHelpers.compute2(x);
208 }
209 return sum;
210 }
211 }
212
213 private static class ReentrantLockLoop extends LockLoop {
214 private final ReentrantLock lock = new ReentrantLock();
215 final int loop(int n) {
216 final ReentrantLock lock = this.lock;
217 int sum = 0;
218 int x = 0;
219 while (n-- > 0) {
220 lock.lock();
221 try {
222 x = setValue(LoopHelpers.compute1(getValue()));
223 }
224 finally {
225 lock.unlock();
226 }
227 sum += LoopHelpers.compute2(x);
228 }
229 return sum;
230 }
231 }
232
233 private static class MutexLoop extends LockLoop {
234 private final Mutex lock = new Mutex();
235 final int loop(int n) {
236 final Mutex lock = this.lock;
237 int sum = 0;
238 int x = 0;
239 while (n-- > 0) {
240 lock.lock();
241 try {
242 x = setValue(LoopHelpers.compute1(getValue()));
243 }
244 finally {
245 lock.unlock();
246 }
247 sum += LoopHelpers.compute2(x);
248 }
249 return sum;
250 }
251 }
252
253 private static class LongMutexLoop extends LockLoop {
254 private final LongMutex lock = new LongMutex();
255 final int loop(int n) {
256 final LongMutex lock = this.lock;
257 int sum = 0;
258 int x = 0;
259 while (n-- > 0) {
260 lock.lock();
261 try {
262 x = setValue(LoopHelpers.compute1(getValue()));
263 }
264 finally {
265 lock.unlock();
266 }
267 sum += LoopHelpers.compute2(x);
268 }
269 return sum;
270 }
271 }
272
273 private static class FairReentrantLockLoop extends LockLoop {
274 private final ReentrantLock lock = new ReentrantLock(true);
275 final int loop(int n) {
276 final ReentrantLock lock = this.lock;
277 int sum = 0;
278 int x = 0;
279 while (n-- > 0) {
280 lock.lock();
281 try {
282 x = setValue(LoopHelpers.compute1(getValue()));
283 }
284 finally {
285 lock.unlock();
286 }
287 sum += LoopHelpers.compute2(x);
288 }
289 return sum;
290 }
291 }
292
293 private static class ReentrantWriteLockLoop extends LockLoop {
294 private final Lock lock = new ReentrantReadWriteLock().writeLock();
295 final int loop(int n) {
296 final Lock lock = this.lock;
297 int sum = 0;
298 int x = 0;
299 while (n-- > 0) {
300 lock.lock();
301 try {
302 x = setValue(LoopHelpers.compute1(getValue()));
303 }
304 finally {
305 lock.unlock();
306 }
307 sum += LoopHelpers.compute2(x);
308 }
309 return sum;
310 }
311 }
312
313 private static class FairReentrantWriteLockLoop extends LockLoop {
314 final Lock lock = new ReentrantReadWriteLock(true).writeLock();
315 final int loop(int n) {
316 final Lock lock = this.lock;
317 int sum = 0;
318 int x = 0;
319 while (n-- > 0) {
320 lock.lock();
321 try {
322 x = setValue(LoopHelpers.compute1(getValue()));
323 }
324 finally {
325 lock.unlock();
326 }
327 sum += LoopHelpers.compute2(x);
328 }
329 return sum;
330 }
331 }
332
333 private static class SemaphoreLoop extends LockLoop {
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;
338 int x = 0;
339 while (n-- > 0) {
340 sem.acquireUninterruptibly();
341 try {
342 x = setValue(LoopHelpers.compute1(getValue()));
343 }
344 finally {
345 sem.release();
346 }
347 sum += LoopHelpers.compute2(x);
348 }
349 return sum;
350 }
351 }
352 private static class FairSemaphoreLoop extends LockLoop {
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;
357 int x = 0;
358 while (n-- > 0) {
359 sem.acquireUninterruptibly();
360 try {
361 x = setValue(LoopHelpers.compute1(getValue()));
362 }
363 finally {
364 sem.release();
365 }
366 sum += LoopHelpers.compute2(x);
367 }
368 return sum;
369 }
370 }
371
372 private static class ReentrantReadWriteLockLoop extends LockLoop {
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();
377 int sum = 0;
378 int x = 0;
379 while (n-- > 0) {
380 if ((n & 16) != 0) {
381 rlock.lock();
382 try {
383 x = LoopHelpers.compute1(getValue());
384 x = LoopHelpers.compute2(x);
385 }
386 finally {
387 rlock.unlock();
388 }
389 }
390 else {
391 wlock.lock();
392 try {
393 setValue(x);
394 }
395 finally {
396 wlock.unlock();
397 }
398 sum += LoopHelpers.compute2(x);
399 }
400 }
401 return sum;
402 }
403
404 }
405
406
407 private static class FairReentrantReadWriteLockLoop extends LockLoop {
408 private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
409 final int loop(int n) {
410 final Lock rlock = lock.readLock();
411 final Lock wlock = lock.writeLock();
412 int sum = 0;
413 int x = 0;
414 while (n-- > 0) {
415 if ((n & 16) != 0) {
416 rlock.lock();
417 try {
418 x = LoopHelpers.compute1(getValue());
419 x = LoopHelpers.compute2(x);
420 }
421 finally {
422 rlock.unlock();
423 }
424 }
425 else {
426 wlock.lock();
427 try {
428 setValue(x);
429 }
430 finally {
431 wlock.unlock();
432 }
433 sum += LoopHelpers.compute2(x);
434 }
435 }
436 return sum;
437 }
438
439 }
440 }