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 tryAcquireExclusiveState(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); |
41 |
> |
setState(0); |
42 |
|
return true; |
43 |
|
} |
44 |
|
|
43 |
– |
public int acquireSharedState(boolean isQueued, int acquires, Thread current) { |
44 |
– |
throw new UnsupportedOperationException(); |
45 |
– |
} |
46 |
– |
|
47 |
– |
public boolean releaseSharedState(int releases) { |
48 |
– |
throw new UnsupportedOperationException(); |
49 |
– |
} |
50 |
– |
|
45 |
|
public void checkConditionAccess(Thread thread, boolean waiting) { |
46 |
< |
if (state().get() == 0) throw new IllegalMonitorStateException(); |
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 |
|
private final Sync sync = new Sync(); |
58 |
|
public boolean tryLock() { |
59 |
< |
return sync.acquireExclusiveState(false, 1, null) >= 0; |
59 |
> |
return sync.tryAcquireExclusiveState(false, 1); |
60 |
|
} |
61 |
|
public void lock() { |
62 |
< |
if (!tryLock()) sync.acquireExclusiveUninterruptibly(1); |
62 |
> |
sync.acquireExclusiveUninterruptibly(1); |
63 |
|
} |
64 |
|
public void lockInterruptibly() throws InterruptedException { |
65 |
|
sync.acquireExclusiveInterruptibly(1); |
69 |
|
} |
70 |
|
public void unlock() { sync.releaseExclusive(1); } |
71 |
|
public Condition newCondition() { return sync.newCondition(); } |
72 |
< |
public boolean isLocked() { return sync.state().get() != 0; } |
72 |
> |
public boolean isLocked() { return sync.isLocked(); } |
73 |
|
public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); } |
74 |
|
} |
75 |
|
|
113 |
|
} |
114 |
|
|
115 |
|
/** |
116 |
< |
* trylock on an unlocked lock succeeds |
116 |
> |
* tryLock on an unlocked lock succeeds |
117 |
|
*/ |
118 |
|
public void testTryLock() { |
119 |
|
Mutex rl = new Mutex(); |
152 |
|
} |
153 |
|
|
154 |
|
/** |
155 |
< |
* timed trylock is interruptible. |
155 |
> |
* timed tryLock is interruptible. |
156 |
|
*/ |
157 |
|
public void testInterruptedException2() { |
158 |
|
final Mutex lock = new Mutex(); |
175 |
|
|
176 |
|
|
177 |
|
/** |
178 |
< |
* Trylock on a locked lock fails |
178 |
> |
* TryLock on a locked lock fails |
179 |
|
*/ |
180 |
|
public void testTryLockWhenLocked() { |
181 |
|
final Mutex lock = new Mutex(); |
195 |
|
} |
196 |
|
|
197 |
|
/** |
198 |
< |
* Timed trylock on a locked lock times out |
198 |
> |
* Timed tryLock on a locked lock times out |
199 |
|
*/ |
200 |
|
public void testTryLock_Timeout() { |
201 |
|
final Mutex lock = new Mutex(); |