Home | History | Annotate | Download | only in jsr166

Lines Matching defs:lock

40         final ReentrantLock lock;
41 InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; }
43 lock.lockInterruptibly();
52 final ReentrantLock lock;
53 InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; }
55 lock.lockInterruptibly();
77 * Releases write lock, checking that it had a hold count of 1.
79 void releaseLock(PublicReentrantLock lock) {
80 assertLockedByMoi(lock);
81 lock.unlock();
82 assertFalse(lock.isHeldByCurrentThread());
83 assertNotLocked(lock);
87 * Spin-waits until lock.hasQueuedThread(t) becomes true.
89 void waitForQueuedThread(PublicReentrantLock lock, Thread t) {
91 while (!lock.hasQueuedThread(t)) {
97 assertNotSame(t, lock.getOwner());
101 * Checks that lock is not locked.
103 void assertNotLocked(PublicReentrantLock lock) {
104 assertFalse(lock.isLocked());
105 assertFalse(lock.isHeldByCurrentThread());
106 assertNull(lock.getOwner());
107 assertEquals(0, lock.getHoldCount());
111 * Checks that lock is locked by the given thread.
113 void assertLockedBy(PublicReentrantLock lock, Thread t) {
114 assertTrue(lock.isLocked());
115 assertSame(t, lock.getOwner());
117 lock.isHeldByCurrentThread());
119 lock.getHoldCount() > 0);
123 * Checks that lock is locked by the current thread.
125 void assertLockedByMoi(PublicReentrantLock lock) {
126 assertLockedBy(lock, Thread.currentThread());
132 void assertHasNoWaiters(PublicReentrantLock lock, Condition c) {
133 assertHasWaiters(lock, c, new Thread[] {});
139 void assertHasWaiters(PublicReentrantLock lock, Condition c,
141 lock.lock();
142 assertEquals(threads.length > 0, lock.hasWaiters(c));
143 assertEquals(threads.length, lock.getWaitQueueLength(c));
144 assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
145 assertEquals(threads.length, lock.getWaitingThreads(c).size());
146 assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
148 lock.unlock();
184 PublicReentrantLock lock;
186 lock = new PublicReentrantLock();
187 assertFalse(lock.isFair());
188 assertNotLocked(lock);
190 lock = new PublicReentrantLock(true);
191 assertTrue(lock.isFair());
192 assertNotLocked(lock);
194 lock = new PublicReentrantLock(false);
195 assertFalse(lock.isFair());
196 assertNotLocked(lock);
200 * locking an unlocked lock succeeds
205 PublicReentrantLock lock = new PublicReentrantLock(fair);
206 lock.lock();
207 assertLockedByMoi(lock);
208 releaseLock(lock);
212 * Unlocking an unlocked lock throws IllegalMonitorStateException
217 ReentrantLock lock = new ReentrantLock(fair);
219 lock.unlock();
225 * tryLock on an unlocked lock succeeds
230 PublicReentrantLock lock = new PublicReentrantLock(fair);
231 assertTrue(lock.tryLock());
232 assertLockedByMoi(lock);
233 assertTrue(lock.tryLock());
234 assertLockedByMoi(lock);
235 lock.unlock();
236 releaseLock(lock);
245 final PublicReentrantLock lock = new PublicReentrantLock(fair);
246 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
247 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
248 assertFalse(lock.hasQueuedThreads());
249 lock.lock();
250 assertFalse(lock.hasQueuedThreads());
252 waitForQueuedThread(lock, t1);
253 assertTrue(lock.hasQueuedThreads());
255 waitForQueuedThread(lock, t2);
256 assertTrue(lock.hasQueuedThreads());
259 assertTrue(lock.hasQueuedThreads());
260 lock.unlock();
262 assertFalse(lock.hasQueuedThreads());
271 final PublicReentrantLock lock = new PublicReentrantLock(fair);
272 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
273 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
274 assertEquals(0, lock.getQueueLength());
275 lock.lock();
277 waitForQueuedThread(lock, t1);
278 assertEquals(1, lock.getQueueLength());
280 waitForQueuedThread(lock, t2);
281 assertEquals(2, lock.getQueueLength());
284 assertEquals(1, lock.getQueueLength());
285 lock.unlock();
287 assertEquals(0, lock.getQueueLength());
296 final ReentrantLock lock = new ReentrantLock(fair);
298 lock.hasQueuedThread(null);
309 final PublicReentrantLock lock = new PublicReentrantLock(fair);
310 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
311 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
312 assertFalse(lock.hasQueuedThread(t1));
313 assertFalse(lock.hasQueuedThread(t2));
314 lock.lock();
316 waitForQueuedThread(lock, t1);
317 assertTrue(lock.hasQueuedThread(t1));
318 assertFalse(lock.hasQueuedThread(t2));
320 waitForQueuedThread(lock, t2);
321 assertTrue(lock.hasQueuedThread(t1));
322 assertTrue(lock.hasQueuedThread(t2));
325 assertFalse(lock.hasQueuedThread(t1));
326 assertTrue(lock.hasQueuedThread(t2));
327 lock.unlock();
329 assertFalse(lock.hasQueuedThread(t1));
330 assertFalse(lock.hasQueuedThread(t2));
339 final PublicReentrantLock lock = new PublicReentrantLock(fair);
340 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
341 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
342 assertTrue(lock.getQueuedThreads().isEmpty());
343 lock.lock();
344 assertTrue(lock.getQueuedThreads().isEmpty());
346 waitForQueuedThread(lock, t1);
347 assertEquals(1, lock.getQueuedThreads().size());
348 assertTrue(lock.getQueuedThreads().contains(t1));
350 waitForQueuedThread(lock, t2);
351 assertEquals(2, lock.getQueuedThreads().size());
352 assertTrue(lock.getQueuedThreads().contains(t1));
353 assertTrue(lock.getQueuedThreads().contains(t2));
356 assertFalse(lock.getQueuedThreads().contains(t1));
357 assertTrue(lock.getQueuedThreads().contains(t2));
358 assertEquals(1, lock.getQueuedThreads().size());
359 lock.unlock();
361 assertTrue(lock.getQueuedThreads().isEmpty());
370 final PublicReentrantLock lock = new PublicReentrantLock(fair);
371 lock.lock();
374 lock.tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
377 waitForQueuedThread(lock, t);
380 releaseLock(lock);
384 * tryLock on a locked lock fails
389 final PublicReentrantLock lock = new PublicReentrantLock(fair);
390 lock.lock();
393 assertFalse(lock.tryLock());
397 releaseLock(lock);
401 * Timed tryLock on a locked lock times out
406 final PublicReentrantLock lock = new PublicReentrantLock(fair);
407 lock.lock();
412 assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS));
417 releaseLock(lock);
426 ReentrantLock lock = new ReentrantLock(fair);
428 lock.lock();
429 assertEquals(i, lock.getHoldCount());
432 lock.unlock();
433 assertEquals(i - 1, lock.getHoldCount());
444 final ReentrantLock lock = new ReentrantLock(fair);
445 assertFalse(lock.isLocked());
446 lock.lock();
447 assertTrue(lock.isLocked());
448 lock.lock();
449 assertTrue(lock.isLocked());
450 lock.unlock();
451 assertTrue(lock.isLocked());
452 lock.unlock();
453 assertFalse(lock.isLocked());
457 lock.lock();
458 assertTrue(lock.isLocked());
461 lock.unlock();
465 assertTrue(lock.isLocked());
468 assertFalse(lock.isLocked());
478 final PublicReentrantLock lock = new PublicReentrantLock(fair);
480 lock.lockInterruptibly();
482 assertLockedByMoi(lock);
483 Thread t = newStartedThread(new InterruptedLockRunnable(lock));
484 waitForQueuedThread(lock, t);
486 assertTrue(lock.isLocked());
487 assertTrue(lock.isHeldByCurrentThread());
489 releaseLock(lock);
493 * Calling await without holding lock throws IllegalMonitorStateException
498 final ReentrantLock lock = new ReentrantLock(fair);
499 final Condition c = lock.newCondition();
512 * Calling signal without holding lock throws IllegalMonitorStateException
517 final ReentrantLock lock = new ReentrantLock(fair);
518 final Condition c = lock.newCondition();
532 final ReentrantLock lock = new ReentrantLock(fair);
533 final Condition c = lock.newCondition();
534 lock.lock();
541 lock.unlock();
552 final ReentrantLock lock = new ReentrantLock(fair);
553 final Condition c = lock.newCondition();
554 lock.lock();
559 lock.unlock();
570 final ReentrantLock lock = new ReentrantLock(fair);
571 final Condition c = lock.newCondition();
572 lock.lock();
578 lock.unlock();
588 final PublicReentrantLock lock = new PublicReentrantLock(fair);
589 final Condition c = lock.newCondition();
593 lock.lock();
596 lock.unlock();
600 lock.lock();
601 assertHasWaiters(lock, c, t);
603 assertHasNoWaiters(lock, c);
605 lock.unlock();
615 final ReentrantLock lock = new ReentrantLock(fair);
617 lock.hasWaiters(null);
628 final ReentrantLock lock = new ReentrantLock(fair);
630 lock.getWaitQueueLength(null);
641 final PublicReentrantLock lock = new PublicReentrantLock(fair);
643 lock.getWaitingThreads(null);
654 final ReentrantLock lock = new ReentrantLock(fair);
655 final Condition c = lock.newCondition();
669 final ReentrantLock lock = new ReentrantLock(fair);
670 final Condition c = lock.newCondition();
672 lock.hasWaiters(c);
683 final ReentrantLock lock = new ReentrantLock(fair);
684 final Condition c = lock.newCondition();
698 final ReentrantLock lock = new ReentrantLock(fair);
699 final Condition c = lock.newCondition();
701 lock.getWaitQueueLength(c);
712 final PublicReentrantLock lock = new PublicReentrantLock(fair);
713 final Condition c = lock.newCondition();
727 final PublicReentrantLock lock = new PublicReentrantLock(fair);
728 final Condition c = lock.newCondition();
730 lock.getWaitingThreads(c);
741 final PublicReentrantLock lock = new PublicReentrantLock(fair);
742 final Condition c = lock.newCondition();
746 lock.lock();
747 assertHasNoWaiters(lock, c);
748 assertFalse(lock.hasWaiters(c));
751 assertHasNoWaiters(lock, c);
752 assertFalse(lock.hasWaiters(c));
753 lock.unlock();
757 lock.lock();
758 assertHasWaiters(lock, c, t);
759 assertTrue(lock.hasWaiters(c));
761 assertHasNoWaiters(lock, c);
762 assertFalse(lock.hasWaiters(c));
763 lock.unlock();
765 assertHasNoWaiters(lock, c);
774 final PublicReentrantLock lock = new PublicReentrantLock(fair);
775 final Condition c = lock.newCondition();
780 lock.lock();
781 assertFalse(lock.hasWaiters(c));
782 assertEquals(0, lock.getWaitQueueLength(c));
785 lock.unlock();
790 lock.lock();
791 assertTrue(lock.hasWaiters(c));
792 assertEquals(1, lock.getWaitQueueLength(c));
795 lock.unlock();
798 lock.lock();
799 assertEquals(0, lock.getWaitQueueLength(c));
800 lock.unlock();
805 lock.lock();
806 assertHasWaiters(lock, c, t1);
807 assertEquals(1, lock.getWaitQueueLength(c));
808 lock.unlock();
813 lock.lock();
814 assertHasWaiters(lock, c, t1, t2);
815 assertEquals(2, lock.getWaitQueueLength(c));
817 assertHasNoWaiters(lock, c);
818 lock.unlock();
823 assertHasNoWaiters(lock, c);
832 final PublicReentrantLock lock = new PublicReentrantLock(fair);
833 final Condition c = lock.newCondition();
838 lock.lock();
839 assertTrue(lock.getWaitingThreads(c).isEmpty());
842 lock.unlock();
847 lock.lock();
848 assertFalse(lock.getWaitingThreads(c).isEmpty());
851 lock.unlock();
854 lock.lock();
855 assertTrue(lock.getWaitingThreads(c).isEmpty());
856 lock.unlock();
861 lock.lock();
862 assertHasWaiters(lock, c, t1);
863 assertTrue(lock.getWaitingThreads(c).contains(t1));
864 assertFalse(lock.getWaitingThreads(c).contains(t2));
865 assertEquals(1, lock.getWaitingThreads(c).size());
866 lock.unlock();
871 lock.lock();
872 assertHasWaiters(lock, c, t1, t2);
873 assertTrue(lock.getWaitingThreads(c).contains(t1));
874 assertTrue(lock.getWaitingThreads(c).contains(t2));
875 assertEquals(2, lock.getWaitingThreads(c).size());
877 assertHasNoWaiters(lock, c);
878 lock.unlock();
883 assertHasNoWaiters(lock, c);
892 final ReentrantLock lock = new ReentrantLock(fair);
893 lock.newCondition();
899 lock.lock();
904 lock.unlock();
910 lock.lock();
914 lock.unlock();
918 lock.lock();
919 lock.unlock();
925 lock.lock();
927 lock.unlock();
945 final PublicReentrantLock lock =
947 final Condition c = lock.newCondition();
951 lock.lock();
952 assertLockedByMoi(lock);
953 assertHasNoWaiters(lock, c);
958 assertLockedByMoi(lock);
959 assertHasNoWaiters(lock, c);
960 lock.unlock();
966 assertHasWaiters(lock, c, t);
969 assertNotLocked(lock);
984 final PublicReentrantLock lock = new PublicReentrantLock(fair);
985 final Condition c = lock.newCondition();
989 lock.lock();
992 lock.unlock();
1000 lock.lock();
1001 assertHasWaiters(lock, c, t1, t2);
1003 assertHasNoWaiters(lock, c);
1004 lock.unlock();
1015 final PublicReentrantLock lock =
1017 final Condition c = lock.newCondition();
1022 lock.lock();
1025 lock.unlock();
1032 lock.lock();
1035 lock.unlock();
1040 lock.lock();
1041 assertHasWaiters(lock, c, t1, t2);
1042 assertFalse(lock.hasQueuedThreads());
1044 assertHasWaiters(lock, c, t2);
1045 assertTrue(lock.hasQueuedThread(t1));
1046 assertFalse(lock.hasQueuedThread(t2));
1048 assertHasNoWaiters(lock, c);
1049 assertTrue(lock.hasQueuedThread(t1));
1050 assertTrue(lock.hasQueuedThread(t2));
1051 lock.unlock();
1057 * await after multiple reentrant locking preserves lock count
1062 final PublicReentrantLock lock = new PublicReentrantLock(fair);
1063 final Condition c = lock.newCondition();
1067 lock.lock();
1068 assertLockedByMoi(lock);
1069 assertEquals(1, lock.getHoldCount());
1072 assertLockedByMoi(lock);
1073 assertEquals(1, lock.getHoldCount());
1074 lock.unlock();
1079 lock.lock();
1080 lock.lock();
1081 assertLockedByMoi(lock);
1082 assertEquals(2, lock.getHoldCount());
1085 assertLockedByMoi(lock);
1086 assertEquals(2, lock.getHoldCount());
1087 lock.unlock();
1088 lock.unlock();
1092 lock.lock();
1093 assertHasWaiters(lock, c, t1, t2);
1094 assertEquals(1, lock.getHoldCount());
1096 assertHasNoWaiters(lock, c);
1097 lock.unlock();
1103 * A serialized lock deserializes as unlocked
1108 ReentrantLock lock = new ReentrantLock(fair);
1109 lock.lock();
1111 ReentrantLock clone = serialClone(lock);
1112 assertEquals(lock.isFair(), clone.isFair());
1113 assertTrue(lock.isLocked());
1115 assertEquals(1, lock.getHoldCount());
1117 clone.lock();
1118 clone.lock();
1121 assertEquals(1, lock.getHoldCount());
1124 assertTrue(lock.isLocked());
1129 * toString indicates current lock state
1134 ReentrantLock lock = new ReentrantLock(fair);
1135 assertTrue(lock.toString().contains("Unlocked"));
1136 lock.lock();
1137 assertTrue(lock.toString().contains("Locked"));
1138 lock.unlock();
1139 assertTrue(lock.toString().contains("Unlocked"));