ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Semaphore.java
Revision: 1.16
Committed: Mon Nov 3 13:49:56 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.15: +544 -124 lines
Log Message:
Merged FairSemaphore into Semaphore

File Contents

# User Rev Content
1 dl 1.2 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain. Use, modify, and
4     * redistribute this code in any way without acknowledgement.
5     */
6    
7 tim 1.1 package java.util.concurrent;
8 dl 1.16 import java.util.*;
9 dl 1.5 import java.util.concurrent.locks.*;
10 dl 1.16 import java.util.concurrent.atomic.*;
11 tim 1.1
12     /**
13     * A counting semaphore. Conceptually, a semaphore maintains a set of
14     * permits. Each {@link #acquire} blocks if necessary until a permit is
15     * available, and then takes it. Each {@link #release} adds a permit,
16     * potentially releasing a blocking acquirer.
17     * However, no actual permit objects are used; the <tt>Semaphore</tt> just
18     * keeps a count of the number available and acts accordingly.
19     *
20 dl 1.16 * <p>Semaphores are often used to restrict the number of threads than can
21 tim 1.1 * access some (physical or logical) resource. For example, here is
22     * a class that uses a semaphore to control access to a pool of items:
23     * <pre>
24     * class Pool {
25     * private static final MAX_AVAILABLE = 100;
26 dl 1.16 * private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);
27 tim 1.1 *
28     * public Object getItem() throws InterruptedException {
29     * available.acquire();
30     * return getNextAvailableItem();
31     * }
32     *
33     * public void putItem(Object x) {
34     * if (markAsUnused(x))
35     * available.release();
36     * }
37     *
38     * // Not a particularly efficient data structure; just for demo
39     *
40     * protected Object[] items = ... whatever kinds of items being managed
41     * protected boolean[] used = new boolean[MAX_AVAILABLE];
42     *
43     * protected synchronized Object getNextAvailableItem() {
44     * for (int i = 0; i < MAX_AVAILABLE; ++i) {
45     * if (!used[i]) {
46     * used[i] = true;
47     * return items[i];
48     * }
49     * }
50     * return null; // not reached
51     * }
52     *
53     * protected synchronized boolean markAsUnused(Object item) {
54     * for (int i = 0; i < MAX_AVAILABLE; ++i) {
55     * if (item == items[i]) {
56     * if (used[i]) {
57     * used[i] = false;
58     * return true;
59 tim 1.8 * } else
60 tim 1.1 * return false;
61     * }
62     * }
63     * return false;
64     * }
65     *
66     * }
67     * </pre>
68     *
69 dl 1.16 * <p>Before obtaining an item each thread must acquire a permit from
70     * the semaphore, guaranteeing that an item is available for use. When
71     * the thread has finished with the item it is returned back to the
72     * pool and a permit is returned to the semaphore, allowing another
73     * thread to acquire that item. Note that no synchronization lock is
74     * held when {@link #acquire} is called as that would prevent an item
75     * from being returned to the pool. The semaphore encapsulates the
76     * synchronization needed to restrict access to the pool, separately
77     * from any synchronization needed to maintain the consistency of the
78     * pool itself.
79     *
80     * <p>A semaphore initialized to one, and which is used such that it
81     * only has at most one permit available, can serve as a mutual
82     * exclusion lock. This is more commonly known as a <em>binary
83     * semaphore</em>, because it only has two states: one permit
84     * available, or zero permits available. When used in this way, the
85     * binary semaphore has the property (unlike many {@link Lock}
86     * implementations, that the &quot;lock&quot; can be released by a
87     * thread other than the owner (as semaphores have no notion of
88     * ownership). This can be useful in some specialized contexts, such
89     * as deadlock recovery.
90     *
91     * <p> The constructor for this class accepts a <em>fairness</em>
92     * parameter. When set false, this class makes no guarantees about the
93     * order in which threads acquire permits. In particular, barging is
94     * permitted, that is, a thread invoking {@link #acquire} can be
95     * allocated a permit ahead of a thread that has been waiting. When
96     * fairness is set true, the semaphore guarantees that threads
97     * invoking any of the {@link #acquire() acquire} methods are
98     * allocated permits in the order in which their invocation of those
99     * methods was processed (first-in-first-out; FIFO). Note that FIFO
100     * ordering necessarily applies to specific internal points of
101     * execution within these methods. So, it is possible for one thread
102     * to invoke <tt>acquire</tt> before another, but reach the ordering
103     * point after the other, and similarly upon return from the method.
104     *
105     * <p>Generally, semaphores used to control resource access should be
106     * initialized as fair, to ensure that no thread is starved out from
107     * accessing a resource. When using semaphores for other kinds of
108     * synchronization control, the throughput advantages of non-fair
109     * ordering often outweigh fairness considerations.
110     *
111     * <p>This class also provides convenience methods to {@link
112     * #acquire(int) acquire} and {@link #release(int) release} multiple
113     * permits at a time. Beware of the increased risk of indefinite
114     * postponement when these methods are used without fairness set true,
115 tim 1.1 *
116     * @since 1.5
117 dl 1.4 * @author Doug Lea
118 tim 1.1 *
119     */
120 dl 1.16
121 dl 1.2 public class Semaphore implements java.io.Serializable {
122 dl 1.16 /*
123     * The underlying algorithm here is a simplified adaptation of
124     * that used for ReentrantLock. See the internal documentation of
125     * ReentrantLock for detailed explanation.
126     */
127    
128     private static final long serialVersionUID = -3222578661600680210L;
129 dl 1.9
130 dl 1.16 /** Node status value to indicate thread has cancelled */
131     private static final int CANCELLED = 1;
132     /** Node status value to indicate successor needs unparking */
133     private static final int SIGNAL = -1;
134     /** Node class for waiting threads */
135     private static class Node {
136     volatile int status;
137     volatile Node prev;
138     volatile Node next;
139     Thread thread;
140     Node(Thread t) { thread = t; }
141     }
142 dl 1.2
143 dl 1.16 /** Number of available permits held in a separate AtomicInteger */
144     private final AtomicInteger perms;
145     /** Head of the wait queue, lazily initialized. */
146     private transient volatile Node head;
147     /** Tail of the wait queue, lazily initialized. */
148     private transient volatile Node tail;
149     /** true if barging disabled */
150     private final boolean fair;
151    
152     // Atomic update support
153    
154     private static final
155     AtomicReferenceFieldUpdater<Semaphore, Node> tailUpdater =
156     AtomicReferenceFieldUpdater.<Semaphore, Node>newUpdater
157     (Semaphore.class, Node.class, "tail");
158     private static final
159     AtomicReferenceFieldUpdater<Semaphore, Node> headUpdater =
160     AtomicReferenceFieldUpdater.<Semaphore, Node>newUpdater
161     (Semaphore.class, Node.class, "head");
162     private static final
163     AtomicIntegerFieldUpdater<Node> statusUpdater =
164     AtomicIntegerFieldUpdater.<Node>newUpdater
165     (Node.class, "status");
166 dl 1.2
167 tim 1.1
168 dl 1.16 /**
169     * Insert node into queue, initializing head and tail if necessary.
170     * @param node the node to insert
171 dl 1.4 */
172 dl 1.16 private void enq(Node node) {
173     Node t = tail;
174     if (t == null) { // Must initialize first
175     Node h = new Node(null);
176     while ((t = tail) == null) {
177     if (headUpdater.compareAndSet(this, null, h))
178     tail = h;
179     }
180     }
181    
182     for (;;) {
183     node.prev = t; // Prev field must be valid before/upon CAS
184     if (tailUpdater.compareAndSet(this, t, node)) {
185     t.next = node; // Next field assignment lags CAS
186     return;
187     }
188     t = tail;
189     }
190     }
191    
192     /**
193     * Unblock the successor of node
194     * @param node the node
195     */
196     private void unparkSuccessor(Node node) {
197     statusUpdater.compareAndSet(node, SIGNAL, 0);
198     Node s = node.next;
199     if (s == null || s.status == CANCELLED) {
200     s = tail;
201     if (s != null && s != node) {
202     Node p = s.prev;
203     while (p != null && p != node) {
204     if (p.status != CANCELLED)
205     s = p;
206     p = p.prev;
207     }
208     }
209     }
210     if (s != null)
211     LockSupport.unpark(s.thread);
212     }
213    
214    
215     /**
216     * Internal version of tryAcquire returning number of remaining
217     * permits, which is nonnegative only if the acquire succeeded.
218     * @param permits requested number of permits
219     * @return remaining number of permits
220     */
221     private int doTryAcquire(int permits) {
222     for (;;) {
223     int available = perms.get();
224     int remaining = available - permits;
225     if (remaining < 0 ||
226     perms.compareAndSet(available, remaining))
227     return remaining;
228     }
229     }
230    
231     /**
232     * Main code for untimed acquires.
233     * @param permits number of permits requested
234     * @param interrupts interrupt control: -1 for abort on interrupt,
235     * 0 for continue on interrupt
236     * @return true if lock acquired (can be false only if interruptible)
237     */
238     private boolean doAcquire(int permits, int interrupts) {
239     // Fast path bypasses queue
240     if ((!fair || head == tail) && doTryAcquire(permits) >= 0)
241     return true;
242     Thread current = Thread.currentThread();
243     Node node = new Node(current);
244     // Retry fast path before enqueuing
245     if (!fair && doTryAcquire(permits) >= 0)
246     return true;
247     enq(node);
248    
249     for (;;) {
250     Node p = node.prev;
251     if (p == head) {
252     int remaining = doTryAcquire(permits);
253     if (remaining >= 0) {
254     p.next = null;
255     node.thread = null;
256     node.prev = null;
257     head = node;
258     // if still some permits left, wake up successor
259     if (remaining > 0 && node.status < 0)
260     unparkSuccessor(node);
261     if (interrupts > 0) // Re-interrupt on normal exit
262     current.interrupt();
263     return true;
264     }
265     }
266     int status = p.status;
267     if (status == 0)
268     statusUpdater.compareAndSet(p, 0, SIGNAL);
269     else if (status == CANCELLED)
270     node.prev = p.prev;
271     else {
272     assert (status == SIGNAL);
273     LockSupport.park();
274     if (Thread.interrupted()) {
275     if (interrupts < 0) {
276     node.thread = null;
277     node.status = CANCELLED;
278     unparkSuccessor(node);
279     return false;
280     }
281     interrupts = 1; // set to re-interrupt on exit
282     }
283     }
284     }
285     }
286    
287     /**
288     * Main code for timed acquires. Same as doAcquire but with
289     * interspersed time checks.
290     * @param permits number of permits requested
291     * @param nanos timeout in nanosecs
292     * @return true if lock acquired
293     */
294     private boolean doTimedAcquire(int permits, long nanos) throws InterruptedException {
295     if ((!fair || head == tail) && doTryAcquire(permits) >= 0)
296     return true;
297     Thread current = Thread.currentThread();
298     long lastTime = System.nanoTime();
299     Node node = new Node(current);
300     // Retry fast path before enqueuing
301     if (!fair && doTryAcquire(permits) >= 0)
302     return true;
303     enq(node);
304    
305     for (;;) {
306     Node p = node.prev;
307     if (p == head) {
308     int remaining = doTryAcquire(permits);
309     if (remaining >= 0) {
310     p.next = null;
311     node.thread = null;
312     node.prev = null;
313     head = node;
314     if (remaining > 0 && node.status < 0)
315     unparkSuccessor(node);
316     return true;
317     }
318     }
319     if (nanos <= 0L) {
320     node.thread = null;
321     node.status = CANCELLED;
322     unparkSuccessor(node);
323     return false;
324     }
325    
326     int status = p.status;
327     if (status == 0)
328     statusUpdater.compareAndSet(p, 0, SIGNAL);
329     else if (status == CANCELLED)
330     node.prev = p.prev;
331     else {
332     LockSupport.parkNanos(nanos);
333     if (Thread.interrupted()) {
334     node.thread = null;
335     node.status = CANCELLED;
336     unparkSuccessor(node);
337     throw new InterruptedException();
338     }
339     long now = System.nanoTime();
340     nanos -= now - lastTime;
341     lastTime = now;
342     }
343     }
344     }
345    
346     /**
347     * Internal version of release
348     */
349     private void doRelease(int permits) {
350     for (;;) {
351     int p = perms.get();
352     if (perms.compareAndSet(p, p + permits)) {
353     Node h = head;
354     if (h != null && h.status < 0)
355     unparkSuccessor(h);
356     return;
357     }
358     }
359 dl 1.2 }
360 tim 1.1
361     /**
362     * Construct a <tt>Semaphore</tt> with the given number of
363 dl 1.16 * permits and the given fairness setting
364     * @param permits the initial number of permits available. This
365     * value may be negative, in which case releases must
366     * occur before any acquires will be granted.
367     * @param fair true if this semaphore will guarantee first-in
368     * first-out granting of permits under contention, else false.
369 tim 1.1 */
370 dl 1.16 public Semaphore(int permits, boolean fair) {
371     this.fair = fair;
372     perms = new AtomicInteger(permits);
373 tim 1.1 }
374    
375     /**
376     * Acquires a permit from this semaphore, blocking until one is
377     * available, or the thread is {@link Thread#interrupt interrupted}.
378     *
379     * <p>Acquires a permit, if one is available and returns immediately,
380     * reducing the number of available permits by one.
381     * <p>If no permit is available then the current thread becomes
382     * disabled for thread scheduling purposes and lies dormant until
383     * one of two things happens:
384     * <ul>
385     * <li>Some other thread invokes the {@link #release} method for this
386 dl 1.16 * semaphore and the current thread is next to be assigned a permit; or
387 tim 1.1 * <li>Some other thread {@link Thread#interrupt interrupts} the current
388     * thread.
389     * </ul>
390     *
391     * <p>If the current thread:
392     * <ul>
393     * <li>has its interrupted status set on entry to this method; or
394     * <li>is {@link Thread#interrupt interrupted} while waiting
395     * for a permit,
396     * </ul>
397     * then {@link InterruptedException} is thrown and the current thread's
398     * interrupted status is cleared.
399     *
400     * @throws InterruptedException if the current thread is interrupted
401     *
402     * @see Thread#interrupt
403     */
404 dl 1.2 public void acquire() throws InterruptedException {
405 dl 1.16 if (Thread.interrupted() || !doAcquire(1, -1))
406     throw new InterruptedException();
407 dl 1.2 }
408 tim 1.1
409     /**
410     * Acquires a permit from this semaphore, blocking until one is
411     * available.
412     *
413     * <p>Acquires a permit, if one is available and returns immediately,
414     * reducing the number of available permits by one.
415     * <p>If no permit is available then the current thread becomes
416     * disabled for thread scheduling purposes and lies dormant until
417     * some other thread invokes the {@link #release} method for this
418 dl 1.16 * semaphore and the current thread is next to be assigned a permit.
419 tim 1.1 *
420     * <p>If the current thread
421     * is {@link Thread#interrupt interrupted} while waiting
422     * for a permit then it will continue to wait, but the time at which
423     * the thread is assigned a permit may change compared to the time it
424     * would have received the permit had no interruption occurred. When the
425     * thread does return from this method its interrupt status will be set.
426     *
427     */
428 dl 1.2 public void acquireUninterruptibly() {
429 dl 1.16 doAcquire(1, 0);
430 dl 1.2 }
431 tim 1.1
432     /**
433     * Acquires a permit from this semaphore, only if one is available at the
434     * time of invocation.
435     * <p>Acquires a permit, if one is available and returns immediately,
436     * with the value <tt>true</tt>,
437     * reducing the number of available permits by one.
438     *
439     * <p>If no permit is available then this method will return
440     * immediately with the value <tt>false</tt>.
441     *
442     * @return <tt>true</tt> if a permit was acquired and <tt>false</tt>
443     * otherwise.
444     */
445     public boolean tryAcquire() {
446 dl 1.16 return doTryAcquire(1) >= 0;
447 tim 1.1 }
448    
449     /**
450     * Acquires a permit from this semaphore, if one becomes available
451     * within the given waiting time and the
452     * current thread has not been {@link Thread#interrupt interrupted}.
453     * <p>Acquires a permit, if one is available and returns immediately,
454     * with the value <tt>true</tt>,
455     * reducing the number of available permits by one.
456     * <p>If no permit is available then
457     * the current thread becomes disabled for thread scheduling
458     * purposes and lies dormant until one of three things happens:
459     * <ul>
460     * <li>Some other thread invokes the {@link #release} method for this
461 dl 1.16 * semaphore and the current thread is next to be assigned a permit; or
462 tim 1.1 * <li>Some other thread {@link Thread#interrupt interrupts} the current
463     * thread; or
464     * <li>The specified waiting time elapses.
465     * </ul>
466     * <p>If a permit is acquired then the value <tt>true</tt> is returned.
467     * <p>If the current thread:
468     * <ul>
469     * <li>has its interrupted status set on entry to this method; or
470     * <li>is {@link Thread#interrupt interrupted} while waiting to acquire
471     * a permit,
472     * </ul>
473     * then {@link InterruptedException} is thrown and the current thread's
474     * interrupted status is cleared.
475     * <p>If the specified waiting time elapses then the value <tt>false</tt>
476     * is returned.
477 dl 1.16 * If the time is less than or equal to zero, the method will not wait
478     * at all.
479 tim 1.1 *
480     * @param timeout the maximum time to wait for a permit
481 dl 1.13 * @param unit the time unit of the <tt>timeout</tt> argument.
482 tim 1.1 * @return <tt>true</tt> if a permit was acquired and <tt>false</tt>
483     * if the waiting time elapsed before a permit was acquired.
484     *
485     * @throws InterruptedException if the current thread is interrupted
486     *
487     * @see Thread#interrupt
488     *
489     */
490 dl 1.16 public boolean tryAcquire(long timeout, TimeUnit unit)
491 tim 1.1 throws InterruptedException {
492 dl 1.16 if (unit == null)
493     throw new NullPointerException();
494     if (Thread.interrupted())
495     throw new InterruptedException();
496     return doTimedAcquire(1, unit.toNanos(timeout));
497 tim 1.1 }
498    
499     /**
500     * Releases a permit, returning it to the semaphore.
501     * <p>Releases a permit, increasing the number of available permits
502     * by one.
503     * If any threads are blocking trying to acquire a permit, then one
504     * is selected and given the permit that was just released.
505     * That thread is re-enabled for thread scheduling purposes.
506     * <p>There is no requirement that a thread that releases a permit must
507     * have acquired that permit by calling {@link #acquire}.
508     * Correct usage of a semaphore is established by programming convention
509     * in the application.
510     */
511 dl 1.2 public void release() {
512 dl 1.16 doRelease(1);
513     }
514    
515     /**
516     * Acquires the given number of permits from this semaphore,
517     * blocking until all are available,
518     * or the thread is {@link Thread#interrupt interrupted}.
519     *
520     * <p>Acquires the given number of permits, if they are available,
521     * and returns immediately,
522     * reducing the number of available permits by the given amount.
523     *
524     * <p>If insufficient permits are available then the current thread becomes
525     * disabled for thread scheduling purposes and lies dormant until
526     * one of two things happens:
527     * <ul>
528     * <li>Some other thread invokes one of the {@link #release() release}
529     * methods for this semaphore, the current thread is next to be assigned
530     * permits and the number of available permits satisfies this request; or
531     * <li>Some other thread {@link Thread#interrupt interrupts} the current
532     * thread.
533     * </ul>
534     *
535     * <p>If the current thread:
536     * <ul>
537     * <li>has its interrupted status set on entry to this method; or
538     * <li>is {@link Thread#interrupt interrupted} while waiting
539     * for a permit,
540     * </ul>
541     * then {@link InterruptedException} is thrown and the current thread's
542     * interrupted status is cleared.
543     * Any permits that were to be assigned to this thread, are instead
544     * assigned to the next waiting thread(s), as if
545     * they had been made available by a call to {@link #release()}.
546     *
547     * @param permits the number of permits to acquire
548     *
549     * @throws InterruptedException if the current thread is interrupted
550     * @throws IllegalArgumentException if permits less than zero.
551     *
552     * @see Thread#interrupt
553     */
554     public void acquire(int permits) throws InterruptedException {
555     if (permits < 0) throw new IllegalArgumentException();
556     if (Thread.interrupted() || !doAcquire(permits, -1))
557     throw new InterruptedException();
558     }
559    
560     /**
561     * Acquires the given number of permits from this semaphore,
562     * blocking until all are available.
563     *
564     * <p>Acquires the given number of permits, if they are available,
565     * and returns immediately,
566     * reducing the number of available permits by the given amount.
567     *
568     * <p>If insufficient permits are available then the current thread becomes
569     * disabled for thread scheduling purposes and lies dormant until
570     * some other thread invokes one of the {@link #release() release}
571     * methods for this semaphore, the current thread is next to be assigned
572     * permits and the number of available permits satisfies this request.
573     *
574     * <p>If the current thread
575     * is {@link Thread#interrupt interrupted} while waiting
576     * for permits then it will continue to wait and its position in the
577     * queue is not affected. When the
578     * thread does return from this method its interrupt status will be set.
579     *
580     * @param permits the number of permits to acquire
581     * @throws IllegalArgumentException if permits less than zero.
582     *
583     */
584     public void acquireUninterruptibly(int permits) {
585     if (permits < 0) throw new IllegalArgumentException();
586     doAcquire(permits, 0);
587     }
588    
589     /**
590     * Acquires the given number of permits from this semaphore, only if
591     * all are available at the time of invocation.
592     * <p>Acquires the given number of permits, if they are available, and
593     * returns immediately, with the value <tt>true</tt>,
594     * reducing the number of available permits by the given amount.
595     *
596     * <p>If insufficient permits are available then this method will return
597     * immediately with the value <tt>false</tt> and the number of available
598     * permits is unchanged.
599     *
600     * @param permits the number of permits to acquire
601     *
602     * @return <tt>true</tt> if the permits were acquired and <tt>false</tt>
603     * otherwise.
604     * @throws IllegalArgumentException if permits less than zero.
605     */
606     public boolean tryAcquire(int permits) {
607     if (permits < 0) throw new IllegalArgumentException();
608     return doTryAcquire(permits) >= 0;
609     }
610    
611     /**
612     * Acquires the given number of permits from this semaphore, if all
613     * become available within the given waiting time and the
614     * current thread has not been {@link Thread#interrupt interrupted}.
615     * <p>Acquires the given number of permits, if they are available and
616     * returns immediately, with the value <tt>true</tt>,
617     * reducing the number of available permits by the given amount.
618     * <p>If insufficient permits are available then
619     * the current thread becomes disabled for thread scheduling
620     * purposes and lies dormant until one of three things happens:
621     * <ul>
622     * <li>Some other thread invokes one of the {@link #release() release}
623     * methods for this semaphore, the current thread is next to be assigned
624     * permits and the number of available permits satisfies this request; or
625     * <li>Some other thread {@link Thread#interrupt interrupts} the current
626     * thread; or
627     * <li>The specified waiting time elapses.
628     * </ul>
629     * <p>If the permits are acquired then the value <tt>true</tt> is returned.
630     * <p>If the current thread:
631     * <ul>
632     * <li>has its interrupted status set on entry to this method; or
633     * <li>is {@link Thread#interrupt interrupted} while waiting to acquire
634     * the permits,
635     * </ul>
636     * then {@link InterruptedException} is thrown and the current thread's
637     * interrupted status is cleared.
638     * Any permits that were to be assigned to this thread, are instead
639     * assigned to the next waiting thread(s), as if
640     * they had been made available by a call to {@link #release()}.
641     *
642     * <p>If the specified waiting time elapses then the value <tt>false</tt>
643     * is returned.
644     * If the time is
645     * less than or equal to zero, the method will not wait at all.
646     * Any permits that were to be assigned to this thread, are instead
647     * assigned to the next waiting thread(s), as if
648     * they had been made available by a call to {@link #release()}.
649     *
650     * @param permits the number of permits to acquire
651     * @param timeout the maximum time to wait for the permits
652     * @param unit the time unit of the <tt>timeout</tt> argument.
653     * @return <tt>true</tt> if all permits were acquired and <tt>false</tt>
654     * if the waiting time elapsed before all permits were acquired.
655     *
656     * @throws InterruptedException if the current thread is interrupted
657     * @throws IllegalArgumentException if permits less than zero.
658     *
659     * @see Thread#interrupt
660     *
661     */
662     public boolean tryAcquire(int permits, long timeout, TimeUnit unit)
663     throws InterruptedException {
664     if (permits < 0) throw new IllegalArgumentException();
665     if (unit == null)
666     throw new NullPointerException();
667     if (Thread.interrupted())
668     throw new InterruptedException();
669     return doTimedAcquire(permits, unit.toNanos(timeout));
670     }
671    
672    
673     /**
674     * Releases the given number of permits, returning them to the semaphore.
675     * <p>Releases the given number of permits, increasing the number of
676     * available permits by that amount.
677     * If any threads are blocking trying to acquire permits, then the
678     * one that has been waiting the intest
679     * is selected and given the permits that were just released.
680     * If the number of available permits satisfies that thread's request
681     * then that thread is re-enabled for thread scheduling purposes; otherwise
682     * the thread continues to wait. If there are still permits available
683     * after the first thread's request has been satisfied, then those permits
684     * are assigned to the next waiting thread. If it is satisfied then it is
685     * re-enabled for thread scheduling purposes. This continues until there
686     * are insufficient permits to satisfy the next waiting thread, or there
687     * are no more waiting threads.
688     *
689     * <p>There is no requirement that a thread that releases a permit must
690     * have acquired that permit by calling {@link Semaphore#acquire acquire}.
691     * Correct usage of a semaphore is established by programming convention
692     * in the application.
693     *
694     * @param permits the number of permits to release
695     * @throws IllegalArgumentException if permits less than zero.
696     */
697     public void release(int permits) {
698     if (permits < 0) throw new IllegalArgumentException();
699     doRelease(permits);
700 dl 1.2 }
701 tim 1.1
702     /**
703     * Return the current number of permits available in this semaphore.
704     * <p>This method is typically used for debugging and testing purposes.
705     * @return the number of permits available in this semaphore.
706     */
707 dl 1.16 public int availablePermits() {
708     return perms.get();
709 tim 1.1 }
710 dl 1.15
711     /**
712     * Shrink the number of available permits by the indicated
713     * reduction. This method can be useful in subclasses that
714     * use semaphores to track available resources that become
715     * unavailable. This method differs from <tt>acquire</tt>
716     * in that it does not block waiting for permits to become
717     * available.
718     * @param reduction the number of permits to remove
719     * @throws IllegalArgumentException if reduction is negative
720     */
721 dl 1.16 protected void reducePermits(int reduction) {
722 dl 1.15 if (reduction < 0) throw new IllegalArgumentException();
723 dl 1.16 perms.getAndAdd(-reduction);
724     }
725    
726     /**
727     * Return true if this semaphore has fairness set true.
728     * @return true if this semaphore has fairness set true.
729     */
730     public boolean isFair() {
731     return fair;
732 dl 1.15 }
733 dl 1.16
734     /**
735     * Returns an estimate of the number of threads waiting to acquire
736     * a permit. The value is only an estimate because the number of
737     * threads may change dynamically while this method traverses
738     * internal data structures. This method is designed for use in
739     * monitoring of the system state, not for synchronization
740     * control.
741     * @return the estimated number of threads waiting for a permit
742     */
743     public int getQueueLength() {
744     int n = 0;
745     for (Node p = tail; p != null && p != head; p = p.prev)
746     ++n;
747     return n;
748     }
749    
750     /**
751     * Returns a collection containing threads that may be waiting to
752     * acquire a permit. Because the actual set of threads may
753     * change dynamically while constructing this result, the returned
754     * collection is only a best-effort estimate. The elements of the
755     * returned collection are in no particular order. This method is
756     * designed to facilitate construction of subclasses that provide
757     * more extensive monitoring facilities.
758     * @return the collection of threads
759     */
760     protected Collection<Thread> getQueuedThreads() {
761     ArrayList<Thread> list = new ArrayList<Thread>();
762     for (Node p = tail; p != null; p = p.prev) {
763     Thread t = p.thread;
764     if (t != null)
765     list.add(t);
766     }
767     return list;
768     }
769    
770 tim 1.1 }