src/hotspot/share/runtime/mutex.cpp
author kbarrett
Sat, 08 Dec 2018 18:52:57 -0500
changeset 52913 bf2f2560dd53
parent 52581 d402a406bbc3
child 53646 043ae846819f
permissions -rw-r--r--
8214315: G1: fatal error: acquiring lock SATB_Q_FL_lock/1 out of order with lock tty_lock/0 Summary: Add new 'tty' lock rank. Reviewed-by: eosterlund, tschatzl
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
50203
39d88709b138 8202853: NotifyCount is not initialized
coleenp
parents: 49449
diff changeset
     2
 * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5403
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5403
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5403
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
40655
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 40628
diff changeset
    26
#include "runtime/atomic.hpp"
49449
ef5d5d343e2a 8199263: Split interfaceSupport.hpp to not require including .inline.hpp files
coleenp
parents: 48488
diff changeset
    27
#include "runtime/interfaceSupport.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "runtime/mutex.hpp"
50429
83aec1d357d4 8204301: Make OrderAccess functions available to hpp rather than inline.hpp files
coleenp
parents: 50280
diff changeset
    29
#include "runtime/orderAccess.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
#include "runtime/osThread.hpp"
47881
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents: 47634
diff changeset
    31
#include "runtime/safepointMechanism.inline.hpp"
14583
d70ee55535f4 8003935: Simplify the needed includes for using Thread::current()
stefank
parents: 11636
diff changeset
    32
#include "runtime/thread.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    33
#include "utilities/events.hpp"
40010
e32d5e545789 8161258: Simplify including platform files.
goetz
parents: 38308
diff changeset
    34
#include "utilities/macros.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// Native Monitor-Mutex locking - theory of operations
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
// * Native Monitors are completely unrelated to Java-level monitors,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
//   although the "back-end" slow-path implementations share a common lineage.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
//   See objectMonitor:: in synchronizer.cpp.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
//   Native Monitors do *not* support nesting or recursion but otherwise
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
//   they're basically Hoare-flavor monitors.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
// * A thread acquires ownership of a Monitor/Mutex by CASing the LockByte
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
//   in the _LockWord from zero to non-zero.  Note that the _Owner field
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
//   is advisory and is used only to verify that the thread calling unlock()
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
//   is indeed the last thread to have acquired the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
// * Contending threads "push" themselves onto the front of the contention
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
//   queue -- called the cxq -- with CAS and then spin/park.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
//   The _LockWord contains the LockByte as well as the pointer to the head
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
//   of the cxq.  Colocating the LockByte with the cxq precludes certain races.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
// * Using a separately addressable LockByte allows for CAS:MEMBAR or CAS:0
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
//   idioms.  We currently use MEMBAR in the uncontended unlock() path, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
//   MEMBAR often has less latency than CAS.  If warranted, we could switch to
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
//   a CAS:0 mode, using timers to close the resultant race, as is done
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
//   with Java Monitors in synchronizer.cpp.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
//   See the following for a discussion of the relative cost of atomics (CAS)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
//   MEMBAR, and ways to eliminate such instructions from the common-case paths:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
//   -- http://blogs.sun.com/dave/entry/biased_locking_in_hotspot
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
//   -- http://blogs.sun.com/dave/resource/MustangSync.pdf
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
//   -- http://blogs.sun.com/dave/resource/synchronization-public2.pdf
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
//   -- synchronizer.cpp
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
// * Overall goals - desiderata
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
//   1. Minimize context switching
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
//   2. Minimize lock migration
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
//   3. Minimize CPI -- affinity and locality
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
//   4. Minimize the execution of high-latency instructions such as CAS or MEMBAR
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
//   5. Minimize outer lock hold times
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
//   6. Behave gracefully on a loaded system
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
// * Thread flow and list residency:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
//   Contention queue --> EntryList --> OnDeck --> Owner --> !Owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
//   [..resident on monitor list..]
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
//   [...........contending..................]
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
//   -- The contention queue (cxq) contains recently-arrived threads (RATs).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
//      Threads on the cxq eventually drain into the EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
//   -- Invariant: a thread appears on at most one list -- cxq, EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
//      or WaitSet -- at any one time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
//   -- For a given monitor there can be at most one "OnDeck" thread at any
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
//      given time but if needbe this particular invariant could be relaxed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
// * The WaitSet and EntryList linked lists are composed of ParkEvents.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
//   I use ParkEvent instead of threads as ParkEvents are immortal and
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
//   type-stable, meaning we can safely unpark() a possibly stale
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
//   list element in the unlock()-path.  (That's benign).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
// * Succession policy - providing for progress:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
//   As necessary, the unlock()ing thread identifies, unlinks, and unparks
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
//   an "heir presumptive" tentative successor thread from the EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
//   This becomes the so-called "OnDeck" thread, of which there can be only
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
//   one at any given time for a given monitor.  The wakee will recontend
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
//   for ownership of monitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
//   Succession is provided for by a policy of competitive handoff.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
//   The exiting thread does _not_ grant or pass ownership to the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
//   successor thread.  (This is also referred to as "handoff" succession").
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
//   Instead the exiting thread releases ownership and possibly wakes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
//   a successor, so the successor can (re)compete for ownership of the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
//   Competitive handoff provides excellent overall throughput at the expense
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
//   of short-term fairness.  If fairness is a concern then one remedy might
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
//   be to add an AcquireCounter field to the monitor.  After a thread acquires
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
//   the lock it will decrement the AcquireCounter field.  When the count
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
//   reaches 0 the thread would reset the AcquireCounter variable, abdicate
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
//   the lock directly to some thread on the EntryList, and then move itself to the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
//   tail of the EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
//   But in practice most threads engage or otherwise participate in resource
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
//   bounded producer-consumer relationships, so lock domination is not usually
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
//   a practical concern.  Recall too, that in general it's easier to construct
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
//   a fair lock from a fast lock, but not vice-versa.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
// * The cxq can have multiple concurrent "pushers" but only one concurrent
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
//   detaching thread.  This mechanism is immune from the ABA corruption.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
//   More precisely, the CAS-based "push" onto cxq is ABA-oblivious.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
//   We use OnDeck as a pseudo-lock to enforce the at-most-one detaching
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
//   thread constraint.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
// * Taken together, the cxq and the EntryList constitute or form a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
//   single logical queue of threads stalled trying to acquire the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
//   We use two distinct lists to reduce heat on the list ends.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
//   Threads in lock() enqueue onto cxq while threads in unlock() will
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
//   dequeue from the EntryList.  (c.f. Michael Scott's "2Q" algorithm).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
//   A key desideratum is to minimize queue & monitor metadata manipulation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
//   that occurs while holding the "outer" monitor lock -- that is, we want to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
//   minimize monitor lock holds times.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
//   The EntryList is ordered by the prevailing queue discipline and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
//   can be organized in any convenient fashion, such as a doubly-linked list or
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
//   a circular doubly-linked list.  If we need a priority queue then something akin
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
//   to Solaris' sleepq would work nicely.  Viz.,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
//   -- http://agg.eng/ws/on10_nightly/source/usr/src/uts/common/os/sleepq.c.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
//   -- http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/os/sleepq.c
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
//   Queue discipline is enforced at ::unlock() time, when the unlocking thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
//   drains the cxq into the EntryList, and orders or reorders the threads on the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
//   EntryList accordingly.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
//   Barring "lock barging", this mechanism provides fair cyclic ordering,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
//   somewhat similar to an elevator-scan.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
// * OnDeck
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
//   --  For a given monitor there can be at most one OnDeck thread at any given
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
//       instant.  The OnDeck thread is contending for the lock, but has been
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
//       unlinked from the EntryList and cxq by some previous unlock() operations.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
//       Once a thread has been designated the OnDeck thread it will remain so
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
//       until it manages to acquire the lock -- being OnDeck is a stable property.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
//   --  Threads on the EntryList or cxq are _not allowed to attempt lock acquisition.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
//   --  OnDeck also serves as an "inner lock" as follows.  Threads in unlock() will, after
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
//       having cleared the LockByte and dropped the outer lock,  attempt to "trylock"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
//       OnDeck by CASing the field from null to non-null.  If successful, that thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
//       is then responsible for progress and succession and can use CAS to detach and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
//       drain the cxq into the EntryList.  By convention, only this thread, the holder of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
//       the OnDeck inner lock, can manipulate the EntryList or detach and drain the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
//       RATs on the cxq into the EntryList.  This avoids ABA corruption on the cxq as
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
//       we allow multiple concurrent "push" operations but restrict detach concurrency
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
//       to at most one thread.  Having selected and detached a successor, the thread then
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
//       changes the OnDeck to refer to that successor, and then unparks the successor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
//       That successor will eventually acquire the lock and clear OnDeck.  Beware
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
//       that the OnDeck usage as a lock is asymmetric.  A thread in unlock() transiently
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
//       "acquires" OnDeck, performs queue manipulations, passes OnDeck to some successor,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
//       and then the successor eventually "drops" OnDeck.  Note that there's never
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
//       any sense of contention on the inner lock, however.  Threads never contend
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
//       or wait for the inner lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
//   --  OnDeck provides for futile wakeup throttling a described in section 3.3 of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
//       See http://www.usenix.org/events/jvm01/full_papers/dice/dice.pdf
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
//       In a sense, OnDeck subsumes the ObjectMonitor _Succ and ObjectWaiter
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
//       TState fields found in Java-level objectMonitors.  (See synchronizer.cpp).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
// * Waiting threads reside on the WaitSet list -- wait() puts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
//   the caller onto the WaitSet.  Notify() or notifyAll() simply
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
//   transfers threads from the WaitSet to either the EntryList or cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
//   Subsequent unlock() operations will eventually unpark the notifyee.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
//   Unparking a notifee in notify() proper is inefficient - if we were to do so
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
//   it's likely the notifyee would simply impale itself on the lock held
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
//   by the notifier.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
// * The mechanism is obstruction-free in that if the holder of the transient
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
//   OnDeck lock in unlock() is preempted or otherwise stalls, other threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
//   can still acquire and release the outer lock and continue to make progress.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
//   At worst, waking of already blocked contending threads may be delayed,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
//   but nothing worse.  (We only use "trylock" operations on the inner OnDeck
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
//   lock).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
// * Note that thread-local storage must be initialized before a thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
//   uses Native monitors or mutexes.  The native monitor-mutex subsystem
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
//   depends on Thread::current().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
// * The monitor synchronization subsystem avoids the use of native
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
//   synchronization primitives except for the narrow platform-specific
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
//   park-unpark abstraction.  See the comments in os_solaris.cpp regarding
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
//   the semantics of park-unpark.  Put another way, this monitor implementation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
//   depends only on atomic operations and park-unpark.  The monitor subsystem
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
//   manages all RUNNING->BLOCKED and BLOCKED->READY transitions while the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
//   underlying OS manages the READY<->RUN transitions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
// * The memory consistency model provide by lock()-unlock() is at least as
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
//   strong or stronger than the Java Memory model defined by JSR-133.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
//   That is, we guarantee at least entry consistency, if not stronger.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
//   See http://g.oswego.edu/dl/jmm/cookbook.html.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
// * Thread:: currently contains a set of purpose-specific ParkEvents:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
//   _MutexEvent, _ParkEvent, etc.  A better approach might be to do away with
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
//   the purpose-specific ParkEvents and instead implement a general per-thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
//   stack of available ParkEvents which we could provision on-demand.  The
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
//   stack acts as a local cache to avoid excessive calls to ParkEvent::Allocate()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
//   and ::Release().  A thread would simply pop an element from the local stack before it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
//   enqueued or park()ed.  When the contention was over the thread would
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
//   push the no-longer-needed ParkEvent back onto its stack.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
// * A slightly reduced form of ILock() and IUnlock() have been partially
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
//   model-checked (Murphi) for safety and progress at T=1,2,3 and 4.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
//   It'd be interesting to see if TLA/TLC could be useful as well.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
// * Mutex-Monitor is a low-level "leaf" subsystem.  That is, the monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
//   code should never call other code in the JVM that might itself need to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
//   acquire monitors or mutexes.  That's true *except* in the case of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
//   ThreadBlockInVM state transition wrappers.  The ThreadBlockInVM DTOR handles
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
//   mutator reentry (ingress) by checking for a pending safepoint in which case it will
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
//   call SafepointSynchronize::block(), which in turn may call Safepoint_lock->lock(), etc.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
//   In that particular case a call to lock() for a given Monitor can end up recursively
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
//   calling lock() on another monitor.   While distasteful, this is largely benign
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
//   as the calls come from jacket that wraps lock(), and not from deep within lock() itself.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
//   It's unfortunate that native mutexes and thread state transitions were convolved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
//   They're really separate concerns and should have remained that way.  Melding
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
//   them together was facile -- a bit too facile.   The current implementation badly
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
//   conflates the two concerns.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
// * TODO-FIXME:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
//   -- Add DTRACE probes for contended acquire, contended acquired, contended unlock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
//      We should also add DTRACE probes in the ParkEvent subsystem for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
//      Park-entry, Park-exit, and Unpark.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
//   -- We have an excess of mutex-like constructs in the JVM, namely:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
//      1. objectMonitors for Java-level synchronization (synchronizer.cpp)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
//      2. low-level muxAcquire and muxRelease
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
//      3. low-level spinAcquire and spinRelease
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
//      4. native Mutex:: and Monitor::
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
//      5. jvm_raw_lock() and _unlock()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
//      6. JVMTI raw monitors -- distinct from (5) despite having a confusingly
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
//         similar name.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
// o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
#define UNS(x) (uintptr_t(x))
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   256
#define TRACE(m)                   \
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   257
  {                                \
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   258
    static volatile int ctr = 0;   \
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   259
    int x = ++ctr;                 \
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   260
    if ((x & (x - 1)) == 0) {      \
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   261
      ::printf("%d:%s\n", x, #m);  \
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   262
      ::fflush(stdout);            \
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   263
    }                              \
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   264
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   266
const intptr_t _LBIT = 1;
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   267
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   268
// Endian-ness ... index of least-significant byte in SplitWord.Bytes[]
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   269
#ifdef VM_LITTLE_ENDIAN
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   270
 #define _LSBINDEX 0
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   271
#else
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   272
 #define _LSBINDEX (sizeof(intptr_t)-1)
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   273
#endif
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   274
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
// Simplistic low-quality Marsaglia SHIFT-XOR RNG.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
// Bijective except for the trailing mask operation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
// Useful for spin loops as the compiler can't optimize it away.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   279
static inline jint MarsagliaXORV(jint x) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   280
  if (x == 0) x = 1|os::random();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  x ^= x << 6;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  x ^= ((unsigned)x) >> 21;
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   283
  x ^= x << 7;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   284
  return x & 0x7FFFFFFF;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   287
static int Stall(int its) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   288
  static volatile jint rv = 1;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   289
  volatile int OnFrame = 0;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   290
  jint v = rv ^ UNS(OnFrame);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  while (--its >= 0) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   292
    v = MarsagliaXORV(v);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  // Make this impossible for the compiler to optimize away,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
  // but (mostly) avoid W coherency sharing on MP systems.
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   296
  if (v == 0x12345) rv = v;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   297
  return v;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   300
int Monitor::TryLock() {
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   301
  intptr_t v = _LockWord.FullWord;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  for (;;) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   303
    if ((v & _LBIT) != 0) return 0;
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   304
    const intptr_t u = Atomic::cmpxchg(v|_LBIT, &_LockWord.FullWord, v);
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   305
    if (v == u) return 1;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   306
    v = u;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   310
int Monitor::TryFast() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  // Optimistic fast-path form ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  // Fast-path attempt for the common uncontended case.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  // Avoid RTS->RTO $ coherence upgrade on typical SMP systems.
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   314
  intptr_t v = Atomic::cmpxchg(_LBIT, &_LockWord.FullWord, (intptr_t)0);  // agro ...
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   315
  if (v == 0) return 1;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  for (;;) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   318
    if ((v & _LBIT) != 0) return 0;
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   319
    const intptr_t u = Atomic::cmpxchg(v|_LBIT, &_LockWord.FullWord, v);
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   320
    if (v == u) return 1;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   321
    v = u;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   325
int Monitor::ILocked() {
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   326
  const intptr_t w = _LockWord.FullWord & 0xFF;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   327
  assert(w == 0 || w == _LBIT, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   328
  return w == _LBIT;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
// Polite TATAS spinlock with exponential backoff - bounded spin.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
// Ideally we'd use processor cycles, time or vtime to control
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
// the loop, but we currently use iterations.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
// All the constants within were derived empirically but work over
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
// over the spectrum of J2SE reference platforms.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
// On Niagara-class systems the back-off is unnecessary but
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
// is relatively harmless.  (At worst it'll slightly retard
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
// acquisition times).  The back-off is critical for older SMP systems
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
// where constant fetching of the LockWord would otherwise impair
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
// scalability.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
// Clamp spinning at approximately 1/2 of a context-switch round-trip.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
// See synchronizer.cpp for details and rationale.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   345
int Monitor::TrySpin(Thread * const Self) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   346
  if (TryLock())    return 1;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   347
  if (!os::is_MP()) return 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   349
  int Probes  = 0;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   350
  int Delay   = 0;
50765
c49a01150a66 8202615: Remove NativeMonitorSpinLimit, NativeMonitorFlags and NativeMonitorTimeout experimental flags
coleenp
parents: 50429
diff changeset
   351
  int SpinMax = 20;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
    intptr_t v = _LockWord.FullWord;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
    if ((v & _LBIT) == 0) {
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   355
      if (Atomic::cmpxchg (v|_LBIT, &_LockWord.FullWord, v) == v) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   356
        return 1;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
      }
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   358
      continue;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
50765
c49a01150a66 8202615: Remove NativeMonitorSpinLimit, NativeMonitorFlags and NativeMonitorTimeout experimental flags
coleenp
parents: 50429
diff changeset
   361
    SpinPause();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
    // Periodically increase Delay -- variable Delay form
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
    // conceptually: delay *= 1 + 1/Exponent
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   365
    ++Probes;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   366
    if (Probes > SpinMax) return 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
    if ((Probes & 0x7) == 0) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   369
      Delay = ((Delay << 1)|1) & 0x7FF;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
      // CONSIDER: Delay += 1 + (Delay/4); Delay &= 0x7FF ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
    // Stall for "Delay" time units - iterations in the current implementation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
    // Avoid generating coherency traffic while stalled.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    // Possible ways to delay:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
    //   PAUSE, SLEEP, MEMBAR #sync, MEMBAR #halt,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
    //   wr %g0,%asi, gethrtime, rdstick, rdtick, rdtsc, etc. ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
    // Note that on Niagara-class systems we want to minimize STs in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
    // spin loop.  N1 and brethren write-around the L1$ over the xbar into the L2$.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
    // Furthermore, they don't have a W$ like traditional SPARC processors.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
    // We currently use a Marsaglia Shift-Xor RNG loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
    if (Self != NULL) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   383
      jint rv = Self->rng[0];
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   384
      for (int k = Delay; --k >= 0;) {
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   385
        rv = MarsagliaXORV(rv);
52450
2790da836dc3 8211403: Rename SafepointMechanism::poll(...)
rehn
parents: 52356
diff changeset
   386
        if (SafepointMechanism::should_block(Self)) return 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
      }
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   388
      Self->rng[0] = rv;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
    } else {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   390
      Stall(Delay);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   395
static int ParkCommon(ParkEvent * ev, jlong timo) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
  // Diagnostic support - periodically unwedge blocked threads
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   397
  int err = OS_OK;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
  if (0 == timo) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   399
    ev->park();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
  } else {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   401
    err = ev->park(timo);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
  }
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   403
  return err;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   406
inline int Monitor::AcquireOrPush(ParkEvent * ESelf) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   407
  intptr_t v = _LockWord.FullWord;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
    if ((v & _LBIT) == 0) {
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   410
      const intptr_t u = Atomic::cmpxchg(v|_LBIT, &_LockWord.FullWord, v);
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   411
      if (u == v) return 1;        // indicate acquired
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   412
      v = u;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
      // Anticipate success ...
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   415
      ESelf->ListNext = (ParkEvent *)(v & ~_LBIT);
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   416
      const intptr_t u = Atomic::cmpxchg(intptr_t(ESelf)|_LBIT, &_LockWord.FullWord, v);
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   417
      if (u == v) return 0;        // indicate pushed onto cxq
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   418
      v = u;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
    // Interference - LockWord change - just retry
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
// ILock and IWait are the lowest level primitive internal blocking
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
// synchronization functions.  The callers of IWait and ILock must have
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
// performed any needed state transitions beforehand.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
// IWait and ILock may directly call park() without any concern for thread state.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
// Note that ILock and IWait do *not* access _owner.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
// _owner is a higher-level logical concept.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   431
void Monitor::ILock(Thread * Self) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   432
  assert(_OnDeck != Self->_MutexEvent, "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  if (TryFast()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
 Exeunt:
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   436
    assert(ILocked(), "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   437
    return;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   440
  ParkEvent * const ESelf = Self->_MutexEvent;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   441
  assert(_OnDeck != ESelf, "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
41704
14be0ae96c86 8166197: assert(RelaxAssert || w != Thread::current()->_MutexEvent) failed: invariant
dholmes
parents: 40655
diff changeset
   443
  // As an optimization, spinners could conditionally try to set _OnDeck to _LBIT
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
  // Synchronizer.cpp uses a similar optimization.
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   445
  if (TrySpin(Self)) goto Exeunt;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  // Slow-path - the lock is contended.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
  // Either Enqueue Self on cxq or acquire the outer lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  // LockWord encoding = (cxq,LOCKBYTE)
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   450
  ESelf->reset();
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   451
  OrderAccess::fence();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   453
  if (AcquireOrPush(ESelf)) goto Exeunt;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
  // At any given time there is at most one ondeck thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
  // ondeck implies not resident on cxq and not resident on EntryList
41704
14be0ae96c86 8166197: assert(RelaxAssert || w != Thread::current()->_MutexEvent) failed: invariant
dholmes
parents: 40655
diff changeset
   457
  // Only the OnDeck thread can try to acquire -- contend for -- the lock.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
  // CONSIDER: use Self->OnDeck instead of m->OnDeck.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
  // Deschedule Self so that others may run.
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   460
  while (OrderAccess::load_acquire(&_OnDeck) != ESelf) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   461
    ParkCommon(ESelf, 0);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
41704
14be0ae96c86 8166197: assert(RelaxAssert || w != Thread::current()->_MutexEvent) failed: invariant
dholmes
parents: 40655
diff changeset
   464
  // Self is now in the OnDeck position and will remain so until it
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
  // manages to acquire the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
  for (;;) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   467
    assert(_OnDeck == ESelf, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   468
    if (TrySpin(Self)) break;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
    // It's probably wise to spin only if we *actually* blocked
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
    // CONSIDER: check the lockbyte, if it remains set then
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
    // preemptively drain the cxq into the EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
    // The best place and time to perform queue operations -- lock metadata --
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
    // is _before having acquired the outer lock, while waiting for the lock to drop.
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   474
    ParkCommon(ESelf, 0);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   477
  assert(_OnDeck == ESelf, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   478
  _OnDeck = NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
  // Note that we current drop the inner lock (clear OnDeck) in the slow-path
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 22234
diff changeset
   481
  // epilogue immediately after having acquired the outer lock.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  // But instead we could consider the following optimizations:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  // A. Shift or defer dropping the inner lock until the subsequent IUnlock() operation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  //    This might avoid potential reacquisition of the inner lock in IUlock().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
  // B. While still holding the inner lock, attempt to opportunistically select
41704
14be0ae96c86 8166197: assert(RelaxAssert || w != Thread::current()->_MutexEvent) failed: invariant
dholmes
parents: 40655
diff changeset
   486
  //    and unlink the next OnDeck thread from the EntryList.
14be0ae96c86 8166197: assert(RelaxAssert || w != Thread::current()->_MutexEvent) failed: invariant
dholmes
parents: 40655
diff changeset
   487
  //    If successful, set OnDeck to refer to that thread, otherwise clear OnDeck.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  //    It's critical that the select-and-unlink operation run in constant-time as
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  //    it executes when holding the outer lock and may artificially increase the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
  //    effective length of the critical section.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
  // Note that (A) and (B) are tantamount to succession by direct handoff for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
  // the inner lock.
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   493
  goto Exeunt;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   496
void Monitor::IUnlock(bool RelaxAssert) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   497
  assert(ILocked(), "invariant");
11408
3d678c27a7e2 7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents: 10565
diff changeset
   498
  // Conceptually we need a MEMBAR #storestore|#loadstore barrier or fence immediately
3d678c27a7e2 7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents: 10565
diff changeset
   499
  // before the store that releases the lock.  Crucially, all the stores and loads in the
3d678c27a7e2 7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents: 10565
diff changeset
   500
  // critical section must be globally visible before the store of 0 into the lock-word
3d678c27a7e2 7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents: 10565
diff changeset
   501
  // that releases the lock becomes globally visible.  That is, memory accesses in the
3d678c27a7e2 7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents: 10565
diff changeset
   502
  // critical section should not be allowed to bypass or overtake the following ST that
3d678c27a7e2 7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents: 10565
diff changeset
   503
  // releases the lock.  As such, to prevent accesses within the critical section
3d678c27a7e2 7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents: 10565
diff changeset
   504
  // from "leaking" out, we need a release fence between the critical section and the
3d678c27a7e2 7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents: 10565
diff changeset
   505
  // store that releases the lock.  In practice that release barrier is elided on
3d678c27a7e2 7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents: 10565
diff changeset
   506
  // platforms with strong memory models such as TSO.
3d678c27a7e2 7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents: 10565
diff changeset
   507
  //
3d678c27a7e2 7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents: 10565
diff changeset
   508
  // Note that the OrderAccess::storeload() fence that appears after unlock store
3d678c27a7e2 7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents: 10565
diff changeset
   509
  // provides for progress conditions and succession and is _not related to exclusion
3d678c27a7e2 7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents: 10565
diff changeset
   510
  // safety or lock release consistency.
47609
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47216
diff changeset
   511
  OrderAccess::release_store(&_LockWord.Bytes[_LSBINDEX], jbyte(0)); // drop outer lock
11408
3d678c27a7e2 7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents: 10565
diff changeset
   512
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   513
  OrderAccess::storeload();
41704
14be0ae96c86 8166197: assert(RelaxAssert || w != Thread::current()->_MutexEvent) failed: invariant
dholmes
parents: 40655
diff changeset
   514
  ParkEvent * const w = _OnDeck; // raw load as we will just return if non-NULL
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   515
  assert(RelaxAssert || w != Thread::current()->_MutexEvent, "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
  if (w != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
    // Either we have a valid ondeck thread or ondeck is transiently "locked"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
    // by some exiting thread as it arranges for succession.  The LSBit of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
    // OnDeck allows us to discriminate two cases.  If the latter, the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
    // responsibility for progress and succession lies with that other thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
    // For good performance, we also depend on the fact that redundant unpark()
41704
14be0ae96c86 8166197: assert(RelaxAssert || w != Thread::current()->_MutexEvent) failed: invariant
dholmes
parents: 40655
diff changeset
   522
    // operations are cheap.  That is, repeated Unpark()ing of the OnDeck thread
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
    // is inexpensive.  This approach provides implicit futile wakeup throttling.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
    // Note that the referent "w" might be stale with respect to the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
    // In that case the following unpark() is harmless and the worst that'll happen
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
    // is a spurious return from a park() operation.  Critically, if "w" _is stale,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
    // then progress is known to have occurred as that means the thread associated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
    // with "w" acquired the lock.  In that case this thread need take no further
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
    // action to guarantee progress.
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   530
    if ((UNS(w) & _LBIT) == 0) w->unpark();
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   531
    return;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   534
  intptr_t cxq = _LockWord.FullWord;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
  if (((cxq & ~_LBIT)|UNS(_EntryList)) == 0) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   536
    return;      // normal fast-path exit - cxq and EntryList both empty
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
  if (cxq & _LBIT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
    // Optional optimization ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
    // Some other thread acquired the lock in the window since this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
    // thread released it.  Succession is now that thread's responsibility.
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   542
    return;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
 Succession:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
  // Slow-path exit - this thread must ensure succession and progress.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
  // OnDeck serves as lock to protect cxq and EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
  // Only the holder of OnDeck can manipulate EntryList or detach the RATs from cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
  // Avoid ABA - allow multiple concurrent producers (enqueue via push-CAS)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
  // but only one concurrent consumer (detacher of RATs).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
  // Unlike a normal lock, however, the exiting thread "locks" OnDeck,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
  // picks a successor and marks that thread as OnDeck.  That successor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
  // thread will then clear OnDeck once it eventually acquires the outer lock.
48488
51825789dd89 8194406: Use Atomic::replace_if_null
kbarrett
parents: 47881
diff changeset
   554
  if (!Atomic::replace_if_null((ParkEvent*)_LBIT, &_OnDeck)) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   555
    return;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   558
  ParkEvent * List = _EntryList;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
  if (List != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
    // Transfer the head of the EntryList to the OnDeck position.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
    // Once OnDeck, a thread stays OnDeck until it acquires the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
    // For a given lock there is at most OnDeck thread at any one instant.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
   WakeOne:
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   564
    assert(List == _EntryList, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   565
    ParkEvent * const w = List;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   566
    assert(RelaxAssert || w != Thread::current()->_MutexEvent, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   567
    _EntryList = w->ListNext;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
    // as a diagnostic measure consider setting w->_ListNext = BAD
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   569
    assert(intptr_t(_OnDeck) == _LBIT, "invariant");
41704
14be0ae96c86 8166197: assert(RelaxAssert || w != Thread::current()->_MutexEvent) failed: invariant
dholmes
parents: 40655
diff changeset
   570
14be0ae96c86 8166197: assert(RelaxAssert || w != Thread::current()->_MutexEvent) failed: invariant
dholmes
parents: 40655
diff changeset
   571
    // Pass OnDeck role to w, ensuring that _EntryList has been set first.
14be0ae96c86 8166197: assert(RelaxAssert || w != Thread::current()->_MutexEvent) failed: invariant
dholmes
parents: 40655
diff changeset
   572
    // w will clear _OnDeck once it acquires the outer lock.
14be0ae96c86 8166197: assert(RelaxAssert || w != Thread::current()->_MutexEvent) failed: invariant
dholmes
parents: 40655
diff changeset
   573
    // Note that once we set _OnDeck that thread can acquire the mutex, proceed
14be0ae96c86 8166197: assert(RelaxAssert || w != Thread::current()->_MutexEvent) failed: invariant
dholmes
parents: 40655
diff changeset
   574
    // with its critical section and then enter this code to unlock the mutex. So
14be0ae96c86 8166197: assert(RelaxAssert || w != Thread::current()->_MutexEvent) failed: invariant
dholmes
parents: 40655
diff changeset
   575
    // you can have multiple threads active in IUnlock at the same time.
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   576
    OrderAccess::release_store(&_OnDeck, w);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
    // Another optional optimization ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
    // For heavily contended locks it's not uncommon that some other
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
    // thread acquired the lock while this thread was arranging succession.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
    // Try to defer the unpark() operation - Delegate the responsibility
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
    // for unpark()ing the OnDeck thread to the current or subsequent owners
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
    // That is, the new owner is responsible for unparking the OnDeck thread.
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   584
    OrderAccess::storeload();
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   585
    cxq = _LockWord.FullWord;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   586
    if (cxq & _LBIT) return;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   588
    w->unpark();
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   589
    return;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   592
  cxq = _LockWord.FullWord;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
  if ((cxq & ~_LBIT) != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
    // The EntryList is empty but the cxq is populated.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
    // drain RATs from cxq into EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
    // Detach RATs segment with CAS and then merge into EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
    for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
      // optional optimization - if locked, the owner is responsible for succession
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   599
      if (cxq & _LBIT) goto Punt;
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   600
      const intptr_t vfy = Atomic::cmpxchg(cxq & _LBIT, &_LockWord.FullWord, cxq);
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   601
      if (vfy == cxq) break;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   602
      cxq = vfy;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
      // Interference - LockWord changed - Just retry
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
      // We can see concurrent interference from contending threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
      // pushing themselves onto the cxq or from lock-unlock operations.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
      // From the perspective of this thread, EntryList is stable and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
      // the cxq is prepend-only -- the head is volatile but the interior
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
      // of the cxq is stable.  In theory if we encounter interference from threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
      // pushing onto cxq we could simply break off the original cxq suffix and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
      // move that segment to the EntryList, avoiding a 2nd or multiple CAS attempts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
      // on the high-traffic LockWord variable.   For instance lets say the cxq is "ABCD"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
      // when we first fetch cxq above.  Between the fetch -- where we observed "A"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
      // -- and CAS -- where we attempt to CAS null over A -- "PQR" arrive,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
      // yielding cxq = "PQRABCD".  In this case we could simply set A.ListNext
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
      // null, leaving cxq = "PQRA" and transfer the "BCD" segment to the EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
      // Note too, that it's safe for this thread to traverse the cxq
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
      // without taking any special concurrency precautions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
    // We don't currently reorder the cxq segment as we move it onto
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
    // the EntryList, but it might make sense to reverse the order
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
    // or perhaps sort by thread priority.  See the comments in
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
    // synchronizer.cpp objectMonitor::exit().
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   624
    assert(_EntryList == NULL, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   625
    _EntryList = List = (ParkEvent *)(cxq & ~_LBIT);
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   626
    assert(List != NULL, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   627
    goto WakeOne;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
  // cxq|EntryList is empty.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
  // w == NULL implies that cxq|EntryList == NULL in the past.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
  // Possible race - rare inopportune interleaving.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
  // A thread could have added itself to cxq since this thread previously checked.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
  // Detect and recover by refetching cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
 Punt:
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   636
  assert(intptr_t(_OnDeck) == _LBIT, "invariant");
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   637
  _OnDeck = NULL;            // Release inner lock.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
  OrderAccess::storeload();   // Dekker duality - pivot point
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
  // Resample LockWord/cxq to recover from possible race.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
  // For instance, while this thread T1 held OnDeck, some other thread T2 might
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
  // acquire the outer lock.  Another thread T3 might try to acquire the outer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
  // lock, but encounter contention and enqueue itself on cxq.  T2 then drops the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
  // outer lock, but skips succession as this thread T1 still holds OnDeck.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
  // T1 is and remains responsible for ensuring succession of T3.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
  // Note that we don't need to recheck EntryList, just cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
  // If threads moved onto EntryList since we dropped OnDeck
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
  // that implies some other thread forced succession.
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   650
  cxq = _LockWord.FullWord;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
  if ((cxq & ~_LBIT) != 0 && (cxq & _LBIT) == 0) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   652
    goto Succession;         // potential race -- re-run succession
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
  }
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   654
  return;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
bool Monitor::notify() {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   658
  assert(_owner == Thread::current(), "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   659
  assert(ILocked(), "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   660
  if (_WaitSet == NULL) return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
  // Transfer one thread from the WaitSet to the EntryList or cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
  // Currently we just unlink the head of the WaitSet and prepend to the cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
  // And of course we could just unlink it and unpark it, too, but
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
  // in that case it'd likely impale itself on the reentry.
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   666
  Thread::muxAcquire(_WaitLock, "notify:WaitLock");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   667
  ParkEvent * nfy = _WaitSet;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
  if (nfy != NULL) {                  // DCL idiom
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   669
    _WaitSet = nfy->ListNext;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   670
    assert(nfy->Notified == 0, "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
    // push nfy onto the cxq
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
    for (;;) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   673
      const intptr_t v = _LockWord.FullWord;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   674
      assert((v & 0xFF) == _LBIT, "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
      nfy->ListNext = (ParkEvent *)(v & ~_LBIT);
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   676
      if (Atomic::cmpxchg(intptr_t(nfy)|_LBIT, &_LockWord.FullWord, v) == v) break;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
      // interference - _LockWord changed -- just retry
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
    // Note that setting Notified before pushing nfy onto the cxq is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
    // also legal and safe, but the safety properties are much more
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
    // subtle, so for the sake of code stewardship ...
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   682
    OrderAccess::fence();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
    nfy->Notified = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
  }
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   685
  Thread::muxRelease(_WaitLock);
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   686
  assert(ILocked(), "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   687
  return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
// Currently notifyAll() transfers the waiters one-at-a-time from the waitset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
// to the cxq.  This could be done more efficiently with a single bulk en-mass transfer,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
// but in practice notifyAll() for large #s of threads is rare and not time-critical.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
// Beware too, that we invert the order of the waiters.  Lets say that the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
// waitset is "ABCD" and the cxq is "XYZ".  After a notifyAll() the waitset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
// will be empty and the cxq will be "DCBAXYZ".  This is benign, of course.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
bool Monitor::notify_all() {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   698
  assert(_owner == Thread::current(), "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   699
  assert(ILocked(), "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   700
  while (_WaitSet != NULL) notify();
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   701
  return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   704
int Monitor::IWait(Thread * Self, jlong timo) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   705
  assert(ILocked(), "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
  // Phases:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
  // 1. Enqueue Self on WaitSet - currently prepend
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
  // 2. unlock - drop the outer lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
  // 3. wait for either notification or timeout
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
  // 4. lock - reentry - reacquire the outer lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   713
  ParkEvent * const ESelf = Self->_MutexEvent;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   714
  ESelf->Notified = 0;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   715
  ESelf->reset();
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   716
  OrderAccess::fence();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
  // Add Self to WaitSet
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
  // Ideally only the holder of the outer lock would manipulate the WaitSet -
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
  // That is, the outer lock would implicitly protect the WaitSet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
  // But if a thread in wait() encounters a timeout it will need to dequeue itself
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
  // from the WaitSet _before it becomes the owner of the lock.  We need to dequeue
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
  // as the ParkEvent -- which serves as a proxy for the thread -- can't reside
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
  // on both the WaitSet and the EntryList|cxq at the same time..  That is, a thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
  // on the WaitSet can't be allowed to compete for the lock until it has managed to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
  // unlink its ParkEvent from WaitSet.  Thus the need for WaitLock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
  // Contention on the WaitLock is minimal.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
  // Another viable approach would be add another ParkEvent, "WaitEvent" to the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
  // thread class.  The WaitSet would be composed of WaitEvents.  Only the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
  // owner of the outer lock would manipulate the WaitSet.  A thread in wait()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
  // could then compete for the outer lock, and then, if necessary, unlink itself
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
  // from the WaitSet only after having acquired the outer lock.  More precisely,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
  // there would be no WaitLock.  A thread in in wait() would enqueue its WaitEvent
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
  // on the WaitSet; release the outer lock; wait for either notification or timeout;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
  // reacquire the inner lock; and then, if needed, unlink itself from the WaitSet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
  // Alternatively, a 2nd set of list link fields in the ParkEvent might suffice.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
  // One set would be for the WaitSet and one for the EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
  // We could also deconstruct the ParkEvent into a "pure" event and add a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
  // new immortal/TSM "ListElement" class that referred to ParkEvents.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
  // In that case we could have one ListElement on the WaitSet and another
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
  // on the EntryList, with both referring to the same pure Event.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   745
  Thread::muxAcquire(_WaitLock, "wait:WaitLock:Add");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   746
  ESelf->ListNext = _WaitSet;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   747
  _WaitSet = ESelf;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   748
  Thread::muxRelease(_WaitLock);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
  // Release the outer lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
  // We call IUnlock (RelaxAssert=true) as a thread T1 might
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
  // enqueue itself on the WaitSet, call IUnlock(), drop the lock,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
  // and then stall before it can attempt to wake a successor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
  // Some other thread T2 acquires the lock, and calls notify(), moving
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
  // T1 from the WaitSet to the cxq.  T2 then drops the lock.  T1 resumes,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
  // and then finds *itself* on the cxq.  During the course of a normal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
  // IUnlock() call a thread should _never find itself on the EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
  // or cxq, but in the case of wait() it's possible.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
  // See synchronizer.cpp objectMonitor::wait().
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   760
  IUnlock(true);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
  // Wait for either notification or timeout
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
  // Beware that in some circumstances we might propagate
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
  // spurious wakeups back to the caller.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
  for (;;) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   767
    if (ESelf->Notified) break;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   768
    int err = ParkCommon(ESelf, timo);
50765
c49a01150a66 8202615: Remove NativeMonitorSpinLimit, NativeMonitorFlags and NativeMonitorTimeout experimental flags
coleenp
parents: 50429
diff changeset
   769
    if (err == OS_TIMEOUT) break;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
  // Prepare for reentry - if necessary, remove ESelf from WaitSet
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
  // ESelf can be:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
  // 1. Still on the WaitSet.  This can happen if we exited the loop by timeout.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
  // 2. On the cxq or EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
  // 3. Not resident on cxq, EntryList or WaitSet, but in the OnDeck position.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   778
  OrderAccess::fence();
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   779
  int WasOnWaitSet = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
  if (ESelf->Notified == 0) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   781
    Thread::muxAcquire(_WaitLock, "wait:WaitLock:remove");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
    if (ESelf->Notified == 0) {     // DCL idiom
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   783
      assert(_OnDeck != ESelf, "invariant");   // can't be both OnDeck and on WaitSet
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
      // ESelf is resident on the WaitSet -- unlink it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
      // A doubly-linked list would be better here so we can unlink in constant-time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
      // We have to unlink before we potentially recontend as ESelf might otherwise
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
      // end up on the cxq|EntryList -- it can't be on two lists at once.
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   788
      ParkEvent * p = _WaitSet;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   789
      ParkEvent * q = NULL;            // classic q chases p
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
      while (p != NULL && p != ESelf) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   791
        q = p;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   792
        p = p->ListNext;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
      }
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   794
      assert(p == ESelf, "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
      if (p == _WaitSet) {      // found at head
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   796
        assert(q == NULL, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   797
        _WaitSet = p->ListNext;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
      } else {                  // found in interior
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   799
        assert(q->ListNext == p, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   800
        q->ListNext = p->ListNext;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
      }
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   802
      WasOnWaitSet = 1;        // We were *not* notified but instead encountered timeout
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
    }
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   804
    Thread::muxRelease(_WaitLock);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
  // Reentry phase - reacquire the lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
  if (WasOnWaitSet) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
    // ESelf was previously on the WaitSet but we just unlinked it above
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
    // because of a timeout.  ESelf is not resident on any list and is not OnDeck
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   811
    assert(_OnDeck != ESelf, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   812
    ILock(Self);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
    // A prior notify() operation moved ESelf from the WaitSet to the cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
    // ESelf is now on the cxq, EntryList or at the OnDeck position.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
    // The following fragment is extracted from Monitor::ILock()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
    for (;;) {
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   818
      if (OrderAccess::load_acquire(&_OnDeck) == ESelf && TrySpin(Self)) break;
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   819
      ParkCommon(ESelf, 0);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
    }
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   821
    assert(_OnDeck == ESelf, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   822
    _OnDeck = NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   825
  assert(ILocked(), "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   826
  return WasOnWaitSet != 0;        // return true IFF timeout
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
// ON THE VMTHREAD SNEAKING PAST HELD LOCKS:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
// In particular, there are certain types of global lock that may be held
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
// by a Java thread while it is blocked at a safepoint but before it has
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
// written the _owner field. These locks may be sneakily acquired by the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
// VM thread during a safepoint to avoid deadlocks. Alternatively, one should
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
// identify all such locks, and ensure that Java threads never block at
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
// safepoints while holding them (_no_safepoint_check_flag). While it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
// seems as though this could increase the time to reach a safepoint
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
// (or at least increase the mean, if not the variance), the latter
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
// approach might make for a cleaner, more maintainable JVM design.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
// Sneaking is vile and reprehensible and should be excised at the 1st
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
// opportunity.  It's possible that the need for sneaking could be obviated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
// as follows.  Currently, a thread might (a) while TBIVM, call pthread_mutex_lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
// or ILock() thus acquiring the "physical" lock underlying Monitor/Mutex.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
// (b) stall at the TBIVM exit point as a safepoint is in effect.  Critically,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
// it'll stall at the TBIVM reentry state transition after having acquired the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
// underlying lock, but before having set _owner and having entered the actual
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
// critical section.  The lock-sneaking facility leverages that fact and allowed the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
// VM thread to logically acquire locks that had already be physically locked by mutators
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
// but where mutators were known blocked by the reentry thread state transition.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
// If we were to modify the Monitor-Mutex so that TBIVM state transitions tightly
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
// wrapped calls to park(), then we could likely do away with sneaking.  We'd
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
// decouple lock acquisition and parking.  The critical invariant  to eliminating
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
// sneaking is to ensure that we never "physically" acquire the lock while TBIVM.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
// An easy way to accomplish this is to wrap the park calls in a narrow TBIVM jacket.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
// One difficulty with this approach is that the TBIVM wrapper could recurse and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
// call lock() deep from within a lock() call, while the MutexEvent was already enqueued.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
// Using a stack (N=2 at minimum) of ParkEvents would take care of that problem.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
// But of course the proper ultimate approach is to avoid schemes that require explicit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
// sneaking or dependence on any any clever invariants or subtle implementation properties
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
// of Mutex-Monitor and instead directly address the underlying design flaw.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   865
void Monitor::lock(Thread * Self) {
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   866
  // Ensure that the Monitor requires/allows safepoint checks.
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   867
  assert(_safepoint_check_required != Monitor::_safepoint_check_never,
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
   868
         "This lock should never have a safepoint check: %s", name());
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   869
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
#ifdef CHECK_UNHANDLED_OOPS
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
  // Clear unhandled oops so we get a crash right away.  Only clear for non-vm
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
  // or GC threads.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
  if (Self->is_Java_thread()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
    Self->clear_unhandled_oops();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
#endif // CHECK_UNHANDLED_OOPS
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
52555
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 52450
diff changeset
   878
  DEBUG_ONLY(check_prelock_state(Self, StrictSafepointChecks);)
52356
19c4a3eec4d7 8213211: [BACKOUT] Allow Klass::_subklass and _next_sibling to have unloaded classes
coleenp
parents: 52311
diff changeset
   879
  assert(_owner != Self, "invariant");
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   880
  assert(_OnDeck != Self->_MutexEvent, "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
  if (TryFast()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
 Exeunt:
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   884
    assert(ILocked(), "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   885
    assert(owner() == NULL, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   886
    set_owner(Self);
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   887
    return;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
  // The lock is contended ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
  bool can_sneak = Self->is_VM_thread() && SafepointSynchronize::is_at_safepoint();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
  if (can_sneak && _owner == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
    // a java thread has locked the lock but has not entered the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
    // critical region -- let's just pretend we've locked the lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
    // and go on.  we note this with _snuck so we can also
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
    // pretend to unlock when the time comes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
    _snuck = true;
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   899
    goto Exeunt;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   901
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
  // Try a brief spin to avoid passing thru thread state transition ...
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   903
  if (TrySpin(Self)) goto Exeunt;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   904
52555
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 52450
diff changeset
   905
  DEBUG_ONLY(check_block_state(Self);)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   906
  if (Self->is_Java_thread()) {
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 22234
diff changeset
   907
    // Horrible dictu - we suffer through a state transition
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
    assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex");
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   909
    ThreadBlockInVM tbivm((JavaThread *) Self);
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   910
    ILock(Self);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   912
    // Mirabile dictu
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   913
    ILock(Self);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
  }
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   915
  goto Exeunt;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
void Monitor::lock() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
  this->lock(Thread::current());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
// Lock without safepoint check - a degenerate variant of lock().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
// Should ONLY be used by safepoint code and other code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
// that is guaranteed not to block while running inside the VM. If this is called with
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
// thread state set to be in VM, the safepoint synchronization code will deadlock!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   927
void Monitor::lock_without_safepoint_check(Thread * Self) {
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   928
  // Ensure that the Monitor does not require or allow safepoint checks.
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   929
  assert(_safepoint_check_required != Monitor::_safepoint_check_always,
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
   930
         "This lock should always have a safepoint check: %s", name());
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   931
  assert(_owner != Self, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   932
  ILock(Self);
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   933
  assert(_owner == NULL, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   934
  set_owner(Self);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   937
void Monitor::lock_without_safepoint_check() {
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   938
  lock_without_safepoint_check(Thread::current());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 22234
diff changeset
   942
// Returns true if thread succeeds in grabbing the lock, otherwise false.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
bool Monitor::try_lock() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
  Thread * const Self = Thread::current();
52555
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 52450
diff changeset
   946
  DEBUG_ONLY(check_prelock_state(Self, false);)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
  // assert(!thread->is_inside_signal_handler(), "don't lock inside signal handler");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
  // Special case, where all Java threads are stopped.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
  // The lock may have been acquired but _owner is not yet set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
  // In that case the VM thread can safely grab the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
  // It strikes me this should appear _after the TryLock() fails, below.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
  bool can_sneak = Self->is_VM_thread() && SafepointSynchronize::is_at_safepoint();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   954
  if (can_sneak && _owner == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
    set_owner(Self); // Do not need to be atomic, since we are at a safepoint
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
    _snuck = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
  if (TryLock()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
    // We got the lock
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   962
    assert(_owner == NULL, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   963
    set_owner(Self);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   967
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   968
489c9b5090e2 Initial load
duke
parents:
diff changeset
   969
void Monitor::unlock() {
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   970
  assert(_owner == Thread::current(), "invariant");
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   971
  assert(_OnDeck != Thread::current()->_MutexEvent, "invariant");
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   972
  set_owner(NULL);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   973
  if (_snuck) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
    assert(SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread(), "sneak");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
    _snuck = false;
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   976
    return;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
  }
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   978
  IUnlock(false);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
// Yet another degenerate version of Monitor::lock() or lock_without_safepoint_check()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
// jvm_raw_lock() and _unlock() can be called by non-Java threads via JVM_RawMonitorEnter.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
// There's no expectation that JVM_RawMonitors will interoperate properly with the native
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
// Mutex-Monitor constructs.  We happen to implement JVM_RawMonitors in terms of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   986
// native Mutex-Monitors simply as a matter of convenience.  A simple abstraction layer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   987
// over a pthread_mutex_t would work equally as well, but require more platform-specific
489c9b5090e2 Initial load
duke
parents:
diff changeset
   988
// code -- a "PlatformMutex".  Alternatively, a simply layer over muxAcquire-muxRelease
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
// would work too.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   990
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   991
// Since the caller might be a foreign thread, we don't necessarily have a Thread.MutexEvent
489c9b5090e2 Initial load
duke
parents:
diff changeset
   992
// instance available.  Instead, we transiently allocate a ParkEvent on-demand if
489c9b5090e2 Initial load
duke
parents:
diff changeset
   993
// we encounter contention.  That ParkEvent remains associated with the thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   994
// until it manages to acquire the lock, at which time we return the ParkEvent
489c9b5090e2 Initial load
duke
parents:
diff changeset
   995
// to the global ParkEvent free list.  This is correct and suffices for our purposes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   996
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   997
// Beware that the original jvm_raw_unlock() had a "_snuck" test but that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   998
// jvm_raw_lock() didn't have the corresponding test.  I suspect that's an
489c9b5090e2 Initial load
duke
parents:
diff changeset
   999
// oversight, but I've replicated the original suspect logic in the new code ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1000
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1001
void Monitor::jvm_raw_lock() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1002
  assert(rank() == native, "invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1003
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1004
  if (TryLock()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1005
 Exeunt:
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1006
    assert(ILocked(), "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1007
    assert(_owner == NULL, "invariant");
34633
2a6c7c7b30a7 8132510: Replace ThreadLocalStorage with compiler/language-based thread-local variables
dholmes
parents: 33148
diff changeset
  1008
    // This can potentially be called by non-java Threads. Thus, the Thread::current_or_null()
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
    // might return NULL. Don't call set_owner since it will break on an NULL owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1010
    // Consider installing a non-null "ANON" distinguished value instead of just NULL.
34633
2a6c7c7b30a7 8132510: Replace ThreadLocalStorage with compiler/language-based thread-local variables
dholmes
parents: 33148
diff changeset
  1011
    _owner = Thread::current_or_null();
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1012
    return;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1013
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1014
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1015
  if (TrySpin(NULL)) goto Exeunt;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1016
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1017
  // slow-path - apparent contention
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1018
  // Allocate a ParkEvent for transient use.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1019
  // The ParkEvent remains associated with this thread until
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1020
  // the time the thread manages to acquire the lock.
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1021
  ParkEvent * const ESelf = ParkEvent::Allocate(NULL);
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1022
  ESelf->reset();
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1023
  OrderAccess::storeload();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1024
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1025
  // Either Enqueue Self on cxq or acquire the outer lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1026
  if (AcquireOrPush (ESelf)) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1027
    ParkEvent::Release(ESelf);      // surrender the ParkEvent
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1028
    goto Exeunt;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1029
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1030
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1031
  // At any given time there is at most one ondeck thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1032
  // ondeck implies not resident on cxq and not resident on EntryList
41704
14be0ae96c86 8166197: assert(RelaxAssert || w != Thread::current()->_MutexEvent) failed: invariant
dholmes
parents: 40655
diff changeset
  1033
  // Only the OnDeck thread can try to acquire -- contend for -- the lock.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1034
  // CONSIDER: use Self->OnDeck instead of m->OnDeck.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1035
  for (;;) {
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
  1036
    if (OrderAccess::load_acquire(&_OnDeck) == ESelf && TrySpin(NULL)) break;
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1037
    ParkCommon(ESelf, 0);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1038
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1039
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1040
  assert(_OnDeck == ESelf, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1041
  _OnDeck = NULL;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1042
  ParkEvent::Release(ESelf);      // surrender the ParkEvent
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1043
  goto Exeunt;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1044
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1045
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1046
void Monitor::jvm_raw_unlock() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1047
  // Nearly the same as Monitor::unlock() ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1048
  // directly set _owner instead of using set_owner(null)
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1049
  _owner = NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1050
  if (_snuck) {         // ???
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1051
    assert(SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread(), "sneak");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1052
    _snuck = false;
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1053
    return;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1054
  }
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1055
  IUnlock(false);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1056
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1057
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
  1058
bool Monitor::wait(bool no_safepoint_check, long timeout,
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
  1059
                   bool as_suspend_equivalent) {
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
  1060
  // Make sure safepoint checking is used properly.
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
  1061
  assert(!(_safepoint_check_required == Monitor::_safepoint_check_never && no_safepoint_check == false),
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
  1062
         "This lock should never have a safepoint check: %s", name());
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
  1063
  assert(!(_safepoint_check_required == Monitor::_safepoint_check_always && no_safepoint_check == true),
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
  1064
         "This lock should always have a safepoint check: %s", name());
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
  1065
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1066
  Thread * const Self = Thread::current();
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1067
  assert(_owner == Self, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1068
  assert(ILocked(), "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1069
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1070
  // as_suspend_equivalent logically implies !no_safepoint_check
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1071
  guarantee(!as_suspend_equivalent || !no_safepoint_check, "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1072
  // !no_safepoint_check logically implies java_thread
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1073
  guarantee(no_safepoint_check || Self->is_Java_thread(), "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1074
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1075
  #ifdef ASSERT
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1076
  Monitor * least = get_least_ranked_lock_besides_this(Self->owned_locks());
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1077
  assert(least != this, "Specification of get_least_... call above");
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1078
  if (least != NULL && least->rank() <= special) {
52913
bf2f2560dd53 8214315: G1: fatal error: acquiring lock SATB_Q_FL_lock/1 out of order with lock tty_lock/0
kbarrett
parents: 52581
diff changeset
  1079
    ::tty->print("Attempting to wait on monitor %s/%d while holding"
bf2f2560dd53 8214315: G1: fatal error: acquiring lock SATB_Q_FL_lock/1 out of order with lock tty_lock/0
kbarrett
parents: 52581
diff changeset
  1080
                 " lock %s/%d -- possible deadlock",
bf2f2560dd53 8214315: G1: fatal error: acquiring lock SATB_Q_FL_lock/1 out of order with lock tty_lock/0
kbarrett
parents: 52581
diff changeset
  1081
                 name(), rank(), least->name(), least->rank());
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1082
    assert(false, "Shouldn't block(wait) while holding a lock of rank special");
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1083
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1084
  #endif // ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1085
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1086
  int wait_status;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1087
  // conceptually set the owner to NULL in anticipation of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1088
  // abdicating the lock in wait
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1089
  set_owner(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1090
  if (no_safepoint_check) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1091
    wait_status = IWait(Self, timeout);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1092
  } else {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1093
    assert(Self->is_Java_thread(), "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1094
    JavaThread *jt = (JavaThread *)Self;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1095
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1096
    // Enter safepoint region - ornate and Rococo ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1097
    ThreadBlockInVM tbivm(jt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1098
    OSThreadWaitState osts(Self->osthread(), false /* not Object.wait() */);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1099
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1100
    if (as_suspend_equivalent) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1101
      jt->set_suspend_equivalent();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1102
      // cleared by handle_special_suspend_equivalent_condition() or
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1103
      // java_suspend_self()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1104
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1105
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1106
    wait_status = IWait(Self, timeout);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1107
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1108
    // were we externally suspended while we were waiting?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1109
    if (as_suspend_equivalent && jt->handle_special_suspend_equivalent_condition()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1110
      // Our event wait has finished and we own the lock, but
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1111
      // while we were waiting another thread suspended us. We don't
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1112
      // want to hold the lock while suspended because that
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1113
      // would surprise the thread that suspended us.
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1114
      assert(ILocked(), "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1115
      IUnlock(true);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1116
      jt->java_suspend_self();
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1117
      ILock(Self);
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1118
      assert(ILocked(), "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1119
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1120
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1121
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1122
  // Conceptually reestablish ownership of the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1123
  // The "real" lock -- the LockByte -- was reacquired by IWait().
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1124
  assert(ILocked(), "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1125
  assert(_owner == NULL, "invariant");
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1126
  set_owner(Self);
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1127
  return wait_status != 0;          // return true IFF timeout
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1128
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1129
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1130
Monitor::~Monitor() {
40628
0912b2911fe3 8152849: share/vm/runtime/mutex.cpp:1161 assert(((uintptr_t(_owner))|(uintptr_t(_LockWord.FullWord))|(uintptr_t(_EntryList))|(uintptr_t(_WaitSet))|(uintptr_t(_OnDeck))) == 0) failed
dholmes
parents: 40010
diff changeset
  1131
#ifdef ASSERT
0912b2911fe3 8152849: share/vm/runtime/mutex.cpp:1161 assert(((uintptr_t(_owner))|(uintptr_t(_LockWord.FullWord))|(uintptr_t(_EntryList))|(uintptr_t(_WaitSet))|(uintptr_t(_OnDeck))) == 0) failed
dholmes
parents: 40010
diff changeset
  1132
  uintptr_t owner = UNS(_owner);
0912b2911fe3 8152849: share/vm/runtime/mutex.cpp:1161 assert(((uintptr_t(_owner))|(uintptr_t(_LockWord.FullWord))|(uintptr_t(_EntryList))|(uintptr_t(_WaitSet))|(uintptr_t(_OnDeck))) == 0) failed
dholmes
parents: 40010
diff changeset
  1133
  uintptr_t lockword = UNS(_LockWord.FullWord);
0912b2911fe3 8152849: share/vm/runtime/mutex.cpp:1161 assert(((uintptr_t(_owner))|(uintptr_t(_LockWord.FullWord))|(uintptr_t(_EntryList))|(uintptr_t(_WaitSet))|(uintptr_t(_OnDeck))) == 0) failed
dholmes
parents: 40010
diff changeset
  1134
  uintptr_t entrylist = UNS(_EntryList);
0912b2911fe3 8152849: share/vm/runtime/mutex.cpp:1161 assert(((uintptr_t(_owner))|(uintptr_t(_LockWord.FullWord))|(uintptr_t(_EntryList))|(uintptr_t(_WaitSet))|(uintptr_t(_OnDeck))) == 0) failed
dholmes
parents: 40010
diff changeset
  1135
  uintptr_t waitset = UNS(_WaitSet);
0912b2911fe3 8152849: share/vm/runtime/mutex.cpp:1161 assert(((uintptr_t(_owner))|(uintptr_t(_LockWord.FullWord))|(uintptr_t(_EntryList))|(uintptr_t(_WaitSet))|(uintptr_t(_OnDeck))) == 0) failed
dholmes
parents: 40010
diff changeset
  1136
  uintptr_t ondeck = UNS(_OnDeck);
46767
e2bb2b8ff65a 8185746: Remove Mutex destructor assertion
kbarrett
parents: 46644
diff changeset
  1137
  // Print _name with precision limit, in case failure is due to memory
e2bb2b8ff65a 8185746: Remove Mutex destructor assertion
kbarrett
parents: 46644
diff changeset
  1138
  // corruption that also trashed _name.
40628
0912b2911fe3 8152849: share/vm/runtime/mutex.cpp:1161 assert(((uintptr_t(_owner))|(uintptr_t(_LockWord.FullWord))|(uintptr_t(_EntryList))|(uintptr_t(_WaitSet))|(uintptr_t(_OnDeck))) == 0) failed
dholmes
parents: 40010
diff changeset
  1139
  assert((owner|lockword|entrylist|waitset|ondeck) == 0,
46767
e2bb2b8ff65a 8185746: Remove Mutex destructor assertion
kbarrett
parents: 46644
diff changeset
  1140
         "%.*s: _owner(" INTPTR_FORMAT ")|_LockWord(" INTPTR_FORMAT ")|_EntryList(" INTPTR_FORMAT ")|_WaitSet("
e2bb2b8ff65a 8185746: Remove Mutex destructor assertion
kbarrett
parents: 46644
diff changeset
  1141
         INTPTR_FORMAT ")|_OnDeck(" INTPTR_FORMAT ") != 0",
e2bb2b8ff65a 8185746: Remove Mutex destructor assertion
kbarrett
parents: 46644
diff changeset
  1142
         MONITOR_NAME_LEN, _name, owner, lockword, entrylist, waitset, ondeck);
40628
0912b2911fe3 8152849: share/vm/runtime/mutex.cpp:1161 assert(((uintptr_t(_owner))|(uintptr_t(_LockWord.FullWord))|(uintptr_t(_EntryList))|(uintptr_t(_WaitSet))|(uintptr_t(_OnDeck))) == 0) failed
dholmes
parents: 40010
diff changeset
  1143
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1144
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1145
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
  1146
void Monitor::ClearMonitor(Monitor * m, const char *name) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1147
  m->_owner             = NULL;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1148
  m->_snuck             = false;
228
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
  1149
  if (name == NULL) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1150
    strcpy(m->_name, "UNKNOWN");
228
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
  1151
  } else {
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
  1152
    strncpy(m->_name, name, MONITOR_NAME_LEN - 1);
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
  1153
    m->_name[MONITOR_NAME_LEN - 1] = '\0';
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
  1154
  }
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1155
  m->_LockWord.FullWord = 0;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1156
  m->_EntryList         = NULL;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1157
  m->_OnDeck            = NULL;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1158
  m->_WaitSet           = NULL;
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1159
  m->_WaitLock[0]       = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1160
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1161
52581
d402a406bbc3 8213723: More Monitor/mutex initialization management
dholmes
parents: 52555
diff changeset
  1162
Monitor::Monitor() {
d402a406bbc3 8213723: More Monitor/mutex initialization management
dholmes
parents: 52555
diff changeset
  1163
  assert(os::mutex_init_done(), "Too early!");
d402a406bbc3 8213723: More Monitor/mutex initialization management
dholmes
parents: 52555
diff changeset
  1164
  ClearMonitor(this);
d402a406bbc3 8213723: More Monitor/mutex initialization management
dholmes
parents: 52555
diff changeset
  1165
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1166
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
  1167
Monitor::Monitor(int Rank, const char * name, bool allow_vm_block,
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
  1168
                 SafepointCheckRequired safepoint_check_required) {
52581
d402a406bbc3 8213723: More Monitor/mutex initialization management
dholmes
parents: 52555
diff changeset
  1169
  assert(os::mutex_init_done(), "Too early!");
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1170
  ClearMonitor(this, name);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1171
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1172
  _allow_vm_block  = allow_vm_block;
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1173
  _rank            = Rank;
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
  1174
  NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1175
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1176
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1177
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
  1178
Mutex::Mutex(int Rank, const char * name, bool allow_vm_block,
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
  1179
             SafepointCheckRequired safepoint_check_required) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1180
  ClearMonitor((Monitor *) this, name);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1181
#ifdef ASSERT
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1182
  _allow_vm_block   = allow_vm_block;
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1183
  _rank             = Rank;
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
  1184
  NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1185
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1186
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1187
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1188
bool Monitor::owned_by_self() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1189
  bool ret = _owner == Thread::current();
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
  1190
  assert(!ret || _LockWord.Bytes[_LSBINDEX] != 0, "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1191
  return ret;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1192
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1193
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1194
void Monitor::print_on_error(outputStream* st) const {
33148
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 33105
diff changeset
  1195
  st->print("[" PTR_FORMAT, p2i(this));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1196
  st->print("] %s", _name);
33148
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 33105
diff changeset
  1197
  st->print(" - owner thread: " PTR_FORMAT, p2i(_owner));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1198
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1199
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1200
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1201
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1202
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1203
// ----------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1204
// Non-product code
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1205
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1206
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1207
void Monitor::print_on(outputStream* st) const {
33148
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 33105
diff changeset
  1208
  st->print_cr("Mutex: [" PTR_FORMAT "/" PTR_FORMAT "] %s - owner: " PTR_FORMAT,
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 33105
diff changeset
  1209
               p2i(this), _LockWord.FullWord, _name, p2i(_owner));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1210
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1211
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1212
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1213
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1214
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1215
Monitor * Monitor::get_least_ranked_lock(Monitor * locks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1216
  Monitor *res, *tmp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1217
  for (res = tmp = locks; tmp != NULL; tmp = tmp->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1218
    if (tmp->rank() < res->rank()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1219
      res = tmp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1220
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1221
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1222
  if (!SafepointSynchronize::is_at_safepoint()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1223
    // In this case, we expect the held locks to be
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1224
    // in increasing rank order (modulo any native ranks)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1225
    for (tmp = locks; tmp != NULL; tmp = tmp->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1226
      if (tmp->next() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1227
        assert(tmp->rank() == Mutex::native ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1228
               tmp->rank() <= tmp->next()->rank(), "mutex rank anomaly?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1229
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1230
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1231
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1232
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1233
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1234
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1235
Monitor* Monitor::get_least_ranked_lock_besides_this(Monitor* locks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1236
  Monitor *res, *tmp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1237
  for (res = NULL, tmp = locks; tmp != NULL; tmp = tmp->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1238
    if (tmp != this && (res == NULL || tmp->rank() < res->rank())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1239
      res = tmp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1240
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1241
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1242
  if (!SafepointSynchronize::is_at_safepoint()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1243
    // In this case, we expect the held locks to be
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1244
    // in increasing rank order (modulo any native ranks)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1245
    for (tmp = locks; tmp != NULL; tmp = tmp->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1246
      if (tmp->next() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1247
        assert(tmp->rank() == Mutex::native ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1248
               tmp->rank() <= tmp->next()->rank(), "mutex rank anomaly?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1249
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1250
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1251
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1252
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1253
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1254
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1255
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1256
bool Monitor::contains(Monitor* locks, Monitor * lock) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1257
  for (; locks != NULL; locks = locks->next()) {
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
  1258
    if (locks == lock) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1259
      return true;
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
  1260
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1261
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1262
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1263
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1264
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1265
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1266
// Called immediately after lock acquisition or release as a diagnostic
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1267
// to track the lock-set of the thread and test for rank violations that
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1268
// might indicate exposure to deadlock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1269
// Rather like an EventListener for _owner (:>).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1270
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1271
void Monitor::set_owner_implementation(Thread *new_owner) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1272
  // This function is solely responsible for maintaining
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1273
  // and checking the invariant that threads and locks
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1274
  // are in a 1/N relation, with some some locks unowned.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1275
  // It uses the Mutex::_owner, Mutex::_next, and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1276
  // Thread::_owned_locks fields, and no other function
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1277
  // changes those fields.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1278
  // It is illegal to set the mutex from one non-NULL
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1279
  // owner to another--it must be owned by NULL as an
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1280
  // intermediate state.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1281
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1282
  if (new_owner != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1283
    // the thread is acquiring this lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1284
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1285
    assert(new_owner == Thread::current(), "Should I be doing this?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1286
    assert(_owner == NULL, "setting the owner thread of an already owned mutex");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1287
    _owner = new_owner; // set the owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1288
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1289
    // link "this" into the owned locks list
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1290
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
  1291
#ifdef ASSERT  // Thread::_owned_locks is under the same ifdef
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1292
    Monitor* locks = get_least_ranked_lock(new_owner->owned_locks());
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1293
    // Mutex::set_owner_implementation is a friend of Thread
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1294
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1295
    assert(this->rank() >= 0, "bad lock rank");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1296
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1297
    // Deadlock avoidance rules require us to acquire Mutexes only in
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1298
    // a global total order. For example m1 is the lowest ranked mutex
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1299
    // that the thread holds and m2 is the mutex the thread is trying
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1300
    // to acquire, then  deadlock avoidance rules require that the rank
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1301
    // of m2 be less  than the rank of m1.
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1302
    // The rank Mutex::native  is an exception in that it is not subject
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1303
    // to the verification rules.
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1304
    // Here are some further notes relating to mutex acquisition anomalies:
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1305
    // . it is also ok to acquire Safepoint_lock at the very end while we
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1306
    //   already hold Terminator_lock - may happen because of periodic safepoints
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1307
    if (this->rank() != Mutex::native &&
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1308
        this->rank() != Mutex::suspend_resume &&
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1309
        locks != NULL && locks->rank() <= this->rank() &&
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1310
        !SafepointSynchronize::is_at_safepoint() &&
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1311
        !(this == Safepoint_lock && contains(locks, Terminator_lock) &&
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1312
        SafepointSynchronize::is_synchronizing())) {
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1313
      new_owner->print_owned_locks();
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
  1314
      fatal("acquiring lock %s/%d out of order with lock %s/%d -- "
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
  1315
            "possible deadlock", this->name(), this->rank(),
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
  1316
            locks->name(), locks->rank());
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1317
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1318
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1319
    this->_next = new_owner->_owned_locks;
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1320
    new_owner->_owned_locks = this;
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
  1321
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1322
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1323
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1324
    // the thread is releasing this lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1325
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1326
    Thread* old_owner = _owner;
52555
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 52450
diff changeset
  1327
    DEBUG_ONLY(_last_owner = old_owner;)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1328
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1329
    assert(old_owner != NULL, "removing the owner thread of an unowned mutex");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1330
    assert(old_owner == Thread::current(), "removing the owner thread of an unowned mutex");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1331
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1332
    _owner = NULL; // set the owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1333
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
  1334
#ifdef ASSERT
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1335
    Monitor *locks = old_owner->owned_locks();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1336
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1337
    // remove "this" from the owned locks list
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1338
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1339
    Monitor *prev = NULL;
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1340
    bool found = false;
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1341
    for (; locks != NULL; prev = locks, locks = locks->next()) {
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1342
      if (locks == this) {
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1343
        found = true;
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1344
        break;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1345
      }
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1346
    }
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1347
    assert(found, "Removing a lock not owned");
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1348
    if (prev == NULL) {
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1349
      old_owner->_owned_locks = _next;
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1350
    } else {
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1351
      prev->_next = _next;
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1352
    }
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1353
    _next = NULL;
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
  1354
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1355
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1356
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1357
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1358
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1359
// Factored out common sanity checks for locking mutex'es. Used by lock() and try_lock()
50280
5aaf3a471172 8203817: Monitor::try_lock() should not call check_prelock_state()
pliden
parents: 50203
diff changeset
  1360
void Monitor::check_prelock_state(Thread *thread, bool safepoint_check) {
5aaf3a471172 8203817: Monitor::try_lock() should not call check_prelock_state()
pliden
parents: 50203
diff changeset
  1361
  if (safepoint_check) {
5aaf3a471172 8203817: Monitor::try_lock() should not call check_prelock_state()
pliden
parents: 50203
diff changeset
  1362
    assert((!thread->is_Java_thread() || ((JavaThread *)thread)->thread_state() == _thread_in_vm)
5aaf3a471172 8203817: Monitor::try_lock() should not call check_prelock_state()
pliden
parents: 50203
diff changeset
  1363
           || rank() == Mutex::special, "wrong thread state for using locks");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1364
    if (thread->is_VM_thread() && !allow_vm_block()) {
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
  1365
      fatal("VM thread using lock %s (not allowed to block on)", name());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1366
    }
52555
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 52450
diff changeset
  1367
    DEBUG_ONLY(if (rank() != Mutex::special) \
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
  1368
               thread->check_for_valid_safepoint_state(false);)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1369
  }
46644
a5813fb66270 8183925: Decouple crash protection from watcher thread
rehn
parents: 41704
diff changeset
  1370
  assert(!os::ThreadCrashProtection::is_crash_protected(thread),
a5813fb66270 8183925: Decouple crash protection from watcher thread
rehn
parents: 41704
diff changeset
  1371
         "locking not allowed when crash protection is set");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1372
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1373
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1374
void Monitor::check_block_state(Thread *thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1375
  if (!_allow_vm_block && thread->is_VM_thread()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1376
    warning("VM thread blocked on lock");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1377
    print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1378
    BREAKPOINT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1379
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1380
  assert(_owner != thread, "deadlock: blocking on monitor owned by current thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1381
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1382
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1383
#endif // PRODUCT