9 |
|
import java.util.concurrent.locks.*; |
10 |
|
import java.util.concurrent.*; |
11 |
|
import java.io.*; |
12 |
+ |
import java.util.*; |
13 |
|
|
14 |
|
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
19 |
|
return new TestSuite(ReentrantReadWriteLockTest.class); |
20 |
|
} |
21 |
|
|
22 |
+ |
/** |
23 |
+ |
* A runnable calling lockInterruptibly |
24 |
+ |
*/ |
25 |
+ |
class InterruptibleLockRunnable implements Runnable { |
26 |
+ |
final ReentrantReadWriteLock lock; |
27 |
+ |
InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
28 |
+ |
public void run() { |
29 |
+ |
try { |
30 |
+ |
lock.writeLock().lockInterruptibly(); |
31 |
+ |
} catch(InterruptedException success){} |
32 |
+ |
} |
33 |
+ |
} |
34 |
|
|
35 |
< |
/* |
35 |
> |
|
36 |
> |
/** |
37 |
> |
* A runnable calling lockInterruptibly that expects to be |
38 |
> |
* interrupted |
39 |
> |
*/ |
40 |
> |
class InterruptedLockRunnable implements Runnable { |
41 |
> |
final ReentrantReadWriteLock lock; |
42 |
> |
InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
43 |
> |
public void run() { |
44 |
> |
try { |
45 |
> |
lock.writeLock().lockInterruptibly(); |
46 |
> |
threadShouldThrow(); |
47 |
> |
} catch(InterruptedException success){} |
48 |
> |
} |
49 |
> |
} |
50 |
> |
|
51 |
> |
/** |
52 |
> |
* Subclass to expose protected methods |
53 |
> |
*/ |
54 |
> |
static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock { |
55 |
> |
PublicReentrantReadWriteLock() { super(); } |
56 |
> |
public Collection<Thread> getQueuedThreads() { |
57 |
> |
return super.getQueuedThreads(); |
58 |
> |
} |
59 |
> |
public PublicCondition newCondition() { |
60 |
> |
return new PublicCondition(this); |
61 |
> |
} |
62 |
> |
|
63 |
> |
static class PublicCondition extends AbstractReentrantLock.ConditionObject { |
64 |
> |
PublicCondition(PublicReentrantReadWriteLock l) { super(l); } |
65 |
> |
public Collection<Thread> getWaitingThreads() { |
66 |
> |
return super.getWaitingThreads(); |
67 |
> |
} |
68 |
> |
} |
69 |
> |
|
70 |
> |
} |
71 |
> |
|
72 |
> |
/** |
73 |
> |
* Constructor sets given fairness, and is in unlocked state |
74 |
> |
*/ |
75 |
> |
public void testConstructor() { |
76 |
> |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
77 |
> |
assertFalse(rl.isFair()); |
78 |
> |
assertFalse(rl.isWriteLocked()); |
79 |
> |
assertEquals(0, rl.getReadLocks()); |
80 |
> |
ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true); |
81 |
> |
assertTrue(r2.isFair()); |
82 |
> |
assertFalse(r2.isWriteLocked()); |
83 |
> |
assertEquals(0, r2.getReadLocks()); |
84 |
> |
} |
85 |
> |
|
86 |
> |
/** |
87 |
|
* write-locking and read-locking an unlocked lock succeed |
88 |
|
*/ |
89 |
|
public void testLock() { |
90 |
|
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
91 |
|
rl.writeLock().lock(); |
92 |
+ |
assertTrue(rl.isWriteLocked()); |
93 |
+ |
assertTrue(rl.isWriteLockedByCurrentThread()); |
94 |
+ |
assertEquals(0, rl.getReadLocks()); |
95 |
|
rl.writeLock().unlock(); |
96 |
+ |
assertFalse(rl.isWriteLocked()); |
97 |
+ |
assertFalse(rl.isWriteLockedByCurrentThread()); |
98 |
+ |
assertEquals(0, rl.getReadLocks()); |
99 |
|
rl.readLock().lock(); |
100 |
+ |
assertFalse(rl.isWriteLocked()); |
101 |
+ |
assertFalse(rl.isWriteLockedByCurrentThread()); |
102 |
+ |
assertEquals(1, rl.getReadLocks()); |
103 |
|
rl.readLock().unlock(); |
104 |
+ |
assertFalse(rl.isWriteLocked()); |
105 |
+ |
assertFalse(rl.isWriteLockedByCurrentThread()); |
106 |
+ |
assertEquals(0, rl.getReadLocks()); |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
< |
/* |
110 |
> |
/** |
111 |
|
* locking an unlocked fair lock succeeds |
112 |
|
*/ |
113 |
|
public void testFairLock() { |
114 |
|
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true); |
115 |
|
rl.writeLock().lock(); |
116 |
+ |
assertTrue(rl.isWriteLocked()); |
117 |
+ |
assertTrue(rl.isWriteLockedByCurrentThread()); |
118 |
+ |
assertEquals(0, rl.getReadLocks()); |
119 |
|
rl.writeLock().unlock(); |
120 |
+ |
assertFalse(rl.isWriteLocked()); |
121 |
+ |
assertFalse(rl.isWriteLockedByCurrentThread()); |
122 |
+ |
assertEquals(0, rl.getReadLocks()); |
123 |
|
rl.readLock().lock(); |
124 |
+ |
assertFalse(rl.isWriteLocked()); |
125 |
+ |
assertFalse(rl.isWriteLockedByCurrentThread()); |
126 |
+ |
assertEquals(1, rl.getReadLocks()); |
127 |
|
rl.readLock().unlock(); |
128 |
+ |
assertFalse(rl.isWriteLocked()); |
129 |
+ |
assertFalse(rl.isWriteLockedByCurrentThread()); |
130 |
+ |
assertEquals(0, rl.getReadLocks()); |
131 |
+ |
} |
132 |
+ |
|
133 |
+ |
/** |
134 |
+ |
* getWriteHoldCount returns number of recursive holds |
135 |
+ |
*/ |
136 |
+ |
public void testGetHoldCount() { |
137 |
+ |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
138 |
+ |
for(int i = 1; i <= SIZE; i++) { |
139 |
+ |
lock.writeLock().lock(); |
140 |
+ |
assertEquals(i,lock.getWriteHoldCount()); |
141 |
+ |
} |
142 |
+ |
for(int i = SIZE; i > 0; i--) { |
143 |
+ |
lock.writeLock().unlock(); |
144 |
+ |
assertEquals(i-1,lock.getWriteHoldCount()); |
145 |
+ |
} |
146 |
|
} |
147 |
+ |
|
148 |
|
|
149 |
|
/** |
150 |
|
* write-unlocking an unlocked lock throws IllegalMonitorStateException |
854 |
|
} |
855 |
|
} |
856 |
|
|
857 |
+ |
/** |
858 |
+ |
* getQueueLength reports number of waiting threads |
859 |
+ |
*/ |
860 |
+ |
public void testGetQueueLength() { |
861 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
862 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
863 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
864 |
+ |
try { |
865 |
+ |
assertEquals(0, lock.getQueueLength()); |
866 |
+ |
lock.writeLock().lock(); |
867 |
+ |
t1.start(); |
868 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
869 |
+ |
assertEquals(1, lock.getQueueLength()); |
870 |
+ |
t2.start(); |
871 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
872 |
+ |
assertEquals(2, lock.getQueueLength()); |
873 |
+ |
t1.interrupt(); |
874 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
875 |
+ |
assertEquals(1, lock.getQueueLength()); |
876 |
+ |
lock.writeLock().unlock(); |
877 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
878 |
+ |
assertEquals(0, lock.getQueueLength()); |
879 |
+ |
t1.join(); |
880 |
+ |
t2.join(); |
881 |
+ |
} catch(Exception e){ |
882 |
+ |
unexpectedException(); |
883 |
+ |
} |
884 |
+ |
} |
885 |
+ |
|
886 |
+ |
/** |
887 |
+ |
* getQueuedThreads includes waiting threads |
888 |
+ |
*/ |
889 |
+ |
public void testGetQueuedThreads() { |
890 |
+ |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
891 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
892 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
893 |
+ |
try { |
894 |
+ |
assertTrue(lock.getQueuedThreads().isEmpty()); |
895 |
+ |
lock.writeLock().lock(); |
896 |
+ |
assertTrue(lock.getQueuedThreads().isEmpty()); |
897 |
+ |
t1.start(); |
898 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
899 |
+ |
assertTrue(lock.getQueuedThreads().contains(t1)); |
900 |
+ |
t2.start(); |
901 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
902 |
+ |
assertTrue(lock.getQueuedThreads().contains(t1)); |
903 |
+ |
assertTrue(lock.getQueuedThreads().contains(t2)); |
904 |
+ |
t1.interrupt(); |
905 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
906 |
+ |
assertFalse(lock.getQueuedThreads().contains(t1)); |
907 |
+ |
assertTrue(lock.getQueuedThreads().contains(t2)); |
908 |
+ |
lock.writeLock().unlock(); |
909 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
910 |
+ |
assertTrue(lock.getQueuedThreads().isEmpty()); |
911 |
+ |
t1.join(); |
912 |
+ |
t2.join(); |
913 |
+ |
} catch(Exception e){ |
914 |
+ |
unexpectedException(); |
915 |
+ |
} |
916 |
+ |
} |
917 |
+ |
|
918 |
+ |
/** |
919 |
+ |
* hasWaiters returns true when a thread is waiting, else false |
920 |
+ |
*/ |
921 |
+ |
public void testHasWaiters() { |
922 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
923 |
+ |
final AbstractReentrantLock.ConditionObject c = (AbstractReentrantLock.ConditionObject)(lock.writeLock().newCondition()); |
924 |
+ |
Thread t = new Thread(new Runnable() { |
925 |
+ |
public void run() { |
926 |
+ |
try { |
927 |
+ |
lock.writeLock().lock(); |
928 |
+ |
threadAssertFalse(c.hasWaiters()); |
929 |
+ |
threadAssertEquals(0, c.getWaitQueueLength()); |
930 |
+ |
c.await(); |
931 |
+ |
lock.writeLock().unlock(); |
932 |
+ |
} |
933 |
+ |
catch(InterruptedException e) { |
934 |
+ |
threadUnexpectedException(); |
935 |
+ |
} |
936 |
+ |
} |
937 |
+ |
}); |
938 |
+ |
|
939 |
+ |
try { |
940 |
+ |
t.start(); |
941 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
942 |
+ |
lock.writeLock().lock(); |
943 |
+ |
assertTrue(c.hasWaiters()); |
944 |
+ |
assertEquals(1, c.getWaitQueueLength()); |
945 |
+ |
c.signal(); |
946 |
+ |
lock.writeLock().unlock(); |
947 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
948 |
+ |
lock.writeLock().lock(); |
949 |
+ |
assertFalse(c.hasWaiters()); |
950 |
+ |
assertEquals(0, c.getWaitQueueLength()); |
951 |
+ |
lock.writeLock().unlock(); |
952 |
+ |
t.join(SHORT_DELAY_MS); |
953 |
+ |
assertFalse(t.isAlive()); |
954 |
+ |
} |
955 |
+ |
catch (Exception ex) { |
956 |
+ |
unexpectedException(); |
957 |
+ |
} |
958 |
+ |
} |
959 |
+ |
|
960 |
+ |
/** |
961 |
+ |
* getWaitQueueLength returns number of waiting threads |
962 |
+ |
*/ |
963 |
+ |
public void testGetWaitQueueLength() { |
964 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
965 |
+ |
final AbstractReentrantLock.ConditionObject c = (AbstractReentrantLock.ConditionObject)(lock.writeLock().newCondition()); |
966 |
+ |
Thread t1 = new Thread(new Runnable() { |
967 |
+ |
public void run() { |
968 |
+ |
try { |
969 |
+ |
lock.writeLock().lock(); |
970 |
+ |
threadAssertFalse(c.hasWaiters()); |
971 |
+ |
threadAssertEquals(0, c.getWaitQueueLength()); |
972 |
+ |
c.await(); |
973 |
+ |
lock.writeLock().unlock(); |
974 |
+ |
} |
975 |
+ |
catch(InterruptedException e) { |
976 |
+ |
threadUnexpectedException(); |
977 |
+ |
} |
978 |
+ |
} |
979 |
+ |
}); |
980 |
+ |
|
981 |
+ |
Thread t2 = new Thread(new Runnable() { |
982 |
+ |
public void run() { |
983 |
+ |
try { |
984 |
+ |
lock.writeLock().lock(); |
985 |
+ |
threadAssertTrue(c.hasWaiters()); |
986 |
+ |
threadAssertEquals(1, c.getWaitQueueLength()); |
987 |
+ |
c.await(); |
988 |
+ |
lock.writeLock().unlock(); |
989 |
+ |
} |
990 |
+ |
catch(InterruptedException e) { |
991 |
+ |
threadUnexpectedException(); |
992 |
+ |
} |
993 |
+ |
} |
994 |
+ |
}); |
995 |
+ |
|
996 |
+ |
try { |
997 |
+ |
t1.start(); |
998 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
999 |
+ |
t2.start(); |
1000 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1001 |
+ |
lock.writeLock().lock(); |
1002 |
+ |
assertTrue(c.hasWaiters()); |
1003 |
+ |
assertEquals(2, c.getWaitQueueLength()); |
1004 |
+ |
c.signalAll(); |
1005 |
+ |
lock.writeLock().unlock(); |
1006 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1007 |
+ |
lock.writeLock().lock(); |
1008 |
+ |
assertFalse(c.hasWaiters()); |
1009 |
+ |
assertEquals(0, c.getWaitQueueLength()); |
1010 |
+ |
lock.writeLock().unlock(); |
1011 |
+ |
t1.join(SHORT_DELAY_MS); |
1012 |
+ |
t2.join(SHORT_DELAY_MS); |
1013 |
+ |
assertFalse(t1.isAlive()); |
1014 |
+ |
assertFalse(t2.isAlive()); |
1015 |
+ |
} |
1016 |
+ |
catch (Exception ex) { |
1017 |
+ |
unexpectedException(); |
1018 |
+ |
} |
1019 |
+ |
} |
1020 |
+ |
|
1021 |
+ |
/** |
1022 |
+ |
* getWaitingThreads returns only and all waiting threads |
1023 |
+ |
*/ |
1024 |
+ |
public void testGetWaitingThreads() { |
1025 |
+ |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1026 |
+ |
final PublicReentrantReadWriteLock.PublicCondition c = (PublicReentrantReadWriteLock.PublicCondition)lock.newCondition(); |
1027 |
+ |
Thread t1 = new Thread(new Runnable() { |
1028 |
+ |
public void run() { |
1029 |
+ |
try { |
1030 |
+ |
lock.writeLock().lock(); |
1031 |
+ |
threadAssertTrue(c.getWaitingThreads().isEmpty()); |
1032 |
+ |
c.await(); |
1033 |
+ |
lock.writeLock().unlock(); |
1034 |
+ |
} |
1035 |
+ |
catch(InterruptedException e) { |
1036 |
+ |
threadUnexpectedException(); |
1037 |
+ |
} |
1038 |
+ |
} |
1039 |
+ |
}); |
1040 |
+ |
|
1041 |
+ |
Thread t2 = new Thread(new Runnable() { |
1042 |
+ |
public void run() { |
1043 |
+ |
try { |
1044 |
+ |
lock.writeLock().lock(); |
1045 |
+ |
threadAssertFalse(c.getWaitingThreads().isEmpty()); |
1046 |
+ |
c.await(); |
1047 |
+ |
lock.writeLock().unlock(); |
1048 |
+ |
} |
1049 |
+ |
catch(InterruptedException e) { |
1050 |
+ |
threadUnexpectedException(); |
1051 |
+ |
} |
1052 |
+ |
} |
1053 |
+ |
}); |
1054 |
+ |
|
1055 |
+ |
try { |
1056 |
+ |
lock.writeLock().lock(); |
1057 |
+ |
assertTrue(c.getWaitingThreads().isEmpty()); |
1058 |
+ |
lock.writeLock().unlock(); |
1059 |
+ |
t1.start(); |
1060 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1061 |
+ |
t2.start(); |
1062 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1063 |
+ |
lock.writeLock().lock(); |
1064 |
+ |
assertTrue(c.hasWaiters()); |
1065 |
+ |
assertTrue(c.getWaitingThreads().contains(t1)); |
1066 |
+ |
assertTrue(c.getWaitingThreads().contains(t2)); |
1067 |
+ |
c.signalAll(); |
1068 |
+ |
lock.writeLock().unlock(); |
1069 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1070 |
+ |
lock.writeLock().lock(); |
1071 |
+ |
assertFalse(c.hasWaiters()); |
1072 |
+ |
assertTrue(c.getWaitingThreads().isEmpty()); |
1073 |
+ |
lock.writeLock().unlock(); |
1074 |
+ |
t1.join(SHORT_DELAY_MS); |
1075 |
+ |
t2.join(SHORT_DELAY_MS); |
1076 |
+ |
assertFalse(t1.isAlive()); |
1077 |
+ |
assertFalse(t2.isAlive()); |
1078 |
+ |
} |
1079 |
+ |
catch (Exception ex) { |
1080 |
+ |
unexpectedException(); |
1081 |
+ |
} |
1082 |
+ |
} |
1083 |
|
|
1084 |
|
} |