120 |
|
|
121 |
|
public class Semaphore implements java.io.Serializable { |
122 |
|
private static final long serialVersionUID = -3222578661600680210L; |
123 |
< |
/** Sync mechanics via AbstractQueuedSynchronizer subclass */ |
123 |
> |
/** All mechanics via AbstractQueuedSynchronizer subclass */ |
124 |
|
private final Sync sync; |
125 |
|
|
126 |
< |
|
126 |
> |
/** |
127 |
> |
* Synchronization implementation for semaphore |
128 |
> |
*/ |
129 |
|
private final static class Sync extends AbstractQueuedSynchronizer { |
130 |
|
final boolean fair; |
131 |
|
Sync(int permits, boolean fair) { |
132 |
|
this.fair = fair; |
133 |
< |
getState().set(permits); |
133 |
> |
state().set(permits); |
134 |
|
} |
135 |
|
|
136 |
|
public final int acquireSharedState(boolean isQueued, int acquires, |
137 |
|
Thread current) { |
138 |
< |
final AtomicInteger perms = getState(); |
139 |
< |
if (!isQueued && fair && hasWaiters()) |
138 |
> |
final AtomicInteger perms = state(); |
139 |
> |
if (!isQueued && fair && hasQueuedThreads()) |
140 |
|
return -1; |
141 |
|
for (;;) { |
142 |
|
int available = perms.get(); |
148 |
|
} |
149 |
|
|
150 |
|
public final boolean releaseSharedState(int releases) { |
151 |
< |
final AtomicInteger perms = getState(); |
151 |
> |
final AtomicInteger perms = state(); |
152 |
|
for (;;) { |
153 |
|
int p = perms.get(); |
154 |
|
if (perms.compareAndSet(p, p + releases)) |
169 |
|
public final void checkConditionAccess(Thread thread, boolean waiting) { |
170 |
|
throw new UnsupportedOperationException(); |
171 |
|
} |
170 |
– |
|
172 |
|
} |
173 |
|
|
174 |
|
/** |
507 |
|
* @return the number of permits available in this semaphore. |
508 |
|
*/ |
509 |
|
public int availablePermits() { |
510 |
< |
return sync.getState().get(); |
510 |
> |
return sync.state().get(); |
511 |
|
} |
512 |
|
|
513 |
|
/** |
522 |
|
*/ |
523 |
|
protected void reducePermits(int reduction) { |
524 |
|
if (reduction < 0) throw new IllegalArgumentException(); |
525 |
< |
sync.getState().getAndAdd(-reduction); |
525 |
> |
sync.state().getAndAdd(-reduction); |
526 |
|
} |
527 |
|
|
528 |
|
/** |
544 |
|
* @return true if there may be other threads waiting to acquire |
545 |
|
* the lock. |
546 |
|
*/ |
547 |
< |
public final boolean hasWaiters() { |
548 |
< |
return sync.hasWaiters(); |
547 |
> |
public final boolean hasQueuedThreads() { |
548 |
> |
return sync.hasQueuedThreads(); |
549 |
|
} |
550 |
|
|
551 |
|
|