138 |
|
return getState(); |
139 |
|
} |
140 |
|
|
141 |
< |
public int tryAcquireSharedState(boolean isQueued, int acquires) { |
141 |
> |
public int tryAcquireShared(boolean isQueued, int acquires) { |
142 |
|
for (;;) { |
143 |
< |
if (!isQueued && fair && hasQueuedThreads()) |
143 |
> |
if (!isQueued && fair && hasContended()) |
144 |
|
return -1; |
145 |
|
int available = getState(); |
146 |
|
int remaining = available - acquires; |
150 |
|
} |
151 |
|
} |
152 |
|
|
153 |
< |
public boolean releaseSharedState(int releases) { |
153 |
> |
protected boolean tryReleaseShared(int releases) { |
154 |
|
for (;;) { |
155 |
|
int p = getState(); |
156 |
|
if (compareAndSetState(p, p + releases)) |
166 |
|
return; |
167 |
|
} |
168 |
|
} |
169 |
+ |
|
170 |
+ |
int drainPermits() { |
171 |
+ |
for (;;) { |
172 |
+ |
int current = getState(); |
173 |
+ |
if (current == 0 || compareAndSetState(current, 0)) |
174 |
+ |
return current; |
175 |
+ |
} |
176 |
+ |
} |
177 |
|
} |
178 |
|
|
179 |
|
/** |
269 |
|
* otherwise. |
270 |
|
*/ |
271 |
|
public boolean tryAcquire() { |
272 |
< |
return sync.tryAcquireSharedState(true, 1) >= 0; |
272 |
> |
return sync.tryAcquireShared(true, 1) >= 0; |
273 |
|
} |
274 |
|
|
275 |
|
/** |
436 |
|
*/ |
437 |
|
public boolean tryAcquire(int permits) { |
438 |
|
if (permits < 0) throw new IllegalArgumentException(); |
439 |
< |
return sync.tryAcquireSharedState(true, permits) >= 0; |
439 |
> |
return sync.tryAcquireShared(true, permits) >= 0; |
440 |
|
} |
441 |
|
|
442 |
|
/** |
535 |
|
} |
536 |
|
|
537 |
|
/** |
538 |
+ |
* Acquire and return all permits that are immediately available. |
539 |
+ |
* @return the number of permits |
540 |
+ |
*/ |
541 |
+ |
public int drainPermits() { |
542 |
+ |
return sync.drainPermits(); |
543 |
+ |
} |
544 |
+ |
|
545 |
+ |
/** |
546 |
|
* Shrink the number of available permits by the indicated |
547 |
|
* reduction. This method can be useful in subclasses that |
548 |
< |
* use semaphores to track available resources that become |
548 |
> |
* use semaphores to track resources that become |
549 |
|
* unavailable. This method differs from <tt>acquire</tt> |
550 |
|
* in that it does not block waiting for permits to become |
551 |
|
* available. |