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(boolean isQueued, 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(false, 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); |
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 |
|
} |
77 |
|
|
78 |
|
/** |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
< |
* trylock on an unlocked lock succeeds |
118 |
> |
* tryLock on an unlocked lock succeeds |
119 |
|
*/ |
120 |
|
public void testTryLock() { |
121 |
|
Mutex rl = new Mutex(); |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
+ |
|
157 |
+ |
/** |
158 |
+ |
* hasContended reports whether there has been contention |
159 |
+ |
*/ |
160 |
+ |
public void testhasContended() { |
161 |
+ |
final Mutex lock = new Mutex(); |
162 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
163 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
164 |
+ |
try { |
165 |
+ |
assertFalse(lock.hasContended()); |
166 |
+ |
lock.lock(); |
167 |
+ |
t1.start(); |
168 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
169 |
+ |
assertTrue(lock.hasContended()); |
170 |
+ |
t2.start(); |
171 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
172 |
+ |
assertTrue(lock.hasContended()); |
173 |
+ |
t1.interrupt(); |
174 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
175 |
+ |
assertTrue(lock.hasContended()); |
176 |
+ |
lock.unlock(); |
177 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
178 |
+ |
assertTrue(lock.hasContended()); |
179 |
+ |
t1.join(); |
180 |
+ |
t2.join(); |
181 |
+ |
} catch(Exception e){ |
182 |
+ |
unexpectedException(); |
183 |
+ |
} |
184 |
+ |
} |
185 |
+ |
|
186 |
|
/** |
187 |
< |
* timed trylock is interruptible. |
187 |
> |
* timed tryLock is interruptible. |
188 |
|
*/ |
189 |
|
public void testInterruptedException2() { |
190 |
|
final Mutex lock = new Mutex(); |
207 |
|
|
208 |
|
|
209 |
|
/** |
210 |
< |
* Trylock on a locked lock fails |
210 |
> |
* TryLock on a locked lock fails |
211 |
|
*/ |
212 |
|
public void testTryLockWhenLocked() { |
213 |
|
final Mutex lock = new Mutex(); |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
< |
* Timed trylock on a locked lock times out |
230 |
> |
* Timed tryLock on a locked lock times out |
231 |
|
*/ |
232 |
|
public void testTryLock_Timeout() { |
233 |
|
final Mutex lock = new Mutex(); |