126 |
|
/** |
127 |
|
* getWriteHoldCount returns number of recursive holds |
128 |
|
*/ |
129 |
< |
public void testGetHoldCount() { |
129 |
> |
public void testGetWriteHoldCount() { |
130 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
131 |
|
for(int i = 1; i <= SIZE; i++) { |
132 |
|
lock.writeLock().lock(); |
137 |
|
assertEquals(i-1,lock.getWriteHoldCount()); |
138 |
|
} |
139 |
|
} |
140 |
+ |
|
141 |
+ |
/** |
142 |
+ |
* getReadHoldCount returns number of recursive holds |
143 |
+ |
*/ |
144 |
+ |
public void testGetReadHoldCount() { |
145 |
+ |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
146 |
+ |
for(int i = 1; i <= SIZE; i++) { |
147 |
+ |
lock.readLock().lock(); |
148 |
+ |
assertEquals(i,lock.getReadHoldCount()); |
149 |
+ |
} |
150 |
+ |
for(int i = SIZE; i > 0; i--) { |
151 |
+ |
lock.readLock().unlock(); |
152 |
+ |
assertEquals(i-1,lock.getReadHoldCount()); |
153 |
+ |
} |
154 |
+ |
} |
155 |
|
|
156 |
|
|
157 |
|
/** |
404 |
|
|
405 |
|
/** |
406 |
|
* Read lock succeeds if write locked by current thread even if |
407 |
< |
* other threads are waiting |
407 |
> |
* other threads are waiting for readlock |
408 |
|
*/ |
409 |
|
public void testReadHoldingWriteLock2() { |
410 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
442 |
|
} |
443 |
|
|
444 |
|
/** |
445 |
+ |
* Read lock succeeds if write locked by current thread even if |
446 |
+ |
* other threads are waiting for writelock |
447 |
+ |
*/ |
448 |
+ |
public void testReadHoldingWriteLock3() { |
449 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
450 |
+ |
lock.writeLock().lock(); |
451 |
+ |
Thread t1 = new Thread(new Runnable() { |
452 |
+ |
public void run() { |
453 |
+ |
lock.writeLock().lock(); |
454 |
+ |
lock.writeLock().unlock(); |
455 |
+ |
} |
456 |
+ |
}); |
457 |
+ |
Thread t2 = new Thread(new Runnable() { |
458 |
+ |
public void run() { |
459 |
+ |
lock.writeLock().lock(); |
460 |
+ |
lock.writeLock().unlock(); |
461 |
+ |
} |
462 |
+ |
}); |
463 |
+ |
|
464 |
+ |
try { |
465 |
+ |
t1.start(); |
466 |
+ |
t2.start(); |
467 |
+ |
lock.readLock().lock(); |
468 |
+ |
lock.readLock().unlock(); |
469 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
470 |
+ |
lock.readLock().lock(); |
471 |
+ |
lock.readLock().unlock(); |
472 |
+ |
lock.writeLock().unlock(); |
473 |
+ |
t1.join(MEDIUM_DELAY_MS); |
474 |
+ |
t2.join(MEDIUM_DELAY_MS); |
475 |
+ |
assertTrue(!t1.isAlive()); |
476 |
+ |
assertTrue(!t2.isAlive()); |
477 |
+ |
|
478 |
+ |
} catch(Exception e){ |
479 |
+ |
unexpectedException(); |
480 |
+ |
} |
481 |
+ |
} |
482 |
+ |
|
483 |
+ |
|
484 |
+ |
/** |
485 |
+ |
* Write lock succeeds if write locked by current thread even if |
486 |
+ |
* other threads are waiting for writelock |
487 |
+ |
*/ |
488 |
+ |
public void testWriteHoldingWriteLock4() { |
489 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
490 |
+ |
lock.writeLock().lock(); |
491 |
+ |
Thread t1 = new Thread(new Runnable() { |
492 |
+ |
public void run() { |
493 |
+ |
lock.writeLock().lock(); |
494 |
+ |
lock.writeLock().unlock(); |
495 |
+ |
} |
496 |
+ |
}); |
497 |
+ |
Thread t2 = new Thread(new Runnable() { |
498 |
+ |
public void run() { |
499 |
+ |
lock.writeLock().lock(); |
500 |
+ |
lock.writeLock().unlock(); |
501 |
+ |
} |
502 |
+ |
}); |
503 |
+ |
|
504 |
+ |
try { |
505 |
+ |
t1.start(); |
506 |
+ |
t2.start(); |
507 |
+ |
lock.writeLock().lock(); |
508 |
+ |
lock.writeLock().unlock(); |
509 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
510 |
+ |
lock.writeLock().lock(); |
511 |
+ |
lock.writeLock().unlock(); |
512 |
+ |
lock.writeLock().unlock(); |
513 |
+ |
t1.join(MEDIUM_DELAY_MS); |
514 |
+ |
t2.join(MEDIUM_DELAY_MS); |
515 |
+ |
assertTrue(!t1.isAlive()); |
516 |
+ |
assertTrue(!t2.isAlive()); |
517 |
+ |
|
518 |
+ |
} catch(Exception e){ |
519 |
+ |
unexpectedException(); |
520 |
+ |
} |
521 |
+ |
} |
522 |
+ |
|
523 |
+ |
|
524 |
+ |
/** |
525 |
|
* Fair Read trylock succeeds if write locked by current thread |
526 |
|
*/ |
527 |
|
public void testReadHoldingWriteLockFair() { |
534 |
|
|
535 |
|
/** |
536 |
|
* Fair Read lock succeeds if write locked by current thread even if |
537 |
< |
* other threads are waiting |
537 |
> |
* other threads are waiting for readlock |
538 |
|
*/ |
539 |
|
public void testReadHoldingWriteLockFair2() { |
540 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
573 |
|
|
574 |
|
|
575 |
|
/** |
576 |
+ |
* Fair Read lock succeeds if write locked by current thread even if |
577 |
+ |
* other threads are waiting for writelock |
578 |
+ |
*/ |
579 |
+ |
public void testReadHoldingWriteLockFair3() { |
580 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
581 |
+ |
lock.writeLock().lock(); |
582 |
+ |
Thread t1 = new Thread(new Runnable() { |
583 |
+ |
public void run() { |
584 |
+ |
lock.writeLock().lock(); |
585 |
+ |
lock.writeLock().unlock(); |
586 |
+ |
} |
587 |
+ |
}); |
588 |
+ |
Thread t2 = new Thread(new Runnable() { |
589 |
+ |
public void run() { |
590 |
+ |
lock.writeLock().lock(); |
591 |
+ |
lock.writeLock().unlock(); |
592 |
+ |
} |
593 |
+ |
}); |
594 |
+ |
|
595 |
+ |
try { |
596 |
+ |
t1.start(); |
597 |
+ |
t2.start(); |
598 |
+ |
lock.readLock().lock(); |
599 |
+ |
lock.readLock().unlock(); |
600 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
601 |
+ |
lock.readLock().lock(); |
602 |
+ |
lock.readLock().unlock(); |
603 |
+ |
lock.writeLock().unlock(); |
604 |
+ |
t1.join(MEDIUM_DELAY_MS); |
605 |
+ |
t2.join(MEDIUM_DELAY_MS); |
606 |
+ |
assertTrue(!t1.isAlive()); |
607 |
+ |
assertTrue(!t2.isAlive()); |
608 |
+ |
|
609 |
+ |
} catch(Exception e){ |
610 |
+ |
unexpectedException(); |
611 |
+ |
} |
612 |
+ |
} |
613 |
+ |
|
614 |
+ |
|
615 |
+ |
/** |
616 |
+ |
* Fair Write lock succeeds if write locked by current thread even if |
617 |
+ |
* other threads are waiting for writelock |
618 |
+ |
*/ |
619 |
+ |
public void testWriteHoldingWriteLockFair4() { |
620 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
621 |
+ |
lock.writeLock().lock(); |
622 |
+ |
Thread t1 = new Thread(new Runnable() { |
623 |
+ |
public void run() { |
624 |
+ |
lock.writeLock().lock(); |
625 |
+ |
lock.writeLock().unlock(); |
626 |
+ |
} |
627 |
+ |
}); |
628 |
+ |
Thread t2 = new Thread(new Runnable() { |
629 |
+ |
public void run() { |
630 |
+ |
lock.writeLock().lock(); |
631 |
+ |
lock.writeLock().unlock(); |
632 |
+ |
} |
633 |
+ |
}); |
634 |
+ |
|
635 |
+ |
try { |
636 |
+ |
t1.start(); |
637 |
+ |
t2.start(); |
638 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
639 |
+ |
assertTrue(lock.isWriteLockedByCurrentThread()); |
640 |
+ |
assertTrue(lock.getWriteHoldCount() == 1); |
641 |
+ |
lock.writeLock().lock(); |
642 |
+ |
assertTrue(lock.getWriteHoldCount() == 2); |
643 |
+ |
lock.writeLock().unlock(); |
644 |
+ |
lock.writeLock().lock(); |
645 |
+ |
lock.writeLock().unlock(); |
646 |
+ |
lock.writeLock().unlock(); |
647 |
+ |
t1.join(MEDIUM_DELAY_MS); |
648 |
+ |
t2.join(MEDIUM_DELAY_MS); |
649 |
+ |
assertTrue(!t1.isAlive()); |
650 |
+ |
assertTrue(!t2.isAlive()); |
651 |
+ |
|
652 |
+ |
} catch(Exception e){ |
653 |
+ |
unexpectedException(); |
654 |
+ |
} |
655 |
+ |
} |
656 |
+ |
|
657 |
+ |
|
658 |
+ |
/** |
659 |
|
* Read tryLock succeeds if readlocked but not writelocked |
660 |
|
*/ |
661 |
|
public void testTryLockWhenReadLocked() { |
925 |
|
} |
926 |
|
} |
927 |
|
|
928 |
+ |
/** A helper class for uninterruptible wait tests */ |
929 |
+ |
class UninterruptableThread extends Thread { |
930 |
+ |
private Lock lock; |
931 |
+ |
private Condition c; |
932 |
+ |
|
933 |
+ |
public volatile boolean canAwake = false; |
934 |
+ |
public volatile boolean interrupted = false; |
935 |
+ |
public volatile boolean lockStarted = false; |
936 |
+ |
|
937 |
+ |
public UninterruptableThread(Lock lock, Condition c) { |
938 |
+ |
this.lock = lock; |
939 |
+ |
this.c = c; |
940 |
+ |
} |
941 |
+ |
|
942 |
+ |
public synchronized void run() { |
943 |
+ |
lock.lock(); |
944 |
+ |
lockStarted = true; |
945 |
+ |
|
946 |
+ |
while (!canAwake) { |
947 |
+ |
c.awaitUninterruptibly(); |
948 |
+ |
} |
949 |
+ |
|
950 |
+ |
interrupted = isInterrupted(); |
951 |
+ |
lock.unlock(); |
952 |
+ |
} |
953 |
+ |
} |
954 |
+ |
|
955 |
|
/** |
956 |
|
* awaitUninterruptibly doesn't abort on interrupt |
957 |
|
*/ |
958 |
|
public void testAwaitUninterruptibly() { |
959 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
959 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
960 |
|
final Condition c = lock.writeLock().newCondition(); |
961 |
< |
Thread t = new Thread(new Runnable() { |
757 |
< |
public void run() { |
758 |
< |
lock.writeLock().lock(); |
759 |
< |
c.awaitUninterruptibly(); |
760 |
< |
lock.writeLock().unlock(); |
761 |
< |
} |
762 |
< |
}); |
961 |
> |
UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c); |
962 |
|
|
963 |
|
try { |
964 |
< |
t.start(); |
965 |
< |
Thread.sleep(SHORT_DELAY_MS); |
966 |
< |
t.interrupt(); |
964 |
> |
thread.start(); |
965 |
> |
|
966 |
> |
while (!thread.lockStarted) { |
967 |
> |
Thread.sleep(100); |
968 |
> |
} |
969 |
> |
|
970 |
|
lock.writeLock().lock(); |
971 |
< |
c.signal(); |
972 |
< |
lock.writeLock().unlock(); |
973 |
< |
assert(t.isInterrupted()); |
974 |
< |
t.join(SHORT_DELAY_MS); |
975 |
< |
assertFalse(t.isAlive()); |
976 |
< |
} |
977 |
< |
catch (Exception ex) { |
971 |
> |
try { |
972 |
> |
thread.interrupt(); |
973 |
> |
thread.canAwake = true; |
974 |
> |
c.signal(); |
975 |
> |
} finally { |
976 |
> |
lock.writeLock().unlock(); |
977 |
> |
} |
978 |
> |
|
979 |
> |
thread.join(); |
980 |
> |
assertTrue(thread.interrupted); |
981 |
> |
assertFalse(thread.isAlive()); |
982 |
> |
} catch (Exception ex) { |
983 |
|
unexpectedException(); |
984 |
|
} |
985 |
|
} |