-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
15
-
generic
-
generic
ADDITIONAL SYSTEM INFORMATION :
Generic
A DESCRIPTION OF THE PROBLEM :
Currently the Semaphore allows to set initial number of permits and is using a hard-coded upper limit as 2^31:
(code snippet from JDK8 source):
protected final boolean tryReleaseShared(int releases) {
for (;;) {
int current = getState();
int next = current + releases;
if (next < current) // overflow <--- here is hardcoded limit.
throw new Error("Maximum permit count exceeded");
if (compareAndSetState(current, next))
return true;
}
I would be glad to see it to be possible to specifiy this limit in constructor.
Reasoning:
In case of binary semaphore or other semaphores the upper limit of permits is known at construction time. Letting it to be set in constructor allows to early capture "double release" bugs. Without it a single buggy code which will release twice may leave a binary semaphore "opened" forever. This bug will be rather tricky to find, because of a long distance in time and cause-result relationship. Setting limit to "1" in that case would easily pin-point it right in place.
Generic
A DESCRIPTION OF THE PROBLEM :
Currently the Semaphore allows to set initial number of permits and is using a hard-coded upper limit as 2^31:
(code snippet from JDK8 source):
protected final boolean tryReleaseShared(int releases) {
for (;;) {
int current = getState();
int next = current + releases;
if (next < current) // overflow <--- here is hardcoded limit.
throw new Error("Maximum permit count exceeded");
if (compareAndSetState(current, next))
return true;
}
I would be glad to see it to be possible to specifiy this limit in constructor.
Reasoning:
In case of binary semaphore or other semaphores the upper limit of permits is known at construction time. Letting it to be set in constructor allows to early capture "double release" bugs. Without it a single buggy code which will release twice may leave a binary semaphore "opened" forever. This bug will be rather tricky to find, because of a long distance in time and cause-result relationship. Setting limit to "1" in that case would easily pin-point it right in place.