Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8255491

Semaphore should allow to user set a maximum number of permits.

XMLWordPrintable

      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.


            dl Doug Lea
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: