ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/UncheckedLockLoops.java
Revision: 1.13
Committed: Sat Dec 31 18:54:28 2016 UTC (7 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +5 -3 lines
Log Message:
organize imports

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.9 * 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.13 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 dl 1.1
17     public final class UncheckedLockLoops {
18     static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
19     static boolean print = false;
20     static boolean doBuiltin = true;
21    
22     public static void main(String[] args) throws Exception {
23     int maxThreads = 100;
24     int iters = 10000000;
25    
26 jsr166 1.4 if (args.length > 0)
27 dl 1.1 maxThreads = Integer.parseInt(args[0]);
28    
29     rng.setSeed(3122688L);
30    
31     print = false;
32     System.out.println("Warmup...");
33     oneTest(1, 100000);
34     Thread.sleep(1000);
35     oneTest(3, 10000);
36     Thread.sleep(1000);
37     oneTest(2, 10000);
38     Thread.sleep(100);
39     oneTest(1, 100000);
40     Thread.sleep(100);
41     oneTest(1, 100000);
42     Thread.sleep(1000);
43     print = true;
44    
45     System.out.println("Threads:" + 1);
46     oneTest(1, iters / 1);
47     Thread.sleep(100);
48    
49     for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) {
50     System.out.println("Threads:" + i);
51     oneTest(i, iters / i);
52     Thread.sleep(100);
53     }
54     }
55    
56     static void oneTest(int nthreads, int iters) throws Exception {
57 jsr166 1.5 int fairIters = (nthreads <= 1) ? iters : iters/20;
58 dl 1.1 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 jsr166 1.4
65 dl 1.1 if (print)
66     System.out.print("ReentrantLock ");
67     new ReentrantLockLoop().test(v, nthreads, iters);
68     Thread.sleep(10);
69    
70     if (false) {
71     if (print)
72     System.out.print("FairReentrantLock ");
73     new FairReentrantLockLoop().test(v, nthreads, fairIters);
74     Thread.sleep(10);
75     }
76     if (doBuiltin) {
77     if (print)
78     System.out.print("builtin lock ");
79     new BuiltinLockLoop().test(v, nthreads, fairIters);
80     Thread.sleep(10);
81     }
82    
83     if (print)
84     System.out.print("Mutex ");
85     new MutexLoop().test(v, nthreads, iters);
86     Thread.sleep(10);
87    
88     if (print)
89     System.out.print("LongMutex ");
90     new LongMutexLoop().test(v, nthreads, iters);
91     Thread.sleep(10);
92    
93     if (print)
94     System.out.print("Semaphore ");
95     new SemaphoreLoop().test(v, nthreads, iters);
96     Thread.sleep(10);
97 jsr166 1.4
98 dl 1.1 if (print)
99     System.out.print("FairSemaphore ");
100     new FairSemaphoreLoop().test(v, nthreads, fairIters);
101     Thread.sleep(10);
102    
103     if (print)
104     System.out.print("ReentrantWriteLock ");
105     new ReentrantWriteLockLoop().test(v, nthreads, iters);
106     Thread.sleep(10);
107    
108     if (print)
109     System.out.print("FairRWriteLock ");
110     new FairReentrantWriteLockLoop().test(v, nthreads, fairIters);
111     Thread.sleep(10);
112 jsr166 1.4
113 dl 1.1 if (print)
114     System.out.print("ReentrantReadWriteLock");
115     new ReentrantReadWriteLockLoop().test(v, nthreads, iters);
116     Thread.sleep(10);
117 jsr166 1.4
118 dl 1.1 if (print)
119     System.out.print("FairRReadWriteLock ");
120     new FairReentrantReadWriteLockLoop().test(v, nthreads, fairIters);
121     Thread.sleep(10);
122     }
123    
124 jsr166 1.8 abstract static class LockLoop implements Runnable {
125 dl 1.1 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 jsr166 1.4 if (checkValue != ~(v ^ 0xAAAAAAAA))
142 dl 1.1 ++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 jsr166 1.4 for (int i = 0; i < nthreads; ++i)
151 dl 1.1 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 jsr166 1.6 // double secs = (double) time / 1000000000.0;
159 dl 1.1 // 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 jsr166 1.4 barrier.await();
172 dl 1.1 result += loop(iters);
173     barrier.await();
174     }
175 jsr166 1.4 catch (Exception ie) {
176     return;
177 dl 1.1 }
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 jsr166 1.3 int x = 0;
187 dl 1.1 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 jsr166 1.3 int x = 0;
203 dl 1.1 while (n-- > 0) {
204 jsr166 1.7 synchronized (this) {
205 dl 1.1 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 jsr166 1.8 private final ReentrantLock lock = new ReentrantLock();
215 dl 1.1 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 jsr166 1.8 private final Mutex lock = new Mutex();
235 dl 1.1 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 jsr166 1.8 private final LongMutex lock = new LongMutex();
255 dl 1.1 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 jsr166 1.8 private final ReentrantLock lock = new ReentrantLock(true);
275 dl 1.1 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 jsr166 1.8 private final Lock lock = new ReentrantReadWriteLock().writeLock();
295 dl 1.1 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 jsr166 1.8 private final Semaphore sem = new Semaphore(1, false);
335 dl 1.1 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 jsr166 1.8 private final Semaphore sem = new Semaphore(1, true);
354 dl 1.1 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 jsr166 1.8 private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
374 dl 1.1 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     private static class FairReentrantReadWriteLockLoop extends LockLoop {
407 jsr166 1.8 private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
408 dl 1.1 final int loop(int n) {
409     final Lock rlock = lock.readLock();
410     final Lock wlock = lock.writeLock();
411     int sum = 0;
412     int x = 0;
413     while (n-- > 0) {
414     if ((n & 16) != 0) {
415     rlock.lock();
416     try {
417     x = LoopHelpers.compute1(getValue());
418     x = LoopHelpers.compute2(x);
419     }
420     finally {
421     rlock.unlock();
422     }
423     }
424     else {
425     wlock.lock();
426     try {
427     setValue(x);
428     }
429     finally {
430     wlock.unlock();
431     }
432     sum += LoopHelpers.compute2(x);
433     }
434     }
435     return sum;
436     }
437    
438     }
439     }