1 |
/* |
2 |
* @test |
3 |
* @summary basic safety and liveness of ReentrantLocks, and other locks based on them |
4 |
*/ |
5 |
|
6 |
import java.util.concurrent.*; |
7 |
import java.util.concurrent.locks.*; |
8 |
import java.util.*; |
9 |
|
10 |
public final class UncheckedLockLoops { |
11 |
static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); |
12 |
static boolean print = false; |
13 |
static boolean doBuiltin = true; |
14 |
|
15 |
public static void main(String[] args) throws Exception { |
16 |
int maxThreads = 100; |
17 |
int iters = 10000000; |
18 |
|
19 |
if (args.length > 0) |
20 |
maxThreads = Integer.parseInt(args[0]); |
21 |
|
22 |
rng.setSeed(3122688L); |
23 |
|
24 |
print = false; |
25 |
System.out.println("Warmup..."); |
26 |
oneTest(1, 100000); |
27 |
Thread.sleep(1000); |
28 |
oneTest(3, 10000); |
29 |
Thread.sleep(1000); |
30 |
oneTest(2, 10000); |
31 |
Thread.sleep(100); |
32 |
oneTest(1, 100000); |
33 |
Thread.sleep(100); |
34 |
oneTest(1, 100000); |
35 |
Thread.sleep(1000); |
36 |
print = true; |
37 |
|
38 |
System.out.println("Threads:" + 1); |
39 |
oneTest(1, iters / 1); |
40 |
Thread.sleep(100); |
41 |
|
42 |
for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) { |
43 |
System.out.println("Threads:" + i); |
44 |
oneTest(i, iters / i); |
45 |
Thread.sleep(100); |
46 |
} |
47 |
|
48 |
} |
49 |
|
50 |
static void oneTest(int nthreads, int iters) throws Exception { |
51 |
int fairIters = (nthreads <= 1)? iters : iters/20; |
52 |
int v = rng.next(); |
53 |
|
54 |
if (print) |
55 |
System.out.print("NoLock (1 thread) "); |
56 |
new NoLockLoop().test(v, 1, iters * nthreads); |
57 |
Thread.sleep(10); |
58 |
|
59 |
if (print) |
60 |
System.out.print("ReentrantLock "); |
61 |
new ReentrantLockLoop().test(v, nthreads, iters); |
62 |
Thread.sleep(10); |
63 |
|
64 |
if (false) { |
65 |
if (print) |
66 |
System.out.print("FairReentrantLock "); |
67 |
new FairReentrantLockLoop().test(v, nthreads, fairIters); |
68 |
Thread.sleep(10); |
69 |
} |
70 |
if (doBuiltin) { |
71 |
if (print) |
72 |
System.out.print("builtin lock "); |
73 |
new BuiltinLockLoop().test(v, nthreads, fairIters); |
74 |
Thread.sleep(10); |
75 |
} |
76 |
|
77 |
if (print) |
78 |
System.out.print("Mutex "); |
79 |
new MutexLoop().test(v, nthreads, iters); |
80 |
Thread.sleep(10); |
81 |
|
82 |
if (print) |
83 |
System.out.print("LongMutex "); |
84 |
new LongMutexLoop().test(v, nthreads, iters); |
85 |
Thread.sleep(10); |
86 |
|
87 |
if (print) |
88 |
System.out.print("Semaphore "); |
89 |
new SemaphoreLoop().test(v, nthreads, iters); |
90 |
Thread.sleep(10); |
91 |
|
92 |
if (print) |
93 |
System.out.print("FairSemaphore "); |
94 |
new FairSemaphoreLoop().test(v, nthreads, fairIters); |
95 |
Thread.sleep(10); |
96 |
|
97 |
if (print) |
98 |
System.out.print("ReentrantWriteLock "); |
99 |
new ReentrantWriteLockLoop().test(v, nthreads, iters); |
100 |
Thread.sleep(10); |
101 |
|
102 |
if (print) |
103 |
System.out.print("FairRWriteLock "); |
104 |
new FairReentrantWriteLockLoop().test(v, nthreads, fairIters); |
105 |
Thread.sleep(10); |
106 |
|
107 |
if (print) |
108 |
System.out.print("ReentrantReadWriteLock"); |
109 |
new ReentrantReadWriteLockLoop().test(v, nthreads, iters); |
110 |
Thread.sleep(10); |
111 |
|
112 |
if (print) |
113 |
System.out.print("FairRReadWriteLock "); |
114 |
new FairReentrantReadWriteLockLoop().test(v, nthreads, fairIters); |
115 |
Thread.sleep(10); |
116 |
|
117 |
} |
118 |
|
119 |
static abstract class LockLoop implements Runnable { |
120 |
int value; |
121 |
int checkValue; |
122 |
int iters; |
123 |
volatile int result; |
124 |
volatile int failures; |
125 |
final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
126 |
CyclicBarrier barrier; |
127 |
|
128 |
final int setValue(int v) { |
129 |
checkValue = v ^ 0x55555555; |
130 |
value = v; |
131 |
return v; |
132 |
} |
133 |
|
134 |
final int getValue() { |
135 |
int v = value; |
136 |
if (checkValue != ~(v ^ 0xAAAAAAAA)) |
137 |
++failures; |
138 |
return v; |
139 |
} |
140 |
|
141 |
final void test(int initialValue, int nthreads, int iters) throws Exception { |
142 |
setValue(initialValue); |
143 |
this.iters = iters; |
144 |
barrier = new CyclicBarrier(nthreads+1, timer); |
145 |
for (int i = 0; i < nthreads; ++i) |
146 |
new Thread(this).start(); |
147 |
barrier.await(); |
148 |
barrier.await(); |
149 |
long time = timer.getTime(); |
150 |
if (print) { |
151 |
long tpi = time / (iters * nthreads); |
152 |
System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update"); |
153 |
// double secs = (double)(time) / 1000000000.0; |
154 |
// System.out.print("\t " + secs + "s run time"); |
155 |
System.out.println(); |
156 |
} |
157 |
|
158 |
if (result == 0) // avoid overoptimization |
159 |
System.out.println("useless result: " + result); |
160 |
if (failures != 0) |
161 |
throw new Error("protection failure?"); |
162 |
} |
163 |
abstract int loop(int n); |
164 |
public final void run() { |
165 |
try { |
166 |
barrier.await(); |
167 |
result += loop(iters); |
168 |
barrier.await(); |
169 |
} |
170 |
catch (Exception ie) { |
171 |
return; |
172 |
} |
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 |
int x = 0;; |
182 |
while (n-- > 0) { |
183 |
int r1 = readBarrier; |
184 |
x = setValue(LoopHelpers.compute1(getValue())); |
185 |
int r2 = readBarrier; |
186 |
if (r1 == r2 && (x & 255) == 0) |
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 |
int x = 0;; |
198 |
while (n-- > 0) { |
199 |
synchronized(this) { |
200 |
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 |
final private ReentrantLock lock = new ReentrantLock(); |
210 |
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 |
final private Mutex lock = new Mutex(); |
230 |
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 |
final private LongMutex lock = new LongMutex(); |
250 |
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 |
final private ReentrantLock lock = new ReentrantLock(true); |
270 |
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 |
final private Lock lock = new ReentrantReadWriteLock().writeLock(); |
290 |
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 |
final private Semaphore sem = new Semaphore(1, false); |
330 |
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 |
final private Semaphore sem = new Semaphore(1, true); |
349 |
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 |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
369 |
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 |
|
402 |
private static class FairReentrantReadWriteLockLoop extends LockLoop { |
403 |
final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
404 |
final int loop(int n) { |
405 |
final Lock rlock = lock.readLock(); |
406 |
final Lock wlock = lock.writeLock(); |
407 |
int sum = 0; |
408 |
int x = 0; |
409 |
while (n-- > 0) { |
410 |
if ((n & 16) != 0) { |
411 |
rlock.lock(); |
412 |
try { |
413 |
x = LoopHelpers.compute1(getValue()); |
414 |
x = LoopHelpers.compute2(x); |
415 |
} |
416 |
finally { |
417 |
rlock.unlock(); |
418 |
} |
419 |
} |
420 |
else { |
421 |
wlock.lock(); |
422 |
try { |
423 |
setValue(x); |
424 |
} |
425 |
finally { |
426 |
wlock.unlock(); |
427 |
} |
428 |
sum += LoopHelpers.compute2(x); |
429 |
} |
430 |
} |
431 |
return sum; |
432 |
} |
433 |
|
434 |
} |
435 |
} |