ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SimpleSpinLockLoops.java
Revision: 1.2
Committed: Fri Jun 10 15:45:19 2005 UTC (18 years, 11 months ago) by dl
Branch: MAIN
Changes since 1.1: +2 -2 lines
Log Message:
Add CAS timing test; use cheaper RNGs in others

File Contents

# Content
1 /*
2 * @test
3 * @synopsis multiple threads using a spinlock
4 */
5 /*
6 * Written by Doug Lea with assistance from members of JCP JSR-166
7 * Expert Group and released to the public domain. Use, modify, and
8 * redistribute this code in any way without acknowledgement.
9 */
10
11 import java.util.concurrent.*;
12 import java.util.concurrent.locks.*;
13 import java.util.concurrent.atomic.*;
14 import java.util.*;
15
16 public final class SimpleSpinLockLoops {
17 static final ExecutorService pool = Executors.newCachedThreadPool();
18 static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
19 static boolean print = false;
20 static int iters = 2000000;
21
22 public static void main(String[] args) throws Exception {
23 int maxThreads = 100;
24 if (args.length > 0)
25 maxThreads = Integer.parseInt(args[0]);
26
27 new LockLoop(1).test();
28 new LockLoop(1).test();
29 print = true;
30
31 int k = 1;
32 for (int i = 1; i <= maxThreads;) {
33 System.out.print("Threads: " + i);
34 new LockLoop(i).test();
35 Thread.sleep(100);
36 if (i == k) {
37 k = i << 1;
38 i = i + (i >>> 1);
39 }
40 else
41 i = k;
42 }
43 pool.shutdown();
44 }
45
46 static final class LockLoop implements Runnable {
47 private int v = rng.next();
48 private volatile int result = 17;
49 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
50 private final CyclicBarrier barrier;
51 private final int nthreads;
52 private volatile int readBarrier;
53 private final AtomicInteger spinlock = new AtomicInteger();
54 LockLoop(int nthreads) {
55 this.nthreads = nthreads;
56 barrier = new CyclicBarrier(nthreads+1, timer);
57 }
58
59 final void test() throws Exception {
60 for (int i = 0; i < nthreads; ++i)
61 pool.execute(this);
62 barrier.await();
63 barrier.await();
64 if (print) {
65 long time = timer.getTime();
66 long tpi = time / ((long)iters * nthreads);
67 System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per lock");
68 double secs = (double)(time) / 1000000000.0;
69 System.out.println("\t " + secs + "s run time");
70 }
71
72 int r = result;
73 if (r == 0) // avoid overoptimization
74 System.out.println("useless result: " + r);
75 }
76
77 public final void run() {
78 final AtomicInteger lock = this.spinlock;
79 try {
80 barrier.await();
81 int sum = v + 1;
82 int x = 0;
83 int n = iters;
84 while (n-- > 0) {
85 while (!lock.compareAndSet(0, 1)) ;
86 int k = (sum & 3);
87 if (k > 0) {
88 x = v;
89 while (k-- > 0)
90 x = LoopHelpers.compute6(x);
91 v = x;
92 }
93 else x = sum + 1;
94 lock.set(0);
95 if ((x += readBarrier) == 0)
96 ++readBarrier;
97 for (int l = x & 1; l > 0; --l)
98 sum += LoopHelpers.compute6(sum);
99 }
100 barrier.await();
101 result += sum;
102 }
103 catch (Exception ie) {
104 return;
105 }
106 }
107 }
108
109 }
110