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.27 by dl, Mon Dec 29 16:38:06 2003 UTC vs.
Revision 1.28 by dl, Tue Dec 30 15:47:48 2003 UTC

# Line 124 | Line 124 | public class Semaphore implements java.i
124      private final Sync sync;
125  
126      /**
127 <     * Synchronization implementation for semaphore
127 >     * Synchronization implementation for semaphore.
128 >     * Uses AQS state to represent permits
129       */
130      private final static class Sync extends AbstractQueuedSynchronizer {
131          final boolean fair;
132          Sync(int permits, boolean fair) {
133              this.fair = fair;
134 <            state().set(permits);
134 >            set(permits);
135          }
136          
137          public int acquireSharedState(boolean isQueued, int acquires) {
137            final AtomicInteger perms = state();
138              if (!isQueued && fair && hasQueuedThreads())
139                  return -1;
140              for (;;) {
141 <                int available = perms.get();
141 >                int available = get();
142                  int remaining = available - acquires;
143                  if (remaining < 0 ||
144 <                    perms.compareAndSet(available, remaining))
144 >                    compareAndSet(available, remaining))
145                      return remaining;
146              }
147          }
148          
149          public boolean releaseSharedState(int releases) {
150            final AtomicInteger perms = state();
150              for (;;) {
151 <                int p = perms.get();
152 <                if (perms.compareAndSet(p, p + releases))
151 >                int p = get();
152 >                if (compareAndSet(p, p + releases))
153                      return true;
154              }
155          }
157        
158        public int acquireExclusiveState(boolean isQueued, int acquires) {
159            throw new UnsupportedOperationException();
160        }
161        
162        public final boolean releaseExclusiveState(int releases) {
163            throw new UnsupportedOperationException();
164        }
165        
166        public final void checkConditionAccess(Thread thread, boolean waiting) {
167            throw new UnsupportedOperationException();
168        }
156      }
157  
158      /**
# Line 488 | Line 475 | public class Semaphore implements java.i
475          return sync.acquireSharedTimed(permits, unit.toNanos(timeout));
476      }
477  
491
478      /**
479       * Releases the given number of permits, returning them to the semaphore.
480       * <p>Releases the given number of permits, increasing the number of
# Line 524 | Line 510 | public class Semaphore implements java.i
510       * @return the number of permits available in this semaphore.
511       */
512      public int availablePermits() {
513 <        return sync.state().get();
513 >        return sync.get();
514      }
515  
516      /**
# Line 539 | Line 525 | public class Semaphore implements java.i
525       */
526      protected void reducePermits(int reduction) {
527          if (reduction < 0) throw new IllegalArgumentException();
528 <        sync.state().getAndAdd(-reduction);
528 >        sync.getAndAdd(-reduction);
529      }
530  
531      /**
# Line 550 | Line 536 | public class Semaphore implements java.i
536          return sync.fair;
537      }
538  
553
539      /**
540       * Queries whether any threads are waiting to acquire. Note that
541       * because cancellations may occur at any time, a <tt>true</tt>
# Line 565 | Line 550 | public class Semaphore implements java.i
550          return sync.hasQueuedThreads();
551      }
552  
568
553      /**
554       * Returns an estimate of the number of threads waiting to
555       * acquire.  The value is only an estimate because the number of
# Line 592 | Line 576 | public class Semaphore implements java.i
576      protected Collection<Thread> getQueuedThreads() {
577          return sync.getQueuedThreads();
578      }
595    
579   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines