Lines Matching refs:sync

135         final Mutex sync;
136 InterruptibleSyncRunnable(Mutex sync) { this.sync = sync; }
138 sync.acquireInterruptibly();
147 final Mutex sync;
148 InterruptedSyncRunnable(Mutex sync) { this.sync = sync; }
150 sync.acquireInterruptibly();
158 * Spin-waits until sync.isQueued(t) becomes true.
160 void waitForQueuedThread(AbstractQueuedLongSynchronizer sync,
163 while (!sync.isQueued(t)) {
172 * Checks that sync has exactly the given queued threads.
174 void assertHasQueuedThreads(AbstractQueuedLongSynchronizer sync,
176 Collection<Thread> actual = sync.getQueuedThreads();
177 assertEquals(expected.length > 0, sync.hasQueuedThreads());
178 assertEquals(expected.length, sync.getQueueLength());
186 * Checks that sync has exactly the given (exclusive) queued threads.
188 void assertHasExclusiveQueuedThreads(AbstractQueuedLongSynchronizer sync,
190 assertHasQueuedThreads(sync, expected);
191 assertEquals(new HashSet<Thread>(sync.getExclusiveQueuedThreads()),
192 new HashSet<Thread>(sync.getQueuedThreads()));
193 assertEquals(0, sync.getSharedQueuedThreads().size());
194 assertTrue(sync.getSharedQueuedThreads().isEmpty());
198 * Checks that sync has exactly the given (shared) queued threads.
200 void assertHasSharedQueuedThreads(AbstractQueuedLongSynchronizer sync,
202 assertHasQueuedThreads(sync, expected);
203 assertEquals(new HashSet<Thread>(sync.getSharedQueuedThreads()),
204 new HashSet<Thread>(sync.getQueuedThreads()));
205 assertEquals(0, sync.getExclusiveQueuedThreads().size());
206 assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
213 void assertHasWaitersUnlocked(Mutex sync, ConditionObject c,
215 sync.acquire();
216 assertHasWaitersLocked(sync, c, threads);
217 sync.release();
223 void assertHasWaitersLocked(Mutex sync, ConditionObject c,
225 assertEquals(threads.length > 0, sync.hasWaiters(c));
226 assertEquals(threads.length, sync.getWaitQueueLength(c));
227 assertEquals(threads.length == 0, sync.getWaitingThreads(c).isEmpty());
228 assertEquals(threads.length, sync.getWaitingThreads(c).size());
229 assertEquals(new HashSet<Thread>(sync.getWaitingThreads(c)),
300 Mutex sync = new Mutex();
301 assertFalse(sync.isHeldExclusively());
305 * acquiring released sync succeeds
308 Mutex sync = new Mutex();
309 sync.acquire();
310 assertTrue(sync.isHeldExclusively());
311 sync.release();
312 assertFalse(sync.isHeldExclusively());
316 * tryAcquire on a released sync succeeds
319 Mutex sync = new Mutex();
320 assertTrue(sync.tryAcquire());
321 assertTrue(sync.isHeldExclusively());
322 sync.release();
323 assertFalse(sync.isHeldExclusively());
330 final Mutex sync = new Mutex();
331 assertFalse(sync.hasQueuedThreads());
332 sync.acquire();
333 Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
334 waitForQueuedThread(sync, t1);
335 assertTrue(sync.hasQueuedThreads());
336 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
337 waitForQueuedThread(sync, t2);
338 assertTrue(sync.hasQueuedThreads());
341 assertTrue(sync.hasQueuedThreads());
342 sync.release();
344 assertFalse(sync.hasQueuedThreads());
351 final Mutex sync = new Mutex();
353 sync.isQueued(null);
362 final Mutex sync = new Mutex();
363 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
364 Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
365 assertFalse(sync.isQueued(t1));
366 assertFalse(sync.isQueued(t2));
367 sync.acquire();
369 waitForQueuedThread(sync, t1);
370 assertTrue(sync.isQueued(t1));
371 assertFalse(sync.isQueued(t2));
373 waitForQueuedThread(sync, t2);
374 assertTrue(sync.isQueued(t1));
375 assertTrue(sync.isQueued(t2));
378 assertFalse(sync.isQueued(t1));
379 assertTrue(sync.isQueued(t2));
380 sync.release();
382 assertFalse(sync.isQueued(t1));
383 assertFalse(sync.isQueued(t2));
390 final Mutex sync = new Mutex();
391 assertNull(sync.getFirstQueuedThread());
392 sync.acquire();
393 Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
394 waitForQueuedThread(sync, t1);
395 assertEquals(t1, sync.getFirstQueuedThread());
396 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
397 waitForQueuedThread(sync, t2);
398 assertEquals(t1, sync.getFirstQueuedThread());
401 assertEquals(t2, sync.getFirstQueuedThread());
402 sync.release();
404 assertNull(sync.getFirstQueuedThread());
411 final Mutex sync = new Mutex();
412 assertFalse(sync.hasContended());
413 sync.acquire();
414 assertFalse(sync.hasContended());
415 Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
416 waitForQueuedThread(sync, t1);
417 assertTrue(sync.hasContended());
418 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
419 waitForQueuedThread(sync, t2);
420 assertTrue(sync.hasContended());
423 assertTrue(sync.hasContended());
424 sync.release();
426 assertTrue(sync.hasContended());
433 final Mutex sync = new Mutex();
434 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
435 Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
436 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
437 sync.acquire();
438 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
440 waitForQueuedThread(sync, t1);
441 assertHasExclusiveQueuedThreads(sync, t1);
442 assertTrue(sync.getQueuedThreads().contains(t1));
443 assertFalse(sync.getQueuedThreads().contains(t2));
445 waitForQueuedThread(sync, t2);
446 assertHasExclusiveQueuedThreads(sync, t1, t2);
447 assertTrue(sync.getQueuedThreads().contains(t1));
448 assertTrue(sync.getQueuedThreads().contains(t2));
451 assertHasExclusiveQueuedThreads(sync, t2);
452 sync.release();
454 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
461 final Mutex sync = new Mutex();
462 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
463 Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
464 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
465 sync.acquire();
466 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
468 waitForQueuedThread(sync, t1);
469 assertHasExclusiveQueuedThreads(sync, t1);
470 assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
471 assertFalse(sync.getExclusiveQueuedThreads().contains(t2));
473 waitForQueuedThread(sync, t2);
474 assertHasExclusiveQueuedThreads(sync, t1, t2);
475 assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
476 assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
479 assertHasExclusiveQueuedThreads(sync, t2);
480 sync.release();
482 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
489 final Mutex sync = new Mutex();
490 assertTrue(sync.getSharedQueuedThreads().isEmpty());
491 sync.acquire();
492 assertTrue(sync.getSharedQueuedThreads().isEmpty());
493 Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
494 waitForQueuedThread(sync, t1);
495 assertTrue(sync.getSharedQueuedThreads().isEmpty());
496 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
497 waitForQueuedThread(sync, t2);
498 assertTrue(sync.getSharedQueuedThreads().isEmpty());
501 assertTrue(sync.getSharedQueuedThreads().isEmpty());
502 sync.release();
504 assertTrue(sync.getSharedQueuedThreads().isEmpty());
537 final Mutex sync = new Mutex();
538 sync.acquire();
541 sync.tryAcquireNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
544 waitForQueuedThread(sync, t);
550 * tryAcquire on exclusively held sync fails
553 final Mutex sync = new Mutex();
554 sync.acquire();
557 assertFalse(sync.tryAcquire());
561 sync.release();
565 * tryAcquireNanos on an exclusively held sync times out
568 final Mutex sync = new Mutex();
569 sync.acquire();
574 assertFalse(sync.tryAcquireNanos(nanos));
579 sync.release();
586 final Mutex sync = new Mutex();
587 sync.acquire();
588 assertTrue(sync.isHeldExclusively());
589 sync.release();
590 assertFalse(sync.isHeldExclusively());
596 sync.acquire();
599 sync.release();
603 assertTrue(sync.isHeldExclusively());
606 assertFalse(sync.isHeldExclusively());
613 final Mutex sync = new Mutex();
615 sync.acquireInterruptibly();
619 sync.acquireInterruptibly();
623 waitForQueuedThread(sync, t);
626 assertTrue(sync.isHeldExclusively());
630 * owns is true for a condition created by sync else false
633 final Mutex sync = new Mutex();
634 final ConditionObject c = sync.newCondition();
636 assertTrue(sync.owns(c));
641 * Calling await without holding sync throws IllegalMonitorStateException
644 final Mutex sync = new Mutex();
645 final ConditionObject c = sync.newCondition();
658 * Calling signal without holding sync throws IllegalMonitorStateException
661 final Mutex sync = new Mutex();
662 final ConditionObject c = sync.newCondition();
667 assertHasWaitersUnlocked(sync, c, NO_THREADS);
671 * Calling signalAll without holding sync throws IllegalMonitorStateException
674 final Mutex sync = new Mutex();
675 final ConditionObject c = sync.newCondition();
689 final Mutex sync = new Mutex();
690 final ConditionObject c = sync.newCondition();
691 sync.acquire();
693 sync.release();
704 final Mutex sync = new Mutex();
705 final ConditionObject c = sync.newCondition();
709 sync.acquire();
712 sync.release();
716 sync.acquire();
717 assertHasWaitersLocked(sync, c, t);
718 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
720 assertHasWaitersLocked(sync, c, NO_THREADS);
721 assertHasExclusiveQueuedThreads(sync, t);
722 sync.release();
730 final Mutex sync = new Mutex();
732 sync.hasWaiters(null);
741 final Mutex sync = new Mutex();
743 sync.getWaitQueueLength(null);
752 final Mutex sync = new Mutex();
754 sync.getWaitingThreads(null);
763 final Mutex sync = new Mutex();
764 final ConditionObject c = sync.newCondition();
770 assertHasWaitersUnlocked(sync, c, NO_THREADS);
777 final Mutex sync = new Mutex();
778 final ConditionObject c = sync.newCondition();
780 sync.hasWaiters(c);
783 assertHasWaitersUnlocked(sync, c, NO_THREADS);
790 final Mutex sync = new Mutex();
791 final ConditionObject c = sync.newCondition();
797 assertHasWaitersUnlocked(sync, c, NO_THREADS);
804 final Mutex sync = new Mutex();
805 final ConditionObject c = sync.newCondition();
807 sync.getWaitQueueLength(c);
810 assertHasWaitersUnlocked(sync, c, NO_THREADS);
817 final Mutex sync = new Mutex();
818 final ConditionObject c = sync.newCondition();
824 assertHasWaitersUnlocked(sync, c, NO_THREADS);
831 final Mutex sync = new Mutex();
832 final ConditionObject c = sync.newCondition();
834 sync.getWaitingThreads(c);
837 assertHasWaitersUnlocked(sync, c, NO_THREADS);
844 final Mutex sync = new Mutex();
845 final ConditionObject c = sync.newCondition();
849 sync.acquire();
850 assertHasWaitersLocked(sync, c, NO_THREADS);
851 assertFalse(sync.hasWaiters(c));
854 sync.release();
858 sync.acquire();
859 assertHasWaitersLocked(sync, c, t);
860 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
861 assertTrue(sync.hasWaiters(c));
863 assertHasWaitersLocked(sync, c, NO_THREADS);
864 assertHasExclusiveQueuedThreads(sync, t);
865 assertFalse(sync.hasWaiters(c));
866 sync.release();
869 assertHasWaitersUnlocked(sync, c, NO_THREADS);
876 final Mutex sync = new Mutex();
877 final ConditionObject c = sync.newCondition();
882 sync.acquire();
883 assertHasWaitersLocked(sync, c, NO_THREADS);
884 assertEquals(0, sync.getWaitQueueLength(c));
887 sync.release();
890 sync.acquire();
891 assertHasWaitersLocked(sync, c, t1);
892 assertEquals(1, sync.getWaitQueueLength(c));
893 sync.release();
897 sync.acquire();
898 assertHasWaitersLocked(sync, c, t1);
899 assertEquals(1, sync.getWaitQueueLength(c));
902 sync.release();
905 sync.acquire();
906 assertHasWaitersLocked(sync, c, t1, t2);
907 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
908 assertEquals(2, sync.getWaitQueueLength(c));
910 assertHasWaitersLocked(sync, c, NO_THREADS);
911 assertHasExclusiveQueuedThreads(sync, t1, t2);
912 assertEquals(0, sync.getWaitQueueLength(c));
913 sync.release();
917 assertHasWaitersUnlocked(sync, c, NO_THREADS);
924 final Mutex sync = new Mutex();
925 final ConditionObject c = sync.newCondition();
930 sync.acquire();
931 assertHasWaitersLocked(sync, c, NO_THREADS);
932 assertTrue(sync.getWaitingThreads(c).isEmpty());
935 sync.release();
940 sync.acquire();
941 assertHasWaitersLocked(sync, c, t1);
942 assertTrue(sync.getWaitingThreads(c).contains(t1));
943 assertFalse(sync.getWaitingThreads(c).isEmpty());
944 assertEquals(1, sync.getWaitingThreads(c).size());
947 sync.release();
950 sync.acquire();
951 assertHasWaitersLocked(sync, c, NO_THREADS);
952 assertFalse(sync.getWaitingThreads(c).contains(t1));
953 assertFalse(sync.getWaitingThreads(c).contains(t2));
954 assertTrue(sync.getWaitingThreads(c).isEmpty());
955 assertEquals(0, sync.getWaitingThreads(c).size());
956 sync.release();
960 sync.acquire();
961 assertHasWaitersLocked(sync, c, t1);
962 assertTrue(sync.getWaitingThreads(c).contains(t1));
963 assertFalse(sync.getWaitingThreads(c).contains(t2));
964 assertFalse(sync.getWaitingThreads(c).isEmpty());
965 assertEquals(1, sync.getWaitingThreads(c).size());
966 sync.release();
970 sync.acquire();
971 assertHasWaitersLocked(sync, c, t1, t2);
972 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
973 assertTrue(sync.getWaitingThreads(c).contains(t1));
974 assertTrue(sync.getWaitingThreads(c).contains(t2));
975 assertFalse(sync.getWaitingThreads(c).isEmpty());
976 assertEquals(2, sync.getWaitingThreads(c).size());
978 assertHasWaitersLocked(sync, c, NO_THREADS);
979 assertHasExclusiveQueuedThreads(sync, t1, t2);
980 assertFalse(sync.getWaitingThreads(c).contains(t1));
981 assertFalse(sync.getWaitingThreads(c).contains(t2));
982 assertTrue(sync.getWaitingThreads(c).isEmpty());
983 assertEquals(0, sync.getWaitingThreads(c).size());
984 sync.release();
988 assertHasWaitersUnlocked(sync, c, NO_THREADS);
995 final Mutex sync = new Mutex();
996 final ConditionObject condition = sync.newCondition();
1000 sync.acquire();
1004 assertHasWaitersLocked(sync, condition, NO_THREADS);
1005 sync.release();
1009 sync.acquire();
1010 assertHasWaitersLocked(sync, condition, t);
1011 sync.release();
1013 assertHasWaitersUnlocked(sync, condition, t);
1015 sync.acquire();
1016 assertHasWaitersLocked(sync, condition, t);
1017 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
1019 assertHasWaitersLocked(sync, condition, NO_THREADS);
1020 assertHasExclusiveQueuedThreads(sync, t);
1021 sync.release();
1033 final Mutex sync = new Mutex();
1034 final ConditionObject c = sync.newCondition();
1038 sync.acquire();
1056 final Mutex sync = new Mutex();
1057 final ConditionObject c = sync.newCondition();
1062 sync.acquire();
1065 sync.release();
1070 sync.acquire();
1073 sync.release();
1078 sync.acquire();
1079 assertHasWaitersLocked(sync, c, t1, t2);
1080 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
1082 assertHasWaitersLocked(sync, c, NO_THREADS);
1083 assertHasExclusiveQueuedThreads(sync, t1, t2);
1084 sync.release();
1093 Mutex sync = new Mutex();
1094 assertTrue(sync.toString().contains("State = " + Mutex.UNLOCKED));
1095 sync.acquire();
1096 assertTrue(sync.toString().contains("State = " + Mutex.LOCKED));
1103 Mutex sync = new Mutex();
1104 assertFalse(serialClone(sync).isHeldExclusively());
1105 sync.acquire();
1106 Thread t = newStartedThread(new InterruptedSyncRunnable(sync));
1107 waitForQueuedThread(sync, t);
1108 assertTrue(sync.isHeldExclusively());
1110 Mutex clone = serialClone(sync);
1112 assertHasExclusiveQueuedThreads(sync, t);
1116 sync.release();
1117 assertFalse(sync.isHeldExclusively());
1119 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
1261 final Mutex sync = new Mutex();
1262 final ConditionObject c = sync.newCondition();
1263 sync.acquire();
1266 sync.release();
1273 final Mutex sync = new Mutex();
1274 final ConditionObject c = sync.newCondition();
1275 sync.acquire();
1278 sync.release();