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