ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java (file contents):
Revision 1.2 by dl, Mon Dec 29 01:18:40 2003 UTC vs.
Revision 1.11 by dl, Thu Jan 8 01:29:46 2004 UTC

# Line 30 | Line 30 | public class AbstractQueuedSynchronizerT
30       */
31      static class Mutex implements Lock, java.io.Serializable {
32          private static class Sync extends AbstractQueuedSynchronizer {
33 <            public int acquireExclusiveState(boolean isQueued, int acquires, Thread current) {
33 >            boolean isLocked() { return getState() == 1; }
34 >
35 >            public boolean tryAcquireExclusive(int acquires) {
36                  assert acquires == 1; // Does not use multiple acquires
37 <                return state().compareAndSet(0, 1)? 0 : -1;
37 >                return compareAndSetState(0, 1);
38              }
39              
40 <            public boolean releaseExclusiveState(int releases) {
41 <                state().set(0);
40 >            public boolean tryReleaseExclusive(int releases) {
41 >                setState(0);
42                  return true;
43              }
44              
45 <            public int acquireSharedState(boolean isQueued, int acquires, Thread current) {
46 <                throw new UnsupportedOperationException();
45 <            }
46 <            
47 <            public boolean releaseSharedState(int releases) {
48 <                throw new UnsupportedOperationException();
49 <            }
50 <            
51 <            public void checkConditionAccess(Thread thread, boolean waiting) {
52 <                if (state().get() == 0) throw new IllegalMonitorStateException();
45 >            public void checkConditionAccess(Thread thread) {
46 >                if (getState() == 0) throw new IllegalMonitorStateException();
47              }
48              
49              Condition newCondition() { return new ConditionObject(); }
50              
51              private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
52                  s.defaultReadObject();
53 <                state().set(0); // reset to unlocked state
53 >                setState(0); // reset to unlocked state
54              }
55 +
56          }
57 <        
57 >        
58          private final Sync sync = new Sync();
59          public boolean tryLock() {
60 <            return sync.acquireExclusiveState(false, 1, null) >= 0;
60 >            return sync.tryAcquireExclusive(1);
61          }
62          public void lock() {
63 <            if (!tryLock()) sync.acquireExclusiveUninterruptibly(1);
63 >            sync.acquireExclusiveUninterruptibly(1);
64          }
65          public void lockInterruptibly() throws InterruptedException {
66              sync.acquireExclusiveInterruptibly(1);
# Line 75 | Line 70 | public class AbstractQueuedSynchronizerT
70          }
71          public void unlock() { sync.releaseExclusive(1); }
72          public Condition newCondition() { return sync.newCondition(); }
73 <        public boolean isLocked() { return sync.state().get() != 0; }
73 >        public boolean isLocked() { return sync.isLocked(); }
74          public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); }
75 +        public boolean hasContended() { return sync.hasContended(); }
76 +        public boolean isQueued(Thread t) { return sync.isQueued(t); }
77      }
78  
79      /**
# Line 119 | Line 116 | public class AbstractQueuedSynchronizerT
116      }
117  
118      /**
119 <     * trylock on an unlocked lock succeeds
119 >     * tryLock on an unlocked lock succeeds
120       */
121      public void testTryLock() {
122          Mutex rl = new Mutex();
# Line 158 | Line 155 | public class AbstractQueuedSynchronizerT
155      }
156  
157      /**
158 <     * timed trylock is interruptible.
158 >     * isQueued(null) throws NPE
159 >     */
160 >    public void testIsQueuedNPE() {
161 >        final Mutex lock = new Mutex();
162 >        try {
163 >            lock.isQueued(null);
164 >            shouldThrow();
165 >        } catch (NullPointerException success) {
166 >        }
167 >    }
168 >
169 >    /**
170 >     * isQueued reports whether a thread is queued.
171 >     */
172 >    public void testIsQueued() {
173 >        final Mutex lock = new Mutex();
174 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
175 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
176 >        try {
177 >            assertFalse(lock.isQueued(t1));
178 >            assertFalse(lock.isQueued(t2));
179 >            lock.lock();
180 >            t1.start();
181 >            Thread.sleep(SHORT_DELAY_MS);
182 >            assertTrue(lock.isQueued(t1));
183 >            t2.start();
184 >            Thread.sleep(SHORT_DELAY_MS);
185 >            assertTrue(lock.isQueued(t1));
186 >            assertTrue(lock.isQueued(t2));
187 >            t1.interrupt();
188 >            Thread.sleep(SHORT_DELAY_MS);
189 >            assertFalse(lock.isQueued(t1));
190 >            assertTrue(lock.isQueued(t2));
191 >            lock.unlock();
192 >            Thread.sleep(SHORT_DELAY_MS);
193 >            assertFalse(lock.isQueued(t1));
194 >            assertFalse(lock.isQueued(t2));
195 >            t1.join();
196 >            t2.join();
197 >        } catch(Exception e){
198 >            unexpectedException();
199 >        }
200 >    }
201 >
202 >
203 >    /**
204 >     * hasContended reports whether there has been contention
205 >     */
206 >    public void testhasContended() {
207 >        final Mutex lock = new Mutex();
208 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
209 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
210 >        try {
211 >            assertFalse(lock.hasContended());
212 >            lock.lock();
213 >            t1.start();
214 >            Thread.sleep(SHORT_DELAY_MS);
215 >            assertTrue(lock.hasContended());
216 >            t2.start();
217 >            Thread.sleep(SHORT_DELAY_MS);
218 >            assertTrue(lock.hasContended());
219 >            t1.interrupt();
220 >            Thread.sleep(SHORT_DELAY_MS);
221 >            assertTrue(lock.hasContended());
222 >            lock.unlock();
223 >            Thread.sleep(SHORT_DELAY_MS);
224 >            assertTrue(lock.hasContended());
225 >            t1.join();
226 >            t2.join();
227 >        } catch(Exception e){
228 >            unexpectedException();
229 >        }
230 >    }
231 >
232 >    /**
233 >     * timed tryLock is interruptible.
234       */
235      public void testInterruptedException2() {
236          final Mutex lock = new Mutex();
# Line 181 | Line 253 | public class AbstractQueuedSynchronizerT
253  
254  
255      /**
256 <     * Trylock on a locked lock fails
256 >     * TryLock on a locked lock fails
257       */
258      public void testTryLockWhenLocked() {
259          final Mutex lock = new Mutex();
# Line 201 | Line 273 | public class AbstractQueuedSynchronizerT
273      }
274  
275      /**
276 <     * Timed trylock on a locked lock times out
276 >     * Timed tryLock on a locked lock times out
277       */
278      public void testTryLock_Timeout() {
279          final Mutex lock = new Mutex();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines