79 private final int nthreads;
80 private volatile Throwable fail = null;
81 ReentrantLockLoop(int nthreads) {
82 this.nthreads = nthreads;
83 barrier = new CyclicBarrier(nthreads+1, timer);
84 }
85
86 final void test() throws Exception {
87 for (int i = 0; i < nthreads; ++i) {
88 lock.lock();
89 pool.execute(this);
90 lock.unlock();
91 }
92 barrier.await();
93 Thread.sleep(ThreadLocalRandom.current().nextInt(5));
94 while (!lock.tryLock()); // Jam lock
95 // lock.lock();
96 barrier.await();
97 if (print) {
98 long time = timer.getTime();
99 double secs = (double)(time) / 1000000000.0;
100 System.out.println("\t " + secs + "s run time");
101 }
102
103 int r = result;
104 if (r == 0) // avoid overoptimization
105 System.out.println("useless result: " + r);
106 if (fail != null) throw new RuntimeException(fail);
107 }
108
109 public final void run() {
110 try {
111 barrier.await();
112 int sum = v;
113 int x = 17;
114 final ReentrantLock lock = this.lock;
115 while (lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
116 try {
117 v = x = LoopHelpers.compute1(v);
118 }
119 finally {
|
79 private final int nthreads;
80 private volatile Throwable fail = null;
81 ReentrantLockLoop(int nthreads) {
82 this.nthreads = nthreads;
83 barrier = new CyclicBarrier(nthreads+1, timer);
84 }
85
86 final void test() throws Exception {
87 for (int i = 0; i < nthreads; ++i) {
88 lock.lock();
89 pool.execute(this);
90 lock.unlock();
91 }
92 barrier.await();
93 Thread.sleep(ThreadLocalRandom.current().nextInt(5));
94 while (!lock.tryLock()); // Jam lock
95 // lock.lock();
96 barrier.await();
97 if (print) {
98 long time = timer.getTime();
99 double secs = (double)time / 1000000000.0;
100 System.out.println("\t " + secs + "s run time");
101 }
102
103 int r = result;
104 if (r == 0) // avoid overoptimization
105 System.out.println("useless result: " + r);
106 if (fail != null) throw new RuntimeException(fail);
107 }
108
109 public final void run() {
110 try {
111 barrier.await();
112 int sum = v;
113 int x = 17;
114 final ReentrantLock lock = this.lock;
115 while (lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
116 try {
117 v = x = LoopHelpers.compute1(v);
118 }
119 finally {
|