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) { |
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 compareAndSet(0, 1)? 0 : -1; |
37 |
> |
return compareAndSetState(0, 1); |
38 |
|
} |
39 |
|
|
40 |
|
public boolean releaseExclusiveState(int releases) { |
41 |
< |
set(0); |
41 |
> |
setState(0); |
42 |
|
return true; |
43 |
|
} |
44 |
|
|
45 |
|
public void checkConditionAccess(Thread thread, boolean waiting) { |
46 |
< |
if (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 |
< |
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) >= 0; |
59 |
> |
return sync.tryAcquireExclusiveState(false, 1); |
60 |
|
} |
61 |
|
public void lock() { |
62 |
|
sync.acquireExclusiveUninterruptibly(1); |
69 |
|
} |
70 |
|
public void unlock() { sync.releaseExclusive(1); } |
71 |
|
public Condition newCondition() { return sync.newCondition(); } |
72 |
< |
public boolean isLocked() { return sync.get() != 0; } |
72 |
> |
public boolean isLocked() { return sync.isLocked(); } |
73 |
|
public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); } |
74 |
|
} |
75 |
|
|