rt/emul/compact/src/main/java/java/util/concurrent/locks/LockSupport.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 19 Mar 2016 11:01:40 +0100
changeset 1894 75ee4eca04e3
parent 1890 212417b74b72
permissions -rw-r--r--
Can compile java.util.concurrent.locks
jaroslav@1890
     1
/*
jaroslav@1890
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1890
     3
 *
jaroslav@1890
     4
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1890
     5
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1890
     6
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1890
     7
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1890
     8
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1890
     9
 *
jaroslav@1890
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1890
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1890
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1890
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1890
    14
 * accompanied this code).
jaroslav@1890
    15
 *
jaroslav@1890
    16
 * You should have received a copy of the GNU General Public License version
jaroslav@1890
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1890
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1890
    19
 *
jaroslav@1890
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1890
    21
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1890
    22
 * questions.
jaroslav@1890
    23
 */
jaroslav@1890
    24
jaroslav@1890
    25
/*
jaroslav@1890
    26
 * This file is available under and governed by the GNU General Public
jaroslav@1890
    27
 * License version 2 only, as published by the Free Software Foundation.
jaroslav@1890
    28
 * However, the following notice accompanied the original version of this
jaroslav@1890
    29
 * file:
jaroslav@1890
    30
 *
jaroslav@1890
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
jaroslav@1890
    32
 * Expert Group and released to the public domain, as explained at
jaroslav@1890
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jaroslav@1890
    34
 */
jaroslav@1890
    35
jaroslav@1890
    36
package java.util.concurrent.locks;
jaroslav@1890
    37
jaroslav@1890
    38
jaroslav@1890
    39
/**
jaroslav@1890
    40
 * Basic thread blocking primitives for creating locks and other
jaroslav@1890
    41
 * synchronization classes.
jaroslav@1890
    42
 *
jaroslav@1890
    43
 * <p>This class associates, with each thread that uses it, a permit
jaroslav@1890
    44
 * (in the sense of the {@link java.util.concurrent.Semaphore
jaroslav@1890
    45
 * Semaphore} class). A call to {@code park} will return immediately
jaroslav@1890
    46
 * if the permit is available, consuming it in the process; otherwise
jaroslav@1890
    47
 * it <em>may</em> block.  A call to {@code unpark} makes the permit
jaroslav@1890
    48
 * available, if it was not already available. (Unlike with Semaphores
jaroslav@1890
    49
 * though, permits do not accumulate. There is at most one.)
jaroslav@1890
    50
 *
jaroslav@1890
    51
 * <p>Methods {@code park} and {@code unpark} provide efficient
jaroslav@1890
    52
 * means of blocking and unblocking threads that do not encounter the
jaroslav@1890
    53
 * problems that cause the deprecated methods {@code Thread.suspend}
jaroslav@1890
    54
 * and {@code Thread.resume} to be unusable for such purposes: Races
jaroslav@1890
    55
 * between one thread invoking {@code park} and another thread trying
jaroslav@1890
    56
 * to {@code unpark} it will preserve liveness, due to the
jaroslav@1890
    57
 * permit. Additionally, {@code park} will return if the caller's
jaroslav@1890
    58
 * thread was interrupted, and timeout versions are supported. The
jaroslav@1890
    59
 * {@code park} method may also return at any other time, for "no
jaroslav@1890
    60
 * reason", so in general must be invoked within a loop that rechecks
jaroslav@1890
    61
 * conditions upon return. In this sense {@code park} serves as an
jaroslav@1890
    62
 * optimization of a "busy wait" that does not waste as much time
jaroslav@1890
    63
 * spinning, but must be paired with an {@code unpark} to be
jaroslav@1890
    64
 * effective.
jaroslav@1890
    65
 *
jaroslav@1890
    66
 * <p>The three forms of {@code park} each also support a
jaroslav@1890
    67
 * {@code blocker} object parameter. This object is recorded while
jaroslav@1890
    68
 * the thread is blocked to permit monitoring and diagnostic tools to
jaroslav@1890
    69
 * identify the reasons that threads are blocked. (Such tools may
jaroslav@1890
    70
 * access blockers using method {@link #getBlocker}.) The use of these
jaroslav@1890
    71
 * forms rather than the original forms without this parameter is
jaroslav@1890
    72
 * strongly encouraged. The normal argument to supply as a
jaroslav@1890
    73
 * {@code blocker} within a lock implementation is {@code this}.
jaroslav@1890
    74
 *
jaroslav@1890
    75
 * <p>These methods are designed to be used as tools for creating
jaroslav@1890
    76
 * higher-level synchronization utilities, and are not in themselves
jaroslav@1890
    77
 * useful for most concurrency control applications.  The {@code park}
jaroslav@1890
    78
 * method is designed for use only in constructions of the form:
jaroslav@1890
    79
 * <pre>while (!canProceed()) { ... LockSupport.park(this); }</pre>
jaroslav@1890
    80
 * where neither {@code canProceed} nor any other actions prior to the
jaroslav@1890
    81
 * call to {@code park} entail locking or blocking.  Because only one
jaroslav@1890
    82
 * permit is associated with each thread, any intermediary uses of
jaroslav@1890
    83
 * {@code park} could interfere with its intended effects.
jaroslav@1890
    84
 *
jaroslav@1890
    85
 * <p><b>Sample Usage.</b> Here is a sketch of a first-in-first-out
jaroslav@1890
    86
 * non-reentrant lock class:
jaroslav@1890
    87
 * <pre>{@code
jaroslav@1890
    88
 * class FIFOMutex {
jaroslav@1890
    89
 *   private final AtomicBoolean locked = new AtomicBoolean(false);
jaroslav@1890
    90
 *   private final Queue<Thread> waiters
jaroslav@1890
    91
 *     = new ConcurrentLinkedQueue<Thread>();
jaroslav@1890
    92
 *
jaroslav@1890
    93
 *   public void lock() {
jaroslav@1890
    94
 *     boolean wasInterrupted = false;
jaroslav@1890
    95
 *     Thread current = Thread.currentThread();
jaroslav@1890
    96
 *     waiters.add(current);
jaroslav@1890
    97
 *
jaroslav@1890
    98
 *     // Block while not first in queue or cannot acquire lock
jaroslav@1890
    99
 *     while (waiters.peek() != current ||
jaroslav@1890
   100
 *            !locked.compareAndSet(false, true)) {
jaroslav@1890
   101
 *        LockSupport.park(this);
jaroslav@1890
   102
 *        if (Thread.interrupted()) // ignore interrupts while waiting
jaroslav@1890
   103
 *          wasInterrupted = true;
jaroslav@1890
   104
 *     }
jaroslav@1890
   105
 *
jaroslav@1890
   106
 *     waiters.remove();
jaroslav@1890
   107
 *     if (wasInterrupted)          // reassert interrupt status on exit
jaroslav@1890
   108
 *        current.interrupt();
jaroslav@1890
   109
 *   }
jaroslav@1890
   110
 *
jaroslav@1890
   111
 *   public void unlock() {
jaroslav@1890
   112
 *     locked.set(false);
jaroslav@1890
   113
 *     LockSupport.unpark(waiters.peek());
jaroslav@1890
   114
 *   }
jaroslav@1890
   115
 * }}</pre>
jaroslav@1890
   116
 */
jaroslav@1890
   117
jaroslav@1890
   118
public class LockSupport {
jaroslav@1890
   119
    private LockSupport() {} // Cannot be instantiated.
jaroslav@1890
   120
jaroslav@1890
   121
    /**
jaroslav@1890
   122
     * Makes available the permit for the given thread, if it
jaroslav@1890
   123
     * was not already available.  If the thread was blocked on
jaroslav@1890
   124
     * {@code park} then it will unblock.  Otherwise, its next call
jaroslav@1890
   125
     * to {@code park} is guaranteed not to block. This operation
jaroslav@1890
   126
     * is not guaranteed to have any effect at all if the given
jaroslav@1890
   127
     * thread has not been started.
jaroslav@1890
   128
     *
jaroslav@1890
   129
     * @param thread the thread to unpark, or {@code null}, in which case
jaroslav@1890
   130
     *        this operation has no effect
jaroslav@1890
   131
     */
jaroslav@1890
   132
    public static void unpark(Thread thread) {
jaroslav@1890
   133
    }
jaroslav@1890
   134
jaroslav@1890
   135
    /**
jaroslav@1890
   136
     * Disables the current thread for thread scheduling purposes unless the
jaroslav@1890
   137
     * permit is available.
jaroslav@1890
   138
     *
jaroslav@1890
   139
     * <p>If the permit is available then it is consumed and the call returns
jaroslav@1890
   140
     * immediately; otherwise
jaroslav@1890
   141
     * the current thread becomes disabled for thread scheduling
jaroslav@1890
   142
     * purposes and lies dormant until one of three things happens:
jaroslav@1890
   143
     *
jaroslav@1890
   144
     * <ul>
jaroslav@1890
   145
     * <li>Some other thread invokes {@link #unpark unpark} with the
jaroslav@1890
   146
     * current thread as the target; or
jaroslav@1890
   147
     *
jaroslav@1890
   148
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
jaroslav@1890
   149
     * the current thread; or
jaroslav@1890
   150
     *
jaroslav@1890
   151
     * <li>The call spuriously (that is, for no reason) returns.
jaroslav@1890
   152
     * </ul>
jaroslav@1890
   153
     *
jaroslav@1890
   154
     * <p>This method does <em>not</em> report which of these caused the
jaroslav@1890
   155
     * method to return. Callers should re-check the conditions which caused
jaroslav@1890
   156
     * the thread to park in the first place. Callers may also determine,
jaroslav@1890
   157
     * for example, the interrupt status of the thread upon return.
jaroslav@1890
   158
     *
jaroslav@1890
   159
     * @param blocker the synchronization object responsible for this
jaroslav@1890
   160
     *        thread parking
jaroslav@1890
   161
     * @since 1.6
jaroslav@1890
   162
     */
jaroslav@1890
   163
    public static void park(Object blocker) {
jaroslav@1890
   164
    }
jaroslav@1890
   165
jaroslav@1890
   166
    /**
jaroslav@1890
   167
     * Disables the current thread for thread scheduling purposes, for up to
jaroslav@1890
   168
     * the specified waiting time, unless the permit is available.
jaroslav@1890
   169
     *
jaroslav@1890
   170
     * <p>If the permit is available then it is consumed and the call
jaroslav@1890
   171
     * returns immediately; otherwise the current thread becomes disabled
jaroslav@1890
   172
     * for thread scheduling purposes and lies dormant until one of four
jaroslav@1890
   173
     * things happens:
jaroslav@1890
   174
     *
jaroslav@1890
   175
     * <ul>
jaroslav@1890
   176
     * <li>Some other thread invokes {@link #unpark unpark} with the
jaroslav@1890
   177
     * current thread as the target; or
jaroslav@1890
   178
     *
jaroslav@1890
   179
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
jaroslav@1890
   180
     * the current thread; or
jaroslav@1890
   181
     *
jaroslav@1890
   182
     * <li>The specified waiting time elapses; or
jaroslav@1890
   183
     *
jaroslav@1890
   184
     * <li>The call spuriously (that is, for no reason) returns.
jaroslav@1890
   185
     * </ul>
jaroslav@1890
   186
     *
jaroslav@1890
   187
     * <p>This method does <em>not</em> report which of these caused the
jaroslav@1890
   188
     * method to return. Callers should re-check the conditions which caused
jaroslav@1890
   189
     * the thread to park in the first place. Callers may also determine,
jaroslav@1890
   190
     * for example, the interrupt status of the thread, or the elapsed time
jaroslav@1890
   191
     * upon return.
jaroslav@1890
   192
     *
jaroslav@1890
   193
     * @param blocker the synchronization object responsible for this
jaroslav@1890
   194
     *        thread parking
jaroslav@1890
   195
     * @param nanos the maximum number of nanoseconds to wait
jaroslav@1890
   196
     * @since 1.6
jaroslav@1890
   197
     */
jaroslav@1890
   198
    public static void parkNanos(Object blocker, long nanos) {
jaroslav@1890
   199
    }
jaroslav@1890
   200
jaroslav@1890
   201
    /**
jaroslav@1890
   202
     * Disables the current thread for thread scheduling purposes, until
jaroslav@1890
   203
     * the specified deadline, unless the permit is available.
jaroslav@1890
   204
     *
jaroslav@1890
   205
     * <p>If the permit is available then it is consumed and the call
jaroslav@1890
   206
     * returns immediately; otherwise the current thread becomes disabled
jaroslav@1890
   207
     * for thread scheduling purposes and lies dormant until one of four
jaroslav@1890
   208
     * things happens:
jaroslav@1890
   209
     *
jaroslav@1890
   210
     * <ul>
jaroslav@1890
   211
     * <li>Some other thread invokes {@link #unpark unpark} with the
jaroslav@1890
   212
     * current thread as the target; or
jaroslav@1890
   213
     *
jaroslav@1890
   214
     * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
jaroslav@1890
   215
     * current thread; or
jaroslav@1890
   216
     *
jaroslav@1890
   217
     * <li>The specified deadline passes; or
jaroslav@1890
   218
     *
jaroslav@1890
   219
     * <li>The call spuriously (that is, for no reason) returns.
jaroslav@1890
   220
     * </ul>
jaroslav@1890
   221
     *
jaroslav@1890
   222
     * <p>This method does <em>not</em> report which of these caused the
jaroslav@1890
   223
     * method to return. Callers should re-check the conditions which caused
jaroslav@1890
   224
     * the thread to park in the first place. Callers may also determine,
jaroslav@1890
   225
     * for example, the interrupt status of the thread, or the current time
jaroslav@1890
   226
     * upon return.
jaroslav@1890
   227
     *
jaroslav@1890
   228
     * @param blocker the synchronization object responsible for this
jaroslav@1890
   229
     *        thread parking
jaroslav@1890
   230
     * @param deadline the absolute time, in milliseconds from the Epoch,
jaroslav@1890
   231
     *        to wait until
jaroslav@1890
   232
     * @since 1.6
jaroslav@1890
   233
     */
jaroslav@1890
   234
    public static void parkUntil(Object blocker, long deadline) {
jaroslav@1890
   235
    }
jaroslav@1890
   236
jaroslav@1890
   237
    /**
jaroslav@1890
   238
     * Returns the blocker object supplied to the most recent
jaroslav@1890
   239
     * invocation of a park method that has not yet unblocked, or null
jaroslav@1890
   240
     * if not blocked.  The value returned is just a momentary
jaroslav@1890
   241
     * snapshot -- the thread may have since unblocked or blocked on a
jaroslav@1890
   242
     * different blocker object.
jaroslav@1890
   243
     *
jaroslav@1890
   244
     * @param t the thread
jaroslav@1890
   245
     * @return the blocker
jaroslav@1890
   246
     * @throws NullPointerException if argument is null
jaroslav@1890
   247
     * @since 1.6
jaroslav@1890
   248
     */
jaroslav@1890
   249
    public static Object getBlocker(Thread t) {
jaroslav@1894
   250
        return null;
jaroslav@1890
   251
    }
jaroslav@1890
   252
jaroslav@1890
   253
    /**
jaroslav@1890
   254
     * Disables the current thread for thread scheduling purposes unless the
jaroslav@1890
   255
     * permit is available.
jaroslav@1890
   256
     *
jaroslav@1890
   257
     * <p>If the permit is available then it is consumed and the call
jaroslav@1890
   258
     * returns immediately; otherwise the current thread becomes disabled
jaroslav@1890
   259
     * for thread scheduling purposes and lies dormant until one of three
jaroslav@1890
   260
     * things happens:
jaroslav@1890
   261
     *
jaroslav@1890
   262
     * <ul>
jaroslav@1890
   263
     *
jaroslav@1890
   264
     * <li>Some other thread invokes {@link #unpark unpark} with the
jaroslav@1890
   265
     * current thread as the target; or
jaroslav@1890
   266
     *
jaroslav@1890
   267
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
jaroslav@1890
   268
     * the current thread; or
jaroslav@1890
   269
     *
jaroslav@1890
   270
     * <li>The call spuriously (that is, for no reason) returns.
jaroslav@1890
   271
     * </ul>
jaroslav@1890
   272
     *
jaroslav@1890
   273
     * <p>This method does <em>not</em> report which of these caused the
jaroslav@1890
   274
     * method to return. Callers should re-check the conditions which caused
jaroslav@1890
   275
     * the thread to park in the first place. Callers may also determine,
jaroslav@1890
   276
     * for example, the interrupt status of the thread upon return.
jaroslav@1890
   277
     */
jaroslav@1890
   278
    public static void park() {
jaroslav@1890
   279
    }
jaroslav@1890
   280
jaroslav@1890
   281
    /**
jaroslav@1890
   282
     * Disables the current thread for thread scheduling purposes, for up to
jaroslav@1890
   283
     * the specified waiting time, unless the permit is available.
jaroslav@1890
   284
     *
jaroslav@1890
   285
     * <p>If the permit is available then it is consumed and the call
jaroslav@1890
   286
     * returns immediately; otherwise the current thread becomes disabled
jaroslav@1890
   287
     * for thread scheduling purposes and lies dormant until one of four
jaroslav@1890
   288
     * things happens:
jaroslav@1890
   289
     *
jaroslav@1890
   290
     * <ul>
jaroslav@1890
   291
     * <li>Some other thread invokes {@link #unpark unpark} with the
jaroslav@1890
   292
     * current thread as the target; or
jaroslav@1890
   293
     *
jaroslav@1890
   294
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
jaroslav@1890
   295
     * the current thread; or
jaroslav@1890
   296
     *
jaroslav@1890
   297
     * <li>The specified waiting time elapses; or
jaroslav@1890
   298
     *
jaroslav@1890
   299
     * <li>The call spuriously (that is, for no reason) returns.
jaroslav@1890
   300
     * </ul>
jaroslav@1890
   301
     *
jaroslav@1890
   302
     * <p>This method does <em>not</em> report which of these caused the
jaroslav@1890
   303
     * method to return. Callers should re-check the conditions which caused
jaroslav@1890
   304
     * the thread to park in the first place. Callers may also determine,
jaroslav@1890
   305
     * for example, the interrupt status of the thread, or the elapsed time
jaroslav@1890
   306
     * upon return.
jaroslav@1890
   307
     *
jaroslav@1890
   308
     * @param nanos the maximum number of nanoseconds to wait
jaroslav@1890
   309
     */
jaroslav@1890
   310
    public static void parkNanos(long nanos) {
jaroslav@1890
   311
    }
jaroslav@1890
   312
jaroslav@1890
   313
    /**
jaroslav@1890
   314
     * Disables the current thread for thread scheduling purposes, until
jaroslav@1890
   315
     * the specified deadline, unless the permit is available.
jaroslav@1890
   316
     *
jaroslav@1890
   317
     * <p>If the permit is available then it is consumed and the call
jaroslav@1890
   318
     * returns immediately; otherwise the current thread becomes disabled
jaroslav@1890
   319
     * for thread scheduling purposes and lies dormant until one of four
jaroslav@1890
   320
     * things happens:
jaroslav@1890
   321
     *
jaroslav@1890
   322
     * <ul>
jaroslav@1890
   323
     * <li>Some other thread invokes {@link #unpark unpark} with the
jaroslav@1890
   324
     * current thread as the target; or
jaroslav@1890
   325
     *
jaroslav@1890
   326
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
jaroslav@1890
   327
     * the current thread; or
jaroslav@1890
   328
     *
jaroslav@1890
   329
     * <li>The specified deadline passes; or
jaroslav@1890
   330
     *
jaroslav@1890
   331
     * <li>The call spuriously (that is, for no reason) returns.
jaroslav@1890
   332
     * </ul>
jaroslav@1890
   333
     *
jaroslav@1890
   334
     * <p>This method does <em>not</em> report which of these caused the
jaroslav@1890
   335
     * method to return. Callers should re-check the conditions which caused
jaroslav@1890
   336
     * the thread to park in the first place. Callers may also determine,
jaroslav@1890
   337
     * for example, the interrupt status of the thread, or the current time
jaroslav@1890
   338
     * upon return.
jaroslav@1890
   339
     *
jaroslav@1890
   340
     * @param deadline the absolute time, in milliseconds from the Epoch,
jaroslav@1890
   341
     *        to wait until
jaroslav@1890
   342
     */
jaroslav@1890
   343
    public static void parkUntil(long deadline) {
jaroslav@1890
   344
    }
jaroslav@1890
   345
}