124 |
|
private final Sync sync; |
125 |
|
|
126 |
|
/** |
127 |
< |
* Synchronization implementation for semaphore |
127 |
> |
* Synchronization implementation for semaphore. |
128 |
> |
* Uses AQS state to represent permits |
129 |
|
*/ |
130 |
|
private final static class Sync extends AbstractQueuedSynchronizer { |
131 |
|
final boolean fair; |
132 |
|
Sync(int permits, boolean fair) { |
133 |
|
this.fair = fair; |
134 |
< |
state().set(permits); |
134 |
> |
set(permits); |
135 |
|
} |
136 |
|
|
137 |
|
public int acquireSharedState(boolean isQueued, int acquires) { |
137 |
– |
final AtomicInteger perms = state(); |
138 |
|
if (!isQueued && fair && hasQueuedThreads()) |
139 |
|
return -1; |
140 |
|
for (;;) { |
141 |
< |
int available = perms.get(); |
141 |
> |
int available = get(); |
142 |
|
int remaining = available - acquires; |
143 |
|
if (remaining < 0 || |
144 |
< |
perms.compareAndSet(available, remaining)) |
144 |
> |
compareAndSet(available, remaining)) |
145 |
|
return remaining; |
146 |
|
} |
147 |
|
} |
148 |
|
|
149 |
|
public boolean releaseSharedState(int releases) { |
150 |
– |
final AtomicInteger perms = state(); |
150 |
|
for (;;) { |
151 |
< |
int p = perms.get(); |
152 |
< |
if (perms.compareAndSet(p, p + releases)) |
151 |
> |
int p = get(); |
152 |
> |
if (compareAndSet(p, p + releases)) |
153 |
|
return true; |
154 |
|
} |
155 |
|
} |
157 |
– |
|
158 |
– |
public int acquireExclusiveState(boolean isQueued, int acquires) { |
159 |
– |
throw new UnsupportedOperationException(); |
160 |
– |
} |
161 |
– |
|
162 |
– |
public final boolean releaseExclusiveState(int releases) { |
163 |
– |
throw new UnsupportedOperationException(); |
164 |
– |
} |
165 |
– |
|
166 |
– |
public final void checkConditionAccess(Thread thread, boolean waiting) { |
167 |
– |
throw new UnsupportedOperationException(); |
168 |
– |
} |
156 |
|
} |
157 |
|
|
158 |
|
/** |
475 |
|
return sync.acquireSharedTimed(permits, unit.toNanos(timeout)); |
476 |
|
} |
477 |
|
|
491 |
– |
|
478 |
|
/** |
479 |
|
* Releases the given number of permits, returning them to the semaphore. |
480 |
|
* <p>Releases the given number of permits, increasing the number of |
510 |
|
* @return the number of permits available in this semaphore. |
511 |
|
*/ |
512 |
|
public int availablePermits() { |
513 |
< |
return sync.state().get(); |
513 |
> |
return sync.get(); |
514 |
|
} |
515 |
|
|
516 |
|
/** |
525 |
|
*/ |
526 |
|
protected void reducePermits(int reduction) { |
527 |
|
if (reduction < 0) throw new IllegalArgumentException(); |
528 |
< |
sync.state().getAndAdd(-reduction); |
528 |
> |
sync.getAndAdd(-reduction); |
529 |
|
} |
530 |
|
|
531 |
|
/** |
536 |
|
return sync.fair; |
537 |
|
} |
538 |
|
|
553 |
– |
|
539 |
|
/** |
540 |
|
* Queries whether any threads are waiting to acquire. Note that |
541 |
|
* because cancellations may occur at any time, a <tt>true</tt> |
550 |
|
return sync.hasQueuedThreads(); |
551 |
|
} |
552 |
|
|
568 |
– |
|
553 |
|
/** |
554 |
|
* Returns an estimate of the number of threads waiting to |
555 |
|
* acquire. The value is only an estimate because the number of |
576 |
|
protected Collection<Thread> getQueuedThreads() { |
577 |
|
return sync.getQueuedThreads(); |
578 |
|
} |
595 |
– |
|
579 |
|
} |