162 |
|
|
163 |
|
protected final boolean tryReleaseShared(int releases) { |
164 |
|
for (;;) { |
165 |
< |
int p = getState(); |
166 |
< |
if (compareAndSetState(p, p + releases)) |
165 |
> |
int current = getState(); |
166 |
> |
int next = current + releases; |
167 |
> |
if (next < current) // overflow |
168 |
> |
throw new Error("Maximum permit count exceeded"); |
169 |
> |
if (compareAndSetState(current, next)) |
170 |
|
return true; |
171 |
|
} |
172 |
|
} |
175 |
|
for (;;) { |
176 |
|
int current = getState(); |
177 |
|
int next = current - reductions; |
178 |
+ |
if (next > current) // underflow |
179 |
+ |
throw new Error("Permit count underflow"); |
180 |
|
if (compareAndSetState(current, next)) |
181 |
|
return; |
182 |
|
} |