ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CheckedLockLoops.java
Revision: 1.11
Committed: Thu Jan 15 18:34:18 2015 UTC (9 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.10: +0 -1 lines
Log Message:
delete extraneous blank lines

File Contents

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