jdk/src/java.base/share/classes/java/util/concurrent/Semaphore.java
author dl
Wed, 21 Dec 2016 14:26:52 -0800
changeset 42927 1d31e540bfcb
parent 42321 a0abf857aaec
child 45937 646816090183
permissions -rw-r--r--
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12 Reviewed-by: martin, smarks, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5202
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5202
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5202
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5202
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5202
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7518
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    37
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    38
import java.util.Collection;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    39
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A counting semaphore.  Conceptually, a semaphore maintains a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * permits.  Each {@link #acquire} blocks if necessary until a permit is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * available, and then takes it.  Each {@link #release} adds a permit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * potentially releasing a blocking acquirer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * However, no actual permit objects are used; the {@code Semaphore} just
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * keeps a count of the number available and acts accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>Semaphores are often used to restrict the number of threads than can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * access some (physical or logical) resource. For example, here is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * a class that uses a semaphore to control access to a pool of items:
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    52
 * <pre> {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * class Pool {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *   private static final int MAX_AVAILABLE = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   public Object getItem() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *     available.acquire();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *     return getNextAvailableItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *   public void putItem(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     if (markAsUnused(x))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *       available.release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   // Not a particularly efficient data structure; just for demo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *   protected Object[] items = ... whatever kinds of items being managed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *   protected boolean[] used = new boolean[MAX_AVAILABLE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *   protected synchronized Object getNextAvailableItem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *     for (int i = 0; i < MAX_AVAILABLE; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *       if (!used[i]) {
40817
4f5fb115676d 8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents: 32991
diff changeset
    75
 *         used[i] = true;
4f5fb115676d 8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents: 32991
diff changeset
    76
 *         return items[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *     return null; // not reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *   protected synchronized boolean markAsUnused(Object item) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *     for (int i = 0; i < MAX_AVAILABLE; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *       if (item == items[i]) {
40817
4f5fb115676d 8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents: 32991
diff changeset
    85
 *         if (used[i]) {
4f5fb115676d 8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents: 32991
diff changeset
    86
 *           used[i] = false;
4f5fb115676d 8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents: 32991
diff changeset
    87
 *           return true;
4f5fb115676d 8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents: 32991
diff changeset
    88
 *         } else
4f5fb115676d 8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents: 32991
diff changeset
    89
 *           return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *     return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *   }
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    94
 * }}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <p>Before obtaining an item each thread must acquire a permit from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * the semaphore, guaranteeing that an item is available for use. When
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * the thread has finished with the item it is returned back to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * pool and a permit is returned to the semaphore, allowing another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * thread to acquire that item.  Note that no synchronization lock is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * held when {@link #acquire} is called as that would prevent an item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * from being returned to the pool.  The semaphore encapsulates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * synchronization needed to restrict access to the pool, separately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * from any synchronization needed to maintain the consistency of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * pool itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <p>A semaphore initialized to one, and which is used such that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * only has at most one permit available, can serve as a mutual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * exclusion lock.  This is more commonly known as a <em>binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * semaphore</em>, because it only has two states: one permit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * available, or zero permits available.  When used in this way, the
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   112
 * binary semaphore has the property (unlike many {@link java.util.concurrent.locks.Lock}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * implementations), that the &quot;lock&quot; can be released by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * thread other than the owner (as semaphores have no notion of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * ownership).  This can be useful in some specialized contexts, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * as deadlock recovery.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   118
 * <p>The constructor for this class optionally accepts a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <em>fairness</em> parameter. When set false, this class makes no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * guarantees about the order in which threads acquire permits. In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * particular, <em>barging</em> is permitted, that is, a thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * invoking {@link #acquire} can be allocated a permit ahead of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * thread that has been waiting - logically the new thread places itself at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * the head of the queue of waiting threads. When fairness is set true, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * semaphore guarantees that threads invoking any of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * #acquire() acquire} methods are selected to obtain permits in the order in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * which their invocation of those methods was processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * (first-in-first-out; FIFO). Note that FIFO ordering necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * applies to specific internal points of execution within these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * methods.  So, it is possible for one thread to invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * {@code acquire} before another, but reach the ordering point after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * the other, and similarly upon return from the method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * Also note that the untimed {@link #tryAcquire() tryAcquire} methods do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * honor the fairness setting, but will take any permits that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * <p>Generally, semaphores used to control resource access should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * initialized as fair, to ensure that no thread is starved out from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * accessing a resource. When using semaphores for other kinds of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * synchronization control, the throughput advantages of non-fair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * ordering often outweigh fairness considerations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * <p>This class also provides convenience methods to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * #acquire(int) acquire} and {@link #release(int) release} multiple
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   145
 * permits at a time. These methods are generally more efficient and
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   146
 * effective than loops. However, they do not establish any preference
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   147
 * order. For example, if thread A invokes {@code s.acquire(3}) and
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   148
 * thread B invokes {@code s.acquire(2)}, and two permits become
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   149
 * available, then there is no guarantee that thread B will obtain
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   150
 * them unless its acquire came first and Semaphore {@code s} is in
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   151
 * fair mode.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <p>Memory consistency effects: Actions in a thread prior to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * a "release" method such as {@code release()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * actions following a successful "acquire" method such as {@code acquire()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * in another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
public class Semaphore implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static final long serialVersionUID = -3222578661600680210L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /** All mechanics via AbstractQueuedSynchronizer subclass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private final Sync sync;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Synchronization implementation for semaphore.  Uses AQS state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * to represent permits. Subclassed into fair and nonfair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * versions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    abstract static class Sync extends AbstractQueuedSynchronizer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        private static final long serialVersionUID = 1192457210091910933L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        Sync(int permits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            setState(permits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        final int getPermits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        final int nonfairTryAcquireShared(int acquires) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                int available = getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                int remaining = available - acquires;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if (remaining < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    compareAndSetState(available, remaining))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    return remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        protected final boolean tryReleaseShared(int releases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            for (;;) {
5202
20198b5f3653 6941130: Semaphore should throw if number of permits overflows or underflows
martin
parents: 2
diff changeset
   195
                int current = getState();
20198b5f3653 6941130: Semaphore should throw if number of permits overflows or underflows
martin
parents: 2
diff changeset
   196
                int next = current + releases;
20198b5f3653 6941130: Semaphore should throw if number of permits overflows or underflows
martin
parents: 2
diff changeset
   197
                if (next < current) // overflow
20198b5f3653 6941130: Semaphore should throw if number of permits overflows or underflows
martin
parents: 2
diff changeset
   198
                    throw new Error("Maximum permit count exceeded");
20198b5f3653 6941130: Semaphore should throw if number of permits overflows or underflows
martin
parents: 2
diff changeset
   199
                if (compareAndSetState(current, next))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        final void reducePermits(int reductions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                int current = getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                int next = current - reductions;
5202
20198b5f3653 6941130: Semaphore should throw if number of permits overflows or underflows
martin
parents: 2
diff changeset
   208
                if (next > current) // underflow
20198b5f3653 6941130: Semaphore should throw if number of permits overflows or underflows
martin
parents: 2
diff changeset
   209
                    throw new Error("Permit count underflow");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                if (compareAndSetState(current, next))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        final int drainPermits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                int current = getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if (current == 0 || compareAndSetState(current, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * NonFair version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   227
    static final class NonfairSync extends Sync {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        private static final long serialVersionUID = -2694183684443567898L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        NonfairSync(int permits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            super(permits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        protected int tryAcquireShared(int acquires) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return nonfairTryAcquireShared(acquires);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Fair version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   242
    static final class FairSync extends Sync {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        private static final long serialVersionUID = 2014338818796000944L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        FairSync(int permits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            super(permits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        protected int tryAcquireShared(int acquires) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                if (hasQueuedPredecessors())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                int available = getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                int remaining = available - acquires;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                if (remaining < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    compareAndSetState(available, remaining))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    return remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Creates a {@code Semaphore} with the given number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * permits and nonfair fairness setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param permits the initial number of permits available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *        This value may be negative, in which case releases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *        must occur before any acquires will be granted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public Semaphore(int permits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        sync = new NonfairSync(permits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Creates a {@code Semaphore} with the given number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * permits and the given fairness setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param permits the initial number of permits available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *        This value may be negative, in which case releases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *        must occur before any acquires will be granted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param fair {@code true} if this semaphore will guarantee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *        first-in first-out granting of permits under contention,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *        else {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public Semaphore(int permits, boolean fair) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   286
        sync = fair ? new FairSync(permits) : new NonfairSync(permits);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Acquires a permit from this semaphore, blocking until one is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * available, or the thread is {@linkplain Thread#interrupt interrupted}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * <p>Acquires a permit, if one is available and returns immediately,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * reducing the number of available permits by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <p>If no permit is available then the current thread becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * disabled for thread scheduling purposes and lies dormant until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * one of two things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <li>Some other thread invokes the {@link #release} method for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * semaphore and the current thread is next to be assigned a permit; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * for a permit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * interrupted status is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public void acquire() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        sync.acquireSharedInterruptibly(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Acquires a permit from this semaphore, blocking until one is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <p>Acquires a permit, if one is available and returns immediately,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * reducing the number of available permits by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * <p>If no permit is available then the current thread becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * disabled for thread scheduling purposes and lies dormant until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * some other thread invokes the {@link #release} method for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * semaphore and the current thread is next to be assigned a permit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <p>If the current thread is {@linkplain Thread#interrupt interrupted}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * while waiting for a permit then it will continue to wait, but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * time at which the thread is assigned a permit may change compared to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * the time it would have received the permit had no interruption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * occurred.  When the thread does return from this method its interrupt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * status will be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public void acquireUninterruptibly() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        sync.acquireShared(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Acquires a permit from this semaphore, only if one is available at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * time of invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * <p>Acquires a permit, if one is available and returns immediately,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * with the value {@code true},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * reducing the number of available permits by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * <p>If no permit is available then this method will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * immediately with the value {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * <p>Even when this semaphore has been set to use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * fair ordering policy, a call to {@code tryAcquire()} <em>will</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * immediately acquire a permit if one is available, whether or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * other threads are currently waiting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * This &quot;barging&quot; behavior can be useful in certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * circumstances, even though it breaks fairness. If you want to honor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * the fairness setting, then use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * {@link #tryAcquire(long, TimeUnit) tryAcquire(0, TimeUnit.SECONDS) }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * which is almost equivalent (it also detects interruption).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @return {@code true} if a permit was acquired and {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *         otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public boolean tryAcquire() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return sync.nonfairTryAcquireShared(1) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Acquires a permit from this semaphore, if one becomes available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * within the given waiting time and the current thread has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * been {@linkplain Thread#interrupt interrupted}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <p>Acquires a permit, if one is available and returns immediately,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * with the value {@code true},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * reducing the number of available permits by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <p>If no permit is available then the current thread becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * disabled for thread scheduling purposes and lies dormant until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * one of three things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * <li>Some other thread invokes the {@link #release} method for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * semaphore and the current thread is next to be assigned a permit; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * <li>The specified waiting time elapses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <p>If a permit is acquired then the value {@code true} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * to acquire a permit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * interrupted status is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * <p>If the specified waiting time elapses then the value {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * is returned.  If the time is less than or equal to zero, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * will not wait at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @param timeout the maximum time to wait for a permit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @param unit the time unit of the {@code timeout} argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @return {@code true} if a permit was acquired and {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *         if the waiting time elapsed before a permit was acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public boolean tryAcquire(long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Releases a permit, returning it to the semaphore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * <p>Releases a permit, increasing the number of available permits by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * one.  If any threads are trying to acquire a permit, then one is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * selected and given the permit that was just released.  That thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * is (re)enabled for thread scheduling purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <p>There is no requirement that a thread that releases a permit must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * have acquired that permit by calling {@link #acquire}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Correct usage of a semaphore is established by programming convention
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * in the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    public void release() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        sync.releaseShared(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Acquires the given number of permits from this semaphore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * blocking until all are available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * or the thread is {@linkplain Thread#interrupt interrupted}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * <p>Acquires the given number of permits, if they are available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * and returns immediately, reducing the number of available permits
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   442
     * by the given amount. This method has the same effect as the
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   443
     * loop {@code for (int i = 0; i < permits; ++i) acquire();} except
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   444
     * that it atomically acquires the permits all at once:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * <p>If insufficient permits are available then the current thread becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * disabled for thread scheduling purposes and lies dormant until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * one of two things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * <li>Some other thread invokes one of the {@link #release() release}
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   451
     * methods for this semaphore and the current thread is next to be assigned
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * permits and the number of available permits satisfies this request; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * for a permit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * interrupted status is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Any permits that were to be assigned to this thread are instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * assigned to other threads trying to acquire permits, as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * permits had been made available by a call to {@link #release()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @param permits the number of permits to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @throws IllegalArgumentException if {@code permits} is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    public void acquire(int permits) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (permits < 0) throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        sync.acquireSharedInterruptibly(permits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Acquires the given number of permits from this semaphore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * blocking until all are available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * <p>Acquires the given number of permits, if they are available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * and returns immediately, reducing the number of available permits
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   484
     * by the given amount. This method has the same effect as the
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   485
     * loop {@code for (int i = 0; i < permits; ++i) acquireUninterruptibly();}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   486
     * except that it atomically acquires the permits all at once:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * <p>If insufficient permits are available then the current thread becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * disabled for thread scheduling purposes and lies dormant until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * some other thread invokes one of the {@link #release() release}
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   491
     * methods for this semaphore and the current thread is next to be assigned
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * permits and the number of available permits satisfies this request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * <p>If the current thread is {@linkplain Thread#interrupt interrupted}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * while waiting for permits then it will continue to wait and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * position in the queue is not affected.  When the thread does return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * from this method its interrupt status will be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @param permits the number of permits to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @throws IllegalArgumentException if {@code permits} is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    public void acquireUninterruptibly(int permits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (permits < 0) throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        sync.acquireShared(permits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Acquires the given number of permits from this semaphore, only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * if all are available at the time of invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * <p>Acquires the given number of permits, if they are available, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * returns immediately, with the value {@code true},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * reducing the number of available permits by the given amount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * <p>If insufficient permits are available then this method will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * immediately with the value {@code false} and the number of available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * permits is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * <p>Even when this semaphore has been set to use a fair ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * policy, a call to {@code tryAcquire} <em>will</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * immediately acquire a permit if one is available, whether or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * not other threads are currently waiting.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * &quot;barging&quot; behavior can be useful in certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * circumstances, even though it breaks fairness. If you want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * honor the fairness setting, then use {@link #tryAcquire(int,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * long, TimeUnit) tryAcquire(permits, 0, TimeUnit.SECONDS) }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * which is almost equivalent (it also detects interruption).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @param permits the number of permits to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @return {@code true} if the permits were acquired and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @throws IllegalArgumentException if {@code permits} is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    public boolean tryAcquire(int permits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (permits < 0) throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return sync.nonfairTryAcquireShared(permits) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * Acquires the given number of permits from this semaphore, if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * become available within the given waiting time and the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * thread has not been {@linkplain Thread#interrupt interrupted}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * <p>Acquires the given number of permits, if they are available and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * returns immediately, with the value {@code true},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * reducing the number of available permits by the given amount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * <p>If insufficient permits are available then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * the current thread becomes disabled for thread scheduling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * purposes and lies dormant until one of three things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * <li>Some other thread invokes one of the {@link #release() release}
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   553
     * methods for this semaphore and the current thread is next to be assigned
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * permits and the number of available permits satisfies this request; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * <li>The specified waiting time elapses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * <p>If the permits are acquired then the value {@code true} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * to acquire the permits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * interrupted status is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * Any permits that were to be assigned to this thread, are instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * assigned to other threads trying to acquire permits, as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * the permits had been made available by a call to {@link #release()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * <p>If the specified waiting time elapses then the value {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * is returned.  If the time is less than or equal to zero, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * will not wait at all.  Any permits that were to be assigned to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * thread, are instead assigned to other threads trying to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * permits, as if the permits had been made available by a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * {@link #release()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @param permits the number of permits to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @param timeout the maximum time to wait for the permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @param unit the time unit of the {@code timeout} argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @return {@code true} if all permits were acquired and {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *         if the waiting time elapsed before all permits were acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @throws IllegalArgumentException if {@code permits} is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public boolean tryAcquire(int permits, long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if (permits < 0) throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        return sync.tryAcquireSharedNanos(permits, unit.toNanos(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * Releases the given number of permits, returning them to the semaphore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * <p>Releases the given number of permits, increasing the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * available permits by that amount.
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   600
     * If any threads are trying to acquire permits, then one thread
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * is selected and given the permits that were just released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * If the number of available permits satisfies that thread's request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * then that thread is (re)enabled for thread scheduling purposes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * otherwise the thread will wait until sufficient permits are available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * If there are still permits available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * after this thread's request has been satisfied, then those permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * are assigned in turn to other threads trying to acquire permits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * <p>There is no requirement that a thread that releases a permit must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * have acquired that permit by calling {@link Semaphore#acquire acquire}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Correct usage of a semaphore is established by programming convention
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * in the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @param permits the number of permits to release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @throws IllegalArgumentException if {@code permits} is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    public void release(int permits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if (permits < 0) throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        sync.releaseShared(permits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * Returns the current number of permits available in this semaphore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <p>This method is typically used for debugging and testing purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @return the number of permits available in this semaphore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    public int availablePermits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        return sync.getPermits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /**
42321
a0abf857aaec 8169272: Clarify Semaphore.drainPermits behavior when current permits are negative
dl
parents: 40817
diff changeset
   634
     * Acquires and returns all permits that are immediately
a0abf857aaec 8169272: Clarify Semaphore.drainPermits behavior when current permits are negative
dl
parents: 40817
diff changeset
   635
     * available, or if negative permits are available, releases them.
a0abf857aaec 8169272: Clarify Semaphore.drainPermits behavior when current permits are negative
dl
parents: 40817
diff changeset
   636
     * Upon return, zero permits are available.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *
42321
a0abf857aaec 8169272: Clarify Semaphore.drainPermits behavior when current permits are negative
dl
parents: 40817
diff changeset
   638
     * @return the number of permits acquired or, if negative, the
a0abf857aaec 8169272: Clarify Semaphore.drainPermits behavior when current permits are negative
dl
parents: 40817
diff changeset
   639
     * number released
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    public int drainPermits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        return sync.drainPermits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * Shrinks the number of available permits by the indicated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * reduction. This method can be useful in subclasses that use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * semaphores to track resources that become unavailable. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * method differs from {@code acquire} in that it does not block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * waiting for permits to become available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @param reduction the number of permits to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @throws IllegalArgumentException if {@code reduction} is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    protected void reducePermits(int reduction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        if (reduction < 0) throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        sync.reducePermits(reduction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * Returns {@code true} if this semaphore has fairness set true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @return {@code true} if this semaphore has fairness set true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    public boolean isFair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        return sync instanceof FairSync;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * Queries whether any threads are waiting to acquire. Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * because cancellations may occur at any time, a {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * return does not guarantee that any other thread will ever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * acquire.  This method is designed primarily for use in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * monitoring of the system state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @return {@code true} if there may be other threads waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *         acquire the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    public final boolean hasQueuedThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        return sync.hasQueuedThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * Returns an estimate of the number of threads waiting to acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * The value is only an estimate because the number of threads may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * change dynamically while this method traverses internal data
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   687
     * structures.  This method is designed for use in monitoring
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * system state, not for synchronization control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @return the estimated number of threads waiting for this lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    public final int getQueueLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        return sync.getQueueLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * Returns a collection containing threads that may be waiting to acquire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * Because the actual set of threads may change dynamically while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * constructing this result, the returned collection is only a best-effort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * estimate.  The elements of the returned collection are in no particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * order.  This method is designed to facilitate construction of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * subclasses that provide more extensive monitoring facilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    protected Collection<Thread> getQueuedThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        return sync.getQueuedThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * Returns a string identifying this semaphore, as well as its state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * The state, in brackets, includes the String {@code "Permits ="}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * followed by the number of permits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @return a string identifying this semaphore, as well as its state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        return super.toString() + "[Permits = " + sync.getPermits() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
}