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 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
|
31 |
|
*/ |
32 |
|
static class Mutex extends AbstractQueuedSynchronizer { |
33 |
|
public boolean isHeldExclusively() { return getState() == 1; } |
34 |
< |
|
34 |
> |
|
35 |
|
public boolean tryAcquire(int acquires) { |
36 |
< |
assertTrue(acquires == 1); |
36 |
> |
assertTrue(acquires == 1); |
37 |
|
return compareAndSetState(0, 1); |
38 |
|
} |
39 |
< |
|
39 |
> |
|
40 |
|
public boolean tryRelease(int releases) { |
41 |
|
if (getState() == 0) throw new IllegalMonitorStateException(); |
42 |
|
setState(0); |
43 |
|
return true; |
44 |
|
} |
45 |
< |
|
45 |
> |
|
46 |
|
public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); } |
47 |
|
|
48 |
|
} |
49 |
|
|
50 |
< |
|
50 |
> |
|
51 |
|
/** |
52 |
|
* A simple latch class, to test shared mode. |
53 |
|
*/ |
54 |
< |
static class BooleanLatch extends AbstractQueuedSynchronizer { |
54 |
> |
static class BooleanLatch extends AbstractQueuedSynchronizer { |
55 |
|
public boolean isSignalled() { return getState() != 0; } |
56 |
|
|
57 |
|
public int tryAcquireShared(int ignore) { |
58 |
|
return isSignalled()? 1 : -1; |
59 |
|
} |
60 |
< |
|
60 |
> |
|
61 |
|
public boolean tryReleaseShared(int ignore) { |
62 |
|
setState(1); |
63 |
|
return true; |
96 |
|
/** |
97 |
|
* isHeldExclusively is false upon construction |
98 |
|
*/ |
99 |
< |
public void testIsHeldExclusively() { |
99 |
> |
public void testIsHeldExclusively() { |
100 |
|
Mutex rl = new Mutex(); |
101 |
|
assertFalse(rl.isHeldExclusively()); |
102 |
|
} |
103 |
< |
|
103 |
> |
|
104 |
|
/** |
105 |
|
* acquiring released sync succeeds |
106 |
|
*/ |
107 |
< |
public void testAcquire() { |
107 |
> |
public void testAcquire() { |
108 |
|
Mutex rl = new Mutex(); |
109 |
|
rl.acquire(1); |
110 |
|
assertTrue(rl.isHeldExclusively()); |
115 |
|
/** |
116 |
|
* tryAcquire on an released sync succeeds |
117 |
|
*/ |
118 |
< |
public void testTryAcquire() { |
118 |
> |
public void testTryAcquire() { |
119 |
|
Mutex rl = new Mutex(); |
120 |
|
assertTrue(rl.tryAcquire(1)); |
121 |
|
assertTrue(rl.isHeldExclusively()); |
125 |
|
/** |
126 |
|
* hasQueuedThreads reports whether there are waiting threads |
127 |
|
*/ |
128 |
< |
public void testhasQueuedThreads() { |
128 |
> |
public void testhasQueuedThreads() { |
129 |
|
final Mutex sync = new Mutex(); |
130 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
131 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
149 |
|
} catch(Exception e){ |
150 |
|
unexpectedException(); |
151 |
|
} |
152 |
< |
} |
152 |
> |
} |
153 |
|
|
154 |
|
/** |
155 |
|
* isQueued(null) throws NPE |
156 |
|
*/ |
157 |
< |
public void testIsQueuedNPE() { |
157 |
> |
public void testIsQueuedNPE() { |
158 |
|
final Mutex sync = new Mutex(); |
159 |
|
try { |
160 |
|
sync.isQueued(null); |
166 |
|
/** |
167 |
|
* isQueued reports whether a thread is queued. |
168 |
|
*/ |
169 |
< |
public void testIsQueued() { |
169 |
> |
public void testIsQueued() { |
170 |
|
final Mutex sync = new Mutex(); |
171 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
172 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
195 |
|
} catch(Exception e){ |
196 |
|
unexpectedException(); |
197 |
|
} |
198 |
< |
} |
198 |
> |
} |
199 |
|
|
200 |
|
/** |
201 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
202 |
|
*/ |
203 |
< |
public void testGetFirstQueuedThread() { |
203 |
> |
public void testGetFirstQueuedThread() { |
204 |
|
final Mutex sync = new Mutex(); |
205 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
206 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
225 |
|
} catch(Exception e){ |
226 |
|
unexpectedException(); |
227 |
|
} |
228 |
< |
} |
228 |
> |
} |
229 |
|
|
230 |
|
|
231 |
|
/** |
232 |
|
* hasContended reports false if no thread has ever blocked, else true |
233 |
|
*/ |
234 |
< |
public void testHasContended() { |
234 |
> |
public void testHasContended() { |
235 |
|
final Mutex sync = new Mutex(); |
236 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
237 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
255 |
|
} catch(Exception e){ |
256 |
|
unexpectedException(); |
257 |
|
} |
258 |
< |
} |
258 |
> |
} |
259 |
|
|
260 |
|
/** |
261 |
|
* getQueuedThreads includes waiting threads |
262 |
|
*/ |
263 |
< |
public void testGetQueuedThreads() { |
263 |
> |
public void testGetQueuedThreads() { |
264 |
|
final Mutex sync = new Mutex(); |
265 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
266 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
287 |
|
} catch(Exception e){ |
288 |
|
unexpectedException(); |
289 |
|
} |
290 |
< |
} |
290 |
> |
} |
291 |
|
|
292 |
|
/** |
293 |
|
* getExclusiveQueuedThreads includes waiting threads |
294 |
|
*/ |
295 |
< |
public void testGetExclusiveQueuedThreads() { |
295 |
> |
public void testGetExclusiveQueuedThreads() { |
296 |
|
final Mutex sync = new Mutex(); |
297 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
298 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
319 |
|
} catch(Exception e){ |
320 |
|
unexpectedException(); |
321 |
|
} |
322 |
< |
} |
322 |
> |
} |
323 |
|
|
324 |
|
/** |
325 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
326 |
|
*/ |
327 |
< |
public void testGetSharedQueuedThreads() { |
327 |
> |
public void testGetSharedQueuedThreads() { |
328 |
|
final Mutex sync = new Mutex(); |
329 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
330 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
349 |
|
} catch(Exception e){ |
350 |
|
unexpectedException(); |
351 |
|
} |
352 |
< |
} |
352 |
> |
} |
353 |
|
|
354 |
|
/** |
355 |
|
* tryAcquireNanos is interruptible. |
356 |
|
*/ |
357 |
< |
public void testInterruptedException2() { |
357 |
> |
public void testInterruptedException2() { |
358 |
|
final Mutex sync = new Mutex(); |
359 |
|
sync.acquire(1); |
360 |
|
Thread t = new Thread(new Runnable() { |
377 |
|
/** |
378 |
|
* TryAcquire on exclusively held sync fails |
379 |
|
*/ |
380 |
< |
public void testTryAcquireWhenSynced() { |
380 |
> |
public void testTryAcquireWhenSynced() { |
381 |
|
final Mutex sync = new Mutex(); |
382 |
|
sync.acquire(1); |
383 |
|
Thread t = new Thread(new Runnable() { |
392 |
|
} catch(Exception e){ |
393 |
|
unexpectedException(); |
394 |
|
} |
395 |
< |
} |
395 |
> |
} |
396 |
|
|
397 |
|
/** |
398 |
|
* tryAcquireNanos on an exclusively held sync times out |
399 |
|
*/ |
400 |
< |
public void testAcquireNanos_Timeout() { |
400 |
> |
public void testAcquireNanos_Timeout() { |
401 |
|
final Mutex sync = new Mutex(); |
402 |
|
sync.acquire(1); |
403 |
|
Thread t = new Thread(new Runnable() { |
416 |
|
} catch(Exception e){ |
417 |
|
unexpectedException(); |
418 |
|
} |
419 |
< |
} |
420 |
< |
|
421 |
< |
|
419 |
> |
} |
420 |
> |
|
421 |
> |
|
422 |
|
/** |
423 |
|
* getState is true when acquired and false when not |
424 |
|
*/ |
428 |
|
assertTrue(sync.isHeldExclusively()); |
429 |
|
sync.release(1); |
430 |
|
assertFalse(sync.isHeldExclusively()); |
431 |
< |
Thread t = new Thread(new Runnable() { |
431 |
> |
Thread t = new Thread(new Runnable() { |
432 |
|
public void run() { |
433 |
|
sync.acquire(1); |
434 |
|
try { |
455 |
|
/** |
456 |
|
* acquireInterruptibly is interruptible. |
457 |
|
*/ |
458 |
< |
public void testAcquireInterruptibly1() { |
458 |
> |
public void testAcquireInterruptibly1() { |
459 |
|
final Mutex sync = new Mutex(); |
460 |
|
sync.acquire(1); |
461 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
469 |
|
} catch(Exception e){ |
470 |
|
unexpectedException(); |
471 |
|
} |
472 |
< |
} |
472 |
> |
} |
473 |
|
|
474 |
|
/** |
475 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
476 |
|
*/ |
477 |
|
public void testAcquireInterruptibly2() { |
478 |
< |
final Mutex sync = new Mutex(); |
478 |
> |
final Mutex sync = new Mutex(); |
479 |
|
try { |
480 |
|
sync.acquireInterruptibly(1); |
481 |
|
} catch(Exception e) { |
496 |
|
* owns is true for a condition created by sync else false |
497 |
|
*/ |
498 |
|
public void testOwns() { |
499 |
< |
final Mutex sync = new Mutex(); |
499 |
> |
final Mutex sync = new Mutex(); |
500 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
501 |
|
final Mutex sync2 = new Mutex(); |
502 |
|
assertTrue(sync.owns(c)); |
507 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
508 |
|
*/ |
509 |
|
public void testAwait_IllegalMonitor() { |
510 |
< |
final Mutex sync = new Mutex(); |
510 |
> |
final Mutex sync = new Mutex(); |
511 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
512 |
|
try { |
513 |
|
c.await(); |
524 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
525 |
|
*/ |
526 |
|
public void testSignal_IllegalMonitor() { |
527 |
< |
final Mutex sync = new Mutex(); |
527 |
> |
final Mutex sync = new Mutex(); |
528 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
529 |
|
try { |
530 |
|
c.signal(); |
541 |
|
* awaitNanos without a signal times out |
542 |
|
*/ |
543 |
|
public void testAwaitNanos_Timeout() { |
544 |
< |
final Mutex sync = new Mutex(); |
544 |
> |
final Mutex sync = new Mutex(); |
545 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
546 |
|
try { |
547 |
|
sync.acquire(1); |
558 |
|
* Timed await without a signal times out |
559 |
|
*/ |
560 |
|
public void testAwait_Timeout() { |
561 |
< |
final Mutex sync = new Mutex(); |
561 |
> |
final Mutex sync = new Mutex(); |
562 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
563 |
|
try { |
564 |
|
sync.acquire(1); |
574 |
|
* awaitUntil without a signal times out |
575 |
|
*/ |
576 |
|
public void testAwaitUntil_Timeout() { |
577 |
< |
final Mutex sync = new Mutex(); |
577 |
> |
final Mutex sync = new Mutex(); |
578 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
579 |
|
try { |
580 |
|
sync.acquire(1); |
591 |
|
* await returns when signalled |
592 |
|
*/ |
593 |
|
public void testAwait() { |
594 |
< |
final Mutex sync = new Mutex(); |
594 |
> |
final Mutex sync = new Mutex(); |
595 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
596 |
< |
Thread t = new Thread(new Runnable() { |
596 |
> |
Thread t = new Thread(new Runnable() { |
597 |
|
public void run() { |
598 |
|
try { |
599 |
|
sync.acquire(1); |
734 |
|
* getWaitingThreads throws IAE if not owned |
735 |
|
*/ |
736 |
|
public void testGetWaitingThreadsIAE() { |
737 |
< |
final Mutex sync = new Mutex(); |
737 |
> |
final Mutex sync = new Mutex(); |
738 |
|
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
739 |
< |
final Mutex sync2 = new Mutex(); |
739 |
> |
final Mutex sync2 = new Mutex(); |
740 |
|
try { |
741 |
|
sync2.getWaitingThreads(c); |
742 |
|
shouldThrow(); |
750 |
|
* getWaitingThreads throws IMSE if not synced |
751 |
|
*/ |
752 |
|
public void testGetWaitingThreadsIMSE() { |
753 |
< |
final Mutex sync = new Mutex(); |
753 |
> |
final Mutex sync = new Mutex(); |
754 |
|
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
755 |
|
try { |
756 |
|
sync.getWaitingThreads(c); |
767 |
|
* hasWaiters returns true when a thread is waiting, else false |
768 |
|
*/ |
769 |
|
public void testHasWaiters() { |
770 |
< |
final Mutex sync = new Mutex(); |
770 |
> |
final Mutex sync = new Mutex(); |
771 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
772 |
< |
Thread t = new Thread(new Runnable() { |
772 |
> |
Thread t = new Thread(new Runnable() { |
773 |
|
public void run() { |
774 |
|
try { |
775 |
|
sync.acquire(1); |
809 |
|
* getWaitQueueLength returns number of waiting threads |
810 |
|
*/ |
811 |
|
public void testGetWaitQueueLength() { |
812 |
< |
final Mutex sync = new Mutex(); |
812 |
> |
final Mutex sync = new Mutex(); |
813 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
814 |
< |
Thread t1 = new Thread(new Runnable() { |
814 |
> |
Thread t1 = new Thread(new Runnable() { |
815 |
|
public void run() { |
816 |
|
try { |
817 |
|
sync.acquire(1); |
826 |
|
} |
827 |
|
}); |
828 |
|
|
829 |
< |
Thread t2 = new Thread(new Runnable() { |
829 |
> |
Thread t2 = new Thread(new Runnable() { |
830 |
|
public void run() { |
831 |
|
try { |
832 |
|
sync.acquire(1); |
870 |
|
* getWaitingThreads returns only and all waiting threads |
871 |
|
*/ |
872 |
|
public void testGetWaitingThreads() { |
873 |
< |
final Mutex sync = new Mutex(); |
873 |
> |
final Mutex sync = new Mutex(); |
874 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
875 |
< |
Thread t1 = new Thread(new Runnable() { |
875 |
> |
Thread t1 = new Thread(new Runnable() { |
876 |
|
public void run() { |
877 |
|
try { |
878 |
|
sync.acquire(1); |
886 |
|
} |
887 |
|
}); |
888 |
|
|
889 |
< |
Thread t2 = new Thread(new Runnable() { |
889 |
> |
Thread t2 = new Thread(new Runnable() { |
890 |
|
public void run() { |
891 |
|
try { |
892 |
|
sync.acquire(1); |
935 |
|
* awaitUninterruptibly doesn't abort on interrupt |
936 |
|
*/ |
937 |
|
public void testAwaitUninterruptibly() { |
938 |
< |
final Mutex sync = new Mutex(); |
938 |
> |
final Mutex sync = new Mutex(); |
939 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
940 |
< |
Thread t = new Thread(new Runnable() { |
940 |
> |
Thread t = new Thread(new Runnable() { |
941 |
|
public void run() { |
942 |
|
sync.acquire(1); |
943 |
|
c.awaitUninterruptibly(); |
964 |
|
* await is interruptible |
965 |
|
*/ |
966 |
|
public void testAwait_Interrupt() { |
967 |
< |
final Mutex sync = new Mutex(); |
967 |
> |
final Mutex sync = new Mutex(); |
968 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
969 |
< |
Thread t = new Thread(new Runnable() { |
969 |
> |
Thread t = new Thread(new Runnable() { |
970 |
|
public void run() { |
971 |
|
try { |
972 |
|
sync.acquire(1); |
995 |
|
* awaitNanos is interruptible |
996 |
|
*/ |
997 |
|
public void testAwaitNanos_Interrupt() { |
998 |
< |
final Mutex sync = new Mutex(); |
998 |
> |
final Mutex sync = new Mutex(); |
999 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
1000 |
< |
Thread t = new Thread(new Runnable() { |
1000 |
> |
Thread t = new Thread(new Runnable() { |
1001 |
|
public void run() { |
1002 |
|
try { |
1003 |
|
sync.acquire(1); |
1026 |
|
* awaitUntil is interruptible |
1027 |
|
*/ |
1028 |
|
public void testAwaitUntil_Interrupt() { |
1029 |
< |
final Mutex sync = new Mutex(); |
1029 |
> |
final Mutex sync = new Mutex(); |
1030 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
1031 |
< |
Thread t = new Thread(new Runnable() { |
1031 |
> |
Thread t = new Thread(new Runnable() { |
1032 |
|
public void run() { |
1033 |
|
try { |
1034 |
|
sync.acquire(1); |
1058 |
|
* signalAll wakes up all threads |
1059 |
|
*/ |
1060 |
|
public void testSignalAll() { |
1061 |
< |
final Mutex sync = new Mutex(); |
1061 |
> |
final Mutex sync = new Mutex(); |
1062 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
1063 |
< |
Thread t1 = new Thread(new Runnable() { |
1063 |
> |
Thread t1 = new Thread(new Runnable() { |
1064 |
|
public void run() { |
1065 |
|
try { |
1066 |
|
sync.acquire(1); |
1073 |
|
} |
1074 |
|
}); |
1075 |
|
|
1076 |
< |
Thread t2 = new Thread(new Runnable() { |
1076 |
> |
Thread t2 = new Thread(new Runnable() { |
1077 |
|
public void run() { |
1078 |
|
try { |
1079 |
|
sync.acquire(1); |
1191 |
|
unexpectedException(); |
1192 |
|
} |
1193 |
|
} |
1194 |
< |
|
1194 |
> |
|
1195 |
|
|
1196 |
|
/** |
1197 |
|
* acquireSharedTimed returns after release |
1222 |
|
unexpectedException(); |
1223 |
|
} |
1224 |
|
} |
1225 |
< |
|
1225 |
> |
|
1226 |
|
/** |
1227 |
|
* acquireSharedInterruptibly throws IE if interrupted before released |
1228 |
|
*/ |
1257 |
|
try { |
1258 |
|
threadAssertFalse(l.isSignalled()); |
1259 |
|
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
1260 |
< |
threadShouldThrow(); |
1260 |
> |
threadShouldThrow(); |
1261 |
|
} catch(InterruptedException success){} |
1262 |
|
} |
1263 |
|
}); |
1297 |
|
} |
1298 |
|
} |
1299 |
|
|
1300 |
< |
|
1300 |
> |
|
1301 |
|
} |