rt/emul/compact/src/main/java/java/util/concurrent/locks/LockSupport.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 19 Mar 2016 10:46:31 +0100
branchjdk7-b147
changeset 1890 212417b74b72
child 1894 75ee4eca04e3
permissions -rw-r--r--
Bringing in all concurrent package from JDK7-b147
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
import java.util.concurrent.*;
jaroslav@1890
    38
import sun.misc.Unsafe;
jaroslav@1890
    39
jaroslav@1890
    40
jaroslav@1890
    41
/**
jaroslav@1890
    42
 * Basic thread blocking primitives for creating locks and other
jaroslav@1890
    43
 * synchronization classes.
jaroslav@1890
    44
 *
jaroslav@1890
    45
 * <p>This class associates, with each thread that uses it, a permit
jaroslav@1890
    46
 * (in the sense of the {@link java.util.concurrent.Semaphore
jaroslav@1890
    47
 * Semaphore} class). A call to {@code park} will return immediately
jaroslav@1890
    48
 * if the permit is available, consuming it in the process; otherwise
jaroslav@1890
    49
 * it <em>may</em> block.  A call to {@code unpark} makes the permit
jaroslav@1890
    50
 * available, if it was not already available. (Unlike with Semaphores
jaroslav@1890
    51
 * though, permits do not accumulate. There is at most one.)
jaroslav@1890
    52
 *
jaroslav@1890
    53
 * <p>Methods {@code park} and {@code unpark} provide efficient
jaroslav@1890
    54
 * means of blocking and unblocking threads that do not encounter the
jaroslav@1890
    55
 * problems that cause the deprecated methods {@code Thread.suspend}
jaroslav@1890
    56
 * and {@code Thread.resume} to be unusable for such purposes: Races
jaroslav@1890
    57
 * between one thread invoking {@code park} and another thread trying
jaroslav@1890
    58
 * to {@code unpark} it will preserve liveness, due to the
jaroslav@1890
    59
 * permit. Additionally, {@code park} will return if the caller's
jaroslav@1890
    60
 * thread was interrupted, and timeout versions are supported. The
jaroslav@1890
    61
 * {@code park} method may also return at any other time, for "no
jaroslav@1890
    62
 * reason", so in general must be invoked within a loop that rechecks
jaroslav@1890
    63
 * conditions upon return. In this sense {@code park} serves as an
jaroslav@1890
    64
 * optimization of a "busy wait" that does not waste as much time
jaroslav@1890
    65
 * spinning, but must be paired with an {@code unpark} to be
jaroslav@1890
    66
 * effective.
jaroslav@1890
    67
 *
jaroslav@1890
    68
 * <p>The three forms of {@code park} each also support a
jaroslav@1890
    69
 * {@code blocker} object parameter. This object is recorded while
jaroslav@1890
    70
 * the thread is blocked to permit monitoring and diagnostic tools to
jaroslav@1890
    71
 * identify the reasons that threads are blocked. (Such tools may
jaroslav@1890
    72
 * access blockers using method {@link #getBlocker}.) The use of these
jaroslav@1890
    73
 * forms rather than the original forms without this parameter is
jaroslav@1890
    74
 * strongly encouraged. The normal argument to supply as a
jaroslav@1890
    75
 * {@code blocker} within a lock implementation is {@code this}.
jaroslav@1890
    76
 *
jaroslav@1890
    77
 * <p>These methods are designed to be used as tools for creating
jaroslav@1890
    78
 * higher-level synchronization utilities, and are not in themselves
jaroslav@1890
    79
 * useful for most concurrency control applications.  The {@code park}
jaroslav@1890
    80
 * method is designed for use only in constructions of the form:
jaroslav@1890
    81
 * <pre>while (!canProceed()) { ... LockSupport.park(this); }</pre>
jaroslav@1890
    82
 * where neither {@code canProceed} nor any other actions prior to the
jaroslav@1890
    83
 * call to {@code park} entail locking or blocking.  Because only one
jaroslav@1890
    84
 * permit is associated with each thread, any intermediary uses of
jaroslav@1890
    85
 * {@code park} could interfere with its intended effects.
jaroslav@1890
    86
 *
jaroslav@1890
    87
 * <p><b>Sample Usage.</b> Here is a sketch of a first-in-first-out
jaroslav@1890
    88
 * non-reentrant lock class:
jaroslav@1890
    89
 * <pre>{@code
jaroslav@1890
    90
 * class FIFOMutex {
jaroslav@1890
    91
 *   private final AtomicBoolean locked = new AtomicBoolean(false);
jaroslav@1890
    92
 *   private final Queue<Thread> waiters
jaroslav@1890
    93
 *     = new ConcurrentLinkedQueue<Thread>();
jaroslav@1890
    94
 *
jaroslav@1890
    95
 *   public void lock() {
jaroslav@1890
    96
 *     boolean wasInterrupted = false;
jaroslav@1890
    97
 *     Thread current = Thread.currentThread();
jaroslav@1890
    98
 *     waiters.add(current);
jaroslav@1890
    99
 *
jaroslav@1890
   100
 *     // Block while not first in queue or cannot acquire lock
jaroslav@1890
   101
 *     while (waiters.peek() != current ||
jaroslav@1890
   102
 *            !locked.compareAndSet(false, true)) {
jaroslav@1890
   103
 *        LockSupport.park(this);
jaroslav@1890
   104
 *        if (Thread.interrupted()) // ignore interrupts while waiting
jaroslav@1890
   105
 *          wasInterrupted = true;
jaroslav@1890
   106
 *     }
jaroslav@1890
   107
 *
jaroslav@1890
   108
 *     waiters.remove();
jaroslav@1890
   109
 *     if (wasInterrupted)          // reassert interrupt status on exit
jaroslav@1890
   110
 *        current.interrupt();
jaroslav@1890
   111
 *   }
jaroslav@1890
   112
 *
jaroslav@1890
   113
 *   public void unlock() {
jaroslav@1890
   114
 *     locked.set(false);
jaroslav@1890
   115
 *     LockSupport.unpark(waiters.peek());
jaroslav@1890
   116
 *   }
jaroslav@1890
   117
 * }}</pre>
jaroslav@1890
   118
 */
jaroslav@1890
   119
jaroslav@1890
   120
public class LockSupport {
jaroslav@1890
   121
    private LockSupport() {} // Cannot be instantiated.
jaroslav@1890
   122
jaroslav@1890
   123
    // Hotspot implementation via intrinsics API
jaroslav@1890
   124
    private static final Unsafe unsafe = Unsafe.getUnsafe();
jaroslav@1890
   125
    private static final long parkBlockerOffset;
jaroslav@1890
   126
jaroslav@1890
   127
    static {
jaroslav@1890
   128
        try {
jaroslav@1890
   129
            parkBlockerOffset = unsafe.objectFieldOffset
jaroslav@1890
   130
                (java.lang.Thread.class.getDeclaredField("parkBlocker"));
jaroslav@1890
   131
        } catch (Exception ex) { throw new Error(ex); }
jaroslav@1890
   132
    }
jaroslav@1890
   133
jaroslav@1890
   134
    private static void setBlocker(Thread t, Object arg) {
jaroslav@1890
   135
        // Even though volatile, hotspot doesn't need a write barrier here.
jaroslav@1890
   136
        unsafe.putObject(t, parkBlockerOffset, arg);
jaroslav@1890
   137
    }
jaroslav@1890
   138
jaroslav@1890
   139
    /**
jaroslav@1890
   140
     * Makes available the permit for the given thread, if it
jaroslav@1890
   141
     * was not already available.  If the thread was blocked on
jaroslav@1890
   142
     * {@code park} then it will unblock.  Otherwise, its next call
jaroslav@1890
   143
     * to {@code park} is guaranteed not to block. This operation
jaroslav@1890
   144
     * is not guaranteed to have any effect at all if the given
jaroslav@1890
   145
     * thread has not been started.
jaroslav@1890
   146
     *
jaroslav@1890
   147
     * @param thread the thread to unpark, or {@code null}, in which case
jaroslav@1890
   148
     *        this operation has no effect
jaroslav@1890
   149
     */
jaroslav@1890
   150
    public static void unpark(Thread thread) {
jaroslav@1890
   151
        if (thread != null)
jaroslav@1890
   152
            unsafe.unpark(thread);
jaroslav@1890
   153
    }
jaroslav@1890
   154
jaroslav@1890
   155
    /**
jaroslav@1890
   156
     * Disables the current thread for thread scheduling purposes unless the
jaroslav@1890
   157
     * permit is available.
jaroslav@1890
   158
     *
jaroslav@1890
   159
     * <p>If the permit is available then it is consumed and the call returns
jaroslav@1890
   160
     * immediately; otherwise
jaroslav@1890
   161
     * the current thread becomes disabled for thread scheduling
jaroslav@1890
   162
     * purposes and lies dormant until one of three things happens:
jaroslav@1890
   163
     *
jaroslav@1890
   164
     * <ul>
jaroslav@1890
   165
     * <li>Some other thread invokes {@link #unpark unpark} with the
jaroslav@1890
   166
     * current thread as the target; or
jaroslav@1890
   167
     *
jaroslav@1890
   168
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
jaroslav@1890
   169
     * the current thread; or
jaroslav@1890
   170
     *
jaroslav@1890
   171
     * <li>The call spuriously (that is, for no reason) returns.
jaroslav@1890
   172
     * </ul>
jaroslav@1890
   173
     *
jaroslav@1890
   174
     * <p>This method does <em>not</em> report which of these caused the
jaroslav@1890
   175
     * method to return. Callers should re-check the conditions which caused
jaroslav@1890
   176
     * the thread to park in the first place. Callers may also determine,
jaroslav@1890
   177
     * for example, the interrupt status of the thread upon return.
jaroslav@1890
   178
     *
jaroslav@1890
   179
     * @param blocker the synchronization object responsible for this
jaroslav@1890
   180
     *        thread parking
jaroslav@1890
   181
     * @since 1.6
jaroslav@1890
   182
     */
jaroslav@1890
   183
    public static void park(Object blocker) {
jaroslav@1890
   184
        Thread t = Thread.currentThread();
jaroslav@1890
   185
        setBlocker(t, blocker);
jaroslav@1890
   186
        unsafe.park(false, 0L);
jaroslav@1890
   187
        setBlocker(t, null);
jaroslav@1890
   188
    }
jaroslav@1890
   189
jaroslav@1890
   190
    /**
jaroslav@1890
   191
     * Disables the current thread for thread scheduling purposes, for up to
jaroslav@1890
   192
     * the specified waiting time, unless the permit is available.
jaroslav@1890
   193
     *
jaroslav@1890
   194
     * <p>If the permit is available then it is consumed and the call
jaroslav@1890
   195
     * returns immediately; otherwise the current thread becomes disabled
jaroslav@1890
   196
     * for thread scheduling purposes and lies dormant until one of four
jaroslav@1890
   197
     * things happens:
jaroslav@1890
   198
     *
jaroslav@1890
   199
     * <ul>
jaroslav@1890
   200
     * <li>Some other thread invokes {@link #unpark unpark} with the
jaroslav@1890
   201
     * current thread as the target; or
jaroslav@1890
   202
     *
jaroslav@1890
   203
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
jaroslav@1890
   204
     * the current thread; or
jaroslav@1890
   205
     *
jaroslav@1890
   206
     * <li>The specified waiting time elapses; or
jaroslav@1890
   207
     *
jaroslav@1890
   208
     * <li>The call spuriously (that is, for no reason) returns.
jaroslav@1890
   209
     * </ul>
jaroslav@1890
   210
     *
jaroslav@1890
   211
     * <p>This method does <em>not</em> report which of these caused the
jaroslav@1890
   212
     * method to return. Callers should re-check the conditions which caused
jaroslav@1890
   213
     * the thread to park in the first place. Callers may also determine,
jaroslav@1890
   214
     * for example, the interrupt status of the thread, or the elapsed time
jaroslav@1890
   215
     * upon return.
jaroslav@1890
   216
     *
jaroslav@1890
   217
     * @param blocker the synchronization object responsible for this
jaroslav@1890
   218
     *        thread parking
jaroslav@1890
   219
     * @param nanos the maximum number of nanoseconds to wait
jaroslav@1890
   220
     * @since 1.6
jaroslav@1890
   221
     */
jaroslav@1890
   222
    public static void parkNanos(Object blocker, long nanos) {
jaroslav@1890
   223
        if (nanos > 0) {
jaroslav@1890
   224
            Thread t = Thread.currentThread();
jaroslav@1890
   225
            setBlocker(t, blocker);
jaroslav@1890
   226
            unsafe.park(false, nanos);
jaroslav@1890
   227
            setBlocker(t, null);
jaroslav@1890
   228
        }
jaroslav@1890
   229
    }
jaroslav@1890
   230
jaroslav@1890
   231
    /**
jaroslav@1890
   232
     * Disables the current thread for thread scheduling purposes, until
jaroslav@1890
   233
     * the specified deadline, unless the permit is available.
jaroslav@1890
   234
     *
jaroslav@1890
   235
     * <p>If the permit is available then it is consumed and the call
jaroslav@1890
   236
     * returns immediately; otherwise the current thread becomes disabled
jaroslav@1890
   237
     * for thread scheduling purposes and lies dormant until one of four
jaroslav@1890
   238
     * things happens:
jaroslav@1890
   239
     *
jaroslav@1890
   240
     * <ul>
jaroslav@1890
   241
     * <li>Some other thread invokes {@link #unpark unpark} with the
jaroslav@1890
   242
     * current thread as the target; or
jaroslav@1890
   243
     *
jaroslav@1890
   244
     * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
jaroslav@1890
   245
     * current thread; or
jaroslav@1890
   246
     *
jaroslav@1890
   247
     * <li>The specified deadline passes; or
jaroslav@1890
   248
     *
jaroslav@1890
   249
     * <li>The call spuriously (that is, for no reason) returns.
jaroslav@1890
   250
     * </ul>
jaroslav@1890
   251
     *
jaroslav@1890
   252
     * <p>This method does <em>not</em> report which of these caused the
jaroslav@1890
   253
     * method to return. Callers should re-check the conditions which caused
jaroslav@1890
   254
     * the thread to park in the first place. Callers may also determine,
jaroslav@1890
   255
     * for example, the interrupt status of the thread, or the current time
jaroslav@1890
   256
     * upon return.
jaroslav@1890
   257
     *
jaroslav@1890
   258
     * @param blocker the synchronization object responsible for this
jaroslav@1890
   259
     *        thread parking
jaroslav@1890
   260
     * @param deadline the absolute time, in milliseconds from the Epoch,
jaroslav@1890
   261
     *        to wait until
jaroslav@1890
   262
     * @since 1.6
jaroslav@1890
   263
     */
jaroslav@1890
   264
    public static void parkUntil(Object blocker, long deadline) {
jaroslav@1890
   265
        Thread t = Thread.currentThread();
jaroslav@1890
   266
        setBlocker(t, blocker);
jaroslav@1890
   267
        unsafe.park(true, deadline);
jaroslav@1890
   268
        setBlocker(t, null);
jaroslav@1890
   269
    }
jaroslav@1890
   270
jaroslav@1890
   271
    /**
jaroslav@1890
   272
     * Returns the blocker object supplied to the most recent
jaroslav@1890
   273
     * invocation of a park method that has not yet unblocked, or null
jaroslav@1890
   274
     * if not blocked.  The value returned is just a momentary
jaroslav@1890
   275
     * snapshot -- the thread may have since unblocked or blocked on a
jaroslav@1890
   276
     * different blocker object.
jaroslav@1890
   277
     *
jaroslav@1890
   278
     * @param t the thread
jaroslav@1890
   279
     * @return the blocker
jaroslav@1890
   280
     * @throws NullPointerException if argument is null
jaroslav@1890
   281
     * @since 1.6
jaroslav@1890
   282
     */
jaroslav@1890
   283
    public static Object getBlocker(Thread t) {
jaroslav@1890
   284
        if (t == null)
jaroslav@1890
   285
            throw new NullPointerException();
jaroslav@1890
   286
        return unsafe.getObjectVolatile(t, parkBlockerOffset);
jaroslav@1890
   287
    }
jaroslav@1890
   288
jaroslav@1890
   289
    /**
jaroslav@1890
   290
     * Disables the current thread for thread scheduling purposes unless the
jaroslav@1890
   291
     * permit is available.
jaroslav@1890
   292
     *
jaroslav@1890
   293
     * <p>If the permit is available then it is consumed and the call
jaroslav@1890
   294
     * returns immediately; otherwise the current thread becomes disabled
jaroslav@1890
   295
     * for thread scheduling purposes and lies dormant until one of three
jaroslav@1890
   296
     * things happens:
jaroslav@1890
   297
     *
jaroslav@1890
   298
     * <ul>
jaroslav@1890
   299
     *
jaroslav@1890
   300
     * <li>Some other thread invokes {@link #unpark unpark} with the
jaroslav@1890
   301
     * current thread as the target; or
jaroslav@1890
   302
     *
jaroslav@1890
   303
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
jaroslav@1890
   304
     * the current thread; or
jaroslav@1890
   305
     *
jaroslav@1890
   306
     * <li>The call spuriously (that is, for no reason) returns.
jaroslav@1890
   307
     * </ul>
jaroslav@1890
   308
     *
jaroslav@1890
   309
     * <p>This method does <em>not</em> report which of these caused the
jaroslav@1890
   310
     * method to return. Callers should re-check the conditions which caused
jaroslav@1890
   311
     * the thread to park in the first place. Callers may also determine,
jaroslav@1890
   312
     * for example, the interrupt status of the thread upon return.
jaroslav@1890
   313
     */
jaroslav@1890
   314
    public static void park() {
jaroslav@1890
   315
        unsafe.park(false, 0L);
jaroslav@1890
   316
    }
jaroslav@1890
   317
jaroslav@1890
   318
    /**
jaroslav@1890
   319
     * Disables the current thread for thread scheduling purposes, for up to
jaroslav@1890
   320
     * the specified waiting time, unless the permit is available.
jaroslav@1890
   321
     *
jaroslav@1890
   322
     * <p>If the permit is available then it is consumed and the call
jaroslav@1890
   323
     * returns immediately; otherwise the current thread becomes disabled
jaroslav@1890
   324
     * for thread scheduling purposes and lies dormant until one of four
jaroslav@1890
   325
     * things happens:
jaroslav@1890
   326
     *
jaroslav@1890
   327
     * <ul>
jaroslav@1890
   328
     * <li>Some other thread invokes {@link #unpark unpark} with the
jaroslav@1890
   329
     * current thread as the target; or
jaroslav@1890
   330
     *
jaroslav@1890
   331
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
jaroslav@1890
   332
     * the current thread; or
jaroslav@1890
   333
     *
jaroslav@1890
   334
     * <li>The specified waiting time elapses; or
jaroslav@1890
   335
     *
jaroslav@1890
   336
     * <li>The call spuriously (that is, for no reason) returns.
jaroslav@1890
   337
     * </ul>
jaroslav@1890
   338
     *
jaroslav@1890
   339
     * <p>This method does <em>not</em> report which of these caused the
jaroslav@1890
   340
     * method to return. Callers should re-check the conditions which caused
jaroslav@1890
   341
     * the thread to park in the first place. Callers may also determine,
jaroslav@1890
   342
     * for example, the interrupt status of the thread, or the elapsed time
jaroslav@1890
   343
     * upon return.
jaroslav@1890
   344
     *
jaroslav@1890
   345
     * @param nanos the maximum number of nanoseconds to wait
jaroslav@1890
   346
     */
jaroslav@1890
   347
    public static void parkNanos(long nanos) {
jaroslav@1890
   348
        if (nanos > 0)
jaroslav@1890
   349
            unsafe.park(false, nanos);
jaroslav@1890
   350
    }
jaroslav@1890
   351
jaroslav@1890
   352
    /**
jaroslav@1890
   353
     * Disables the current thread for thread scheduling purposes, until
jaroslav@1890
   354
     * the specified deadline, unless the permit is available.
jaroslav@1890
   355
     *
jaroslav@1890
   356
     * <p>If the permit is available then it is consumed and the call
jaroslav@1890
   357
     * returns immediately; otherwise the current thread becomes disabled
jaroslav@1890
   358
     * for thread scheduling purposes and lies dormant until one of four
jaroslav@1890
   359
     * things happens:
jaroslav@1890
   360
     *
jaroslav@1890
   361
     * <ul>
jaroslav@1890
   362
     * <li>Some other thread invokes {@link #unpark unpark} with the
jaroslav@1890
   363
     * current thread as the target; or
jaroslav@1890
   364
     *
jaroslav@1890
   365
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
jaroslav@1890
   366
     * the current thread; or
jaroslav@1890
   367
     *
jaroslav@1890
   368
     * <li>The specified deadline passes; or
jaroslav@1890
   369
     *
jaroslav@1890
   370
     * <li>The call spuriously (that is, for no reason) returns.
jaroslav@1890
   371
     * </ul>
jaroslav@1890
   372
     *
jaroslav@1890
   373
     * <p>This method does <em>not</em> report which of these caused the
jaroslav@1890
   374
     * method to return. Callers should re-check the conditions which caused
jaroslav@1890
   375
     * the thread to park in the first place. Callers may also determine,
jaroslav@1890
   376
     * for example, the interrupt status of the thread, or the current time
jaroslav@1890
   377
     * upon return.
jaroslav@1890
   378
     *
jaroslav@1890
   379
     * @param deadline the absolute time, in milliseconds from the Epoch,
jaroslav@1890
   380
     *        to wait until
jaroslav@1890
   381
     */
jaroslav@1890
   382
    public static void parkUntil(long deadline) {
jaroslav@1890
   383
        unsafe.park(true, deadline);
jaroslav@1890
   384
    }
jaroslav@1890
   385
}