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 |
|
|
32 |
|
static class Mutex extends AbstractQueuedLongSynchronizer { |
33 |
|
// Use value > 32 bits for locked state |
34 |
|
static final long LOCKED = 1 << 48; |
35 |
< |
public boolean isHeldExclusively() { |
36 |
< |
return getState() == LOCKED; |
35 |
> |
public boolean isHeldExclusively() { |
36 |
> |
return getState() == LOCKED; |
37 |
|
} |
38 |
< |
|
38 |
> |
|
39 |
|
public boolean tryAcquire(long acquires) { |
40 |
|
return compareAndSetState(0, LOCKED); |
41 |
|
} |
42 |
< |
|
42 |
> |
|
43 |
|
public boolean tryRelease(long releases) { |
44 |
|
if (getState() == 0) throw new IllegalMonitorStateException(); |
45 |
|
setState(0); |
46 |
|
return true; |
47 |
|
} |
48 |
< |
|
48 |
> |
|
49 |
|
public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { return new AbstractQueuedLongSynchronizer.ConditionObject(); } |
50 |
|
|
51 |
|
} |
52 |
|
|
53 |
< |
|
53 |
> |
|
54 |
|
/** |
55 |
|
* A simple latch class, to test shared mode. |
56 |
|
*/ |
57 |
< |
static class BooleanLatch extends AbstractQueuedLongSynchronizer { |
57 |
> |
static class BooleanLatch extends AbstractQueuedLongSynchronizer { |
58 |
|
public boolean isSignalled() { return getState() != 0; } |
59 |
|
|
60 |
|
public long tryAcquireShared(long ignore) { |
61 |
|
return isSignalled()? 1 : -1; |
62 |
|
} |
63 |
< |
|
63 |
> |
|
64 |
|
public boolean tryReleaseShared(long ignore) { |
65 |
|
setState(1 << 62); |
66 |
|
return true; |
99 |
|
/** |
100 |
|
* isHeldExclusively is false upon construction |
101 |
|
*/ |
102 |
< |
public void testIsHeldExclusively() { |
102 |
> |
public void testIsHeldExclusively() { |
103 |
|
Mutex rl = new Mutex(); |
104 |
|
assertFalse(rl.isHeldExclusively()); |
105 |
|
} |
106 |
< |
|
106 |
> |
|
107 |
|
/** |
108 |
|
* acquiring released sync succeeds |
109 |
|
*/ |
110 |
< |
public void testAcquire() { |
110 |
> |
public void testAcquire() { |
111 |
|
Mutex rl = new Mutex(); |
112 |
|
rl.acquire(1); |
113 |
|
assertTrue(rl.isHeldExclusively()); |
118 |
|
/** |
119 |
|
* tryAcquire on an released sync succeeds |
120 |
|
*/ |
121 |
< |
public void testTryAcquire() { |
121 |
> |
public void testTryAcquire() { |
122 |
|
Mutex rl = new Mutex(); |
123 |
|
assertTrue(rl.tryAcquire(1)); |
124 |
|
assertTrue(rl.isHeldExclusively()); |
128 |
|
/** |
129 |
|
* hasQueuedThreads reports whether there are waiting threads |
130 |
|
*/ |
131 |
< |
public void testhasQueuedThreads() { |
131 |
> |
public void testhasQueuedThreads() { |
132 |
|
final Mutex sync = new Mutex(); |
133 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
134 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
152 |
|
} catch(Exception e){ |
153 |
|
unexpectedException(); |
154 |
|
} |
155 |
< |
} |
155 |
> |
} |
156 |
|
|
157 |
|
/** |
158 |
|
* isQueued(null) throws NPE |
159 |
|
*/ |
160 |
< |
public void testIsQueuedNPE() { |
160 |
> |
public void testIsQueuedNPE() { |
161 |
|
final Mutex sync = new Mutex(); |
162 |
|
try { |
163 |
|
sync.isQueued(null); |
169 |
|
/** |
170 |
|
* isQueued reports whether a thread is queued. |
171 |
|
*/ |
172 |
< |
public void testIsQueued() { |
172 |
> |
public void testIsQueued() { |
173 |
|
final Mutex sync = new Mutex(); |
174 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
175 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
198 |
|
} catch(Exception e){ |
199 |
|
unexpectedException(); |
200 |
|
} |
201 |
< |
} |
201 |
> |
} |
202 |
|
|
203 |
|
/** |
204 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
205 |
|
*/ |
206 |
< |
public void testGetFirstQueuedThread() { |
206 |
> |
public void testGetFirstQueuedThread() { |
207 |
|
final Mutex sync = new Mutex(); |
208 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
209 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
228 |
|
} catch(Exception e){ |
229 |
|
unexpectedException(); |
230 |
|
} |
231 |
< |
} |
231 |
> |
} |
232 |
|
|
233 |
|
|
234 |
|
/** |
235 |
|
* hasContended reports false if no thread has ever blocked, else true |
236 |
|
*/ |
237 |
< |
public void testHasContended() { |
237 |
> |
public void testHasContended() { |
238 |
|
final Mutex sync = new Mutex(); |
239 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
240 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
258 |
|
} catch(Exception e){ |
259 |
|
unexpectedException(); |
260 |
|
} |
261 |
< |
} |
261 |
> |
} |
262 |
|
|
263 |
|
/** |
264 |
|
* getQueuedThreads includes waiting threads |
265 |
|
*/ |
266 |
< |
public void testGetQueuedThreads() { |
266 |
> |
public void testGetQueuedThreads() { |
267 |
|
final Mutex sync = new Mutex(); |
268 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
269 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
290 |
|
} catch(Exception e){ |
291 |
|
unexpectedException(); |
292 |
|
} |
293 |
< |
} |
293 |
> |
} |
294 |
|
|
295 |
|
/** |
296 |
|
* getExclusiveQueuedThreads includes waiting threads |
297 |
|
*/ |
298 |
< |
public void testGetExclusiveQueuedThreads() { |
298 |
> |
public void testGetExclusiveQueuedThreads() { |
299 |
|
final Mutex sync = new Mutex(); |
300 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
301 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
322 |
|
} catch(Exception e){ |
323 |
|
unexpectedException(); |
324 |
|
} |
325 |
< |
} |
325 |
> |
} |
326 |
|
|
327 |
|
/** |
328 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
329 |
|
*/ |
330 |
< |
public void testGetSharedQueuedThreads() { |
330 |
> |
public void testGetSharedQueuedThreads() { |
331 |
|
final Mutex sync = new Mutex(); |
332 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
333 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
352 |
|
} catch(Exception e){ |
353 |
|
unexpectedException(); |
354 |
|
} |
355 |
< |
} |
355 |
> |
} |
356 |
|
|
357 |
|
/** |
358 |
|
* tryAcquireNanos is interruptible. |
359 |
|
*/ |
360 |
< |
public void testInterruptedException2() { |
360 |
> |
public void testInterruptedException2() { |
361 |
|
final Mutex sync = new Mutex(); |
362 |
|
sync.acquire(1); |
363 |
|
Thread t = new Thread(new Runnable() { |
380 |
|
/** |
381 |
|
* TryAcquire on exclusively held sync fails |
382 |
|
*/ |
383 |
< |
public void testTryAcquireWhenSynced() { |
383 |
> |
public void testTryAcquireWhenSynced() { |
384 |
|
final Mutex sync = new Mutex(); |
385 |
|
sync.acquire(1); |
386 |
|
Thread t = new Thread(new Runnable() { |
395 |
|
} catch(Exception e){ |
396 |
|
unexpectedException(); |
397 |
|
} |
398 |
< |
} |
398 |
> |
} |
399 |
|
|
400 |
|
/** |
401 |
|
* tryAcquireNanos on an exclusively held sync times out |
402 |
|
*/ |
403 |
< |
public void testAcquireNanos_Timeout() { |
403 |
> |
public void testAcquireNanos_Timeout() { |
404 |
|
final Mutex sync = new Mutex(); |
405 |
|
sync.acquire(1); |
406 |
|
Thread t = new Thread(new Runnable() { |
419 |
|
} catch(Exception e){ |
420 |
|
unexpectedException(); |
421 |
|
} |
422 |
< |
} |
423 |
< |
|
424 |
< |
|
422 |
> |
} |
423 |
> |
|
424 |
> |
|
425 |
|
/** |
426 |
|
* getState is true when acquired and false when not |
427 |
|
*/ |
431 |
|
assertTrue(sync.isHeldExclusively()); |
432 |
|
sync.release(1); |
433 |
|
assertFalse(sync.isHeldExclusively()); |
434 |
< |
Thread t = new Thread(new Runnable() { |
434 |
> |
Thread t = new Thread(new Runnable() { |
435 |
|
public void run() { |
436 |
|
sync.acquire(1); |
437 |
|
try { |
458 |
|
/** |
459 |
|
* acquireInterruptibly is interruptible. |
460 |
|
*/ |
461 |
< |
public void testAcquireInterruptibly1() { |
461 |
> |
public void testAcquireInterruptibly1() { |
462 |
|
final Mutex sync = new Mutex(); |
463 |
|
sync.acquire(1); |
464 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
472 |
|
} catch(Exception e){ |
473 |
|
unexpectedException(); |
474 |
|
} |
475 |
< |
} |
475 |
> |
} |
476 |
|
|
477 |
|
/** |
478 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
479 |
|
*/ |
480 |
|
public void testAcquireInterruptibly2() { |
481 |
< |
final Mutex sync = new Mutex(); |
481 |
> |
final Mutex sync = new Mutex(); |
482 |
|
try { |
483 |
|
sync.acquireInterruptibly(1); |
484 |
|
} catch(Exception e) { |
499 |
|
* owns is true for a condition created by sync else false |
500 |
|
*/ |
501 |
|
public void testOwns() { |
502 |
< |
final Mutex sync = new Mutex(); |
502 |
> |
final Mutex sync = new Mutex(); |
503 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
504 |
|
final Mutex sync2 = new Mutex(); |
505 |
|
assertTrue(sync.owns(c)); |
510 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
511 |
|
*/ |
512 |
|
public void testAwait_IllegalMonitor() { |
513 |
< |
final Mutex sync = new Mutex(); |
513 |
> |
final Mutex sync = new Mutex(); |
514 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
515 |
|
try { |
516 |
|
c.await(); |
527 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
528 |
|
*/ |
529 |
|
public void testSignal_IllegalMonitor() { |
530 |
< |
final Mutex sync = new Mutex(); |
530 |
> |
final Mutex sync = new Mutex(); |
531 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
532 |
|
try { |
533 |
|
c.signal(); |
544 |
|
* awaitNanos without a signal times out |
545 |
|
*/ |
546 |
|
public void testAwaitNanos_Timeout() { |
547 |
< |
final Mutex sync = new Mutex(); |
547 |
> |
final Mutex sync = new Mutex(); |
548 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
549 |
|
try { |
550 |
|
sync.acquire(1); |
561 |
|
* Timed await without a signal times out |
562 |
|
*/ |
563 |
|
public void testAwait_Timeout() { |
564 |
< |
final Mutex sync = new Mutex(); |
564 |
> |
final Mutex sync = new Mutex(); |
565 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
566 |
|
try { |
567 |
|
sync.acquire(1); |
577 |
|
* awaitUntil without a signal times out |
578 |
|
*/ |
579 |
|
public void testAwaitUntil_Timeout() { |
580 |
< |
final Mutex sync = new Mutex(); |
580 |
> |
final Mutex sync = new Mutex(); |
581 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
582 |
|
try { |
583 |
|
sync.acquire(1); |
594 |
|
* await returns when signalled |
595 |
|
*/ |
596 |
|
public void testAwait() { |
597 |
< |
final Mutex sync = new Mutex(); |
597 |
> |
final Mutex sync = new Mutex(); |
598 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
599 |
< |
Thread t = new Thread(new Runnable() { |
599 |
> |
Thread t = new Thread(new Runnable() { |
600 |
|
public void run() { |
601 |
|
try { |
602 |
|
sync.acquire(1); |
737 |
|
* getWaitingThreads throws IAE if not owned |
738 |
|
*/ |
739 |
|
public void testGetWaitingThreadsIAE() { |
740 |
< |
final Mutex sync = new Mutex(); |
740 |
> |
final Mutex sync = new Mutex(); |
741 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
742 |
< |
final Mutex sync2 = new Mutex(); |
742 |
> |
final Mutex sync2 = new Mutex(); |
743 |
|
try { |
744 |
|
sync2.getWaitingThreads(c); |
745 |
|
shouldThrow(); |
753 |
|
* getWaitingThreads throws IMSE if not synced |
754 |
|
*/ |
755 |
|
public void testGetWaitingThreadsIMSE() { |
756 |
< |
final Mutex sync = new Mutex(); |
756 |
> |
final Mutex sync = new Mutex(); |
757 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
758 |
|
try { |
759 |
|
sync.getWaitingThreads(c); |
770 |
|
* hasWaiters returns true when a thread is waiting, else false |
771 |
|
*/ |
772 |
|
public void testHasWaiters() { |
773 |
< |
final Mutex sync = new Mutex(); |
773 |
> |
final Mutex sync = new Mutex(); |
774 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
775 |
< |
Thread t = new Thread(new Runnable() { |
775 |
> |
Thread t = new Thread(new Runnable() { |
776 |
|
public void run() { |
777 |
|
try { |
778 |
|
sync.acquire(1); |
812 |
|
* getWaitQueueLength returns number of waiting threads |
813 |
|
*/ |
814 |
|
public void testGetWaitQueueLength() { |
815 |
< |
final Mutex sync = new Mutex(); |
815 |
> |
final Mutex sync = new Mutex(); |
816 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
817 |
< |
Thread t1 = new Thread(new Runnable() { |
817 |
> |
Thread t1 = new Thread(new Runnable() { |
818 |
|
public void run() { |
819 |
|
try { |
820 |
|
sync.acquire(1); |
829 |
|
} |
830 |
|
}); |
831 |
|
|
832 |
< |
Thread t2 = new Thread(new Runnable() { |
832 |
> |
Thread t2 = new Thread(new Runnable() { |
833 |
|
public void run() { |
834 |
|
try { |
835 |
|
sync.acquire(1); |
873 |
|
* getWaitingThreads returns only and all waiting threads |
874 |
|
*/ |
875 |
|
public void testGetWaitingThreads() { |
876 |
< |
final Mutex sync = new Mutex(); |
876 |
> |
final Mutex sync = new Mutex(); |
877 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
878 |
< |
Thread t1 = new Thread(new Runnable() { |
878 |
> |
Thread t1 = new Thread(new Runnable() { |
879 |
|
public void run() { |
880 |
|
try { |
881 |
|
sync.acquire(1); |
889 |
|
} |
890 |
|
}); |
891 |
|
|
892 |
< |
Thread t2 = new Thread(new Runnable() { |
892 |
> |
Thread t2 = new Thread(new Runnable() { |
893 |
|
public void run() { |
894 |
|
try { |
895 |
|
sync.acquire(1); |
938 |
|
* awaitUninterruptibly doesn't abort on interrupt |
939 |
|
*/ |
940 |
|
public void testAwaitUninterruptibly() { |
941 |
< |
final Mutex sync = new Mutex(); |
941 |
> |
final Mutex sync = new Mutex(); |
942 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
943 |
< |
Thread t = new Thread(new Runnable() { |
943 |
> |
Thread t = new Thread(new Runnable() { |
944 |
|
public void run() { |
945 |
|
sync.acquire(1); |
946 |
|
c.awaitUninterruptibly(); |
967 |
|
* await is interruptible |
968 |
|
*/ |
969 |
|
public void testAwait_Interrupt() { |
970 |
< |
final Mutex sync = new Mutex(); |
970 |
> |
final Mutex sync = new Mutex(); |
971 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
972 |
< |
Thread t = new Thread(new Runnable() { |
972 |
> |
Thread t = new Thread(new Runnable() { |
973 |
|
public void run() { |
974 |
|
try { |
975 |
|
sync.acquire(1); |
998 |
|
* awaitNanos is interruptible |
999 |
|
*/ |
1000 |
|
public void testAwaitNanos_Interrupt() { |
1001 |
< |
final Mutex sync = new Mutex(); |
1001 |
> |
final Mutex sync = new Mutex(); |
1002 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
1003 |
< |
Thread t = new Thread(new Runnable() { |
1003 |
> |
Thread t = new Thread(new Runnable() { |
1004 |
|
public void run() { |
1005 |
|
try { |
1006 |
|
sync.acquire(1); |
1029 |
|
* awaitUntil is interruptible |
1030 |
|
*/ |
1031 |
|
public void testAwaitUntil_Interrupt() { |
1032 |
< |
final Mutex sync = new Mutex(); |
1032 |
> |
final Mutex sync = new Mutex(); |
1033 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
1034 |
< |
Thread t = new Thread(new Runnable() { |
1034 |
> |
Thread t = new Thread(new Runnable() { |
1035 |
|
public void run() { |
1036 |
|
try { |
1037 |
|
sync.acquire(1); |
1061 |
|
* signalAll wakes up all threads |
1062 |
|
*/ |
1063 |
|
public void testSignalAll() { |
1064 |
< |
final Mutex sync = new Mutex(); |
1064 |
> |
final Mutex sync = new Mutex(); |
1065 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
1066 |
< |
Thread t1 = new Thread(new Runnable() { |
1066 |
> |
Thread t1 = new Thread(new Runnable() { |
1067 |
|
public void run() { |
1068 |
|
try { |
1069 |
|
sync.acquire(1); |
1076 |
|
} |
1077 |
|
}); |
1078 |
|
|
1079 |
< |
Thread t2 = new Thread(new Runnable() { |
1079 |
> |
Thread t2 = new Thread(new Runnable() { |
1080 |
|
public void run() { |
1081 |
|
try { |
1082 |
|
sync.acquire(1); |
1194 |
|
unexpectedException(); |
1195 |
|
} |
1196 |
|
} |
1197 |
< |
|
1197 |
> |
|
1198 |
|
|
1199 |
|
/** |
1200 |
|
* acquireSharedTimed returns after release |
1225 |
|
unexpectedException(); |
1226 |
|
} |
1227 |
|
} |
1228 |
< |
|
1228 |
> |
|
1229 |
|
/** |
1230 |
|
* acquireSharedInterruptibly throws IE if interrupted before released |
1231 |
|
*/ |
1260 |
|
try { |
1261 |
|
threadAssertFalse(l.isSignalled()); |
1262 |
|
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
1263 |
< |
threadShouldThrow(); |
1263 |
> |
threadShouldThrow(); |
1264 |
|
} catch(InterruptedException success){} |
1265 |
|
} |
1266 |
|
}); |
1300 |
|
} |
1301 |
|
} |
1302 |
|
|
1303 |
< |
|
1303 |
> |
|
1304 |
|
} |