src/java.base/share/classes/java/util/concurrent/locks/LockSupport.java
author martin
Thu, 29 Mar 2018 16:23:35 -0700
changeset 49491 f7363de371c9
parent 47216 71c04702a3d5
child 52220 9c260a6b6471
permissions -rw-r--r--
8200123: Replace Thread.init with telescoping constructor Reviewed-by: dholmes, mchung, plevart
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
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 34348
diff changeset
    38
import jdk.internal.misc.Unsafe;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 34348
diff changeset
    39
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Basic thread blocking primitives for creating locks and other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * synchronization classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>This class associates, with each thread that uses it, a permit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * (in the sense of the {@link java.util.concurrent.Semaphore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Semaphore} class). A call to {@code park} will return immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * if the permit is available, consuming it in the process; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * it <em>may</em> block.  A call to {@code unpark} makes the permit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * available, if it was not already available. (Unlike with Semaphores
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * though, permits do not accumulate. There is at most one.)
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    51
 * Reliable usage requires the use of volatile (or atomic) variables
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    52
 * to control when to park or unpark.  Orderings of calls to these
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    53
 * methods are maintained with respect to volatile variable accesses,
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    54
 * but not necessarily non-volatile variable accesses.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>Methods {@code park} and {@code unpark} provide efficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * means of blocking and unblocking threads that do not encounter the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * problems that cause the deprecated methods {@code Thread.suspend}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * and {@code Thread.resume} to be unusable for such purposes: Races
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * between one thread invoking {@code park} and another thread trying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * to {@code unpark} it will preserve liveness, due to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * permit. Additionally, {@code park} will return if the caller's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * thread was interrupted, and timeout versions are supported. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * {@code park} method may also return at any other time, for "no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * reason", so in general must be invoked within a loop that rechecks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * conditions upon return. In this sense {@code park} serves as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * optimization of a "busy wait" that does not waste as much time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * spinning, but must be paired with an {@code unpark} to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * effective.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>The three forms of {@code park} each also support a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * {@code blocker} object parameter. This object is recorded while
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * the thread is blocked to permit monitoring and diagnostic tools to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * identify the reasons that threads are blocked. (Such tools may
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 15648
diff changeset
    75
 * access blockers using method {@link #getBlocker(Thread)}.)
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 15648
diff changeset
    76
 * The use of these forms rather than the original forms without this
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 15648
diff changeset
    77
 * parameter is strongly encouraged. The normal argument to supply as
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 15648
diff changeset
    78
 * a {@code blocker} within a lock implementation is {@code this}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <p>These methods are designed to be used as tools for creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * higher-level synchronization utilities, and are not in themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * useful for most concurrency control applications.  The {@code park}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * method is designed for use only in constructions of the form:
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9675
diff changeset
    84
 *
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    85
 * <pre> {@code
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
    86
 * while (!canProceed()) {
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
    87
 *   // ensure request to unpark is visible to other threads
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
    88
 *   ...
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
    89
 *   LockSupport.park(this);
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
    90
 * }}</pre>
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9675
diff changeset
    91
 *
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
    92
 * where no actions by the thread publishing a request to unpark,
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
    93
 * prior to the call to {@code park}, entail locking or blocking.
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
    94
 * Because only one permit is associated with each thread, any
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
    95
 * intermediary uses of {@code park}, including implicitly via class
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
    96
 * loading, could lead to an unresponsive thread (a "lost unpark").
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <p><b>Sample Usage.</b> Here is a sketch of a first-in-first-out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * non-reentrant lock class:
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   100
 * <pre> {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * class FIFOMutex {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *   private final AtomicBoolean locked = new AtomicBoolean(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *   private final Queue<Thread> waiters
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   104
 *     = new ConcurrentLinkedQueue<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *   public void lock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *     boolean wasInterrupted = false;
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   108
 *     // publish current thread for unparkers
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   109
 *     waiters.add(Thread.currentThread());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *     // Block while not first in queue or cannot acquire lock
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   112
 *     while (waiters.peek() != Thread.currentThread() ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *            !locked.compareAndSet(false, true)) {
15648
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   114
 *       LockSupport.park(this);
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   115
 *       // ignore interrupts while waiting
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   116
 *       if (Thread.interrupted())
15648
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   117
 *         wasInterrupted = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *     waiters.remove();
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   121
 *     // ensure correct interrupt status on return
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   122
 *     if (wasInterrupted)
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   123
 *       Thread.currentThread().interrupt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *   public void unlock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *     locked.set(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *     LockSupport.unpark(waiters.peek());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *   }
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   130
 *
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   131
 *   static {
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   132
 *     // Reduce the risk of "lost unpark" due to classloading
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   133
 *     Class<?> ensureLoaded = LockSupport.class;
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   134
 *   }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * }}</pre>
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 43522
diff changeset
   136
 *
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 43522
diff changeset
   137
 * @since 1.5
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
public class LockSupport {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private LockSupport() {} // Cannot be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static void setBlocker(Thread t, Object arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // Even though volatile, hotspot doesn't need a write barrier here.
43226
6fc9bdd300f1 8173083: VarHandle usages in LockSupport and ThreadLocalRandom result in circularity issues
psandoz
parents: 43219
diff changeset
   144
        U.putObject(t, PARKBLOCKER, arg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Makes available the permit for the given thread, if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * was not already available.  If the thread was blocked on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * {@code park} then it will unblock.  Otherwise, its next call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * to {@code park} is guaranteed not to block. This operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * is not guaranteed to have any effect at all if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * thread has not been started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param thread the thread to unpark, or {@code null}, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *        this operation has no effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public static void unpark(Thread thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (thread != null)
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   160
            U.unpark(thread);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Disables the current thread for thread scheduling purposes unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * permit is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p>If the permit is available then it is consumed and the call returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * immediately; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * the current thread becomes disabled for thread scheduling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * purposes and lies dormant until one of three things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <li>Some other thread invokes {@link #unpark unpark} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * current thread as the target; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <li>The call spuriously (that is, for no reason) returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <p>This method does <em>not</em> report which of these caused the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * method to return. Callers should re-check the conditions which caused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * the thread to park in the first place. Callers may also determine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * for example, the interrupt status of the thread upon return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param blocker the synchronization object responsible for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *        thread parking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public static void park(Object blocker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        Thread t = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        setBlocker(t, blocker);
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   194
        U.park(false, 0L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        setBlocker(t, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Disables the current thread for thread scheduling purposes, for up to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * the specified waiting time, unless the permit is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <p>If the permit is available then it is consumed and the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * returns immediately; otherwise the current thread becomes disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * for thread scheduling purposes and lies dormant until one of four
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <li>Some other thread invokes {@link #unpark unpark} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * current thread as the target; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   211
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   212
     * the current thread; or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <li>The specified waiting time elapses; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <li>The call spuriously (that is, for no reason) returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <p>This method does <em>not</em> report which of these caused the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * method to return. Callers should re-check the conditions which caused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * the thread to park in the first place. Callers may also determine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * for example, the interrupt status of the thread, or the elapsed time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * upon return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param blocker the synchronization object responsible for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *        thread parking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param nanos the maximum number of nanoseconds to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public static void parkNanos(Object blocker, long nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (nanos > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            Thread t = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            setBlocker(t, blocker);
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   234
            U.park(false, nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            setBlocker(t, null);
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
     * Disables the current thread for thread scheduling purposes, until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * the specified deadline, unless the permit is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <p>If the permit is available then it is consumed and the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * returns immediately; otherwise the current thread becomes disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * for thread scheduling purposes and lies dormant until one of four
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <li>Some other thread invokes {@link #unpark unpark} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * current thread as the target; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <li>The specified deadline passes; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <li>The call spuriously (that is, for no reason) returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <p>This method does <em>not</em> report which of these caused the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * method to return. Callers should re-check the conditions which caused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * the thread to park in the first place. Callers may also determine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * for example, the interrupt status of the thread, or the current time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * upon return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param blocker the synchronization object responsible for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *        thread parking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param deadline the absolute time, in milliseconds from the Epoch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *        to wait until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public static void parkUntil(Object blocker, long deadline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        Thread t = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        setBlocker(t, blocker);
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   275
        U.park(true, deadline);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        setBlocker(t, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Returns the blocker object supplied to the most recent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * invocation of a park method that has not yet unblocked, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * if not blocked.  The value returned is just a momentary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * snapshot -- the thread may have since unblocked or blocked on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * different blocker object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
9675
5aaa241a1c44 7042673: LockSupport.getBlocker(null) crashes
dl
parents: 9242
diff changeset
   286
     * @param t the thread
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @return the blocker
9675
5aaa241a1c44 7042673: LockSupport.getBlocker(null) crashes
dl
parents: 9242
diff changeset
   288
     * @throws NullPointerException if argument is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public static Object getBlocker(Thread t) {
9675
5aaa241a1c44 7042673: LockSupport.getBlocker(null) crashes
dl
parents: 9242
diff changeset
   292
        if (t == null)
5aaa241a1c44 7042673: LockSupport.getBlocker(null) crashes
dl
parents: 9242
diff changeset
   293
            throw new NullPointerException();
43226
6fc9bdd300f1 8173083: VarHandle usages in LockSupport and ThreadLocalRandom result in circularity issues
psandoz
parents: 43219
diff changeset
   294
        return U.getObjectVolatile(t, PARKBLOCKER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Disables the current thread for thread scheduling purposes unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * permit is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <p>If the permit is available then it is consumed and the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * returns immediately; otherwise the current thread becomes disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * for thread scheduling purposes and lies dormant until one of three
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <li>Some other thread invokes {@link #unpark unpark} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * current thread as the target; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * <li>The call spuriously (that is, for no reason) returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <p>This method does <em>not</em> report which of these caused the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * method to return. Callers should re-check the conditions which caused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * the thread to park in the first place. Callers may also determine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * for example, the interrupt status of the thread upon return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public static void park() {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   323
        U.park(false, 0L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Disables the current thread for thread scheduling purposes, for up to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * the specified waiting time, unless the permit is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <p>If the permit is available then it is consumed and the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * returns immediately; otherwise the current thread becomes disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * for thread scheduling purposes and lies dormant until one of four
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * <li>Some other thread invokes {@link #unpark unpark} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * current thread as the target; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <li>The specified waiting time elapses; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * <li>The call spuriously (that is, for no reason) returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * <p>This method does <em>not</em> report which of these caused the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * method to return. Callers should re-check the conditions which caused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * the thread to park in the first place. Callers may also determine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * for example, the interrupt status of the thread, or the elapsed time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * upon return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param nanos the maximum number of nanoseconds to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public static void parkNanos(long nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (nanos > 0)
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   357
            U.park(false, nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Disables the current thread for thread scheduling purposes, until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * the specified deadline, unless the permit is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * <p>If the permit is available then it is consumed and the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * returns immediately; otherwise the current thread becomes disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * for thread scheduling purposes and lies dormant until one of four
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <li>Some other thread invokes {@link #unpark unpark} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * current thread as the target; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <li>The specified deadline passes; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * <li>The call spuriously (that is, for no reason) returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <p>This method does <em>not</em> report which of these caused the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * method to return. Callers should re-check the conditions which caused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * the thread to park in the first place. Callers may also determine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * for example, the interrupt status of the thread, or the current time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * upon return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @param deadline the absolute time, in milliseconds from the Epoch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *        to wait until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public static void parkUntil(long deadline) {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   391
        U.park(true, deadline);
15648
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   392
    }
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   393
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   394
    /**
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   395
     * Returns the pseudo-randomly initialized or updated secondary seed.
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   396
     * Copied from ThreadLocalRandom due to package access restrictions.
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   397
     */
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   398
    static final int nextSecondarySeed() {
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   399
        int r;
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   400
        Thread t = Thread.currentThread();
43226
6fc9bdd300f1 8173083: VarHandle usages in LockSupport and ThreadLocalRandom result in circularity issues
psandoz
parents: 43219
diff changeset
   401
        if ((r = U.getInt(t, SECONDARY)) != 0) {
15648
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   402
            r ^= r << 13;   // xorshift
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   403
            r ^= r >>> 17;
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   404
            r ^= r << 5;
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   405
        }
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   406
        else if ((r = java.util.concurrent.ThreadLocalRandom.current().nextInt()) == 0)
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   407
            r = 1; // avoid zero
43226
6fc9bdd300f1 8173083: VarHandle usages in LockSupport and ThreadLocalRandom result in circularity issues
psandoz
parents: 43219
diff changeset
   408
        U.putInt(t, SECONDARY, r);
15648
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   409
        return r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
15648
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   411
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 34348
diff changeset
   412
    /**
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 34348
diff changeset
   413
     * Returns the thread id for the given thread.  We must access
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 34348
diff changeset
   414
     * this directly rather than via method Thread.getId() because
49491
f7363de371c9 8200123: Replace Thread.init with telescoping constructor
martin
parents: 47216
diff changeset
   415
     * getId() has been known to be overridden in ways that do not
f7363de371c9 8200123: Replace Thread.init with telescoping constructor
martin
parents: 47216
diff changeset
   416
     * preserve unique mappings.
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 34348
diff changeset
   417
     */
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 34348
diff changeset
   418
    static final long getThreadId(Thread thread) {
49491
f7363de371c9 8200123: Replace Thread.init with telescoping constructor
martin
parents: 47216
diff changeset
   419
        return U.getLong(thread, TID);
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 34348
diff changeset
   420
    }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 34348
diff changeset
   421
15648
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   422
    // Hotspot implementation via intrinsics API
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 34348
diff changeset
   423
    private static final Unsafe U = Unsafe.getUnsafe();
46873
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45434
diff changeset
   424
    private static final long PARKBLOCKER = U.objectFieldOffset
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45434
diff changeset
   425
            (Thread.class, "parkBlocker");
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45434
diff changeset
   426
    private static final long SECONDARY = U.objectFieldOffset
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45434
diff changeset
   427
            (Thread.class, "threadLocalRandomSecondarySeed");
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45434
diff changeset
   428
    private static final long TID = U.objectFieldOffset
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45434
diff changeset
   429
            (Thread.class, "tid");
15648
6a38cf764825 8005697: Add StampedLock
dl
parents: 14325
diff changeset
   430
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
}