ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Semaphore.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/Semaphore.java (file contents):
Revision 1.30 by dl, Fri Jan 2 00:38:33 2004 UTC vs.
Revision 1.31 by dl, Fri Jan 2 21:02:31 2004 UTC

# Line 138 | Line 138 | public class Semaphore implements java.i
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;
# Line 150 | Line 150 | public class Semaphore implements java.i
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))
# Line 166 | Line 166 | public class Semaphore implements java.i
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      /**
# Line 261 | Line 269 | public class Semaphore implements java.i
269       * otherwise.
270       */
271      public boolean tryAcquire() {
272 <        return sync.tryAcquireSharedState(true, 1) >= 0;
272 >        return sync.tryAcquireShared(true, 1) >= 0;
273      }
274  
275      /**
# Line 428 | Line 436 | public class Semaphore implements java.i
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      /**
# Line 527 | Line 535 | public class Semaphore implements java.i
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.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines