hotspot/src/share/vm/runtime/synchronizer.cpp
author acorn
Fri, 02 Jul 2010 17:23:43 -0400
changeset 5920 8fdbb85e62d3
parent 5712 7e82752d3fdf
child 6975 dc9b63952682
permissions -rw-r--r--
6964164: MonitorInUseLists leak of contended objects Summary: fix MonitorInUseLists memory leak and MonitorBound now works Reviewed-by: chrisphi, dice
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2526
diff changeset
     2
 * Copyright (c) 1998, 2009, 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: 2526
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2526
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: 2526
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
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_synchronizer.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
#if defined(__GNUC__) && !defined(IA64)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
  // Need to inhibit inlining for older versions of GCC to avoid build-time failures
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
  #define ATTR __attribute__((noinline))
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  #define ATTR
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// Native markword accessors for synchronization and hashCode().
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// The "core" versions of monitor enter and exit reside in this file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// The interpreter and compilers contain specialized transliterated
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
// variants of the enter-exit fast-path operations.  See i486.ad fast_lock(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
// for instance.  If you make changes here, make sure to modify the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
// interpreter, and both C1 and C2 fast-path inline locking code emission.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
// TODO: merge the objectMonitor and synchronizer classes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
// -----------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
#ifdef DTRACE_ENABLED
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
// Only bother with this argument setup if dtrace is available
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
// TODO-FIXME: probes should not fire when caller is _blocked.  assert() accordingly.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
HS_DTRACE_PROBE_DECL5(hotspot, monitor__wait,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  jlong, uintptr_t, char*, int, long);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
HS_DTRACE_PROBE_DECL4(hotspot, monitor__waited,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  jlong, uintptr_t, char*, int);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
HS_DTRACE_PROBE_DECL4(hotspot, monitor__notify,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  jlong, uintptr_t, char*, int);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
HS_DTRACE_PROBE_DECL4(hotspot, monitor__notifyAll,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  jlong, uintptr_t, char*, int);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
HS_DTRACE_PROBE_DECL4(hotspot, monitor__contended__enter,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  jlong, uintptr_t, char*, int);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
HS_DTRACE_PROBE_DECL4(hotspot, monitor__contended__entered,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  jlong, uintptr_t, char*, int);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
HS_DTRACE_PROBE_DECL4(hotspot, monitor__contended__exit,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  jlong, uintptr_t, char*, int);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
#define DTRACE_MONITOR_PROBE_COMMON(klassOop, thread)                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  char* bytes = NULL;                                                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  int len = 0;                                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  jlong jtid = SharedRuntime::get_java_tid(thread);                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  symbolOop klassname = ((oop)(klassOop))->klass()->klass_part()->name();  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  if (klassname != NULL) {                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    bytes = (char*)klassname->bytes();                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    len = klassname->utf8_length();                                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
#define DTRACE_MONITOR_WAIT_PROBE(monitor, klassOop, thread, millis)       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  {                                                                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    if (DTraceMonitorProbes) {                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
      DTRACE_MONITOR_PROBE_COMMON(klassOop, thread);                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
      HS_DTRACE_PROBE5(hotspot, monitor__wait, jtid,                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
                       (monitor), bytes, len, (millis));                   \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
    }                                                                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
#define DTRACE_MONITOR_PROBE(probe, monitor, klassOop, thread)             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  {                                                                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    if (DTraceMonitorProbes) {                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
      DTRACE_MONITOR_PROBE_COMMON(klassOop, thread);                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
      HS_DTRACE_PROBE4(hotspot, monitor__##probe, jtid,                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
                       (uintptr_t)(monitor), bytes, len);                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    }                                                                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
#else //  ndef DTRACE_ENABLED
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
#define DTRACE_MONITOR_WAIT_PROBE(klassOop, thread, millis, mon)    {;}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
#define DTRACE_MONITOR_PROBE(probe, klassOop, thread, mon)          {;}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
#endif // ndef DTRACE_ENABLED
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
// ObjectWaiter serves as a "proxy" or surrogate thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
// TODO-FIXME: Eliminate ObjectWaiter and use the thread-specific
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
// ParkEvent instead.  Beware, however, that the JVMTI code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
// knows about ObjectWaiters, so we'll have to reconcile that code.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
// See next_waiter(), first_waiter(), etc.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
class ObjectWaiter : public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  enum TStates { TS_UNDEF, TS_READY, TS_RUN, TS_WAIT, TS_ENTER, TS_CXQ } ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  enum Sorted  { PREPEND, APPEND, SORTED } ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  ObjectWaiter * volatile _next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  ObjectWaiter * volatile _prev;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  Thread*       _thread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  ParkEvent *   _event;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  volatile int  _notified ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  volatile TStates TState ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  Sorted        _Sorted ;           // List placement disposition
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  bool          _active ;           // Contention monitoring is enabled
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  ObjectWaiter(Thread* thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    _next     = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    _prev     = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    _notified = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
    TState    = TS_RUN ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    _thread   = thread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    _event    = thread->_ParkEvent ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    _active   = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    assert (_event != NULL, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  void wait_reenter_begin(ObjectMonitor *mon) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
    JavaThread *jt = (JavaThread *)this->_thread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    _active = JavaThreadBlockedOnMonitorEnterState::wait_reenter_begin(jt, mon);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  void wait_reenter_end(ObjectMonitor *mon) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
    JavaThread *jt = (JavaThread *)this->_thread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
    JavaThreadBlockedOnMonitorEnterState::wait_reenter_end(jt, _active);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
enum ManifestConstants {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    ClearResponsibleAtSTW   = 0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    MaximumRecheckInterval  = 1000
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
} ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
#undef TEVENT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
#define TEVENT(nom) {if (SyncVerbose) FEVENT(nom); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
#define FEVENT(nom) { static volatile int ctr = 0 ; int v = ++ctr ; if ((v & (v-1)) == 0) { ::printf (#nom " : %d \n", v); ::fflush(stdout); }}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
#undef  TEVENT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
#define TEVENT(nom) {;}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
// Performance concern:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
// OrderAccess::storestore() calls release() which STs 0 into the global volatile
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
// OrderAccess::Dummy variable.  This store is unnecessary for correctness.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
// Many threads STing into a common location causes considerable cache migration
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
// or "sloshing" on large SMP system.  As such, I avoid using OrderAccess::storestore()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
// until it's repaired.  In some cases OrderAccess::fence() -- which incurs local
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
// latency on the executing processor -- is a better choice as it scales on SMP
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
// systems.  See http://blogs.sun.com/dave/entry/biased_locking_in_hotspot for a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
// discussion of coherency costs.  Note that all our current reference platforms
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
// provide strong ST-ST order, so the issue is moot on IA32, x64, and SPARC.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
// As a general policy we use "volatile" to control compiler-based reordering
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
// and explicit fences (barriers) to control for architectural reordering performed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
// by the CPU(s) or platform.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
static int  MBFence (int x) { OrderAccess::fence(); return x; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
struct SharedGlobals {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    // These are highly shared mostly-read variables.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    // To avoid false-sharing they need to be the sole occupants of a $ line.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
    double padPrefix [8];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    volatile int stwRandom ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    volatile int stwCycle ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
    // Hot RW variables -- Sequester to avoid false-sharing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    double padSuffix [16];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
    volatile int hcSequence ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
    double padFinal [8] ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
} ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
static SharedGlobals GVars ;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   188
static int MonitorScavengeThreshold = 1000000 ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   189
static volatile int ForceMonitorScavenge = 0 ; // Scavenge required and pending
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
// Tunables ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
// The knob* variables are effectively final.  Once set they should
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
// never be modified hence.  Consider using __read_mostly with GCC.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
static int Knob_LogSpins           = 0 ;       // enable jvmstat tally for spins
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
static int Knob_HandOff            = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
static int Knob_Verbose            = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
static int Knob_ReportSettings     = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
static int Knob_SpinLimit          = 5000 ;    // derived by an external tool -
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
static int Knob_SpinBase           = 0 ;       // Floor AKA SpinMin
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
static int Knob_SpinBackOff        = 0 ;       // spin-loop backoff
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
static int Knob_CASPenalty         = -1 ;      // Penalty for failed CAS
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
static int Knob_OXPenalty          = -1 ;      // Penalty for observed _owner change
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
static int Knob_SpinSetSucc        = 1 ;       // spinners set the _succ field
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
static int Knob_SpinEarly          = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
static int Knob_SuccEnabled        = 1 ;       // futile wake throttling
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
static int Knob_SuccRestrict       = 0 ;       // Limit successors + spinners to at-most-one
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
static int Knob_MaxSpinners        = -1 ;      // Should be a function of # CPUs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
static int Knob_Bonus              = 100 ;     // spin success bonus
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
static int Knob_BonusB             = 100 ;     // spin success bonus
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
static int Knob_Penalty            = 200 ;     // spin failure penalty
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
static int Knob_Poverty            = 1000 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
static int Knob_SpinAfterFutile    = 1 ;       // Spin after returning from park()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
static int Knob_FixedSpin          = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
static int Knob_OState             = 3 ;       // Spinner checks thread state of _owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
static int Knob_UsePause           = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
static int Knob_ExitPolicy         = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
static int Knob_PreSpin            = 10 ;      // 20-100 likely better
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
static int Knob_ResetEvent         = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
static int BackOffMask             = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
static int Knob_FastHSSEC          = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
static int Knob_MoveNotifyee       = 2 ;       // notify() - disposition of notifyee
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
static int Knob_QMode              = 0 ;       // EntryList-cxq policy - queue discipline
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
static volatile int InitDone       = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
// hashCode() generation :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
// Possibilities:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
// * MD5Digest of {obj,stwRandom}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
// * CRC32 of {obj,stwRandom} or any linear-feedback shift register function.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
// * A DES- or AES-style SBox[] mechanism
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
// * One of the Phi-based schemes, such as:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
//   2654435761 = 2^32 * Phi (golden ratio)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
//   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stwRandom ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
// * A variation of Marsaglia's shift-xor RNG scheme.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
// * (obj ^ stwRandom) is appealing, but can result
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
//   in undesirable regularity in the hashCode values of adjacent objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
//   (objects allocated back-to-back, in particular).  This could potentially
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
//   result in hashtable collisions and reduced hashtable efficiency.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
//   There are simple ways to "diffuse" the middle address bits over the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
//   generated hashCode values:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
static inline intptr_t get_next_hash(Thread * Self, oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
  intptr_t value = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  if (hashCode == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
     // This form uses an unguarded global Park-Miller RNG,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
     // so it's possible for two threads to race and generate the same RNG.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
     // On MP system we'll have lots of RW access to a global, so the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
     // mechanism induces lots of coherency traffic.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
     value = os::random() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  if (hashCode == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
     // This variation has the property of being stable (idempotent)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
     // between STW operations.  This can be useful in some of the 1-0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
     // synchronization schemes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
     intptr_t addrBits = intptr_t(obj) >> 3 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
     value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  if (hashCode == 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
     value = 1 ;            // for sensitivity testing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
  } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  if (hashCode == 3) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
     value = ++GVars.hcSequence ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  if (hashCode == 4) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
     value = intptr_t(obj) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
     // Marsaglia's xor-shift scheme with thread-specific state
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
     // This is probably the best overall implementation -- we'll
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
     // likely make this the default in future releases.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
     unsigned t = Self->_hashStateX ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
     t ^= (t << 11) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
     Self->_hashStateX = Self->_hashStateY ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
     Self->_hashStateY = Self->_hashStateZ ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
     Self->_hashStateZ = Self->_hashStateW ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
     unsigned v = Self->_hashStateW ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
     v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
     Self->_hashStateW = v ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
     value = v ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  value &= markOopDesc::hash_mask;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  if (value == 0) value = 0xBAD ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
  assert (value != markOopDesc::no_hash, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  TEVENT (hashCode: GENERATE) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  return value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
void BasicLock::print_on(outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
  st->print("monitor");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
void BasicLock::move_to(oop obj, BasicLock* dest) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  // Check to see if we need to inflate the lock. This is only needed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
  // if an object is locked using "this" lightweight monitor. In that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  // case, the displaced_header() is unlocked, because the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  // displaced_header() contains the header for the originally unlocked
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
  // object. However the object could have already been inflated. But it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
  // does not matter, the inflation will just a no-op. For other cases,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
  // the displaced header will be either 0x0 or 0x3, which are location
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
  // independent, therefore the BasicLock is free to move.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  // During OSR we may need to relocate a BasicLock (which contains a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  // displaced word) from a location in an interpreter frame to a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
  // new location in a compiled frame.  "this" refers to the source
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  // basiclock in the interpreter frame.  "dest" refers to the destination
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  // basiclock in the new compiled frame.  We *always* inflate in move_to().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  // The always-Inflate policy works properly, but in 1.5.0 it can sometimes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  // cause performance problems in code that makes heavy use of a small # of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  // uncontended locks.   (We'd inflate during OSR, and then sync performance
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  // would subsequently plummet because the thread would be forced thru the slow-path).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  // This problem has been made largely moot on IA32 by inlining the inflated fast-path
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  // operations in Fast_Lock and Fast_Unlock in i486.ad.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  // Note that there is a way to safely swing the object's markword from
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  // one stack location to another.  This avoids inflation.  Obviously,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  // we need to ensure that both locations refer to the current thread's stack.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  // There are some subtle concurrency issues, however, and since the benefit is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  // is small (given the support for inflated fast-path locking in the fast_lock, etc)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
  // we'll leave that optimization for another time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
  if (displaced_header()->is_neutral()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
    ObjectSynchronizer::inflate_helper(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    // WARNING: We can not put check here, because the inflation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
    // will not update the displaced header. Once BasicLock is inflated,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    // no one should ever look at its content.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    // Typically the displaced header will be 0 (recursive stack lock) or
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
    // unused_mark.  Naively we'd like to assert that the displaced mark
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
    // value is either 0, neutral, or 3.  But with the advent of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
    // store-before-CAS avoidance in fast_lock/compiler_lock_object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
    // we can find any flavor mark in the displaced mark.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
// [RGV] The next line appears to do nothing!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
  intptr_t dh = (intptr_t) displaced_header();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
  dest->set_displaced_header(displaced_header());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
// -----------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
// standard constructor, allows locking failures
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
ObjectLocker::ObjectLocker(Handle obj, Thread* thread, bool doLock) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  _dolock = doLock;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  _thread = thread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  debug_only(if (StrictSafepointChecks) _thread->check_for_valid_safepoint_state(false);)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  _obj = obj;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
  if (_dolock) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
    TEVENT (ObjectLocker) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
    ObjectSynchronizer::fast_enter(_obj, &_lock, false, _thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
ObjectLocker::~ObjectLocker() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
  if (_dolock) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
    ObjectSynchronizer::fast_exit(_obj(), &_lock, _thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
// -----------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
PerfCounter * ObjectSynchronizer::_sync_Inflations                  = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
PerfCounter * ObjectSynchronizer::_sync_Deflations                  = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
PerfCounter * ObjectSynchronizer::_sync_ContendedLockAttempts       = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
PerfCounter * ObjectSynchronizer::_sync_FutileWakeups               = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
PerfCounter * ObjectSynchronizer::_sync_Parks                       = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
PerfCounter * ObjectSynchronizer::_sync_EmptyNotifications          = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
PerfCounter * ObjectSynchronizer::_sync_Notifications               = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
PerfCounter * ObjectSynchronizer::_sync_PrivateA                    = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
PerfCounter * ObjectSynchronizer::_sync_PrivateB                    = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
PerfCounter * ObjectSynchronizer::_sync_SlowExit                    = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
PerfCounter * ObjectSynchronizer::_sync_SlowEnter                   = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
PerfCounter * ObjectSynchronizer::_sync_SlowNotify                  = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
PerfCounter * ObjectSynchronizer::_sync_SlowNotifyAll               = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
PerfCounter * ObjectSynchronizer::_sync_FailedSpins                 = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
PerfCounter * ObjectSynchronizer::_sync_SuccessfulSpins             = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
PerfCounter * ObjectSynchronizer::_sync_MonInCirculation            = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
PerfCounter * ObjectSynchronizer::_sync_MonScavenged                = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
PerfLongVariable * ObjectSynchronizer::_sync_MonExtant              = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
// One-shot global initialization for the sync subsystem.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
// We could also defer initialization and initialize on-demand
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
// the first time we call inflate().  Initialization would
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
// be protected - like so many things - by the MonitorCache_lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
void ObjectSynchronizer::Initialize () {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
  static int InitializationCompleted = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  assert (InitializationCompleted == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
  InitializationCompleted = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  if (UsePerfData) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
      EXCEPTION_MARK ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
      #define NEWPERFCOUNTER(n)   {n = PerfDataManager::create_counter(SUN_RT, #n, PerfData::U_Events,CHECK); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
      #define NEWPERFVARIABLE(n)  {n = PerfDataManager::create_variable(SUN_RT, #n, PerfData::U_Events,CHECK); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
      NEWPERFCOUNTER(_sync_Inflations) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
      NEWPERFCOUNTER(_sync_Deflations) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
      NEWPERFCOUNTER(_sync_ContendedLockAttempts) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
      NEWPERFCOUNTER(_sync_FutileWakeups) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
      NEWPERFCOUNTER(_sync_Parks) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
      NEWPERFCOUNTER(_sync_EmptyNotifications) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
      NEWPERFCOUNTER(_sync_Notifications) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
      NEWPERFCOUNTER(_sync_SlowEnter) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
      NEWPERFCOUNTER(_sync_SlowExit) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
      NEWPERFCOUNTER(_sync_SlowNotify) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
      NEWPERFCOUNTER(_sync_SlowNotifyAll) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
      NEWPERFCOUNTER(_sync_FailedSpins) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
      NEWPERFCOUNTER(_sync_SuccessfulSpins) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
      NEWPERFCOUNTER(_sync_PrivateA) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
      NEWPERFCOUNTER(_sync_PrivateB) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
      NEWPERFCOUNTER(_sync_MonInCirculation) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
      NEWPERFCOUNTER(_sync_MonScavenged) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
      NEWPERFVARIABLE(_sync_MonExtant) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
      #undef NEWPERFCOUNTER
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
// Compile-time asserts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
// When possible, it's better to catch errors deterministically at
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
// compile-time than at runtime.  The down-side to using compile-time
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
// asserts is that error message -- often something about negative array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
// indices -- is opaque.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
1889
24b003a6fe46 6781583: Hotspot build fails on linux 64 bit platform with gcc 4.3.2
xlu
parents: 1665
diff changeset
   429
#define CTASSERT(x) { int tag[1-(2*!(x))]; printf ("Tag @" INTPTR_FORMAT "\n", (intptr_t)tag); }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
void ObjectMonitor::ctAsserts() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  CTASSERT(offset_of (ObjectMonitor, _header) == 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
static int Adjust (volatile int * adr, int dx) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
  int v ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
  for (v = *adr ; Atomic::cmpxchg (v + dx, adr, v) != v; v = *adr) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
  return v ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
// Ad-hoc mutual exclusion primitives: SpinLock and Mux
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
// We employ SpinLocks _only for low-contention, fixed-length
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
// short-duration critical sections where we're concerned
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
// about native mutex_t or HotSpot Mutex:: latency.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
// The mux construct provides a spin-then-block mutual exclusion
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
// mechanism.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
// Testing has shown that contention on the ListLock guarding gFreeList
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
// is common.  If we implement ListLock as a simple SpinLock it's common
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
// for the JVM to devolve to yielding with little progress.  This is true
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
// despite the fact that the critical sections protected by ListLock are
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
// extremely short.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
// TODO-FIXME: ListLock should be of type SpinLock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
// We should make this a 1st-class type, integrated into the lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
// hierarchy as leaf-locks.  Critically, the SpinLock structure
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
// should have sufficient padding to avoid false-sharing and excessive
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
// cache-coherency traffic.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
typedef volatile int SpinLockT ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
void Thread::SpinAcquire (volatile int * adr, const char * LockName) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
  if (Atomic::cmpxchg (1, adr, 0) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
     return ;   // normal fast-path return
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
  // Slow-path : We've encountered contention -- Spin/Yield/Block strategy.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
  TEVENT (SpinAcquire - ctx) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  int ctr = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  int Yields = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
  for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
     while (*adr != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
        ++ctr ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
        if ((ctr & 0xFFF) == 0 || !os::is_MP()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
           if (Yields > 5) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
             // Consider using a simple NakedSleep() instead.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
             // Then SpinAcquire could be called by non-JVM threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
             Thread::current()->_ParkEvent->park(1) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
           } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
             os::NakedYield() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
             ++Yields ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
           }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
           SpinPause() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
     if (Atomic::cmpxchg (1, adr, 0) == 0) return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
void Thread::SpinRelease (volatile int * adr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
  assert (*adr != 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
  OrderAccess::fence() ;      // guarantee at least release consistency.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
  // Roach-motel semantics.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  // It's safe if subsequent LDs and STs float "up" into the critical section,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
  // but prior LDs and STs within the critical section can't be allowed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  // to reorder or float past the ST that releases the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  *adr = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
// muxAcquire and muxRelease:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
// *  muxAcquire and muxRelease support a single-word lock-word construct.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
//    The LSB of the word is set IFF the lock is held.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
//    The remainder of the word points to the head of a singly-linked list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
//    of threads blocked on the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
// *  The current implementation of muxAcquire-muxRelease uses its own
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
//    dedicated Thread._MuxEvent instance.  If we're interested in
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
//    minimizing the peak number of extant ParkEvent instances then
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
//    we could eliminate _MuxEvent and "borrow" _ParkEvent as long
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
//    as certain invariants were satisfied.  Specifically, care would need
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
//    to be taken with regards to consuming unpark() "permits".
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
//    A safe rule of thumb is that a thread would never call muxAcquire()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
//    if it's enqueued (cxq, EntryList, WaitList, etc) and will subsequently
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
//    park().  Otherwise the _ParkEvent park() operation in muxAcquire() could
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
//    consume an unpark() permit intended for monitorenter, for instance.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
//    One way around this would be to widen the restricted-range semaphore
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
//    implemented in park().  Another alternative would be to provide
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
//    multiple instances of the PlatformEvent() for each thread.  One
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
//    instance would be dedicated to muxAcquire-muxRelease, for instance.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
// *  Usage:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
//    -- Only as leaf locks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
//    -- for short-term locking only as muxAcquire does not perform
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
//       thread state transitions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
// Alternatives:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
// *  We could implement muxAcquire and muxRelease with MCS or CLH locks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
//    but with parking or spin-then-park instead of pure spinning.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
// *  Use Taura-Oyama-Yonenzawa locks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
// *  It's possible to construct a 1-0 lock if we encode the lockword as
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
//    (List,LockByte).  Acquire will CAS the full lockword while Release
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
//    will STB 0 into the LockByte.  The 1-0 scheme admits stranding, so
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
//    acquiring threads use timers (ParkTimed) to detect and recover from
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
//    the stranding window.  Thread/Node structures must be aligned on 256-byte
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
//    boundaries by using placement-new.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
// *  Augment MCS with advisory back-link fields maintained with CAS().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
//    Pictorially:  LockWord -> T1 <-> T2 <-> T3 <-> ... <-> Tn <-> Owner.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
//    The validity of the backlinks must be ratified before we trust the value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
//    If the backlinks are invalid the exiting thread must back-track through the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
//    the forward links, which are always trustworthy.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
// *  Add a successor indication.  The LockWord is currently encoded as
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
//    (List, LOCKBIT:1).  We could also add a SUCCBIT or an explicit _succ variable
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
//    to provide the usual futile-wakeup optimization.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
//    See RTStt for details.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
// *  Consider schedctl.sc_nopreempt to cover the critical section.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
typedef volatile intptr_t MutexT ;      // Mux Lock-word
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
enum MuxBits { LOCKBIT = 1 } ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
void Thread::muxAcquire (volatile intptr_t * Lock, const char * LockName) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
  intptr_t w = Atomic::cmpxchg_ptr (LOCKBIT, Lock, 0) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
  if (w == 0) return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
  if ((w & LOCKBIT) == 0 && Atomic::cmpxchg_ptr (w|LOCKBIT, Lock, w) == w) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
     return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
  TEVENT (muxAcquire - Contention) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
  ParkEvent * const Self = Thread::current()->_MuxEvent ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
  assert ((intptr_t(Self) & LOCKBIT) == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
  for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
     int its = (os::is_MP() ? 100 : 0) + 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
     // Optional spin phase: spin-then-park strategy
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
     while (--its >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
       w = *Lock ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
       if ((w & LOCKBIT) == 0 && Atomic::cmpxchg_ptr (w|LOCKBIT, Lock, w) == w) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
          return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
     Self->reset() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
     Self->OnList = intptr_t(Lock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
     // The following fence() isn't _strictly necessary as the subsequent
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
     // CAS() both serializes execution and ratifies the fetched *Lock value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
     OrderAccess::fence();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
     for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
        w = *Lock ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
        if ((w & LOCKBIT) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
            if (Atomic::cmpxchg_ptr (w|LOCKBIT, Lock, w) == w) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
                Self->OnList = 0 ;   // hygiene - allows stronger asserts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
                return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
            continue ;      // Interference -- *Lock changed -- Just retry
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
        assert (w & LOCKBIT, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
        Self->ListNext = (ParkEvent *) (w & ~LOCKBIT );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
        if (Atomic::cmpxchg_ptr (intptr_t(Self)|LOCKBIT, Lock, w) == w) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
     while (Self->OnList != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
        Self->park() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
void Thread::muxAcquireW (volatile intptr_t * Lock, ParkEvent * ev) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
  intptr_t w = Atomic::cmpxchg_ptr (LOCKBIT, Lock, 0) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
  if (w == 0) return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
  if ((w & LOCKBIT) == 0 && Atomic::cmpxchg_ptr (w|LOCKBIT, Lock, w) == w) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
    return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
  TEVENT (muxAcquire - Contention) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
  ParkEvent * ReleaseAfter = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
  if (ev == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
    ev = ReleaseAfter = ParkEvent::Allocate (NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
  assert ((intptr_t(ev) & LOCKBIT) == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
  for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
    guarantee (ev->OnList == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
    int its = (os::is_MP() ? 100 : 0) + 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
    // Optional spin phase: spin-then-park strategy
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
    while (--its >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
      w = *Lock ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
      if ((w & LOCKBIT) == 0 && Atomic::cmpxchg_ptr (w|LOCKBIT, Lock, w) == w) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
        if (ReleaseAfter != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
          ParkEvent::Release (ReleaseAfter) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
        return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
    ev->reset() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
    ev->OnList = intptr_t(Lock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
    // The following fence() isn't _strictly necessary as the subsequent
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
    // CAS() both serializes execution and ratifies the fetched *Lock value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
    OrderAccess::fence();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
    for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
      w = *Lock ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
      if ((w & LOCKBIT) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
        if (Atomic::cmpxchg_ptr (w|LOCKBIT, Lock, w) == w) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
          ev->OnList = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
          // We call ::Release while holding the outer lock, thus
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
          // artificially lengthening the critical section.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
          // Consider deferring the ::Release() until the subsequent unlock(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
          // after we've dropped the outer lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
          if (ReleaseAfter != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
            ParkEvent::Release (ReleaseAfter) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
          return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
        continue ;      // Interference -- *Lock changed -- Just retry
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
      assert (w & LOCKBIT, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
      ev->ListNext = (ParkEvent *) (w & ~LOCKBIT );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
      if (Atomic::cmpxchg_ptr (intptr_t(ev)|LOCKBIT, Lock, w) == w) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
    while (ev->OnList != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
      ev->park() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
// Release() must extract a successor from the list and then wake that thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
// It can "pop" the front of the list or use a detach-modify-reattach (DMR) scheme
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
// similar to that used by ParkEvent::Allocate() and ::Release().  DMR-based
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
// Release() would :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
// (A) CAS() or swap() null to *Lock, releasing the lock and detaching the list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
// (B) Extract a successor from the private list "in-hand"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
// (C) attempt to CAS() the residual back into *Lock over null.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
//     If there were any newly arrived threads and the CAS() would fail.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
//     In that case Release() would detach the RATs, re-merge the list in-hand
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
//     with the RATs and repeat as needed.  Alternately, Release() might
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
//     detach and extract a successor, but then pass the residual list to the wakee.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
//     The wakee would be responsible for reattaching and remerging before it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
//     competed for the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
// Both "pop" and DMR are immune from ABA corruption -- there can be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
// multiple concurrent pushers, but only one popper or detacher.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
// This implementation pops from the head of the list.  This is unfair,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
// but tends to provide excellent throughput as hot threads remain hot.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
// (We wake recently run threads first).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
void Thread::muxRelease (volatile intptr_t * Lock)  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
  for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
    const intptr_t w = Atomic::cmpxchg_ptr (0, Lock, LOCKBIT) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
    assert (w & LOCKBIT, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
    if (w == LOCKBIT) return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
    ParkEvent * List = (ParkEvent *) (w & ~LOCKBIT) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
    assert (List != NULL, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
    assert (List->OnList == intptr_t(Lock), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
    ParkEvent * nxt = List->ListNext ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
    // The following CAS() releases the lock and pops the head element.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
    if (Atomic::cmpxchg_ptr (intptr_t(nxt), Lock, w) != w) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
      continue ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
    List->OnList = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
    OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
    List->unpark () ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
    return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
// ObjectMonitor Lifecycle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
// -----------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
// Inflation unlinks monitors from the global gFreeList and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
// associates them with objects.  Deflation -- which occurs at
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
// STW-time -- disassociates idle monitors from objects.  Such
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
// scavenged monitors are returned to the gFreeList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
// The global list is protected by ListLock.  All the critical sections
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
// are short and operate in constant-time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
// ObjectMonitors reside in type-stable memory (TSM) and are immortal.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
// Lifecycle:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
// --   unassigned and on the global free list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
// --   unassigned and on a thread's private omFreeList
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
// --   assigned to an object.  The object is inflated and the mark refers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
//      to the objectmonitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
// TODO-FIXME:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
// *  We currently protect the gFreeList with a simple lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
//    An alternate lock-free scheme would be to pop elements from the gFreeList
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
//    with CAS.  This would be safe from ABA corruption as long we only
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
//    recycled previously appearing elements onto the list in deflate_idle_monitors()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
//    at STW-time.  Completely new elements could always be pushed onto the gFreeList
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
//    with CAS.  Elements that appeared previously on the list could only
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
//    be installed at STW-time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
// *  For efficiency and to help reduce the store-before-CAS penalty
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
//    the objectmonitors on gFreeList or local free lists should be ready to install
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
//    with the exception of _header and _object.  _object can be set after inflation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
//    In particular, keep all objectMonitors on a thread's private list in ready-to-install
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
//    state with m.Owner set properly.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
// *  We could all diffuse contention by using multiple global (FreeList, Lock)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
//    pairs -- threads could use trylock() and a cyclic-scan strategy to search for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
//    an unlocked free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
// *  Add lifecycle tags and assert()s.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
// *  Be more consistent about when we clear an objectmonitor's fields:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
//    A.  After extracting the objectmonitor from a free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
//    B.  After adding an objectmonitor to a free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
ObjectMonitor * ObjectSynchronizer::gBlockList = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
ObjectMonitor * volatile ObjectSynchronizer::gFreeList  = NULL ;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   750
ObjectMonitor * volatile ObjectSynchronizer::gOmInUseList  = NULL ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   751
int ObjectSynchronizer::gOmInUseCount = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
static volatile intptr_t ListLock = 0 ;      // protects global monitor free-list cache
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   753
static volatile int MonitorFreeCount  = 0 ;      // # on gFreeList
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   754
static volatile int MonitorPopulation = 0 ;      // # Extant -- in circulation
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
#define CHAINMARKER ((oop)-1)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   757
// Constraining monitor pool growth via MonitorBound ...
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   758
//
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   759
// The monitor pool is grow-only.  We scavenge at STW safepoint-time, but the
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   760
// the rate of scavenging is driven primarily by GC.  As such,  we can find
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   761
// an inordinate number of monitors in circulation.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   762
// To avoid that scenario we can artificially induce a STW safepoint
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   763
// if the pool appears to be growing past some reasonable bound.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   764
// Generally we favor time in space-time tradeoffs, but as there's no
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   765
// natural back-pressure on the # of extant monitors we need to impose some
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   766
// type of limit.  Beware that if MonitorBound is set to too low a value
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   767
// we could just loop. In addition, if MonitorBound is set to a low value
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   768
// we'll incur more safepoints, which are harmful to performance.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   769
// See also: GuaranteedSafepointInterval
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   770
//
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   771
// As noted elsewhere, the correct long-term solution is to deflate at
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   772
// monitorexit-time, in which case the number of inflated objects is bounded
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   773
// by the number of threads.  That policy obviates the need for scavenging at
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   774
// STW safepoint time.   As an aside, scavenging can be time-consuming when the
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   775
// # of extant monitors is large.   Unfortunately there's a day-1 assumption baked
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   776
// into much HotSpot code that the object::monitor relationship, once established
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   777
// or observed, will remain stable except over potential safepoints.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   778
//
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   779
// We can use either a blocking synchronous VM operation or an async VM operation.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   780
// -- If we use a blocking VM operation :
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   781
//    Calls to ScavengeCheck() should be inserted only into 'safe' locations in paths
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   782
//    that lead to ::inflate() or ::omAlloc().
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   783
//    Even though the safepoint will not directly induce GC, a GC might
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   784
//    piggyback on the safepoint operation, so the caller should hold no naked oops.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   785
//    Furthermore, monitor::object relationships are NOT necessarily stable over this call
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   786
//    unless the caller has made provisions to "pin" the object to the monitor, say
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   787
//    by incrementing the monitor's _count field.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   788
// -- If we use a non-blocking asynchronous VM operation :
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   789
//    the constraints above don't apply.  The safepoint will fire in the future
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   790
//    at a more convenient time.  On the other hand the latency between posting and
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   791
//    running the safepoint introduces or admits "slop" or laxity during which the
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   792
//    monitor population can climb further above the threshold.  The monitor population,
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   793
//    however, tends to converge asymptotically over time to a count that's slightly
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   794
//    above the target value specified by MonitorBound.   That is, we avoid unbounded
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   795
//    growth, albeit with some imprecision.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   796
//
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   797
// The current implementation uses asynchronous VM operations.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   798
//
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   799
// Ideally we'd check if (MonitorPopulation > MonitorBound) in omAlloc()
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   800
// immediately before trying to grow the global list via allocation.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   801
// If the predicate was true then we'd induce a synchronous safepoint, wait
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   802
// for the safepoint to complete, and then again to allocate from the global
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   803
// free list.  This approach is much simpler and precise, admitting no "slop".
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   804
// Unfortunately we can't safely safepoint in the midst of omAlloc(), so
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   805
// instead we use asynchronous safepoints.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   806
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   807
static void InduceScavenge (Thread * Self, const char * Whence) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   808
  // Induce STW safepoint to trim monitors
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   809
  // Ultimately, this results in a call to deflate_idle_monitors() in the near future.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   810
  // More precisely, trigger an asynchronous STW safepoint as the number
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   811
  // of active monitors passes the specified threshold.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   812
  // TODO: assert thread state is reasonable
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   813
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   814
  if (ForceMonitorScavenge == 0 && Atomic::xchg (1, &ForceMonitorScavenge) == 0) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   815
    if (Knob_Verbose) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   816
      ::printf ("Monitor scavenge - Induced STW @%s (%d)\n", Whence, ForceMonitorScavenge) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   817
      ::fflush(stdout) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   818
    }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   819
    // Induce a 'null' safepoint to scavenge monitors
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   820
    // Must VM_Operation instance be heap allocated as the op will be enqueue and posted
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   821
    // to the VMthread and have a lifespan longer than that of this activation record.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   822
    // The VMThread will delete the op when completed.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   823
    VMThread::execute (new VM_ForceAsyncSafepoint()) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   824
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   825
    if (Knob_Verbose) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   826
      ::printf ("Monitor scavenge - STW posted @%s (%d)\n", Whence, ForceMonitorScavenge) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   827
      ::fflush(stdout) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   828
    }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   829
  }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   830
}
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   831
/* Too slow for general assert or debug
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   832
void ObjectSynchronizer::verifyInUse (Thread *Self) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   833
   ObjectMonitor* mid;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   834
   int inusetally = 0;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   835
   for (mid = Self->omInUseList; mid != NULL; mid = mid->FreeNext) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   836
     inusetally ++;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   837
   }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   838
   assert(inusetally == Self->omInUseCount, "inuse count off");
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   839
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   840
   int freetally = 0;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   841
   for (mid = Self->omFreeList; mid != NULL; mid = mid->FreeNext) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   842
     freetally ++;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   843
   }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   844
   assert(freetally == Self->omFreeCount, "free count off");
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   845
}
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   846
*/
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   847
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
ObjectMonitor * ATTR ObjectSynchronizer::omAlloc (Thread * Self) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
    // A large MAXPRIVATE value reduces both list lock contention
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
    // and list coherency traffic, but also tends to increase the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
    // number of objectMonitors in circulation as well as the STW
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
    // scavenge costs.  As usual, we lean toward time in space-time
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
    // tradeoffs.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
    const int MAXPRIVATE = 1024 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
    for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
        ObjectMonitor * m ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
        // 1: try to allocate from the thread's local omFreeList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
        // Threads will attempt to allocate first from their local list, then
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
        // from the global list, and only after those attempts fail will the thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
        // attempt to instantiate new monitors.   Thread-local free lists take
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
        // heat off the ListLock and improve allocation latency, as well as reducing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
        // coherency traffic on the shared global list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
        m = Self->omFreeList ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
        if (m != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
           Self->omFreeList = m->FreeNext ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
           Self->omFreeCount -- ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
           // CONSIDER: set m->FreeNext = BAD -- diagnostic hygiene
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
           guarantee (m->object() == NULL, "invariant") ;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   870
           if (MonitorInUseLists) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   871
             m->FreeNext = Self->omInUseList;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   872
             Self->omInUseList = m;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   873
             Self->omInUseCount ++;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   874
             // verifyInUse(Self);
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   875
           } else {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   876
             m->FreeNext = NULL;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   877
           }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
           return m ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
        // 2: try to allocate from the global gFreeList
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
        // CONSIDER: use muxTry() instead of muxAcquire().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
        // If the muxTry() fails then drop immediately into case 3.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
        // If we're using thread-local free lists then try
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
        // to reprovision the caller's free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
        if (gFreeList != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
            // Reprovision the thread's omFreeList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
            // Use bulk transfers to reduce the allocation rate and heat
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
            // on various locks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
            Thread::muxAcquire (&ListLock, "omAlloc") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
            for (int i = Self->omFreeProvision; --i >= 0 && gFreeList != NULL; ) {
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   892
                MonitorFreeCount --;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
                ObjectMonitor * take = gFreeList ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
                gFreeList = take->FreeNext ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
                guarantee (take->object() == NULL, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
                guarantee (!take->is_busy(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
                take->Recycle() ;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   898
                omRelease (Self, take, false) ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
            Thread::muxRelease (&ListLock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   901
            Self->omFreeProvision += 1 + (Self->omFreeProvision/2) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
            if (Self->omFreeProvision > MAXPRIVATE ) Self->omFreeProvision = MAXPRIVATE ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
            TEVENT (omFirst - reprovision) ;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   904
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   905
            const int mx = MonitorBound ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   906
            if (mx > 0 && (MonitorPopulation-MonitorFreeCount) > mx) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   907
              // We can't safely induce a STW safepoint from omAlloc() as our thread
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   908
              // state may not be appropriate for such activities and callers may hold
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   909
              // naked oops, so instead we defer the action.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   910
              InduceScavenge (Self, "omAlloc") ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   911
            }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   912
            continue;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
        // 3: allocate a block of new ObjectMonitors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
        // Both the local and global free lists are empty -- resort to malloc().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
        // In the current implementation objectMonitors are TSM - immortal.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
        assert (_BLOCKSIZE > 1, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
        ObjectMonitor * temp = new ObjectMonitor[_BLOCKSIZE];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
        // NOTE: (almost) no way to recover if allocation failed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
        // We might be able to induce a STW safepoint and scavenge enough
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
        // objectMonitors to permit progress.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
        if (temp == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
            vm_exit_out_of_memory (sizeof (ObjectMonitor[_BLOCKSIZE]), "Allocate ObjectMonitors") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
        // Format the block.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
        // initialize the linked list, each monitor points to its next
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
        // forming the single linked free list, the very first monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
        // will points to next block, which forms the block list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
        // The trick of using the 1st element in the block as gBlockList
489c9b5090e2 Initial load
duke
parents:
diff changeset
   933
        // linkage should be reconsidered.  A better implementation would
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
        // look like: class Block { Block * next; int N; ObjectMonitor Body [N] ; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
        for (int i = 1; i < _BLOCKSIZE ; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
           temp[i].FreeNext = &temp[i+1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
        // terminate the last monitor as the end of list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
        temp[_BLOCKSIZE - 1].FreeNext = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
        // Element [0] is reserved for global list linkage
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
        temp[0].set_object(CHAINMARKER);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
489c9b5090e2 Initial load
duke
parents:
diff changeset
   946
        // Consider carving out this thread's current request from the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
        // block in hand.  This avoids some lock traffic and redundant
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
        // list activity.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
        // Acquire the ListLock to manipulate BlockList and FreeList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
        // An Oyama-Taura-Yonezawa scheme might be more efficient.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
        Thread::muxAcquire (&ListLock, "omAlloc [2]") ;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   953
        MonitorPopulation += _BLOCKSIZE-1;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   954
        MonitorFreeCount += _BLOCKSIZE-1;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
        // Add the new block to the list of extant blocks (gBlockList).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
        // The very first objectMonitor in a block is reserved and dedicated.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
        // It serves as blocklist "next" linkage.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
        temp[0].FreeNext = gBlockList;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
        gBlockList = temp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
        // Add the new string of objectMonitors to the global free list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
        temp[_BLOCKSIZE - 1].FreeNext = gFreeList ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
        gFreeList = temp + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
        Thread::muxRelease (&ListLock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
        TEVENT (Allocate block of monitors) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   967
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   968
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   969
489c9b5090e2 Initial load
duke
parents:
diff changeset
   970
// Place "m" on the caller's private per-thread omFreeList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   971
// In practice there's no need to clamp or limit the number of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   972
// monitors on a thread's omFreeList as the only time we'll call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   973
// omRelease is to return a monitor to the free list after a CAS
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
// attempt failed.  This doesn't allow unbounded #s of monitors to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
// accumulate on a thread's free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
// In the future the usage of omRelease() might change and monitors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   978
// could migrate between free lists.  In that case to avoid excessive
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
// accumulation we could  limit omCount to (omProvision*2), otherwise return
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
// the objectMonitor to the global list.  We should drain (return) in reasonable chunks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
// That is, *not* one-at-a-time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   984
void ObjectSynchronizer::omRelease (Thread * Self, ObjectMonitor * m, bool fromPerThreadAlloc) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
    guarantee (m->object() == NULL, "invariant") ;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   986
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   987
    // Remove from omInUseList
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   988
    if (MonitorInUseLists && fromPerThreadAlloc) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   989
      ObjectMonitor* curmidinuse = NULL;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   990
      for (ObjectMonitor* mid = Self->omInUseList; mid != NULL; ) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   991
       if (m == mid) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   992
         // extract from per-thread in-use-list
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   993
         if (mid == Self->omInUseList) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   994
           Self->omInUseList = mid->FreeNext;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   995
         } else if (curmidinuse != NULL) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   996
           curmidinuse->FreeNext = mid->FreeNext; // maintain the current thread inuselist
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   997
         }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   998
         Self->omInUseCount --;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   999
         // verifyInUse(Self);
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1000
         break;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1001
       } else {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1002
         curmidinuse = mid;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1003
         mid = mid->FreeNext;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1004
      }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1005
    }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1006
  }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1007
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1008
  // FreeNext is used for both onInUseList and omFreeList, so clear old before setting new
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1009
  m->FreeNext = Self->omFreeList ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1010
  Self->omFreeList = m ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1011
  Self->omFreeCount ++ ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1012
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1013
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1014
// Return the monitors of a moribund thread's local free list to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1015
// the global free list.  Typically a thread calls omFlush() when
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1016
// it's dying.  We could also consider having the VM thread steal
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1017
// monitors from threads that have not run java code over a few
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1018
// consecutive STW safepoints.  Relatedly, we might decay
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1019
// omFreeProvision at STW safepoints.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1020
//
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1021
// Also return the monitors of a moribund thread"s omInUseList to
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1022
// a global gOmInUseList under the global list lock so these
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1023
// will continue to be scanned.
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1024
//
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1025
// We currently call omFlush() from the Thread:: dtor _after the thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1026
// has been excised from the thread list and is no longer a mutator.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1027
// That means that omFlush() can run concurrently with a safepoint and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1028
// the scavenge operator.  Calling omFlush() from JavaThread::exit() might
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1029
// be a better choice as we could safely reason that that the JVM is
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1030
// not at a safepoint at the time of the call, and thus there could
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1031
// be not inopportune interleavings between omFlush() and the scavenge
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1032
// operator.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1033
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1034
void ObjectSynchronizer::omFlush (Thread * Self) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1035
    ObjectMonitor * List = Self->omFreeList ;  // Null-terminated SLL
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1036
    Self->omFreeList = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1037
    ObjectMonitor * Tail = NULL ;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1038
    int Tally = 0;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1039
    if (List != NULL) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1040
      ObjectMonitor * s ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1041
      for (s = List ; s != NULL ; s = s->FreeNext) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1042
          Tally ++ ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1043
          Tail = s ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1044
          guarantee (s->object() == NULL, "invariant") ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1045
          guarantee (!s->is_busy(), "invariant") ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1046
          s->set_owner (NULL) ;   // redundant but good hygiene
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1047
          TEVENT (omFlush - Move one) ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1048
      }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1049
      guarantee (Tail != NULL && List != NULL, "invariant") ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1050
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1051
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1052
    ObjectMonitor * InUseList = Self->omInUseList;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1053
    ObjectMonitor * InUseTail = NULL ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1054
    int InUseTally = 0;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1055
    if (InUseList != NULL) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1056
      Self->omInUseList = NULL;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1057
      ObjectMonitor *curom;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1058
      for (curom = InUseList; curom != NULL; curom = curom->FreeNext) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1059
        InUseTail = curom;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1060
        InUseTally++;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1061
      }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1062
// TODO debug
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1063
      assert(Self->omInUseCount == InUseTally, "inuse count off");
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1064
      Self->omInUseCount = 0;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1065
      guarantee (InUseTail != NULL && InUseList != NULL, "invariant");
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1066
    }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1067
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1068
    Thread::muxAcquire (&ListLock, "omFlush") ;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1069
    if (Tail != NULL) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1070
      Tail->FreeNext = gFreeList ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1071
      gFreeList = List ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1072
      MonitorFreeCount += Tally;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1073
    }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1074
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1075
    if (InUseTail != NULL) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1076
      InUseTail->FreeNext = gOmInUseList;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1077
      gOmInUseList = InUseList;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1078
      gOmInUseCount += InUseTally;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1079
    }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1080
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1081
    Thread::muxRelease (&ListLock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1082
    TEVENT (omFlush) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1083
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1084
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1085
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1086
// Get the next block in the block list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1087
static inline ObjectMonitor* next(ObjectMonitor* block) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1088
  assert(block->object() == CHAINMARKER, "must be a block header");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1089
  block = block->FreeNext ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1090
  assert(block == NULL || block->object() == CHAINMARKER, "must be a block header");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1091
  return block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1092
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1093
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1094
// Fast path code shared by multiple functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1095
ObjectMonitor* ObjectSynchronizer::inflate_helper(oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1096
  markOop mark = obj->mark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1097
  if (mark->has_monitor()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1098
    assert(ObjectSynchronizer::verify_objmon_isinpool(mark->monitor()), "monitor is invalid");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1099
    assert(mark->monitor()->header()->is_neutral(), "monitor must record a good object header");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1100
    return mark->monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1101
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1102
  return ObjectSynchronizer::inflate(Thread::current(), obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1103
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1104
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1105
// Note that we could encounter some performance loss through false-sharing as
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1106
// multiple locks occupy the same $ line.  Padding might be appropriate.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1107
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1108
#define NINFLATIONLOCKS 256
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1109
static volatile intptr_t InflationLocks [NINFLATIONLOCKS] ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1110
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1111
static markOop ReadStableMark (oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1112
  markOop mark = obj->mark() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1113
  if (!mark->is_being_inflated()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1114
    return mark ;       // normal fast-path return
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1115
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1116
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1117
  int its = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1118
  for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1119
    markOop mark = obj->mark() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1120
    if (!mark->is_being_inflated()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1121
      return mark ;    // normal fast-path return
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1122
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1123
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1124
    // The object is being inflated by some other thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1125
    // The caller of ReadStableMark() must wait for inflation to complete.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1126
    // Avoid live-lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1127
    // TODO: consider calling SafepointSynchronize::do_call_back() while
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1128
    // spinning to see if there's a safepoint pending.  If so, immediately
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1129
    // yielding or blocking would be appropriate.  Avoid spinning while
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1130
    // there is a safepoint pending.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1131
    // TODO: add inflation contention performance counters.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1132
    // TODO: restrict the aggregate number of spinners.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1133
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1134
    ++its ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1135
    if (its > 10000 || !os::is_MP()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1136
       if (its & 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1137
         os::NakedYield() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1138
         TEVENT (Inflate: INFLATING - yield) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1139
       } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1140
         // Note that the following code attenuates the livelock problem but is not
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1141
         // a complete remedy.  A more complete solution would require that the inflating
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1142
         // thread hold the associated inflation lock.  The following code simply restricts
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1143
         // the number of spinners to at most one.  We'll have N-2 threads blocked
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1144
         // on the inflationlock, 1 thread holding the inflation lock and using
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1145
         // a yield/park strategy, and 1 thread in the midst of inflation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1146
         // A more refined approach would be to change the encoding of INFLATING
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1147
         // to allow encapsulation of a native thread pointer.  Threads waiting for
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1148
         // inflation to complete would use CAS to push themselves onto a singly linked
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1149
         // list rooted at the markword.  Once enqueued, they'd loop, checking a per-thread flag
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1150
         // and calling park().  When inflation was complete the thread that accomplished inflation
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1151
         // would detach the list and set the markword to inflated with a single CAS and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1152
         // then for each thread on the list, set the flag and unpark() the thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1153
         // This is conceptually similar to muxAcquire-muxRelease, except that muxRelease
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1154
         // wakes at most one thread whereas we need to wake the entire list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1155
         int ix = (intptr_t(obj) >> 5) & (NINFLATIONLOCKS-1) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1156
         int YieldThenBlock = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1157
         assert (ix >= 0 && ix < NINFLATIONLOCKS, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1158
         assert ((NINFLATIONLOCKS & (NINFLATIONLOCKS-1)) == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1159
         Thread::muxAcquire (InflationLocks + ix, "InflationLock") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1160
         while (obj->mark() == markOopDesc::INFLATING()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1161
           // Beware: NakedYield() is advisory and has almost no effect on some platforms
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1162
           // so we periodically call Self->_ParkEvent->park(1).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1163
           // We use a mixed spin/yield/block mechanism.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1164
           if ((YieldThenBlock++) >= 16) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1165
              Thread::current()->_ParkEvent->park(1) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1166
           } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1167
              os::NakedYield() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1168
           }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1169
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1170
         Thread::muxRelease (InflationLocks + ix ) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1171
         TEVENT (Inflate: INFLATING - yield/park) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1172
       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1173
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1174
       SpinPause() ;       // SMP-polite spinning
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1175
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1176
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1177
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1178
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1179
ObjectMonitor * ATTR ObjectSynchronizer::inflate (Thread * Self, oop object) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1180
  // Inflate mutates the heap ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1181
  // Relaxing assertion for bug 6320749.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1182
  assert (Universe::verify_in_progress() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1183
          !SafepointSynchronize::is_at_safepoint(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1184
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1185
  for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1186
      const markOop mark = object->mark() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1187
      assert (!mark->has_bias_pattern(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1188
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1189
      // The mark can be in one of the following states:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1190
      // *  Inflated     - just return
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1191
      // *  Stack-locked - coerce it to inflated
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1192
      // *  INFLATING    - busy wait for conversion to complete
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1193
      // *  Neutral      - aggressively inflate the object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1194
      // *  BIASED       - Illegal.  We should never see this
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1195
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1196
      // CASE: inflated
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1197
      if (mark->has_monitor()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1198
          ObjectMonitor * inf = mark->monitor() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1199
          assert (inf->header()->is_neutral(), "invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1200
          assert (inf->object() == object, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1201
          assert (ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1202
          return inf ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1203
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1204
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1205
      // CASE: inflation in progress - inflating over a stack-lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1206
      // Some other thread is converting from stack-locked to inflated.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1207
      // Only that thread can complete inflation -- other threads must wait.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1208
      // The INFLATING value is transient.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1209
      // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1210
      // We could always eliminate polling by parking the thread on some auxiliary list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1211
      if (mark == markOopDesc::INFLATING()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1212
         TEVENT (Inflate: spin while INFLATING) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1213
         ReadStableMark(object) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1214
         continue ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1215
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1216
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1217
      // CASE: stack-locked
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1218
      // Could be stack-locked either by this thread or by some other thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1219
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1220
      // Note that we allocate the objectmonitor speculatively, _before_ attempting
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1221
      // to install INFLATING into the mark word.  We originally installed INFLATING,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1222
      // allocated the objectmonitor, and then finally STed the address of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1223
      // objectmonitor into the mark.  This was correct, but artificially lengthened
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1224
      // the interval in which INFLATED appeared in the mark, thus increasing
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1225
      // the odds of inflation contention.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1226
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1227
      // We now use per-thread private objectmonitor free lists.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1228
      // These list are reprovisioned from the global free list outside the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1229
      // critical INFLATING...ST interval.  A thread can transfer
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1230
      // multiple objectmonitors en-mass from the global free list to its local free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1231
      // This reduces coherency traffic and lock contention on the global free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1232
      // Using such local free lists, it doesn't matter if the omAlloc() call appears
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1233
      // before or after the CAS(INFLATING) operation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1234
      // See the comments in omAlloc().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1235
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1236
      if (mark->has_locker()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1237
          ObjectMonitor * m = omAlloc (Self) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1238
          // Optimistically prepare the objectmonitor - anticipate successful CAS
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1239
          // We do this before the CAS in order to minimize the length of time
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1240
          // in which INFLATING appears in the mark.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1241
          m->Recycle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1242
          m->_Responsible  = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1243
          m->OwnerIsThread = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1244
          m->_recursions   = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1245
          m->_SpinDuration = Knob_SpinLimit ;   // Consider: maintain by type/class
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1246
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1247
          markOop cmp = (markOop) Atomic::cmpxchg_ptr (markOopDesc::INFLATING(), object->mark_addr(), mark) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1248
          if (cmp != mark) {
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1249
             omRelease (Self, m, true) ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1250
             continue ;       // Interference -- just retry
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1251
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1252
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1253
          // We've successfully installed INFLATING (0) into the mark-word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1254
          // This is the only case where 0 will appear in a mark-work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1255
          // Only the singular thread that successfully swings the mark-word
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1256
          // to 0 can perform (or more precisely, complete) inflation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1257
          //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1258
          // Why do we CAS a 0 into the mark-word instead of just CASing the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1259
          // mark-word from the stack-locked value directly to the new inflated state?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1260
          // Consider what happens when a thread unlocks a stack-locked object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1261
          // It attempts to use CAS to swing the displaced header value from the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1262
          // on-stack basiclock back into the object header.  Recall also that the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1263
          // header value (hashcode, etc) can reside in (a) the object header, or
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1264
          // (b) a displaced header associated with the stack-lock, or (c) a displaced
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1265
          // header in an objectMonitor.  The inflate() routine must copy the header
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1266
          // value from the basiclock on the owner's stack to the objectMonitor, all
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1267
          // the while preserving the hashCode stability invariants.  If the owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1268
          // decides to release the lock while the value is 0, the unlock will fail
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1269
          // and control will eventually pass from slow_exit() to inflate.  The owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1270
          // will then spin, waiting for the 0 value to disappear.   Put another way,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1271
          // the 0 causes the owner to stall if the owner happens to try to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1272
          // drop the lock (restoring the header from the basiclock to the object)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1273
          // while inflation is in-progress.  This protocol avoids races that might
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1274
          // would otherwise permit hashCode values to change or "flicker" for an object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1275
          // Critically, while object->mark is 0 mark->displaced_mark_helper() is stable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1276
          // 0 serves as a "BUSY" inflate-in-progress indicator.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1277
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1278
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1279
          // fetch the displaced mark from the owner's stack.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1280
          // The owner can't die or unwind past the lock while our INFLATING
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1281
          // object is in the mark.  Furthermore the owner can't complete
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1282
          // an unlock on the object, either.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1283
          markOop dmw = mark->displaced_mark_helper() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1284
          assert (dmw->is_neutral(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1285
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1286
          // Setup monitor fields to proper values -- prepare the monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1287
          m->set_header(dmw) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1288
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1289
          // Optimization: if the mark->locker stack address is associated
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1290
          // with this thread we could simply set m->_owner = Self and
2526
39a58a50be35 6699669: Hotspot server leaves synchronized block with monitor in bad state
xlu
parents: 2105
diff changeset
  1291
          // m->OwnerIsThread = 1. Note that a thread can inflate an object
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1292
          // that it has stack-locked -- as might happen in wait() -- directly
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1293
          // with CAS.  That is, we can avoid the xchg-NULL .... ST idiom.
2526
39a58a50be35 6699669: Hotspot server leaves synchronized block with monitor in bad state
xlu
parents: 2105
diff changeset
  1294
          m->set_owner(mark->locker());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1295
          m->set_object(object);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1296
          // TODO-FIXME: assert BasicLock->dhw != 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1297
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1298
          // Must preserve store ordering. The monitor state must
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1299
          // be stable at the time of publishing the monitor address.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1300
          guarantee (object->mark() == markOopDesc::INFLATING(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1301
          object->release_set_mark(markOopDesc::encode(m));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1302
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1303
          // Hopefully the performance counters are allocated on distinct cache lines
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1304
          // to avoid false sharing on MP systems ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1305
          if (_sync_Inflations != NULL) _sync_Inflations->inc() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1306
          TEVENT(Inflate: overwrite stacklock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1307
          if (TraceMonitorInflation) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1308
            if (object->is_instance()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1309
              ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1310
              tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1311
                (intptr_t) object, (intptr_t) object->mark(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1312
                Klass::cast(object->klass())->external_name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1313
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1314
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1315
          return m ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1316
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1317
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1318
      // CASE: neutral
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1319
      // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1320
      // If we know we're inflating for entry it's better to inflate by swinging a
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1321
      // pre-locked objectMonitor pointer into the object header.   A successful
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1322
      // CAS inflates the object *and* confers ownership to the inflating thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1323
      // In the current implementation we use a 2-step mechanism where we CAS()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1324
      // to inflate and then CAS() again to try to swing _owner from NULL to Self.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1325
      // An inflateTry() method that we could call from fast_enter() and slow_enter()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1326
      // would be useful.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1327
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1328
      assert (mark->is_neutral(), "invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1329
      ObjectMonitor * m = omAlloc (Self) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1330
      // prepare m for installation - set monitor to initial state
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1331
      m->Recycle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1332
      m->set_header(mark);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1333
      m->set_owner(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1334
      m->set_object(object);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1335
      m->OwnerIsThread = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1336
      m->_recursions   = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1337
      m->_Responsible  = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1338
      m->_SpinDuration = Knob_SpinLimit ;       // consider: keep metastats by type/class
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1339
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1340
      if (Atomic::cmpxchg_ptr (markOopDesc::encode(m), object->mark_addr(), mark) != mark) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1341
          m->set_object (NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1342
          m->set_owner  (NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1343
          m->OwnerIsThread = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1344
          m->Recycle() ;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1345
          omRelease (Self, m, true) ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1346
          m = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1347
          continue ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1348
          // interference - the markword changed - just retry.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1349
          // The state-transitions are one-way, so there's no chance of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1350
          // live-lock -- "Inflated" is an absorbing state.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1351
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1352
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1353
      // Hopefully the performance counters are allocated on distinct
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1354
      // cache lines to avoid false sharing on MP systems ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1355
      if (_sync_Inflations != NULL) _sync_Inflations->inc() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1356
      TEVENT(Inflate: overwrite neutral) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1357
      if (TraceMonitorInflation) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1358
        if (object->is_instance()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1359
          ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1360
          tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1361
            (intptr_t) object, (intptr_t) object->mark(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1362
            Klass::cast(object->klass())->external_name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1363
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1364
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1365
      return m ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1366
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1367
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1368
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1369
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1370
// This the fast monitor enter. The interpreter and compiler use
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1371
// some assembly copies of this code. Make sure update those code
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1372
// if the following function is changed. The implementation is
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1373
// extremely sensitive to race condition. Be careful.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1374
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1375
void ObjectSynchronizer::fast_enter(Handle obj, BasicLock* lock, bool attempt_rebias, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1376
 if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1377
    if (!SafepointSynchronize::is_at_safepoint()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1378
      BiasedLocking::Condition cond = BiasedLocking::revoke_and_rebias(obj, attempt_rebias, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1379
      if (cond == BiasedLocking::BIAS_REVOKED_AND_REBIASED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1380
        return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1381
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1382
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1383
      assert(!attempt_rebias, "can not rebias toward VM thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1384
      BiasedLocking::revoke_at_safepoint(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1385
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1386
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
2526
39a58a50be35 6699669: Hotspot server leaves synchronized block with monitor in bad state
xlu
parents: 2105
diff changeset
  1387
 }
39a58a50be35 6699669: Hotspot server leaves synchronized block with monitor in bad state
xlu
parents: 2105
diff changeset
  1388
39a58a50be35 6699669: Hotspot server leaves synchronized block with monitor in bad state
xlu
parents: 2105
diff changeset
  1389
 slow_enter (obj, lock, THREAD) ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1390
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1391
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1392
void ObjectSynchronizer::fast_exit(oop object, BasicLock* lock, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1393
  assert(!object->mark()->has_bias_pattern(), "should not see bias pattern here");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1394
  // if displaced header is null, the previous enter is recursive enter, no-op
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1395
  markOop dhw = lock->displaced_header();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1396
  markOop mark ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1397
  if (dhw == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1398
     // Recursive stack-lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1399
     // Diagnostics -- Could be: stack-locked, inflating, inflated.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1400
     mark = object->mark() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1401
     assert (!mark->is_neutral(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1402
     if (mark->has_locker() && mark != markOopDesc::INFLATING()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1403
        assert(THREAD->is_lock_owned((address)mark->locker()), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1404
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1405
     if (mark->has_monitor()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1406
        ObjectMonitor * m = mark->monitor() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1407
        assert(((oop)(m->object()))->mark() == mark, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1408
        assert(m->is_entered(THREAD), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1409
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1410
     return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1411
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1412
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1413
  mark = object->mark() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1414
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1415
  // If the object is stack-locked by the current thread, try to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1416
  // swing the displaced header from the box back to the mark.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1417
  if (mark == (markOop) lock) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1418
     assert (dhw->is_neutral(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1419
     if ((markOop) Atomic::cmpxchg_ptr (dhw, object->mark_addr(), mark) == mark) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1420
        TEVENT (fast_exit: release stacklock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1421
        return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1422
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1423
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1424
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1425
  ObjectSynchronizer::inflate(THREAD, object)->exit (THREAD) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1426
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1427
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1428
// This routine is used to handle interpreter/compiler slow case
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1429
// We don't need to use fast path here, because it must have been
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1430
// failed in the interpreter/compiler code.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1431
void ObjectSynchronizer::slow_enter(Handle obj, BasicLock* lock, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1432
  markOop mark = obj->mark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1433
  assert(!mark->has_bias_pattern(), "should not see bias pattern here");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1434
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1435
  if (mark->is_neutral()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1436
    // Anticipate successful CAS -- the ST of the displaced mark must
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1437
    // be visible <= the ST performed by the CAS.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1438
    lock->set_displaced_header(mark);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1439
    if (mark == (markOop) Atomic::cmpxchg_ptr(lock, obj()->mark_addr(), mark)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1440
      TEVENT (slow_enter: release stacklock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1441
      return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1442
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1443
    // Fall through to inflate() ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1444
  } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1445
  if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1446
    assert(lock != mark->locker(), "must not re-lock the same lock");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1447
    assert(lock != (BasicLock*)obj->mark(), "don't relock with same BasicLock");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1448
    lock->set_displaced_header(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1449
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1450
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1451
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1452
#if 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1453
  // The following optimization isn't particularly useful.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1454
  if (mark->has_monitor() && mark->monitor()->is_entered(THREAD)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1455
    lock->set_displaced_header (NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1456
    return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1457
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1458
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1459
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1460
  // The object header will never be displaced to this lock,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1461
  // so it does not matter what the value is, except that it
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1462
  // must be non-zero to avoid looking like a re-entrant lock,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1463
  // and must not look locked either.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1464
  lock->set_displaced_header(markOopDesc::unused_mark());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1465
  ObjectSynchronizer::inflate(THREAD, obj())->enter(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1466
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1467
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1468
// This routine is used to handle interpreter/compiler slow case
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1469
// We don't need to use fast path here, because it must have
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1470
// failed in the interpreter/compiler code. Simply use the heavy
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1471
// weight monitor should be ok, unless someone find otherwise.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1472
void ObjectSynchronizer::slow_exit(oop object, BasicLock* lock, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1473
  fast_exit (object, lock, THREAD) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1474
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1475
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1476
// NOTE: must use heavy weight monitor to handle jni monitor enter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1477
void ObjectSynchronizer::jni_enter(Handle obj, TRAPS) { // possible entry from jni enter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1478
  // the current locking is from JNI instead of Java code
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1479
  TEVENT (jni_enter) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1480
  if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1481
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1482
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1483
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1484
  THREAD->set_current_pending_monitor_is_from_java(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1485
  ObjectSynchronizer::inflate(THREAD, obj())->enter(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1486
  THREAD->set_current_pending_monitor_is_from_java(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1487
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1488
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1489
// NOTE: must use heavy weight monitor to handle jni monitor enter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1490
bool ObjectSynchronizer::jni_try_enter(Handle obj, Thread* THREAD) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1491
  if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1492
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1493
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1494
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1495
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1496
  ObjectMonitor* monitor = ObjectSynchronizer::inflate_helper(obj());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1497
  return monitor->try_enter(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1498
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1499
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1500
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1501
// NOTE: must use heavy weight monitor to handle jni monitor exit
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1502
void ObjectSynchronizer::jni_exit(oop obj, Thread* THREAD) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1503
  TEVENT (jni_exit) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1504
  if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1505
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1506
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1507
  assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1508
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1509
  ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1510
  // If this thread has locked the object, exit the monitor.  Note:  can't use
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1511
  // monitor->check(CHECK); must exit even if an exception is pending.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1512
  if (monitor->check(THREAD)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1513
     monitor->exit(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1514
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1515
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1516
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1517
// complete_exit()/reenter() are used to wait on a nested lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1518
// i.e. to give up an outer lock completely and then re-enter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1519
// Used when holding nested locks - lock acquisition order: lock1 then lock2
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1520
//  1) complete_exit lock1 - saving recursion count
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1521
//  2) wait on lock2
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1522
//  3) when notified on lock2, unlock lock2
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1523
//  4) reenter lock1 with original recursion count
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1524
//  5) lock lock2
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1525
// NOTE: must use heavy weight monitor to handle complete_exit/reenter()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1526
intptr_t ObjectSynchronizer::complete_exit(Handle obj, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1527
  TEVENT (complete_exit) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1528
  if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1529
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1530
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1531
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1532
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1533
  ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1534
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1535
  return monitor->complete_exit(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1536
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1537
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1538
// NOTE: must use heavy weight monitor to handle complete_exit/reenter()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1539
void ObjectSynchronizer::reenter(Handle obj, intptr_t recursion, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1540
  TEVENT (reenter) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1541
  if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1542
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1543
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1544
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1545
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1546
  ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1547
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1548
  monitor->reenter(recursion, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1549
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1550
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1551
// This exists only as a workaround of dtrace bug 6254741
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1552
int dtrace_waited_probe(ObjectMonitor* monitor, Handle obj, Thread* thr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1553
  DTRACE_MONITOR_PROBE(waited, monitor, obj(), thr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1554
  return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1555
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1556
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1557
// NOTE: must use heavy weight monitor to handle wait()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1558
void ObjectSynchronizer::wait(Handle obj, jlong millis, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1559
  if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1560
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1561
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1562
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1563
  if (millis < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1564
    TEVENT (wait - throw IAX) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1565
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1566
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1567
  ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1568
  DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), THREAD, millis);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1569
  monitor->wait(millis, true, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1570
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1571
  /* This dummy call is in place to get around dtrace bug 6254741.  Once
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1572
     that's fixed we can uncomment the following line and remove the call */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1573
  // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1574
  dtrace_waited_probe(monitor, obj, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1575
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1576
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1577
void ObjectSynchronizer::waitUninterruptibly (Handle obj, jlong millis, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1578
  if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1579
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1580
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1581
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1582
  if (millis < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1583
    TEVENT (wait - throw IAX) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1584
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1585
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1586
  ObjectSynchronizer::inflate(THREAD, obj()) -> wait(millis, false, THREAD) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1587
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1588
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1589
void ObjectSynchronizer::notify(Handle obj, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1590
 if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1591
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1592
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1593
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1594
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1595
  markOop mark = obj->mark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1596
  if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1597
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1598
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1599
  ObjectSynchronizer::inflate(THREAD, obj())->notify(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1600
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1601
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1602
// NOTE: see comment of notify()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1603
void ObjectSynchronizer::notifyall(Handle obj, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1604
  if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1605
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1606
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1607
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1608
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1609
  markOop mark = obj->mark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1610
  if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1611
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1612
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1613
  ObjectSynchronizer::inflate(THREAD, obj())->notifyAll(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1614
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1615
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1616
intptr_t ObjectSynchronizer::FastHashCode (Thread * Self, oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1617
  if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1618
    // NOTE: many places throughout the JVM do not expect a safepoint
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1619
    // to be taken here, in particular most operations on perm gen
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1620
    // objects. However, we only ever bias Java instances and all of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1621
    // the call sites of identity_hash that might revoke biases have
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1622
    // been checked to make sure they can handle a safepoint. The
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1623
    // added check of the bias pattern is to avoid useless calls to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1624
    // thread-local storage.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1625
    if (obj->mark()->has_bias_pattern()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1626
      // Box and unbox the raw reference just in case we cause a STW safepoint.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1627
      Handle hobj (Self, obj) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1628
      // Relaxing assertion for bug 6320749.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1629
      assert (Universe::verify_in_progress() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1630
              !SafepointSynchronize::is_at_safepoint(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1631
             "biases should not be seen by VM thread here");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1632
      BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1633
      obj = hobj() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1634
      assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1635
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1636
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1637
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1638
  // hashCode() is a heap mutator ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1639
  // Relaxing assertion for bug 6320749.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1640
  assert (Universe::verify_in_progress() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1641
          !SafepointSynchronize::is_at_safepoint(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1642
  assert (Universe::verify_in_progress() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1643
          Self->is_Java_thread() , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1644
  assert (Universe::verify_in_progress() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1645
         ((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1646
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1647
  ObjectMonitor* monitor = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1648
  markOop temp, test;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1649
  intptr_t hash;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1650
  markOop mark = ReadStableMark (obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1651
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1652
  // object should remain ineligible for biased locking
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1653
  assert (!mark->has_bias_pattern(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1654
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1655
  if (mark->is_neutral()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1656
    hash = mark->hash();              // this is a normal header
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1657
    if (hash) {                       // if it has hash, just return it
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1658
      return hash;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1659
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1660
    hash = get_next_hash(Self, obj);  // allocate a new hash code
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1661
    temp = mark->copy_set_hash(hash); // merge the hash code into header
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1662
    // use (machine word version) atomic operation to install the hash
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1663
    test = (markOop) Atomic::cmpxchg_ptr(temp, obj->mark_addr(), mark);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1664
    if (test == mark) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1665
      return hash;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1666
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1667
    // If atomic operation failed, we must inflate the header
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1668
    // into heavy weight monitor. We could add more code here
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1669
    // for fast path, but it does not worth the complexity.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1670
  } else if (mark->has_monitor()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1671
    monitor = mark->monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1672
    temp = monitor->header();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1673
    assert (temp->is_neutral(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1674
    hash = temp->hash();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1675
    if (hash) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1676
      return hash;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1677
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1678
    // Skip to the following code to reduce code size
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1679
  } else if (Self->is_lock_owned((address)mark->locker())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1680
    temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1681
    assert (temp->is_neutral(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1682
    hash = temp->hash();              // by current thread, check if the displaced
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1683
    if (hash) {                       // header contains hash code
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1684
      return hash;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1685
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1686
    // WARNING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1687
    //   The displaced header is strictly immutable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1688
    // It can NOT be changed in ANY cases. So we have
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1689
    // to inflate the header into heavyweight monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1690
    // even the current thread owns the lock. The reason
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1691
    // is the BasicLock (stack slot) will be asynchronously
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1692
    // read by other threads during the inflate() function.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1693
    // Any change to stack may not propagate to other threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1694
    // correctly.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1695
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1696
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1697
  // Inflate the monitor to set hash code
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1698
  monitor = ObjectSynchronizer::inflate(Self, obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1699
  // Load displaced header and check it has hash code
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1700
  mark = monitor->header();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1701
  assert (mark->is_neutral(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1702
  hash = mark->hash();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1703
  if (hash == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1704
    hash = get_next_hash(Self, obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1705
    temp = mark->copy_set_hash(hash); // merge hash code into header
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1706
    assert (temp->is_neutral(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1707
    test = (markOop) Atomic::cmpxchg_ptr(temp, monitor, mark);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1708
    if (test != mark) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1709
      // The only update to the header in the monitor (outside GC)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1710
      // is install the hash code. If someone add new usage of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1711
      // displaced header, please update this code
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1712
      hash = test->hash();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1713
      assert (test->is_neutral(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1714
      assert (hash != 0, "Trivial unexpected object/monitor header usage.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1715
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1716
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1717
  // We finally get the hash
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1718
  return hash;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1719
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1720
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1721
// Deprecated -- use FastHashCode() instead.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1722
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1723
intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1724
  return FastHashCode (Thread::current(), obj()) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1725
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1726
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1727
bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* thread,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1728
                                                   Handle h_obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1729
  if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1730
    BiasedLocking::revoke_and_rebias(h_obj, false, thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1731
    assert(!h_obj->mark()->has_bias_pattern(), "biases should be revoked by now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1732
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1733
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1734
  assert(thread == JavaThread::current(), "Can only be called on current thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1735
  oop obj = h_obj();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1736
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1737
  markOop mark = ReadStableMark (obj) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1738
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1739
  // Uncontended case, header points to stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1740
  if (mark->has_locker()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1741
    return thread->is_lock_owned((address)mark->locker());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1742
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1743
  // Contended case, header points to ObjectMonitor (tagged pointer)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1744
  if (mark->has_monitor()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1745
    ObjectMonitor* monitor = mark->monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1746
    return monitor->is_entered(thread) != 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1747
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1748
  // Unlocked case, header in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1749
  assert(mark->is_neutral(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1750
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1751
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1752
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1753
// Be aware of this method could revoke bias of the lock object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1754
// This method querys the ownership of the lock handle specified by 'h_obj'.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1755
// If the current thread owns the lock, it returns owner_self. If no
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1756
// thread owns the lock, it returns owner_none. Otherwise, it will return
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1757
// ower_other.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1758
ObjectSynchronizer::LockOwnership ObjectSynchronizer::query_lock_ownership
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1759
(JavaThread *self, Handle h_obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1760
  // The caller must beware this method can revoke bias, and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1761
  // revocation can result in a safepoint.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1762
  assert (!SafepointSynchronize::is_at_safepoint(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1763
  assert (self->thread_state() != _thread_blocked , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1764
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1765
  // Possible mark states: neutral, biased, stack-locked, inflated
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1766
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1767
  if (UseBiasedLocking && h_obj()->mark()->has_bias_pattern()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1768
    // CASE: biased
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1769
    BiasedLocking::revoke_and_rebias(h_obj, false, self);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1770
    assert(!h_obj->mark()->has_bias_pattern(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1771
           "biases should be revoked by now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1772
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1773
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1774
  assert(self == JavaThread::current(), "Can only be called on current thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1775
  oop obj = h_obj();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1776
  markOop mark = ReadStableMark (obj) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1777
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1778
  // CASE: stack-locked.  Mark points to a BasicLock on the owner's stack.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1779
  if (mark->has_locker()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1780
    return self->is_lock_owned((address)mark->locker()) ?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1781
      owner_self : owner_other;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1782
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1783
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1784
  // CASE: inflated. Mark (tagged pointer) points to an objectMonitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1785
  // The Object:ObjectMonitor relationship is stable as long as we're
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1786
  // not at a safepoint.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1787
  if (mark->has_monitor()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1788
    void * owner = mark->monitor()->_owner ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1789
    if (owner == NULL) return owner_none ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1790
    return (owner == self ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1791
            self->is_lock_owned((address)owner)) ? owner_self : owner_other;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1792
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1793
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1794
  // CASE: neutral
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1795
  assert(mark->is_neutral(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1796
  return owner_none ;           // it's unlocked
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1797
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1798
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1799
// FIXME: jvmti should call this
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1800
JavaThread* ObjectSynchronizer::get_lock_owner(Handle h_obj, bool doLock) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1801
  if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1802
    if (SafepointSynchronize::is_at_safepoint()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1803
      BiasedLocking::revoke_at_safepoint(h_obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1804
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1805
      BiasedLocking::revoke_and_rebias(h_obj, false, JavaThread::current());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1806
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1807
    assert(!h_obj->mark()->has_bias_pattern(), "biases should be revoked by now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1808
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1809
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1810
  oop obj = h_obj();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1811
  address owner = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1812
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1813
  markOop mark = ReadStableMark (obj) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1814
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1815
  // Uncontended case, header points to stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1816
  if (mark->has_locker()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1817
    owner = (address) mark->locker();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1818
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1819
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1820
  // Contended case, header points to ObjectMonitor (tagged pointer)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1821
  if (mark->has_monitor()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1822
    ObjectMonitor* monitor = mark->monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1823
    assert(monitor != NULL, "monitor should be non-null");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1824
    owner = (address) monitor->owner();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1825
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1826
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1827
  if (owner != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1828
    return Threads::owning_thread_from_monitor_owner(owner, doLock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1829
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1830
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1831
  // Unlocked case, header in place
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1832
  // Cannot have assertion since this object may have been
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1833
  // locked by another thread when reaching here.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1834
  // assert(mark->is_neutral(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1835
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1836
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1837
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1838
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1839
// Iterate through monitor cache and attempt to release thread's monitors
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1840
// Gives up on a particular monitor if an exception occurs, but continues
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1841
// the overall iteration, swallowing the exception.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1842
class ReleaseJavaMonitorsClosure: public MonitorClosure {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1843
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1844
  TRAPS;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1845
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1846
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1847
  ReleaseJavaMonitorsClosure(Thread* thread) : THREAD(thread) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1848
  void do_monitor(ObjectMonitor* mid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1849
    if (mid->owner() == THREAD) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1850
      (void)mid->complete_exit(CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1851
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1852
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1853
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1854
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1855
// Release all inflated monitors owned by THREAD.  Lightweight monitors are
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1856
// ignored.  This is meant to be called during JNI thread detach which assumes
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1857
// all remaining monitors are heavyweight.  All exceptions are swallowed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1858
// Scanning the extant monitor list can be time consuming.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1859
// A simple optimization is to add a per-thread flag that indicates a thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1860
// called jni_monitorenter() during its lifetime.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1861
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1862
// Instead of No_Savepoint_Verifier it might be cheaper to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1863
// use an idiom of the form:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1864
//   auto int tmp = SafepointSynchronize::_safepoint_counter ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1865
//   <code that must not run at safepoint>
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1866
//   guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1867
// Since the tests are extremely cheap we could leave them enabled
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1868
// for normal product builds.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1869
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1870
void ObjectSynchronizer::release_monitors_owned_by_thread(TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1871
  assert(THREAD == JavaThread::current(), "must be current Java thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1872
  No_Safepoint_Verifier nsv ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1873
  ReleaseJavaMonitorsClosure rjmc(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1874
  Thread::muxAcquire(&ListLock, "release_monitors_owned_by_thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1875
  ObjectSynchronizer::monitors_iterate(&rjmc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1876
  Thread::muxRelease(&ListLock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1877
  THREAD->clear_pending_exception();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1878
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1879
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1880
// Visitors ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1881
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1882
void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1883
  ObjectMonitor* block = gBlockList;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1884
  ObjectMonitor* mid;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1885
  while (block) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1886
    assert(block->object() == CHAINMARKER, "must be a block header");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1887
    for (int i = _BLOCKSIZE - 1; i > 0; i--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1888
      mid = block + i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1889
      oop object = (oop) mid->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1890
      if (object != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1891
        closure->do_monitor(mid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1892
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1893
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1894
    block = (ObjectMonitor*) block->FreeNext;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1895
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1896
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1897
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1898
void ObjectSynchronizer::oops_do(OopClosure* f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1899
  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1900
  for (ObjectMonitor* block = gBlockList; block != NULL; block = next(block)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1901
    assert(block->object() == CHAINMARKER, "must be a block header");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1902
    for (int i = 1; i < _BLOCKSIZE; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1903
      ObjectMonitor* mid = &block[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1904
      if (mid->object() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1905
        f->do_oop((oop*)mid->object_addr());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1906
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1907
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1908
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1909
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1910
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1911
// Deflate_idle_monitors() is called at all safepoints, immediately
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1912
// after all mutators are stopped, but before any objects have moved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1913
// It traverses the list of known monitors, deflating where possible.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1914
// The scavenged monitor are returned to the monitor free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1915
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1916
// Beware that we scavenge at *every* stop-the-world point.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1917
// Having a large number of monitors in-circulation negatively
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1918
// impacts the performance of some applications (e.g., PointBase).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1919
// Broadly, we want to minimize the # of monitors in circulation.
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1920
//
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1921
// We have added a flag, MonitorInUseLists, which creates a list
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1922
// of active monitors for each thread. deflate_idle_monitors()
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1923
// only scans the per-thread inuse lists. omAlloc() puts all
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1924
// assigned monitors on the per-thread list. deflate_idle_monitors()
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1925
// returns the non-busy monitors to the global free list.
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1926
// When a thread dies, omFlush() adds the list of active monitors for
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1927
// that thread to a global gOmInUseList acquiring the
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1928
// global list lock. deflate_idle_monitors() acquires the global
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1929
// list lock to scan for non-busy monitors to the global free list.
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1930
// An alternative could have used a single global inuse list. The
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1931
// downside would have been the additional cost of acquiring the global list lock
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1932
// for every omAlloc().
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1933
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1934
// Perversely, the heap size -- and thus the STW safepoint rate --
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1935
// typically drives the scavenge rate.  Large heaps can mean infrequent GC,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1936
// which in turn can mean large(r) numbers of objectmonitors in circulation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1937
// This is an unfortunate aspect of this design.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1938
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1939
// Another refinement would be to refrain from calling deflate_idle_monitors()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1940
// except at stop-the-world points associated with garbage collections.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1941
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1942
// An even better solution would be to deflate on-the-fly, aggressively,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1943
// at monitorexit-time as is done in EVM's metalock or Relaxed Locks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1944
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1945
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1946
// Deflate a single monitor if not in use
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1947
// Return true if deflated, false if in use
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1948
bool ObjectSynchronizer::deflate_monitor(ObjectMonitor* mid, oop obj,
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1949
                                         ObjectMonitor** FreeHeadp, ObjectMonitor** FreeTailp) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1950
  bool deflated;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1951
  // Normal case ... The monitor is associated with obj.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1952
  guarantee (obj->mark() == markOopDesc::encode(mid), "invariant") ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1953
  guarantee (mid == obj->mark()->monitor(), "invariant");
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1954
  guarantee (mid->header()->is_neutral(), "invariant");
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1955
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1956
  if (mid->is_busy()) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1957
     if (ClearResponsibleAtSTW) mid->_Responsible = NULL ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1958
     deflated = false;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1959
  } else {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1960
     // Deflate the monitor if it is no longer being used
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1961
     // It's idle - scavenge and return to the global free list
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1962
     // plain old deflation ...
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1963
     TEVENT (deflate_idle_monitors - scavenge1) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1964
     if (TraceMonitorInflation) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1965
       if (obj->is_instance()) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1966
         ResourceMark rm;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1967
           tty->print_cr("Deflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1968
                (intptr_t) obj, (intptr_t) obj->mark(), Klass::cast(obj->klass())->external_name());
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1969
       }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1970
     }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1971
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1972
     // Restore the header back to obj
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1973
     obj->release_set_mark(mid->header());
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1974
     mid->clear();
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1975
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1976
     assert (mid->object() == NULL, "invariant") ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1977
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1978
     // Move the object to the working free list defined by FreeHead,FreeTail.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1979
     if (*FreeHeadp == NULL) *FreeHeadp = mid;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1980
     if (*FreeTailp != NULL) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1981
       ObjectMonitor * prevtail = *FreeTailp;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1982
       assert(prevtail->FreeNext == NULL, "cleaned up deflated?"); // TODO KK
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1983
       prevtail->FreeNext = mid;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1984
      }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1985
     *FreeTailp = mid;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1986
     deflated = true;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1987
  }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1988
  return deflated;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1989
}
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1990
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1991
// Caller acquires ListLock
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1992
int ObjectSynchronizer::walk_monitor_list(ObjectMonitor** listheadp,
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1993
                                          ObjectMonitor** FreeHeadp, ObjectMonitor** FreeTailp) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1994
  ObjectMonitor* mid;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1995
  ObjectMonitor* next;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1996
  ObjectMonitor* curmidinuse = NULL;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1997
  int deflatedcount = 0;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1998
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1999
  for (mid = *listheadp; mid != NULL; ) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2000
     oop obj = (oop) mid->object();
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2001
     bool deflated = false;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2002
     if (obj != NULL) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2003
       deflated = deflate_monitor(mid, obj, FreeHeadp, FreeTailp);
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2004
     }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2005
     if (deflated) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2006
       // extract from per-thread in-use-list
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2007
       if (mid == *listheadp) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2008
         *listheadp = mid->FreeNext;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2009
       } else if (curmidinuse != NULL) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2010
         curmidinuse->FreeNext = mid->FreeNext; // maintain the current thread inuselist
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2011
       }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2012
       next = mid->FreeNext;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2013
       mid->FreeNext = NULL;  // This mid is current tail in the FreeHead list
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2014
       mid = next;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2015
       deflatedcount++;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2016
     } else {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2017
       curmidinuse = mid;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2018
       mid = mid->FreeNext;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2019
    }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2020
  }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2021
  return deflatedcount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2022
}
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2023
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2024
void ObjectSynchronizer::deflate_idle_monitors() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2025
  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2026
  int nInuse = 0 ;              // currently associated with objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2027
  int nInCirculation = 0 ;      // extant
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2028
  int nScavenged = 0 ;          // reclaimed
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2029
  bool deflated = false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2030
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2031
  ObjectMonitor * FreeHead = NULL ;  // Local SLL of scavenged monitors
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2032
  ObjectMonitor * FreeTail = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2033
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2034
  TEVENT (deflate_idle_monitors) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2035
  // Prevent omFlush from changing mids in Thread dtor's during deflation
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2036
  // And in case the vm thread is acquiring a lock during a safepoint
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2037
  // See e.g. 6320749
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2038
  Thread::muxAcquire (&ListLock, "scavenge - return") ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2039
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2040
  if (MonitorInUseLists) {
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2041
    int inUse = 0;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2042
    for (JavaThread* cur = Threads::first(); cur != NULL; cur = cur->next()) {
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2043
      nInCirculation+= cur->omInUseCount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2044
      int deflatedcount = walk_monitor_list(cur->omInUseList_addr(), &FreeHead, &FreeTail);
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2045
      cur->omInUseCount-= deflatedcount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2046
      // verifyInUse(cur);
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2047
      nScavenged += deflatedcount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2048
      nInuse += cur->omInUseCount;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2049
     }
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2050
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2051
   // For moribund threads, scan gOmInUseList
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2052
   if (gOmInUseList) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2053
     nInCirculation += gOmInUseCount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2054
     int deflatedcount = walk_monitor_list((ObjectMonitor **)&gOmInUseList, &FreeHead, &FreeTail);
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2055
     gOmInUseCount-= deflatedcount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2056
     nScavenged += deflatedcount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2057
     nInuse += gOmInUseCount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2058
    }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  2059
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2060
  } else for (ObjectMonitor* block = gBlockList; block != NULL; block = next(block)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2061
  // Iterate over all extant monitors - Scavenge all idle monitors.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2062
    assert(block->object() == CHAINMARKER, "must be a block header");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2063
    nInCirculation += _BLOCKSIZE ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2064
    for (int i = 1 ; i < _BLOCKSIZE; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2065
      ObjectMonitor* mid = &block[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2066
      oop obj = (oop) mid->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2067
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2068
      if (obj == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2069
        // The monitor is not associated with an object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2070
        // The monitor should either be a thread-specific private
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2071
        // free list or the global free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2072
        // obj == NULL IMPLIES mid->is_busy() == 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2073
        guarantee (!mid->is_busy(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2074
        continue ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2075
      }
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2076
      deflated = deflate_monitor(mid, obj, &FreeHead, &FreeTail);
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2077
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2078
      if (deflated) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2079
        mid->FreeNext = NULL ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2080
        nScavenged ++ ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2081
      } else {
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2082
        nInuse ++;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2083
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2084
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2085
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2086
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2087
  MonitorFreeCount += nScavenged;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2088
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2089
  // Consider: audit gFreeList to ensure that MonitorFreeCount and list agree.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2090
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2091
  if (Knob_Verbose) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2092
    ::printf ("Deflate: InCirc=%d InUse=%d Scavenged=%d ForceMonitorScavenge=%d : pop=%d free=%d\n",
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2093
        nInCirculation, nInuse, nScavenged, ForceMonitorScavenge,
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2094
        MonitorPopulation, MonitorFreeCount) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2095
    ::fflush(stdout) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2096
  }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2097
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2098
  ForceMonitorScavenge = 0;    // Reset
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2099
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2100
  // Move the scavenged monitors back to the global free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2101
  if (FreeHead != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2102
     guarantee (FreeTail != NULL && nScavenged > 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2103
     assert (FreeTail->FreeNext == NULL, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2104
     // constant-time list splice - prepend scavenged segment to gFreeList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2105
     FreeTail->FreeNext = gFreeList ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2106
     gFreeList = FreeHead ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2107
  }
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  2108
  Thread::muxRelease (&ListLock) ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2109
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2110
  if (_sync_Deflations != NULL) _sync_Deflations->inc(nScavenged) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2111
  if (_sync_MonExtant  != NULL) _sync_MonExtant ->set_value(nInCirculation);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2112
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2113
  // TODO: Add objectMonitor leak detection.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2114
  // Audit/inventory the objectMonitors -- make sure they're all accounted for.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2115
  GVars.stwRandom = os::random() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2116
  GVars.stwCycle ++ ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2117
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2118
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2119
// A macro is used below because there may already be a pending
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2120
// exception which should not abort the execution of the routines
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2121
// which use this (which is why we don't put this into check_slow and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2122
// call it with a CHECK argument).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2123
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2124
#define CHECK_OWNER()                                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2125
  do {                                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2126
    if (THREAD != _owner) {                                                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2127
      if (THREAD->is_lock_owned((address) _owner)) {                              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2128
        _owner = THREAD ;  /* Convert from basiclock addr to Thread addr */       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2129
        _recursions = 0;                                                          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2130
        OwnerIsThread = 1 ;                                                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2131
      } else {                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2132
        TEVENT (Throw IMSX) ;                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2133
        THROW(vmSymbols::java_lang_IllegalMonitorStateException());               \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2134
      }                                                                           \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2135
    }                                                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2136
  } while (false)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2137
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2138
// TODO-FIXME: eliminate ObjectWaiters.  Replace this visitor/enumerator
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2139
// interface with a simple FirstWaitingThread(), NextWaitingThread() interface.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2140
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2141
ObjectWaiter* ObjectMonitor::first_waiter() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2142
  return _WaitSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2143
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2144
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2145
ObjectWaiter* ObjectMonitor::next_waiter(ObjectWaiter* o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2146
  return o->_next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2147
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2148
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2149
Thread* ObjectMonitor::thread_of_waiter(ObjectWaiter* o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2150
  return o->_thread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2151
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2152
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2153
// initialize the monitor, exception the semaphore, all other fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2154
// are simple integers or pointers
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2155
ObjectMonitor::ObjectMonitor() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2156
  _header       = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2157
  _count        = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2158
  _waiters      = 0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2159
  _recursions   = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2160
  _object       = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2161
  _owner        = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2162
  _WaitSet      = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2163
  _WaitSetLock  = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2164
  _Responsible  = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2165
  _succ         = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2166
  _cxq          = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2167
  FreeNext      = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2168
  _EntryList    = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2169
  _SpinFreq     = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2170
  _SpinClock    = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2171
  OwnerIsThread = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2172
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2173
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2174
ObjectMonitor::~ObjectMonitor() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2175
   // TODO: Add asserts ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2176
   // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2177
   // _count == 0 _EntryList  == NULL etc
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2178
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2179
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2180
intptr_t ObjectMonitor::is_busy() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2181
  // TODO-FIXME: merge _count and _waiters.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2182
  // TODO-FIXME: assert _owner == null implies _recursions = 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2183
  // TODO-FIXME: assert _WaitSet != null implies _count > 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2184
  return _count|_waiters|intptr_t(_owner)|intptr_t(_cxq)|intptr_t(_EntryList ) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2185
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2186
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2187
void ObjectMonitor::Recycle () {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2188
  // TODO: add stronger asserts ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2189
  // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2190
  // _count == 0 EntryList  == NULL
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2191
  // _recursions == 0 _WaitSet == NULL
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2192
  // TODO: assert (is_busy()|_recursions) == 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2193
  _succ          = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2194
  _EntryList     = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2195
  _cxq           = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2196
  _WaitSet       = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2197
  _recursions    = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2198
  _SpinFreq      = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2199
  _SpinClock     = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2200
  OwnerIsThread  = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2201
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2202
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2203
// WaitSet management ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2204
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2205
inline void ObjectMonitor::AddWaiter(ObjectWaiter* node) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2206
  assert(node != NULL, "should not dequeue NULL node");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2207
  assert(node->_prev == NULL, "node already in list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2208
  assert(node->_next == NULL, "node already in list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2209
  // put node at end of queue (circular doubly linked list)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2210
  if (_WaitSet == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2211
    _WaitSet = node;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2212
    node->_prev = node;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2213
    node->_next = node;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2214
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2215
    ObjectWaiter* head = _WaitSet ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2216
    ObjectWaiter* tail = head->_prev;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2217
    assert(tail->_next == head, "invariant check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2218
    tail->_next = node;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2219
    head->_prev = node;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2220
    node->_next = head;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2221
    node->_prev = tail;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2222
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2223
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2224
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2225
inline ObjectWaiter* ObjectMonitor::DequeueWaiter() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2226
  // dequeue the very first waiter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2227
  ObjectWaiter* waiter = _WaitSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2228
  if (waiter) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2229
    DequeueSpecificWaiter(waiter);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2230
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2231
  return waiter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2232
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2233
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2234
inline void ObjectMonitor::DequeueSpecificWaiter(ObjectWaiter* node) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2235
  assert(node != NULL, "should not dequeue NULL node");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2236
  assert(node->_prev != NULL, "node already removed from list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2237
  assert(node->_next != NULL, "node already removed from list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2238
  // when the waiter has woken up because of interrupt,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2239
  // timeout or other spurious wake-up, dequeue the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2240
  // waiter from waiting list
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2241
  ObjectWaiter* next = node->_next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2242
  if (next == node) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2243
    assert(node->_prev == node, "invariant check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2244
    _WaitSet = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2245
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2246
    ObjectWaiter* prev = node->_prev;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2247
    assert(prev->_next == node, "invariant check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2248
    assert(next->_prev == node, "invariant check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2249
    next->_prev = prev;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2250
    prev->_next = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2251
    if (_WaitSet == node) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2252
      _WaitSet = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2253
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2254
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2255
  node->_next = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2256
  node->_prev = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2257
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2258
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2259
static char * kvGet (char * kvList, const char * Key) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2260
    if (kvList == NULL) return NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2261
    size_t n = strlen (Key) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2262
    char * Search ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2263
    for (Search = kvList ; *Search ; Search += strlen(Search) + 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2264
        if (strncmp (Search, Key, n) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2265
            if (Search[n] == '=') return Search + n + 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2266
            if (Search[n] == 0)   return (char *) "1" ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2267
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2268
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2269
    return NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2270
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2271
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2272
static int kvGetInt (char * kvList, const char * Key, int Default) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2273
    char * v = kvGet (kvList, Key) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2274
    int rslt = v ? ::strtol (v, NULL, 0) : Default ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2275
    if (Knob_ReportSettings && v != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2276
        ::printf ("  SyncKnob: %s %d(%d)\n", Key, rslt, Default) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2277
        ::fflush (stdout) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2278
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2279
    return rslt ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2280
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2281
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2282
// By convention we unlink a contending thread from EntryList|cxq immediately
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2283
// after the thread acquires the lock in ::enter().  Equally, we could defer
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2284
// unlinking the thread until ::exit()-time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2285
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2286
void ObjectMonitor::UnlinkAfterAcquire (Thread * Self, ObjectWaiter * SelfNode)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2287
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2288
    assert (_owner == Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2289
    assert (SelfNode->_thread == Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2290
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2291
    if (SelfNode->TState == ObjectWaiter::TS_ENTER) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2292
        // Normal case: remove Self from the DLL EntryList .
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2293
        // This is a constant-time operation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2294
        ObjectWaiter * nxt = SelfNode->_next ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2295
        ObjectWaiter * prv = SelfNode->_prev ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2296
        if (nxt != NULL) nxt->_prev = prv ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2297
        if (prv != NULL) prv->_next = nxt ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2298
        if (SelfNode == _EntryList ) _EntryList = nxt ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2299
        assert (nxt == NULL || nxt->TState == ObjectWaiter::TS_ENTER, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2300
        assert (prv == NULL || prv->TState == ObjectWaiter::TS_ENTER, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2301
        TEVENT (Unlink from EntryList) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2302
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2303
        guarantee (SelfNode->TState == ObjectWaiter::TS_CXQ, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2304
        // Inopportune interleaving -- Self is still on the cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2305
        // This usually means the enqueue of self raced an exiting thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2306
        // Normally we'll find Self near the front of the cxq, so
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2307
        // dequeueing is typically fast.  If needbe we can accelerate
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2308
        // this with some MCS/CHL-like bidirectional list hints and advisory
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2309
        // back-links so dequeueing from the interior will normally operate
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2310
        // in constant-time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2311
        // Dequeue Self from either the head (with CAS) or from the interior
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2312
        // with a linear-time scan and normal non-atomic memory operations.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2313
        // CONSIDER: if Self is on the cxq then simply drain cxq into EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2314
        // and then unlink Self from EntryList.  We have to drain eventually,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2315
        // so it might as well be now.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2316
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2317
        ObjectWaiter * v = _cxq ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2318
        assert (v != NULL, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2319
        if (v != SelfNode || Atomic::cmpxchg_ptr (SelfNode->_next, &_cxq, v) != v) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2320
            // The CAS above can fail from interference IFF a "RAT" arrived.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2321
            // In that case Self must be in the interior and can no longer be
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2322
            // at the head of cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2323
            if (v == SelfNode) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2324
                assert (_cxq != v, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2325
                v = _cxq ;          // CAS above failed - start scan at head of list
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2326
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2327
            ObjectWaiter * p ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2328
            ObjectWaiter * q = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2329
            for (p = v ; p != NULL && p != SelfNode; p = p->_next) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2330
                q = p ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2331
                assert (p->TState == ObjectWaiter::TS_CXQ, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2332
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2333
            assert (v != SelfNode,  "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2334
            assert (p == SelfNode,  "Node not found on cxq") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2335
            assert (p != _cxq,      "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2336
            assert (q != NULL,      "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2337
            assert (q->_next == p,  "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2338
            q->_next = p->_next ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2339
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2340
        TEVENT (Unlink from cxq) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2341
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2342
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2343
    // Diagnostic hygiene ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2344
    SelfNode->_prev  = (ObjectWaiter *) 0xBAD ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2345
    SelfNode->_next  = (ObjectWaiter *) 0xBAD ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2346
    SelfNode->TState = ObjectWaiter::TS_RUN ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2347
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2348
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2349
// Caveat: TryLock() is not necessarily serializing if it returns failure.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2350
// Callers must compensate as needed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2351
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2352
int ObjectMonitor::TryLock (Thread * Self) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2353
   for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2354
      void * own = _owner ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2355
      if (own != NULL) return 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2356
      if (Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2357
         // Either guarantee _recursions == 0 or set _recursions = 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2358
         assert (_recursions == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2359
         assert (_owner == Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2360
         // CONSIDER: set or assert that OwnerIsThread == 1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2361
         return 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2362
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2363
      // The lock had been free momentarily, but we lost the race to the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2364
      // Interference -- the CAS failed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2365
      // We can either return -1 or retry.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2366
      // Retry doesn't make as much sense because the lock was just acquired.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2367
      if (true) return -1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2368
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2369
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2370
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2371
// NotRunnable() -- informed spinning
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2372
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2373
// Don't bother spinning if the owner is not eligible to drop the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2374
// Peek at the owner's schedctl.sc_state and Thread._thread_values and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2375
// spin only if the owner thread is _thread_in_Java or _thread_in_vm.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2376
// The thread must be runnable in order to drop the lock in timely fashion.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2377
// If the _owner is not runnable then spinning will not likely be
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2378
// successful (profitable).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2379
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2380
// Beware -- the thread referenced by _owner could have died
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2381
// so a simply fetch from _owner->_thread_state might trap.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2382
// Instead, we use SafeFetchXX() to safely LD _owner->_thread_state.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2383
// Because of the lifecycle issues the schedctl and _thread_state values
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2384
// observed by NotRunnable() might be garbage.  NotRunnable must
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2385
// tolerate this and consider the observed _thread_state value
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2386
// as advisory.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2387
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2388
// Beware too, that _owner is sometimes a BasicLock address and sometimes
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2389
// a thread pointer.  We differentiate the two cases with OwnerIsThread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2390
// Alternately, we might tag the type (thread pointer vs basiclock pointer)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2391
// with the LSB of _owner.  Another option would be to probablistically probe
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2392
// the putative _owner->TypeTag value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2393
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2394
// Checking _thread_state isn't perfect.  Even if the thread is
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2395
// in_java it might be blocked on a page-fault or have been preempted
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2396
// and sitting on a ready/dispatch queue.  _thread state in conjunction
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2397
// with schedctl.sc_state gives us a good picture of what the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2398
// thread is doing, however.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2399
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2400
// TODO: check schedctl.sc_state.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2401
// We'll need to use SafeFetch32() to read from the schedctl block.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2402
// See RFE #5004247 and http://sac.sfbay.sun.com/Archives/CaseLog/arc/PSARC/2005/351/
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2403
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2404
// The return value from NotRunnable() is *advisory* -- the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2405
// result is based on sampling and is not necessarily coherent.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2406
// The caller must tolerate false-negative and false-positive errors.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2407
// Spinning, in general, is probabilistic anyway.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2408
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2409
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2410
int ObjectMonitor::NotRunnable (Thread * Self, Thread * ox) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2411
    // Check either OwnerIsThread or ox->TypeTag == 2BAD.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2412
    if (!OwnerIsThread) return 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2413
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2414
    if (ox == NULL) return 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2415
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2416
    // Avoid transitive spinning ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2417
    // Say T1 spins or blocks trying to acquire L.  T1._Stalled is set to L.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2418
    // Immediately after T1 acquires L it's possible that T2, also
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2419
    // spinning on L, will see L.Owner=T1 and T1._Stalled=L.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2420
    // This occurs transiently after T1 acquired L but before
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2421
    // T1 managed to clear T1.Stalled.  T2 does not need to abort
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2422
    // its spin in this circumstance.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2423
    intptr_t BlockedOn = SafeFetchN ((intptr_t *) &ox->_Stalled, intptr_t(1)) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2424
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2425
    if (BlockedOn == 1) return 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2426
    if (BlockedOn != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2427
      return BlockedOn != intptr_t(this) && _owner == ox ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2428
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2429
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2430
    assert (sizeof(((JavaThread *)ox)->_thread_state == sizeof(int)), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2431
    int jst = SafeFetch32 ((int *) &((JavaThread *) ox)->_thread_state, -1) ; ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2432
    // consider also: jst != _thread_in_Java -- but that's overspecific.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2433
    return jst == _thread_blocked || jst == _thread_in_native ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2434
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2435
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2436
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2437
// Adaptive spin-then-block - rational spinning
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2438
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2439
// Note that we spin "globally" on _owner with a classic SMP-polite TATAS
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2440
// algorithm.  On high order SMP systems it would be better to start with
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2441
// a brief global spin and then revert to spinning locally.  In the spirit of MCS/CLH,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2442
// a contending thread could enqueue itself on the cxq and then spin locally
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2443
// on a thread-specific variable such as its ParkEvent._Event flag.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2444
// That's left as an exercise for the reader.  Note that global spinning is
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2445
// not problematic on Niagara, as the L2$ serves the interconnect and has both
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2446
// low latency and massive bandwidth.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2447
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2448
// Broadly, we can fix the spin frequency -- that is, the % of contended lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2449
// acquisition attempts where we opt to spin --  at 100% and vary the spin count
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2450
// (duration) or we can fix the count at approximately the duration of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2451
// a context switch and vary the frequency.   Of course we could also
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2452
// vary both satisfying K == Frequency * Duration, where K is adaptive by monitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2453
// See http://j2se.east/~dice/PERSIST/040824-AdaptiveSpinning.html.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2454
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2455
// This implementation varies the duration "D", where D varies with
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2456
// the success rate of recent spin attempts. (D is capped at approximately
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2457
// length of a round-trip context switch).  The success rate for recent
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2458
// spin attempts is a good predictor of the success rate of future spin
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2459
// attempts.  The mechanism adapts automatically to varying critical
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2460
// section length (lock modality), system load and degree of parallelism.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2461
// D is maintained per-monitor in _SpinDuration and is initialized
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2462
// optimistically.  Spin frequency is fixed at 100%.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2463
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2464
// Note that _SpinDuration is volatile, but we update it without locks
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2465
// or atomics.  The code is designed so that _SpinDuration stays within
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2466
// a reasonable range even in the presence of races.  The arithmetic
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2467
// operations on _SpinDuration are closed over the domain of legal values,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2468
// so at worst a race will install and older but still legal value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2469
// At the very worst this introduces some apparent non-determinism.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2470
// We might spin when we shouldn't or vice-versa, but since the spin
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2471
// count are relatively short, even in the worst case, the effect is harmless.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2472
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2473
// Care must be taken that a low "D" value does not become an
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2474
// an absorbing state.  Transient spinning failures -- when spinning
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2475
// is overall profitable -- should not cause the system to converge
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2476
// on low "D" values.  We want spinning to be stable and predictable
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2477
// and fairly responsive to change and at the same time we don't want
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2478
// it to oscillate, become metastable, be "too" non-deterministic,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2479
// or converge on or enter undesirable stable absorbing states.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2480
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2481
// We implement a feedback-based control system -- using past behavior
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2482
// to predict future behavior.  We face two issues: (a) if the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2483
// input signal is random then the spin predictor won't provide optimal
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2484
// results, and (b) if the signal frequency is too high then the control
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2485
// system, which has some natural response lag, will "chase" the signal.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2486
// (b) can arise from multimodal lock hold times.  Transient preemption
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2487
// can also result in apparent bimodal lock hold times.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2488
// Although sub-optimal, neither condition is particularly harmful, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2489
// in the worst-case we'll spin when we shouldn't or vice-versa.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2490
// The maximum spin duration is rather short so the failure modes aren't bad.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2491
// To be conservative, I've tuned the gain in system to bias toward
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2492
// _not spinning.  Relatedly, the system can sometimes enter a mode where it
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2493
// "rings" or oscillates between spinning and not spinning.  This happens
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2494
// when spinning is just on the cusp of profitability, however, so the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2495
// situation is not dire.  The state is benign -- there's no need to add
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2496
// hysteresis control to damp the transition rate between spinning and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2497
// not spinning.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2498
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2499
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2500
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2501
// Spin-then-block strategies ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2502
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2503
// Thoughts on ways to improve spinning :
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2504
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2505
// *  Periodically call {psr_}getloadavg() while spinning, and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2506
//    permit unbounded spinning if the load average is <
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2507
//    the number of processors.  Beware, however, that getloadavg()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2508
//    is exceptionally fast on solaris (about 1/10 the cost of a full
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2509
//    spin cycle, but quite expensive on linux.  Beware also, that
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2510
//    multiple JVMs could "ring" or oscillate in a feedback loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2511
//    Sufficient damping would solve that problem.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2512
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2513
// *  We currently use spin loops with iteration counters to approximate
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2514
//    spinning for some interval.  Given the availability of high-precision
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2515
//    time sources such as gethrtime(), %TICK, %STICK, RDTSC, etc., we should
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2516
//    someday reimplement the spin loops to duration-based instead of iteration-based.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2517
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2518
// *  Don't spin if there are more than N = (CPUs/2) threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2519
//        currently spinning on the monitor (or globally).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2520
//    That is, limit the number of concurrent spinners.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2521
//    We might also limit the # of spinners in the JVM, globally.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2522
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2523
// *  If a spinning thread observes _owner change hands it should
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2524
//    abort the spin (and park immediately) or at least debit
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2525
//    the spin counter by a large "penalty".
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2526
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2527
// *  Classically, the spin count is either K*(CPUs-1) or is a
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2528
//        simple constant that approximates the length of a context switch.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2529
//    We currently use a value -- computed by a special utility -- that
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2530
//    approximates round-trip context switch times.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2531
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2532
// *  Normally schedctl_start()/_stop() is used to advise the kernel
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2533
//    to avoid preempting threads that are running in short, bounded
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2534
//    critical sections.  We could use the schedctl hooks in an inverted
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2535
//    sense -- spinners would set the nopreempt flag, but poll the preempt
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2536
//    pending flag.  If a spinner observed a pending preemption it'd immediately
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2537
//    abort the spin and park.   As such, the schedctl service acts as
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2538
//    a preemption warning mechanism.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2539
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2540
// *  In lieu of spinning, if the system is running below saturation
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2541
//    (that is, loadavg() << #cpus), we can instead suppress futile
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2542
//    wakeup throttling, or even wake more than one successor at exit-time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2543
//    The net effect is largely equivalent to spinning.  In both cases,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2544
//    contending threads go ONPROC and opportunistically attempt to acquire
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2545
//    the lock, decreasing lock handover latency at the expense of wasted
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2546
//    cycles and context switching.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2547
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2548
// *  We might to spin less after we've parked as the thread will
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2549
//    have less $ and TLB affinity with the processor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2550
//    Likewise, we might spin less if we come ONPROC on a different
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2551
//    processor or after a long period (>> rechose_interval).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2552
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2553
// *  A table-driven state machine similar to Solaris' dispadmin scheduling
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2554
//    tables might be a better design.  Instead of encoding information in
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2555
//    _SpinDuration, _SpinFreq and _SpinClock we'd just use explicit,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2556
//    discrete states.   Success or failure during a spin would drive
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2557
//    state transitions, and each state node would contain a spin count.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2558
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2559
// *  If the processor is operating in a mode intended to conserve power
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2560
//    (such as Intel's SpeedStep) or to reduce thermal output (thermal
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2561
//    step-down mode) then the Java synchronization subsystem should
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2562
//    forgo spinning.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2563
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2564
// *  The minimum spin duration should be approximately the worst-case
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2565
//    store propagation latency on the platform.  That is, the time
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2566
//    it takes a store on CPU A to become visible on CPU B, where A and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2567
//    B are "distant".
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2568
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2569
// *  We might want to factor a thread's priority in the spin policy.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2570
//    Threads with a higher priority might spin for slightly longer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2571
//    Similarly, if we use back-off in the TATAS loop, lower priority
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2572
//    threads might back-off longer.  We don't currently use a
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2573
//    thread's priority when placing it on the entry queue.  We may
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2574
//    want to consider doing so in future releases.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2575
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2576
// *  We might transiently drop a thread's scheduling priority while it spins.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2577
//    SCHED_BATCH on linux and FX scheduling class at priority=0 on Solaris
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2578
//    would suffice.  We could even consider letting the thread spin indefinitely at
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2579
//    a depressed or "idle" priority.  This brings up fairness issues, however --
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2580
//    in a saturated system a thread would with a reduced priority could languish
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2581
//    for extended periods on the ready queue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2582
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2583
// *  While spinning try to use the otherwise wasted time to help the VM make
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2584
//    progress:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2585
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2586
//    -- YieldTo() the owner, if the owner is OFFPROC but ready
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2587
//       Done our remaining quantum directly to the ready thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2588
//       This helps "push" the lock owner through the critical section.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2589
//       It also tends to improve affinity/locality as the lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2590
//       "migrates" less frequently between CPUs.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2591
//    -- Walk our own stack in anticipation of blocking.  Memoize the roots.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2592
//    -- Perform strand checking for other thread.  Unpark potential strandees.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2593
//    -- Help GC: trace or mark -- this would need to be a bounded unit of work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2594
//       Unfortunately this will pollute our $ and TLBs.  Recall that we
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2595
//       spin to avoid context switching -- context switching has an
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2596
//       immediate cost in latency, a disruptive cost to other strands on a CMT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2597
//       processor, and an amortized cost because of the D$ and TLB cache
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2598
//       reload transient when the thread comes back ONPROC and repopulates
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2599
//       $s and TLBs.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2600
//    -- call getloadavg() to see if the system is saturated.  It'd probably
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2601
//       make sense to call getloadavg() half way through the spin.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2602
//       If the system isn't at full capacity the we'd simply reset
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2603
//       the spin counter to and extend the spin attempt.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2604
//    -- Doug points out that we should use the same "helping" policy
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2605
//       in thread.yield().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2606
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2607
// *  Try MONITOR-MWAIT on systems that support those instructions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2608
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2609
// *  The spin statistics that drive spin decisions & frequency are
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2610
//    maintained in the objectmonitor structure so if we deflate and reinflate
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2611
//    we lose spin state.  In practice this is not usually a concern
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2612
//    as the default spin state after inflation is aggressive (optimistic)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2613
//    and tends toward spinning.  So in the worst case for a lock where
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2614
//    spinning is not profitable we may spin unnecessarily for a brief
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2615
//    period.  But then again, if a lock is contended it'll tend not to deflate
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2616
//    in the first place.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2617
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2618
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2619
intptr_t ObjectMonitor::SpinCallbackArgument = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2620
int (*ObjectMonitor::SpinCallbackFunction)(intptr_t, int) = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2621
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2622
// Spinning: Fixed frequency (100%), vary duration
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2623
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2624
int ObjectMonitor::TrySpin_VaryDuration (Thread * Self) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2625
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2626
    // Dumb, brutal spin.  Good for comparative measurements against adaptive spinning.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2627
    int ctr = Knob_FixedSpin ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2628
    if (ctr != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2629
        while (--ctr >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2630
            if (TryLock (Self) > 0) return 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2631
            SpinPause () ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2632
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2633
        return 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2634
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2635
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2636
    for (ctr = Knob_PreSpin + 1; --ctr >= 0 ; ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2637
      if (TryLock(Self) > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2638
        // Increase _SpinDuration ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2639
        // Note that we don't clamp SpinDuration precisely at SpinLimit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2640
        // Raising _SpurDuration to the poverty line is key.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2641
        int x = _SpinDuration ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2642
        if (x < Knob_SpinLimit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2643
           if (x < Knob_Poverty) x = Knob_Poverty ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2644
           _SpinDuration = x + Knob_BonusB ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2645
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2646
        return 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2647
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2648
      SpinPause () ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2649
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2650
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2651
    // Admission control - verify preconditions for spinning
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2652
    //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2653
    // We always spin a little bit, just to prevent _SpinDuration == 0 from
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2654
    // becoming an absorbing state.  Put another way, we spin briefly to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2655
    // sample, just in case the system load, parallelism, contention, or lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2656
    // modality changed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2657
    //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2658
    // Consider the following alternative:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2659
    // Periodically set _SpinDuration = _SpinLimit and try a long/full
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2660
    // spin attempt.  "Periodically" might mean after a tally of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2661
    // the # of failed spin attempts (or iterations) reaches some threshold.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2662
    // This takes us into the realm of 1-out-of-N spinning, where we
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2663
    // hold the duration constant but vary the frequency.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2664
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2665
    ctr = _SpinDuration  ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2666
    if (ctr < Knob_SpinBase) ctr = Knob_SpinBase ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2667
    if (ctr <= 0) return 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2668
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2669
    if (Knob_SuccRestrict && _succ != NULL) return 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2670
    if (Knob_OState && NotRunnable (Self, (Thread *) _owner)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2671
       TEVENT (Spin abort - notrunnable [TOP]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2672
       return 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2673
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2674
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2675
    int MaxSpin = Knob_MaxSpinners ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2676
    if (MaxSpin >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2677
       if (_Spinner > MaxSpin) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2678
          TEVENT (Spin abort -- too many spinners) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2679
          return 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2680
       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2681
       // Slighty racy, but benign ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2682
       Adjust (&_Spinner, 1) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2683
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2684
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2685
    // We're good to spin ... spin ingress.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2686
    // CONSIDER: use Prefetch::write() to avoid RTS->RTO upgrades
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2687
    // when preparing to LD...CAS _owner, etc and the CAS is likely
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2688
    // to succeed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2689
    int hits    = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2690
    int msk     = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2691
    int caspty  = Knob_CASPenalty ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2692
    int oxpty   = Knob_OXPenalty ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2693
    int sss     = Knob_SpinSetSucc ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2694
    if (sss && _succ == NULL ) _succ = Self ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2695
    Thread * prv = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2696
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2697
    // There are three ways to exit the following loop:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2698
    // 1.  A successful spin where this thread has acquired the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2699
    // 2.  Spin failure with prejudice
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2700
    // 3.  Spin failure without prejudice
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2701
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2702
    while (--ctr >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2703
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2704
      // Periodic polling -- Check for pending GC
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2705
      // Threads may spin while they're unsafe.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2706
      // We don't want spinning threads to delay the JVM from reaching
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2707
      // a stop-the-world safepoint or to steal cycles from GC.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2708
      // If we detect a pending safepoint we abort in order that
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2709
      // (a) this thread, if unsafe, doesn't delay the safepoint, and (b)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2710
      // this thread, if safe, doesn't steal cycles from GC.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2711
      // This is in keeping with the "no loitering in runtime" rule.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2712
      // We periodically check to see if there's a safepoint pending.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2713
      if ((ctr & 0xFF) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2714
         if (SafepointSynchronize::do_call_back()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2715
            TEVENT (Spin: safepoint) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2716
            goto Abort ;           // abrupt spin egress
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2717
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2718
         if (Knob_UsePause & 1) SpinPause () ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2719
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2720
         int (*scb)(intptr_t,int) = SpinCallbackFunction ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2721
         if (hits > 50 && scb != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2722
            int abend = (*scb)(SpinCallbackArgument, 0) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2723
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2724
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2725
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2726
      if (Knob_UsePause & 2) SpinPause() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2727
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2728
      // Exponential back-off ...  Stay off the bus to reduce coherency traffic.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2729
      // This is useful on classic SMP systems, but is of less utility on
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2730
      // N1-style CMT platforms.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2731
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2732
      // Trade-off: lock acquisition latency vs coherency bandwidth.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2733
      // Lock hold times are typically short.  A histogram
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2734
      // of successful spin attempts shows that we usually acquire
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2735
      // the lock early in the spin.  That suggests we want to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2736
      // sample _owner frequently in the early phase of the spin,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2737
      // but then back-off and sample less frequently as the spin
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2738
      // progresses.  The back-off makes a good citizen on SMP big
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2739
      // SMP systems.  Oversampling _owner can consume excessive
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2740
      // coherency bandwidth.  Relatedly, if we _oversample _owner we
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2741
      // can inadvertently interfere with the the ST m->owner=null.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2742
      // executed by the lock owner.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2743
      if (ctr & msk) continue ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2744
      ++hits ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2745
      if ((hits & 0xF) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2746
        // The 0xF, above, corresponds to the exponent.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2747
        // Consider: (msk+1)|msk
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2748
        msk = ((msk << 2)|3) & BackOffMask ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2749
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2750
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2751
      // Probe _owner with TATAS
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2752
      // If this thread observes the monitor transition or flicker
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2753
      // from locked to unlocked to locked, then the odds that this
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2754
      // thread will acquire the lock in this spin attempt go down
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2755
      // considerably.  The same argument applies if the CAS fails
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2756
      // or if we observe _owner change from one non-null value to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2757
      // another non-null value.   In such cases we might abort
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2758
      // the spin without prejudice or apply a "penalty" to the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2759
      // spin count-down variable "ctr", reducing it by 100, say.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2760
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2761
      Thread * ox = (Thread *) _owner ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2762
      if (ox == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2763
         ox = (Thread *) Atomic::cmpxchg_ptr (Self, &_owner, NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2764
         if (ox == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2765
            // The CAS succeeded -- this thread acquired ownership
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2766
            // Take care of some bookkeeping to exit spin state.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2767
            if (sss && _succ == Self) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2768
               _succ = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2769
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2770
            if (MaxSpin > 0) Adjust (&_Spinner, -1) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2771
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2772
            // Increase _SpinDuration :
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2773
            // The spin was successful (profitable) so we tend toward
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2774
            // longer spin attempts in the future.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2775
            // CONSIDER: factor "ctr" into the _SpinDuration adjustment.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2776
            // If we acquired the lock early in the spin cycle it
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2777
            // makes sense to increase _SpinDuration proportionally.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2778
            // Note that we don't clamp SpinDuration precisely at SpinLimit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2779
            int x = _SpinDuration ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2780
            if (x < Knob_SpinLimit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2781
                if (x < Knob_Poverty) x = Knob_Poverty ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2782
                _SpinDuration = x + Knob_Bonus ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2783
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2784
            return 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2785
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2786
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2787
         // The CAS failed ... we can take any of the following actions:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2788
         // * penalize: ctr -= Knob_CASPenalty
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2789
         // * exit spin with prejudice -- goto Abort;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2790
         // * exit spin without prejudice.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2791
         // * Since CAS is high-latency, retry again immediately.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2792
         prv = ox ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2793
         TEVENT (Spin: cas failed) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2794
         if (caspty == -2) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2795
         if (caspty == -1) goto Abort ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2796
         ctr -= caspty ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2797
         continue ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2798
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2799
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2800
      // Did lock ownership change hands ?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2801
      if (ox != prv && prv != NULL ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2802
          TEVENT (spin: Owner changed)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2803
          if (oxpty == -2) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2804
          if (oxpty == -1) goto Abort ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2805
          ctr -= oxpty ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2806
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2807
      prv = ox ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2808
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2809
      // Abort the spin if the owner is not executing.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2810
      // The owner must be executing in order to drop the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2811
      // Spinning while the owner is OFFPROC is idiocy.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2812
      // Consider: ctr -= RunnablePenalty ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2813
      if (Knob_OState && NotRunnable (Self, ox)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2814
         TEVENT (Spin abort - notrunnable);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2815
         goto Abort ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2816
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2817
      if (sss && _succ == NULL ) _succ = Self ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2818
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2819
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2820
   // Spin failed with prejudice -- reduce _SpinDuration.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2821
   // TODO: Use an AIMD-like policy to adjust _SpinDuration.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2822
   // AIMD is globally stable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2823
   TEVENT (Spin failure) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2824
   {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2825
     int x = _SpinDuration ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2826
     if (x > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2827
        // Consider an AIMD scheme like: x -= (x >> 3) + 100
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2828
        // This is globally sample and tends to damp the response.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2829
        x -= Knob_Penalty ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2830
        if (x < 0) x = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2831
        _SpinDuration = x ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2832
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2833
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2834
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2835
 Abort:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2836
   if (MaxSpin >= 0) Adjust (&_Spinner, -1) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2837
   if (sss && _succ == Self) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2838
      _succ = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2839
      // Invariant: after setting succ=null a contending thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2840
      // must recheck-retry _owner before parking.  This usually happens
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2841
      // in the normal usage of TrySpin(), but it's safest
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2842
      // to make TrySpin() as foolproof as possible.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2843
      OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2844
      if (TryLock(Self) > 0) return 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2845
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2846
   return 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2847
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2848
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2849
#define TrySpin TrySpin_VaryDuration
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2850
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2851
static void DeferredInitialize () {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2852
  if (InitDone > 0) return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2853
  if (Atomic::cmpxchg (-1, &InitDone, 0) != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2854
      while (InitDone != 1) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2855
      return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2856
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2857
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2858
  // One-shot global initialization ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2859
  // The initialization is idempotent, so we don't need locks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2860
  // In the future consider doing this via os::init_2().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2861
  // SyncKnobs consist of <Key>=<Value> pairs in the style
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2862
  // of environment variables.  Start by converting ':' to NUL.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2863
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2864
  if (SyncKnobs == NULL) SyncKnobs = "" ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2865
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2866
  size_t sz = strlen (SyncKnobs) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2867
  char * knobs = (char *) malloc (sz + 2) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2868
  if (knobs == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2869
     vm_exit_out_of_memory (sz + 2, "Parse SyncKnobs") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2870
     guarantee (0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2871
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2872
  strcpy (knobs, SyncKnobs) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2873
  knobs[sz+1] = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2874
  for (char * p = knobs ; *p ; p++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2875
     if (*p == ':') *p = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2876
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2877
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2878
  #define SETKNOB(x) { Knob_##x = kvGetInt (knobs, #x, Knob_##x); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2879
  SETKNOB(ReportSettings) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2880
  SETKNOB(Verbose) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2881
  SETKNOB(FixedSpin) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2882
  SETKNOB(SpinLimit) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2883
  SETKNOB(SpinBase) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2884
  SETKNOB(SpinBackOff);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2885
  SETKNOB(CASPenalty) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2886
  SETKNOB(OXPenalty) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2887
  SETKNOB(LogSpins) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2888
  SETKNOB(SpinSetSucc) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2889
  SETKNOB(SuccEnabled) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2890
  SETKNOB(SuccRestrict) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2891
  SETKNOB(Penalty) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2892
  SETKNOB(Bonus) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2893
  SETKNOB(BonusB) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2894
  SETKNOB(Poverty) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2895
  SETKNOB(SpinAfterFutile) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2896
  SETKNOB(UsePause) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2897
  SETKNOB(SpinEarly) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2898
  SETKNOB(OState) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2899
  SETKNOB(MaxSpinners) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2900
  SETKNOB(PreSpin) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2901
  SETKNOB(ExitPolicy) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2902
  SETKNOB(QMode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2903
  SETKNOB(ResetEvent) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2904
  SETKNOB(MoveNotifyee) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2905
  SETKNOB(FastHSSEC) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2906
  #undef SETKNOB
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2907
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2908
  if (os::is_MP()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2909
     BackOffMask = (1 << Knob_SpinBackOff) - 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2910
     if (Knob_ReportSettings) ::printf ("BackOffMask=%X\n", BackOffMask) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2911
     // CONSIDER: BackOffMask = ROUNDUP_NEXT_POWER2 (ncpus-1)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2912
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2913
     Knob_SpinLimit = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2914
     Knob_SpinBase  = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2915
     Knob_PreSpin   = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2916
     Knob_FixedSpin = -1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2917
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2918
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2919
  if (Knob_LogSpins == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2920
     ObjectSynchronizer::_sync_FailedSpins = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2921
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2922
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2923
  free (knobs) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2924
  OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2925
  InitDone = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2926
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2927
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2928
// Theory of operations -- Monitors lists, thread residency, etc:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2929
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2930
// * A thread acquires ownership of a monitor by successfully
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2931
//   CAS()ing the _owner field from null to non-null.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2932
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2933
// * Invariant: A thread appears on at most one monitor list --
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2934
//   cxq, EntryList or WaitSet -- at any one time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2935
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2936
// * Contending threads "push" themselves onto the cxq with CAS
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2937
//   and then spin/park.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2938
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2939
// * After a contending thread eventually acquires the lock it must
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2940
//   dequeue itself from either the EntryList or the cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2941
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2942
// * The exiting thread identifies and unparks an "heir presumptive"
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2943
//   tentative successor thread on the EntryList.  Critically, the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2944
//   exiting thread doesn't unlink the successor thread from the EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2945
//   After having been unparked, the wakee will recontend for ownership of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2946
//   the monitor.   The successor (wakee) will either acquire the lock or
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2947
//   re-park itself.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2948
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2949
//   Succession is provided for by a policy of competitive handoff.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2950
//   The exiting thread does _not_ grant or pass ownership to the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2951
//   successor thread.  (This is also referred to as "handoff" succession").
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2952
//   Instead the exiting thread releases ownership and possibly wakes
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2953
//   a successor, so the successor can (re)compete for ownership of the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2954
//   If the EntryList is empty but the cxq is populated the exiting
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2955
//   thread will drain the cxq into the EntryList.  It does so by
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2956
//   by detaching the cxq (installing null with CAS) and folding
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2957
//   the threads from the cxq into the EntryList.  The EntryList is
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2958
//   doubly linked, while the cxq is singly linked because of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2959
//   CAS-based "push" used to enqueue recently arrived threads (RATs).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2960
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2961
// * Concurrency invariants:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2962
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2963
//   -- only the monitor owner may access or mutate the EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2964
//      The mutex property of the monitor itself protects the EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2965
//      from concurrent interference.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2966
//   -- Only the monitor owner may detach the cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2967
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2968
// * The monitor entry list operations avoid locks, but strictly speaking
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2969
//   they're not lock-free.  Enter is lock-free, exit is not.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2970
//   See http://j2se.east/~dice/PERSIST/040825-LockFreeQueues.html
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2971
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2972
// * The cxq can have multiple concurrent "pushers" but only one concurrent
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2973
//   detaching thread.  This mechanism is immune from the ABA corruption.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2974
//   More precisely, the CAS-based "push" onto cxq is ABA-oblivious.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2975
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2976
// * Taken together, the cxq and the EntryList constitute or form a
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2977
//   single logical queue of threads stalled trying to acquire the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2978
//   We use two distinct lists to improve the odds of a constant-time
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2979
//   dequeue operation after acquisition (in the ::enter() epilog) and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2980
//   to reduce heat on the list ends.  (c.f. Michael Scott's "2Q" algorithm).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2981
//   A key desideratum is to minimize queue & monitor metadata manipulation
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2982
//   that occurs while holding the monitor lock -- that is, we want to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2983
//   minimize monitor lock holds times.  Note that even a small amount of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2984
//   fixed spinning will greatly reduce the # of enqueue-dequeue operations
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2985
//   on EntryList|cxq.  That is, spinning relieves contention on the "inner"
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2986
//   locks and monitor metadata.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2987
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2988
//   Cxq points to the the set of Recently Arrived Threads attempting entry.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2989
//   Because we push threads onto _cxq with CAS, the RATs must take the form of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2990
//   a singly-linked LIFO.  We drain _cxq into EntryList  at unlock-time when
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2991
//   the unlocking thread notices that EntryList is null but _cxq is != null.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2992
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2993
//   The EntryList is ordered by the prevailing queue discipline and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2994
//   can be organized in any convenient fashion, such as a doubly-linked list or
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2995
//   a circular doubly-linked list.  Critically, we want insert and delete operations
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2996
//   to operate in constant-time.  If we need a priority queue then something akin
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2997
//   to Solaris' sleepq would work nicely.  Viz.,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2998
//   http://agg.eng/ws/on10_nightly/source/usr/src/uts/common/os/sleepq.c.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2999
//   Queue discipline is enforced at ::exit() time, when the unlocking thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3000
//   drains the cxq into the EntryList, and orders or reorders the threads on the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3001
//   EntryList accordingly.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3002
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3003
//   Barring "lock barging", this mechanism provides fair cyclic ordering,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3004
//   somewhat similar to an elevator-scan.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3005
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3006
// * The monitor synchronization subsystem avoids the use of native
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3007
//   synchronization primitives except for the narrow platform-specific
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3008
//   park-unpark abstraction.  See the comments in os_solaris.cpp regarding
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3009
//   the semantics of park-unpark.  Put another way, this monitor implementation
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3010
//   depends only on atomic operations and park-unpark.  The monitor subsystem
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3011
//   manages all RUNNING->BLOCKED and BLOCKED->READY transitions while the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3012
//   underlying OS manages the READY<->RUN transitions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3013
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3014
// * Waiting threads reside on the WaitSet list -- wait() puts
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3015
//   the caller onto the WaitSet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3016
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3017
// * notify() or notifyAll() simply transfers threads from the WaitSet to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3018
//   either the EntryList or cxq.  Subsequent exit() operations will
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3019
//   unpark the notifyee.  Unparking a notifee in notify() is inefficient -
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3020
//   it's likely the notifyee would simply impale itself on the lock held
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3021
//   by the notifier.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3022
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3023
// * An interesting alternative is to encode cxq as (List,LockByte) where
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3024
//   the LockByte is 0 iff the monitor is owned.  _owner is simply an auxiliary
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3025
//   variable, like _recursions, in the scheme.  The threads or Events that form
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3026
//   the list would have to be aligned in 256-byte addresses.  A thread would
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3027
//   try to acquire the lock or enqueue itself with CAS, but exiting threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3028
//   could use a 1-0 protocol and simply STB to set the LockByte to 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3029
//   Note that is is *not* word-tearing, but it does presume that full-word
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3030
//   CAS operations are coherent with intermix with STB operations.  That's true
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3031
//   on most common processors.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3032
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3033
// * See also http://blogs.sun.com/dave
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3034
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3035
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3036
void ATTR ObjectMonitor::EnterI (TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3037
    Thread * Self = THREAD ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3038
    assert (Self->is_Java_thread(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3039
    assert (((JavaThread *) Self)->thread_state() == _thread_blocked   , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3040
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3041
    // Try the lock - TATAS
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3042
    if (TryLock (Self) > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3043
        assert (_succ != Self              , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3044
        assert (_owner == Self             , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3045
        assert (_Responsible != Self       , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3046
        return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3047
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3048
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3049
    DeferredInitialize () ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3050
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3051
    // We try one round of spinning *before* enqueueing Self.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3052
    //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3053
    // If the _owner is ready but OFFPROC we could use a YieldTo()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3054
    // operation to donate the remainder of this thread's quantum
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3055
    // to the owner.  This has subtle but beneficial affinity
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3056
    // effects.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3057
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3058
    if (TrySpin (Self) > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3059
        assert (_owner == Self        , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3060
        assert (_succ != Self         , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3061
        assert (_Responsible != Self  , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3062
        return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3063
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3064
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3065
    // The Spin failed -- Enqueue and park the thread ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3066
    assert (_succ  != Self            , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3067
    assert (_owner != Self            , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3068
    assert (_Responsible != Self      , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3069
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3070
    // Enqueue "Self" on ObjectMonitor's _cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3071
    //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3072
    // Node acts as a proxy for Self.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3073
    // As an aside, if were to ever rewrite the synchronization code mostly
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3074
    // in Java, WaitNodes, ObjectMonitors, and Events would become 1st-class
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3075
    // Java objects.  This would avoid awkward lifecycle and liveness issues,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3076
    // as well as eliminate a subset of ABA issues.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3077
    // TODO: eliminate ObjectWaiter and enqueue either Threads or Events.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3078
    //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3079
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3080
    ObjectWaiter node(Self) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3081
    Self->_ParkEvent->reset() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3082
    node._prev   = (ObjectWaiter *) 0xBAD ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3083
    node.TState  = ObjectWaiter::TS_CXQ ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3084
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3085
    // Push "Self" onto the front of the _cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3086
    // Once on cxq/EntryList, Self stays on-queue until it acquires the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3087
    // Note that spinning tends to reduce the rate at which threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3088
    // enqueue and dequeue on EntryList|cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3089
    ObjectWaiter * nxt ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3090
    for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3091
        node._next = nxt = _cxq ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3092
        if (Atomic::cmpxchg_ptr (&node, &_cxq, nxt) == nxt) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3093
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3094
        // Interference - the CAS failed because _cxq changed.  Just retry.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3095
        // As an optional optimization we retry the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3096
        if (TryLock (Self) > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3097
            assert (_succ != Self         , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3098
            assert (_owner == Self        , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3099
            assert (_Responsible != Self  , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3100
            return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3101
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3102
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3103
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3104
    // Check for cxq|EntryList edge transition to non-null.  This indicates
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3105
    // the onset of contention.  While contention persists exiting threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3106
    // will use a ST:MEMBAR:LD 1-1 exit protocol.  When contention abates exit
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3107
    // operations revert to the faster 1-0 mode.  This enter operation may interleave
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3108
    // (race) a concurrent 1-0 exit operation, resulting in stranding, so we
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3109
    // arrange for one of the contending thread to use a timed park() operations
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3110
    // to detect and recover from the race.  (Stranding is form of progress failure
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3111
    // where the monitor is unlocked but all the contending threads remain parked).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3112
    // That is, at least one of the contended threads will periodically poll _owner.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3113
    // One of the contending threads will become the designated "Responsible" thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3114
    // The Responsible thread uses a timed park instead of a normal indefinite park
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3115
    // operation -- it periodically wakes and checks for and recovers from potential
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3116
    // strandings admitted by 1-0 exit operations.   We need at most one Responsible
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3117
    // thread per-monitor at any given moment.  Only threads on cxq|EntryList may
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3118
    // be responsible for a monitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3119
    //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3120
    // Currently, one of the contended threads takes on the added role of "Responsible".
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3121
    // A viable alternative would be to use a dedicated "stranding checker" thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3122
    // that periodically iterated over all the threads (or active monitors) and unparked
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3123
    // successors where there was risk of stranding.  This would help eliminate the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3124
    // timer scalability issues we see on some platforms as we'd only have one thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3125
    // -- the checker -- parked on a timer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3126
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3127
    if ((SyncFlags & 16) == 0 && nxt == NULL && _EntryList == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3128
        // Try to assume the role of responsible thread for the monitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3129
        // CONSIDER:  ST vs CAS vs { if (Responsible==null) Responsible=Self }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3130
        Atomic::cmpxchg_ptr (Self, &_Responsible, NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3131
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3132
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3133
    // The lock have been released while this thread was occupied queueing
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3134
    // itself onto _cxq.  To close the race and avoid "stranding" and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3135
    // progress-liveness failure we must resample-retry _owner before parking.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3136
    // Note the Dekker/Lamport duality: ST cxq; MEMBAR; LD Owner.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3137
    // In this case the ST-MEMBAR is accomplished with CAS().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3138
    //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3139
    // TODO: Defer all thread state transitions until park-time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3140
    // Since state transitions are heavy and inefficient we'd like
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3141
    // to defer the state transitions until absolutely necessary,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3142
    // and in doing so avoid some transitions ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3143
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3144
    TEVENT (Inflated enter - Contention) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3145
    int nWakeups = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3146
    int RecheckInterval = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3147
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3148
    for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3149
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3150
        if (TryLock (Self) > 0) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3151
        assert (_owner != Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3152
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3153
        if ((SyncFlags & 2) && _Responsible == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3154
           Atomic::cmpxchg_ptr (Self, &_Responsible, NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3155
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3156
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3157
        // park self
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3158
        if (_Responsible == Self || (SyncFlags & 1)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3159
            TEVENT (Inflated enter - park TIMED) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3160
            Self->_ParkEvent->park ((jlong) RecheckInterval) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3161
            // Increase the RecheckInterval, but clamp the value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3162
            RecheckInterval *= 8 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3163
            if (RecheckInterval > 1000) RecheckInterval = 1000 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3164
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3165
            TEVENT (Inflated enter - park UNTIMED) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3166
            Self->_ParkEvent->park() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3167
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3168
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3169
        if (TryLock(Self) > 0) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3170
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3171
        // The lock is still contested.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3172
        // Keep a tally of the # of futile wakeups.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3173
        // Note that the counter is not protected by a lock or updated by atomics.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3174
        // That is by design - we trade "lossy" counters which are exposed to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3175
        // races during updates for a lower probe effect.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3176
        TEVENT (Inflated enter - Futile wakeup) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3177
        if (ObjectSynchronizer::_sync_FutileWakeups != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3178
           ObjectSynchronizer::_sync_FutileWakeups->inc() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3179
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3180
        ++ nWakeups ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3181
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3182
        // Assuming this is not a spurious wakeup we'll normally find _succ == Self.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3183
        // We can defer clearing _succ until after the spin completes
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3184
        // TrySpin() must tolerate being called with _succ == Self.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3185
        // Try yet another round of adaptive spinning.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3186
        if ((Knob_SpinAfterFutile & 1) && TrySpin (Self) > 0) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3187
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3188
        // We can find that we were unpark()ed and redesignated _succ while
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3189
        // we were spinning.  That's harmless.  If we iterate and call park(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3190
        // park() will consume the event and return immediately and we'll
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3191
        // just spin again.  This pattern can repeat, leaving _succ to simply
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3192
        // spin on a CPU.  Enable Knob_ResetEvent to clear pending unparks().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3193
        // Alternately, we can sample fired() here, and if set, forgo spinning
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3194
        // in the next iteration.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3195
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3196
        if ((Knob_ResetEvent & 1) && Self->_ParkEvent->fired()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3197
           Self->_ParkEvent->reset() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3198
           OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3199
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3200
        if (_succ == Self) _succ = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3201
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3202
        // Invariant: after clearing _succ a thread *must* retry _owner before parking.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3203
        OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3204
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3205
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3206
    // Egress :
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3207
    // Self has acquired the lock -- Unlink Self from the cxq or EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3208
    // Normally we'll find Self on the EntryList .
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3209
    // From the perspective of the lock owner (this thread), the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3210
    // EntryList is stable and cxq is prepend-only.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3211
    // The head of cxq is volatile but the interior is stable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3212
    // In addition, Self.TState is stable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3213
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3214
    assert (_owner == Self      , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3215
    assert (object() != NULL    , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3216
    // I'd like to write:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3217
    //   guarantee (((oop)(object()))->mark() == markOopDesc::encode(this), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3218
    // but as we're at a safepoint that's not safe.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3219
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3220
    UnlinkAfterAcquire (Self, &node) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3221
    if (_succ == Self) _succ = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3222
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3223
    assert (_succ != Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3224
    if (_Responsible == Self) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3225
        _Responsible = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3226
        // Dekker pivot-point.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3227
        // Consider OrderAccess::storeload() here
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3228
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3229
        // We may leave threads on cxq|EntryList without a designated
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3230
        // "Responsible" thread.  This is benign.  When this thread subsequently
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3231
        // exits the monitor it can "see" such preexisting "old" threads --
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3232
        // threads that arrived on the cxq|EntryList before the fence, above --
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3233
        // by LDing cxq|EntryList.  Newly arrived threads -- that is, threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3234
        // that arrive on cxq after the ST:MEMBAR, above -- will set Responsible
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3235
        // non-null and elect a new "Responsible" timer thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3236
        //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3237
        // This thread executes:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3238
        //    ST Responsible=null; MEMBAR    (in enter epilog - here)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3239
        //    LD cxq|EntryList               (in subsequent exit)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3240
        //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3241
        // Entering threads in the slow/contended path execute:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3242
        //    ST cxq=nonnull; MEMBAR; LD Responsible (in enter prolog)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3243
        //    The (ST cxq; MEMBAR) is accomplished with CAS().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3244
        //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3245
        // The MEMBAR, above, prevents the LD of cxq|EntryList in the subsequent
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3246
        // exit operation from floating above the ST Responsible=null.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3247
        //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3248
        // In *practice* however, EnterI() is always followed by some atomic
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3249
        // operation such as the decrement of _count in ::enter().  Those atomics
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3250
        // obviate the need for the explicit MEMBAR, above.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3251
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3252
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3253
    // We've acquired ownership with CAS().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3254
    // CAS is serializing -- it has MEMBAR/FENCE-equivalent semantics.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3255
    // But since the CAS() this thread may have also stored into _succ,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3256
    // EntryList, cxq or Responsible.  These meta-data updates must be
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3257
    // visible __before this thread subsequently drops the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3258
    // Consider what could occur if we didn't enforce this constraint --
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3259
    // STs to monitor meta-data and user-data could reorder with (become
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3260
    // visible after) the ST in exit that drops ownership of the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3261
    // Some other thread could then acquire the lock, but observe inconsistent
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3262
    // or old monitor meta-data and heap data.  That violates the JMM.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3263
    // To that end, the 1-0 exit() operation must have at least STST|LDST
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3264
    // "release" barrier semantics.  Specifically, there must be at least a
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3265
    // STST|LDST barrier in exit() before the ST of null into _owner that drops
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3266
    // the lock.   The barrier ensures that changes to monitor meta-data and data
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3267
    // protected by the lock will be visible before we release the lock, and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3268
    // therefore before some other thread (CPU) has a chance to acquire the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3269
    // See also: http://gee.cs.oswego.edu/dl/jmm/cookbook.html.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3270
    //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3271
    // Critically, any prior STs to _succ or EntryList must be visible before
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3272
    // the ST of null into _owner in the *subsequent* (following) corresponding
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3273
    // monitorexit.  Recall too, that in 1-0 mode monitorexit does not necessarily
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3274
    // execute a serializing instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3275
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3276
    if (SyncFlags & 8) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3277
       OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3278
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3279
    return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3280
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3281
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3282
// ExitSuspendEquivalent:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3283
// A faster alternate to handle_special_suspend_equivalent_condition()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3284
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3285
// handle_special_suspend_equivalent_condition() unconditionally
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3286
// acquires the SR_lock.  On some platforms uncontended MutexLocker()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3287
// operations have high latency.  Note that in ::enter() we call HSSEC
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3288
// while holding the monitor, so we effectively lengthen the critical sections.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3289
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3290
// There are a number of possible solutions:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3291
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3292
// A.  To ameliorate the problem we might also defer state transitions
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3293
//     to as late as possible -- just prior to parking.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3294
//     Given that, we'd call HSSEC after having returned from park(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3295
//     but before attempting to acquire the monitor.  This is only a
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3296
//     partial solution.  It avoids calling HSSEC while holding the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3297
//     monitor (good), but it still increases successor reacquisition latency --
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3298
//     the interval between unparking a successor and the time the successor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3299
//     resumes and retries the lock.  See ReenterI(), which defers state transitions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3300
//     If we use this technique we can also avoid EnterI()-exit() loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3301
//     in ::enter() where we iteratively drop the lock and then attempt
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3302
//     to reacquire it after suspending.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3303
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3304
// B.  In the future we might fold all the suspend bits into a
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3305
//     composite per-thread suspend flag and then update it with CAS().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3306
//     Alternately, a Dekker-like mechanism with multiple variables
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3307
//     would suffice:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3308
//       ST Self->_suspend_equivalent = false
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3309
//       MEMBAR
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3310
//       LD Self_>_suspend_flags
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3311
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3312
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3313
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3314
bool ObjectMonitor::ExitSuspendEquivalent (JavaThread * jSelf) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3315
   int Mode = Knob_FastHSSEC ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3316
   if (Mode && !jSelf->is_external_suspend()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3317
      assert (jSelf->is_suspend_equivalent(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3318
      jSelf->clear_suspend_equivalent() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3319
      if (2 == Mode) OrderAccess::storeload() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3320
      if (!jSelf->is_external_suspend()) return false ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3321
      // We raced a suspension -- fall thru into the slow path
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3322
      TEVENT (ExitSuspendEquivalent - raced) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3323
      jSelf->set_suspend_equivalent() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3324
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3325
   return jSelf->handle_special_suspend_equivalent_condition() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3326
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3327
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3328
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3329
// ReenterI() is a specialized inline form of the latter half of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3330
// contended slow-path from EnterI().  We use ReenterI() only for
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3331
// monitor reentry in wait().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3332
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3333
// In the future we should reconcile EnterI() and ReenterI(), adding
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3334
// Knob_Reset and Knob_SpinAfterFutile support and restructuring the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3335
// loop accordingly.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3336
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3337
void ATTR ObjectMonitor::ReenterI (Thread * Self, ObjectWaiter * SelfNode) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3338
    assert (Self != NULL                , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3339
    assert (SelfNode != NULL            , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3340
    assert (SelfNode->_thread == Self   , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3341
    assert (_waiters > 0                , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3342
    assert (((oop)(object()))->mark() == markOopDesc::encode(this) , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3343
    assert (((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3344
    JavaThread * jt = (JavaThread *) Self ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3345
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3346
    int nWakeups = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3347
    for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3348
        ObjectWaiter::TStates v = SelfNode->TState ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3349
        guarantee (v == ObjectWaiter::TS_ENTER || v == ObjectWaiter::TS_CXQ, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3350
        assert    (_owner != Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3351
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3352
        if (TryLock (Self) > 0) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3353
        if (TrySpin (Self) > 0) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3354
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3355
        TEVENT (Wait Reentry - parking) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3356
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3357
        // State transition wrappers around park() ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3358
        // ReenterI() wisely defers state transitions until
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3359
        // it's clear we must park the thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3360
        {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3361
           OSThreadContendState osts(Self->osthread());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3362
           ThreadBlockInVM tbivm(jt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3363
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3364
           // cleared by handle_special_suspend_equivalent_condition()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3365
           // or java_suspend_self()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3366
           jt->set_suspend_equivalent();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3367
           if (SyncFlags & 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3368
              Self->_ParkEvent->park ((jlong)1000) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3369
           } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3370
              Self->_ParkEvent->park () ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3371
           }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3372
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3373
           // were we externally suspended while we were waiting?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3374
           for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3375
              if (!ExitSuspendEquivalent (jt)) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3376
              if (_succ == Self) { _succ = NULL; OrderAccess::fence(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3377
              jt->java_suspend_self();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3378
              jt->set_suspend_equivalent();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3379
           }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3380
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3381
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3382
        // Try again, but just so we distinguish between futile wakeups and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3383
        // successful wakeups.  The following test isn't algorithmically
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3384
        // necessary, but it helps us maintain sensible statistics.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3385
        if (TryLock(Self) > 0) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3386
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3387
        // The lock is still contested.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3388
        // Keep a tally of the # of futile wakeups.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3389
        // Note that the counter is not protected by a lock or updated by atomics.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3390
        // That is by design - we trade "lossy" counters which are exposed to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3391
        // races during updates for a lower probe effect.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3392
        TEVENT (Wait Reentry - futile wakeup) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3393
        ++ nWakeups ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3394
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3395
        // Assuming this is not a spurious wakeup we'll normally
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3396
        // find that _succ == Self.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3397
        if (_succ == Self) _succ = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3398
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3399
        // Invariant: after clearing _succ a contending thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3400
        // *must* retry  _owner before parking.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3401
        OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3402
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3403
        if (ObjectSynchronizer::_sync_FutileWakeups != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3404
          ObjectSynchronizer::_sync_FutileWakeups->inc() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3405
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3406
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3407
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3408
    // Self has acquired the lock -- Unlink Self from the cxq or EntryList .
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3409
    // Normally we'll find Self on the EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3410
    // Unlinking from the EntryList is constant-time and atomic-free.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3411
    // From the perspective of the lock owner (this thread), the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3412
    // EntryList is stable and cxq is prepend-only.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3413
    // The head of cxq is volatile but the interior is stable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3414
    // In addition, Self.TState is stable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3415
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3416
    assert (_owner == Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3417
    assert (((oop)(object()))->mark() == markOopDesc::encode(this), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3418
    UnlinkAfterAcquire (Self, SelfNode) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3419
    if (_succ == Self) _succ = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3420
    assert (_succ != Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3421
    SelfNode->TState = ObjectWaiter::TS_RUN ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3422
    OrderAccess::fence() ;      // see comments at the end of EnterI()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3423
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3424
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3425
bool ObjectMonitor::try_enter(Thread* THREAD) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3426
  if (THREAD != _owner) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3427
    if (THREAD->is_lock_owned ((address)_owner)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3428
       assert(_recursions == 0, "internal state error");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3429
       _owner = THREAD ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3430
       _recursions = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3431
       OwnerIsThread = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3432
       return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3433
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3434
    if (Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3435
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3436
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3437
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3438
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3439
    _recursions++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3440
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3441
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3442
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3443
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3444
void ATTR ObjectMonitor::enter(TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3445
  // The following code is ordered to check the most common cases first
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3446
  // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3447
  Thread * const Self = THREAD ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3448
  void * cur ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3449
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3450
  cur = Atomic::cmpxchg_ptr (Self, &_owner, NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3451
  if (cur == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3452
     // Either ASSERT _recursions == 0 or explicitly set _recursions = 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3453
     assert (_recursions == 0   , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3454
     assert (_owner      == Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3455
     // CONSIDER: set or assert OwnerIsThread == 1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3456
     return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3457
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3458
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3459
  if (cur == Self) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3460
     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3461
     _recursions ++ ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3462
     return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3463
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3464
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3465
  if (Self->is_lock_owned ((address)cur)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3466
    assert (_recursions == 0, "internal state error");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3467
    _recursions = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3468
    // Commute owner from a thread-specific on-stack BasicLockObject address to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3469
    // a full-fledged "Thread *".
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3470
    _owner = Self ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3471
    OwnerIsThread = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3472
    return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3473
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3474
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3475
  // We've encountered genuine contention.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3476
  assert (Self->_Stalled == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3477
  Self->_Stalled = intptr_t(this) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3478
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3479
  // Try one round of spinning *before* enqueueing Self
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3480
  // and before going through the awkward and expensive state
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3481
  // transitions.  The following spin is strictly optional ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3482
  // Note that if we acquire the monitor from an initial spin
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3483
  // we forgo posting JVMTI events and firing DTRACE probes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3484
  if (Knob_SpinEarly && TrySpin (Self) > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3485
     assert (_owner == Self      , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3486
     assert (_recursions == 0    , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3487
     assert (((oop)(object()))->mark() == markOopDesc::encode(this), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3488
     Self->_Stalled = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3489
     return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3490
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3491
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3492
  assert (_owner != Self          , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3493
  assert (_succ  != Self          , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3494
  assert (Self->is_Java_thread()  , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3495
  JavaThread * jt = (JavaThread *) Self ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3496
  assert (!SafepointSynchronize::is_at_safepoint(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3497
  assert (jt->thread_state() != _thread_blocked   , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3498
  assert (this->object() != NULL  , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3499
  assert (_count >= 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3500
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3501
  // Prevent deflation at STW-time.  See deflate_idle_monitors() and is_busy().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3502
  // Ensure the object-monitor relationship remains stable while there's contention.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3503
  Atomic::inc_ptr(&_count);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3504
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3505
  { // Change java thread status to indicate blocked on monitor enter.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3506
    JavaThreadBlockedOnMonitorEnterState jtbmes(jt, this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3507
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3508
    DTRACE_MONITOR_PROBE(contended__enter, this, object(), jt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3509
    if (JvmtiExport::should_post_monitor_contended_enter()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3510
      JvmtiExport::post_monitor_contended_enter(jt, this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3511
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3512
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3513
    OSThreadContendState osts(Self->osthread());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3514
    ThreadBlockInVM tbivm(jt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3515
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3516
    Self->set_current_pending_monitor(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3517
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3518
    // TODO-FIXME: change the following for(;;) loop to straight-line code.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3519
    for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3520
      jt->set_suspend_equivalent();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3521
      // cleared by handle_special_suspend_equivalent_condition()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3522
      // or java_suspend_self()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3523
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3524
      EnterI (THREAD) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3525
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3526
      if (!ExitSuspendEquivalent(jt)) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3527
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3528
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3529
      // We have acquired the contended monitor, but while we were
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3530
      // waiting another thread suspended us. We don't want to enter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3531
      // the monitor while suspended because that would surprise the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3532
      // thread that suspended us.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3533
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3534
          _recursions = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3535
      _succ = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3536
      exit (Self) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3537
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3538
      jt->java_suspend_self();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3539
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3540
    Self->set_current_pending_monitor(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3541
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3542
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3543
  Atomic::dec_ptr(&_count);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3544
  assert (_count >= 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3545
  Self->_Stalled = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3546
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3547
  // Must either set _recursions = 0 or ASSERT _recursions == 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3548
  assert (_recursions == 0     , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3549
  assert (_owner == Self       , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3550
  assert (_succ  != Self       , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3551
  assert (((oop)(object()))->mark() == markOopDesc::encode(this), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3552
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3553
  // The thread -- now the owner -- is back in vm mode.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3554
  // Report the glorious news via TI,DTrace and jvmstat.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3555
  // The probe effect is non-trivial.  All the reportage occurs
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3556
  // while we hold the monitor, increasing the length of the critical
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3557
  // section.  Amdahl's parallel speedup law comes vividly into play.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3558
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3559
  // Another option might be to aggregate the events (thread local or
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3560
  // per-monitor aggregation) and defer reporting until a more opportune
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3561
  // time -- such as next time some thread encounters contention but has
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3562
  // yet to acquire the lock.  While spinning that thread could
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3563
  // spinning we could increment JVMStat counters, etc.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3564
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3565
  DTRACE_MONITOR_PROBE(contended__entered, this, object(), jt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3566
  if (JvmtiExport::should_post_monitor_contended_entered()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3567
    JvmtiExport::post_monitor_contended_entered(jt, this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3568
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3569
  if (ObjectSynchronizer::_sync_ContendedLockAttempts != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3570
     ObjectSynchronizer::_sync_ContendedLockAttempts->inc() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3571
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3572
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3573
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3574
void ObjectMonitor::ExitEpilog (Thread * Self, ObjectWaiter * Wakee) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3575
   assert (_owner == Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3576
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3577
   // Exit protocol:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3578
   // 1. ST _succ = wakee
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3579
   // 2. membar #loadstore|#storestore;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3580
   // 2. ST _owner = NULL
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3581
   // 3. unpark(wakee)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3582
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3583
   _succ = Knob_SuccEnabled ? Wakee->_thread : NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3584
   ParkEvent * Trigger = Wakee->_event ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3585
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3586
   // Hygiene -- once we've set _owner = NULL we can't safely dereference Wakee again.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3587
   // The thread associated with Wakee may have grabbed the lock and "Wakee" may be
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3588
   // out-of-scope (non-extant).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3589
   Wakee  = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3590
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3591
   // Drop the lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3592
   OrderAccess::release_store_ptr (&_owner, NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3593
   OrderAccess::fence() ;                               // ST _owner vs LD in unpark()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3594
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3595
   // TODO-FIXME:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3596
   // If there's a safepoint pending the best policy would be to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3597
   // get _this thread to a safepoint and only wake the successor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3598
   // after the safepoint completed.  monitorexit uses a "leaf"
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3599
   // state transition, however, so this thread can't become
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3600
   // safe at this point in time.  (Its stack isn't walkable).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3601
   // The next best thing is to defer waking the successor by
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3602
   // adding to a list of thread to be unparked after at the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3603
   // end of the forthcoming STW).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3604
   if (SafepointSynchronize::do_call_back()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3605
      TEVENT (unpark before SAFEPOINT) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3606
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3607
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3608
   // Possible optimizations ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3609
   //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3610
   // * Consider: set Wakee->UnparkTime = timeNow()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3611
   //   When the thread wakes up it'll compute (timeNow() - Self->UnparkTime()).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3612
   //   By measuring recent ONPROC latency we can approximate the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3613
   //   system load.  In turn, we can feed that information back
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3614
   //   into the spinning & succession policies.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3615
   //   (ONPROC latency correlates strongly with load).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3616
   //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3617
   // * Pull affinity:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3618
   //   If the wakee is cold then transiently setting it's affinity
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3619
   //   to the current CPU is a good idea.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3620
   //   See http://j2se.east/~dice/PERSIST/050624-PullAffinity.txt
1665
7b9a44b26afd 6639341: sometimes contended-exit event comes after contended-entered on another thread
blacklion
parents: 1
diff changeset
  3621
   DTRACE_MONITOR_PROBE(contended__exit, this, object(), Self);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3622
   Trigger->unpark() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3623
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3624
   // Maintain stats and report events to JVMTI
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3625
   if (ObjectSynchronizer::_sync_Parks != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3626
      ObjectSynchronizer::_sync_Parks->inc() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3627
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3628
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3629
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3630
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3631
// exit()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3632
// ~~~~~~
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3633
// Note that the collector can't reclaim the objectMonitor or deflate
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3634
// the object out from underneath the thread calling ::exit() as the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3635
// thread calling ::exit() never transitions to a stable state.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3636
// This inhibits GC, which in turn inhibits asynchronous (and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3637
// inopportune) reclamation of "this".
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3638
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3639
// We'd like to assert that: (THREAD->thread_state() != _thread_blocked) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3640
// There's one exception to the claim above, however.  EnterI() can call
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3641
// exit() to drop a lock if the acquirer has been externally suspended.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3642
// In that case exit() is called with _thread_state as _thread_blocked,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3643
// but the monitor's _count field is > 0, which inhibits reclamation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3644
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3645
// 1-0 exit
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3646
// ~~~~~~~~
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3647
// ::exit() uses a canonical 1-1 idiom with a MEMBAR although some of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3648
// the fast-path operators have been optimized so the common ::exit()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3649
// operation is 1-0.  See i486.ad fast_unlock(), for instance.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3650
// The code emitted by fast_unlock() elides the usual MEMBAR.  This
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3651
// greatly improves latency -- MEMBAR and CAS having considerable local
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3652
// latency on modern processors -- but at the cost of "stranding".  Absent the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3653
// MEMBAR, a thread in fast_unlock() can race a thread in the slow
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3654
// ::enter() path, resulting in the entering thread being stranding
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3655
// and a progress-liveness failure.   Stranding is extremely rare.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3656
// We use timers (timed park operations) & periodic polling to detect
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3657
// and recover from stranding.  Potentially stranded threads periodically
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3658
// wake up and poll the lock.  See the usage of the _Responsible variable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3659
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3660
// The CAS() in enter provides for safety and exclusion, while the CAS or
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3661
// MEMBAR in exit provides for progress and avoids stranding.  1-0 locking
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3662
// eliminates the CAS/MEMBAR from the exist path, but it admits stranding.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3663
// We detect and recover from stranding with timers.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3664
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3665
// If a thread transiently strands it'll park until (a) another
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3666
// thread acquires the lock and then drops the lock, at which time the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3667
// exiting thread will notice and unpark the stranded thread, or, (b)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3668
// the timer expires.  If the lock is high traffic then the stranding latency
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3669
// will be low due to (a).  If the lock is low traffic then the odds of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3670
// stranding are lower, although the worst-case stranding latency
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3671
// is longer.  Critically, we don't want to put excessive load in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3672
// platform's timer subsystem.  We want to minimize both the timer injection
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3673
// rate (timers created/sec) as well as the number of timers active at
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3674
// any one time.  (more precisely, we want to minimize timer-seconds, which is
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3675
// the integral of the # of active timers at any instant over time).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3676
// Both impinge on OS scalability.  Given that, at most one thread parked on
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3677
// a monitor will use a timer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3678
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3679
void ATTR ObjectMonitor::exit(TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3680
   Thread * Self = THREAD ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3681
   if (THREAD != _owner) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3682
     if (THREAD->is_lock_owned((address) _owner)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3683
       // Transmute _owner from a BasicLock pointer to a Thread address.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3684
       // We don't need to hold _mutex for this transition.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3685
       // Non-null to Non-null is safe as long as all readers can
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3686
       // tolerate either flavor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3687
       assert (_recursions == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3688
       _owner = THREAD ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3689
       _recursions = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3690
       OwnerIsThread = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3691
     } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3692
       // NOTE: we need to handle unbalanced monitor enter/exit
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3693
       // in native code by throwing an exception.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3694
       // TODO: Throw an IllegalMonitorStateException ?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3695
       TEVENT (Exit - Throw IMSX) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3696
       assert(false, "Non-balanced monitor enter/exit!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3697
       if (false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3698
          THROW(vmSymbols::java_lang_IllegalMonitorStateException());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3699
       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3700
       return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3701
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3702
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3703
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3704
   if (_recursions != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3705
     _recursions--;        // this is simple recursive enter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3706
     TEVENT (Inflated exit - recursive) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3707
     return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3708
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3709
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3710
   // Invariant: after setting Responsible=null an thread must execute
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3711
   // a MEMBAR or other serializing instruction before fetching EntryList|cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3712
   if ((SyncFlags & 4) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3713
      _Responsible = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3714
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3715
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3716
   for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3717
      assert (THREAD == _owner, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3718
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3719
      // Fast-path monitor exit:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3720
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3721
      // Observe the Dekker/Lamport duality:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3722
      // A thread in ::exit() executes:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3723
      //   ST Owner=null; MEMBAR; LD EntryList|cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3724
      // A thread in the contended ::enter() path executes the complementary:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3725
      //   ST EntryList|cxq = nonnull; MEMBAR; LD Owner.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3726
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3727
      // Note that there's a benign race in the exit path.  We can drop the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3728
      // lock, another thread can reacquire the lock immediately, and we can
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3729
      // then wake a thread unnecessarily (yet another flavor of futile wakeup).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3730
      // This is benign, and we've structured the code so the windows are short
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3731
      // and the frequency of such futile wakeups is low.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3732
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3733
      // We could eliminate the race by encoding both the "LOCKED" state and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3734
      // the queue head in a single word.  Exit would then use either CAS to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3735
      // clear the LOCKED bit/byte.  This precludes the desirable 1-0 optimization,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3736
      // however.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3737
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3738
      // Possible fast-path ::exit() optimization:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3739
      // The current fast-path exit implementation fetches both cxq and EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3740
      // See also i486.ad fast_unlock().  Testing has shown that two LDs
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3741
      // isn't measurably slower than a single LD on any platforms.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3742
      // Still, we could reduce the 2 LDs to one or zero by one of the following:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3743
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3744
      // - Use _count instead of cxq|EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3745
      //   We intend to eliminate _count, however, when we switch
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3746
      //   to on-the-fly deflation in ::exit() as is used in
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3747
      //   Metalocks and RelaxedLocks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3748
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3749
      // - Establish the invariant that cxq == null implies EntryList == null.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3750
      //   set cxq == EMPTY (1) to encode the state where cxq is empty
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3751
      //   by EntryList != null.  EMPTY is a distinguished value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3752
      //   The fast-path exit() would fetch cxq but not EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3753
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3754
      // - Encode succ as follows:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3755
      //   succ = t :  Thread t is the successor -- t is ready or is spinning.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3756
      //               Exiting thread does not need to wake a successor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3757
      //   succ = 0 :  No successor required -> (EntryList|cxq) == null
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3758
      //               Exiting thread does not need to wake a successor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3759
      //   succ = 1 :  Successor required    -> (EntryList|cxq) != null and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3760
      //               logically succ == null.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3761
      //               Exiting thread must wake a successor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3762
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3763
      //   The 1-1 fast-exit path would appear as :
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3764
      //     _owner = null ; membar ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3765
      //     if (_succ == 1 && CAS (&_owner, null, Self) == null) goto SlowPath
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3766
      //     goto FastPathDone ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3767
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3768
      //   and the 1-0 fast-exit path would appear as:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3769
      //      if (_succ == 1) goto SlowPath
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3770
      //      Owner = null ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3771
      //      goto FastPathDone
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3772
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3773
      // - Encode the LSB of _owner as 1 to indicate that exit()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3774
      //   must use the slow-path and make a successor ready.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3775
      //   (_owner & 1) == 0 IFF succ != null || (EntryList|cxq) == null
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3776
      //   (_owner & 1) == 0 IFF succ == null && (EntryList|cxq) != null (obviously)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3777
      //   The 1-0 fast exit path would read:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3778
      //      if (_owner != Self) goto SlowPath
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3779
      //      _owner = null
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3780
      //      goto FastPathDone
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3781
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3782
      if (Knob_ExitPolicy == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3783
         // release semantics: prior loads and stores from within the critical section
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3784
         // must not float (reorder) past the following store that drops the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3785
         // On SPARC that requires MEMBAR #loadstore|#storestore.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3786
         // But of course in TSO #loadstore|#storestore is not required.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3787
         // I'd like to write one of the following:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3788
         // A.  OrderAccess::release() ; _owner = NULL
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3789
         // B.  OrderAccess::loadstore(); OrderAccess::storestore(); _owner = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3790
         // Unfortunately OrderAccess::release() and OrderAccess::loadstore() both
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3791
         // store into a _dummy variable.  That store is not needed, but can result
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3792
         // in massive wasteful coherency traffic on classic SMP systems.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3793
         // Instead, I use release_store(), which is implemented as just a simple
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3794
         // ST on x64, x86 and SPARC.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3795
         OrderAccess::release_store_ptr (&_owner, NULL) ;   // drop the lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3796
         OrderAccess::storeload() ;                         // See if we need to wake a successor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3797
         if ((intptr_t(_EntryList)|intptr_t(_cxq)) == 0 || _succ != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3798
            TEVENT (Inflated exit - simple egress) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3799
            return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3800
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3801
         TEVENT (Inflated exit - complex egress) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3802
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3803
         // Normally the exiting thread is responsible for ensuring succession,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3804
         // but if other successors are ready or other entering threads are spinning
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3805
         // then this thread can simply store NULL into _owner and exit without
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3806
         // waking a successor.  The existence of spinners or ready successors
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3807
         // guarantees proper succession (liveness).  Responsibility passes to the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3808
         // ready or running successors.  The exiting thread delegates the duty.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3809
         // More precisely, if a successor already exists this thread is absolved
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3810
         // of the responsibility of waking (unparking) one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3811
         //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3812
         // The _succ variable is critical to reducing futile wakeup frequency.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3813
         // _succ identifies the "heir presumptive" thread that has been made
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3814
         // ready (unparked) but that has not yet run.  We need only one such
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3815
         // successor thread to guarantee progress.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3816
         // See http://www.usenix.org/events/jvm01/full_papers/dice/dice.pdf
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3817
         // section 3.3 "Futile Wakeup Throttling" for details.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3818
         //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3819
         // Note that spinners in Enter() also set _succ non-null.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3820
         // In the current implementation spinners opportunistically set
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3821
         // _succ so that exiting threads might avoid waking a successor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3822
         // Another less appealing alternative would be for the exiting thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3823
         // to drop the lock and then spin briefly to see if a spinner managed
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3824
         // to acquire the lock.  If so, the exiting thread could exit
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3825
         // immediately without waking a successor, otherwise the exiting
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3826
         // thread would need to dequeue and wake a successor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3827
         // (Note that we'd need to make the post-drop spin short, but no
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3828
         // shorter than the worst-case round-trip cache-line migration time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3829
         // The dropped lock needs to become visible to the spinner, and then
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3830
         // the acquisition of the lock by the spinner must become visible to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3831
         // the exiting thread).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3832
         //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3833
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3834
         // It appears that an heir-presumptive (successor) must be made ready.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3835
         // Only the current lock owner can manipulate the EntryList or
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3836
         // drain _cxq, so we need to reacquire the lock.  If we fail
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3837
         // to reacquire the lock the responsibility for ensuring succession
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3838
         // falls to the new owner.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3839
         //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3840
         if (Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3841
            return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3842
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3843
         TEVENT (Exit - Reacquired) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3844
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3845
         if ((intptr_t(_EntryList)|intptr_t(_cxq)) == 0 || _succ != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3846
            OrderAccess::release_store_ptr (&_owner, NULL) ;   // drop the lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3847
            OrderAccess::storeload() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3848
            // Ratify the previously observed values.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3849
            if (_cxq == NULL || _succ != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3850
                TEVENT (Inflated exit - simple egress) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3851
                return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3852
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3853
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3854
            // inopportune interleaving -- the exiting thread (this thread)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3855
            // in the fast-exit path raced an entering thread in the slow-enter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3856
            // path.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3857
            // We have two choices:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3858
            // A.  Try to reacquire the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3859
            //     If the CAS() fails return immediately, otherwise
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3860
            //     we either restart/rerun the exit operation, or simply
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3861
            //     fall-through into the code below which wakes a successor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3862
            // B.  If the elements forming the EntryList|cxq are TSM
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3863
            //     we could simply unpark() the lead thread and return
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3864
            //     without having set _succ.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3865
            if (Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3866
               TEVENT (Inflated exit - reacquired succeeded) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3867
               return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3868
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3869
            TEVENT (Inflated exit - reacquired failed) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3870
         } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3871
            TEVENT (Inflated exit - complex egress) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3872
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3873
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3874
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3875
      guarantee (_owner == THREAD, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3876
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3877
      // Select an appropriate successor ("heir presumptive") from the EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3878
      // and make it ready.  Generally we just wake the head of EntryList .
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3879
      // There's no algorithmic constraint that we use the head - it's just
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3880
      // a policy decision.   Note that the thread at head of the EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3881
      // remains at the head until it acquires the lock.  This means we'll
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3882
      // repeatedly wake the same thread until it manages to grab the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3883
      // This is generally a good policy - if we're seeing lots of futile wakeups
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3884
      // at least we're waking/rewaking a thread that's like to be hot or warm
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3885
      // (have residual D$ and TLB affinity).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3886
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3887
      // "Wakeup locality" optimization:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3888
      // http://j2se.east/~dice/PERSIST/040825-WakeLocality.txt
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3889
      // In the future we'll try to bias the selection mechanism
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3890
      // to preferentially pick a thread that recently ran on
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3891
      // a processor element that shares cache with the CPU on which
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3892
      // the exiting thread is running.   We need access to Solaris'
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3893
      // schedctl.sc_cpu to make that work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3894
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3895
      ObjectWaiter * w = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3896
      int QMode = Knob_QMode ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3897
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3898
      if (QMode == 2 && _cxq != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3899
          // QMode == 2 : cxq has precedence over EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3900
          // Try to directly wake a successor from the cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3901
          // If successful, the successor will need to unlink itself from cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3902
          w = _cxq ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3903
          assert (w != NULL, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3904
          assert (w->TState == ObjectWaiter::TS_CXQ, "Invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3905
          ExitEpilog (Self, w) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3906
          return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3907
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3908
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3909
      if (QMode == 3 && _cxq != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3910
          // Aggressively drain cxq into EntryList at the first opportunity.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3911
          // This policy ensure that recently-run threads live at the head of EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3912
          // Drain _cxq into EntryList - bulk transfer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3913
          // First, detach _cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3914
          // The following loop is tantamount to: w = swap (&cxq, NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3915
          w = _cxq ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3916
          for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3917
             assert (w != NULL, "Invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3918
             ObjectWaiter * u = (ObjectWaiter *) Atomic::cmpxchg_ptr (NULL, &_cxq, w) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3919
             if (u == w) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3920
             w = u ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3921
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3922
          assert (w != NULL              , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3923
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3924
          ObjectWaiter * q = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3925
          ObjectWaiter * p ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3926
          for (p = w ; p != NULL ; p = p->_next) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3927
              guarantee (p->TState == ObjectWaiter::TS_CXQ, "Invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3928
              p->TState = ObjectWaiter::TS_ENTER ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3929
              p->_prev = q ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3930
              q = p ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3931
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3932
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3933
          // Append the RATs to the EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3934
          // TODO: organize EntryList as a CDLL so we can locate the tail in constant-time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3935
          ObjectWaiter * Tail ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3936
          for (Tail = _EntryList ; Tail != NULL && Tail->_next != NULL ; Tail = Tail->_next) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3937
          if (Tail == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3938
              _EntryList = w ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3939
          } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3940
              Tail->_next = w ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3941
              w->_prev = Tail ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3942
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3943
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3944
          // Fall thru into code that tries to wake a successor from EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3945
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3946
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3947
      if (QMode == 4 && _cxq != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3948
          // Aggressively drain cxq into EntryList at the first opportunity.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3949
          // This policy ensure that recently-run threads live at the head of EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3950
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3951
          // Drain _cxq into EntryList - bulk transfer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3952
          // First, detach _cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3953
          // The following loop is tantamount to: w = swap (&cxq, NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3954
          w = _cxq ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3955
          for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3956
             assert (w != NULL, "Invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3957
             ObjectWaiter * u = (ObjectWaiter *) Atomic::cmpxchg_ptr (NULL, &_cxq, w) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3958
             if (u == w) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3959
             w = u ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3960
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3961
          assert (w != NULL              , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3962
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3963
          ObjectWaiter * q = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3964
          ObjectWaiter * p ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3965
          for (p = w ; p != NULL ; p = p->_next) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3966
              guarantee (p->TState == ObjectWaiter::TS_CXQ, "Invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3967
              p->TState = ObjectWaiter::TS_ENTER ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3968
              p->_prev = q ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3969
              q = p ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3970
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3971
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3972
          // Prepend the RATs to the EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3973
          if (_EntryList != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3974
              q->_next = _EntryList ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3975
              _EntryList->_prev = q ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3976
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3977
          _EntryList = w ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3978
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3979
          // Fall thru into code that tries to wake a successor from EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3980
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3981
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3982
      w = _EntryList  ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3983
      if (w != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3984
          // I'd like to write: guarantee (w->_thread != Self).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3985
          // But in practice an exiting thread may find itself on the EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3986
          // Lets say thread T1 calls O.wait().  Wait() enqueues T1 on O's waitset and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3987
          // then calls exit().  Exit release the lock by setting O._owner to NULL.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3988
          // Lets say T1 then stalls.  T2 acquires O and calls O.notify().  The
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3989
          // notify() operation moves T1 from O's waitset to O's EntryList. T2 then
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3990
          // release the lock "O".  T2 resumes immediately after the ST of null into
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3991
          // _owner, above.  T2 notices that the EntryList is populated, so it
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3992
          // reacquires the lock and then finds itself on the EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3993
          // Given all that, we have to tolerate the circumstance where "w" is
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3994
          // associated with Self.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3995
          assert (w->TState == ObjectWaiter::TS_ENTER, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3996
          ExitEpilog (Self, w) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3997
          return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3998
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3999
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4000
      // If we find that both _cxq and EntryList are null then just
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4001
      // re-run the exit protocol from the top.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4002
      w = _cxq ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4003
      if (w == NULL) continue ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4004
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4005
      // Drain _cxq into EntryList - bulk transfer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4006
      // First, detach _cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4007
      // The following loop is tantamount to: w = swap (&cxq, NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4008
      for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4009
          assert (w != NULL, "Invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4010
          ObjectWaiter * u = (ObjectWaiter *) Atomic::cmpxchg_ptr (NULL, &_cxq, w) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4011
          if (u == w) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4012
          w = u ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4013
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4014
      TEVENT (Inflated exit - drain cxq into EntryList) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4015
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4016
      assert (w != NULL              , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4017
      assert (_EntryList  == NULL    , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4018
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4019
      // Convert the LIFO SLL anchored by _cxq into a DLL.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4020
      // The list reorganization step operates in O(LENGTH(w)) time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4021
      // It's critical that this step operate quickly as
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4022
      // "Self" still holds the outer-lock, restricting parallelism
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4023
      // and effectively lengthening the critical section.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4024
      // Invariant: s chases t chases u.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4025
      // TODO-FIXME: consider changing EntryList from a DLL to a CDLL so
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4026
      // we have faster access to the tail.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4027
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4028
      if (QMode == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4029
         // QMode == 1 : drain cxq to EntryList, reversing order
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4030
         // We also reverse the order of the list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4031
         ObjectWaiter * s = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4032
         ObjectWaiter * t = w ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4033
         ObjectWaiter * u = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4034
         while (t != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4035
             guarantee (t->TState == ObjectWaiter::TS_CXQ, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4036
             t->TState = ObjectWaiter::TS_ENTER ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4037
             u = t->_next ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4038
             t->_prev = u ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4039
             t->_next = s ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4040
             s = t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4041
             t = u ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4042
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4043
         _EntryList  = s ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4044
         assert (s != NULL, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4045
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4046
         // QMode == 0 or QMode == 2
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4047
         _EntryList = w ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4048
         ObjectWaiter * q = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4049
         ObjectWaiter * p ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4050
         for (p = w ; p != NULL ; p = p->_next) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4051
             guarantee (p->TState == ObjectWaiter::TS_CXQ, "Invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4052
             p->TState = ObjectWaiter::TS_ENTER ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4053
             p->_prev = q ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4054
             q = p ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4055
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4056
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4057
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4058
      // In 1-0 mode we need: ST EntryList; MEMBAR #storestore; ST _owner = NULL
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4059
      // The MEMBAR is satisfied by the release_store() operation in ExitEpilog().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4060
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4061
      // See if we can abdicate to a spinner instead of waking a thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4062
      // A primary goal of the implementation is to reduce the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4063
      // context-switch rate.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4064
      if (_succ != NULL) continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4065
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4066
      w = _EntryList  ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4067
      if (w != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4068
          guarantee (w->TState == ObjectWaiter::TS_ENTER, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4069
          ExitEpilog (Self, w) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4070
          return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4071
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4072
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4073
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4074
// complete_exit exits a lock returning recursion count
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4075
// complete_exit/reenter operate as a wait without waiting
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4076
// complete_exit requires an inflated monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4077
// The _owner field is not always the Thread addr even with an
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4078
// inflated monitor, e.g. the monitor can be inflated by a non-owning
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4079
// thread due to contention.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4080
intptr_t ObjectMonitor::complete_exit(TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4081
   Thread * const Self = THREAD;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4082
   assert(Self->is_Java_thread(), "Must be Java thread!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4083
   JavaThread *jt = (JavaThread *)THREAD;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4084
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4085
   DeferredInitialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4086
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4087
   if (THREAD != _owner) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4088
    if (THREAD->is_lock_owned ((address)_owner)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4089
       assert(_recursions == 0, "internal state error");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4090
       _owner = THREAD ;   /* Convert from basiclock addr to Thread addr */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4091
       _recursions = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4092
       OwnerIsThread = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4093
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4094
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4095
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4096
   guarantee(Self == _owner, "complete_exit not owner");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4097
   intptr_t save = _recursions; // record the old recursion count
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4098
   _recursions = 0;        // set the recursion level to be 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4099
   exit (Self) ;           // exit the monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4100
   guarantee (_owner != Self, "invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4101
   return save;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4102
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4103
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4104
// reenter() enters a lock and sets recursion count
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4105
// complete_exit/reenter operate as a wait without waiting
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4106
void ObjectMonitor::reenter(intptr_t recursions, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4107
   Thread * const Self = THREAD;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4108
   assert(Self->is_Java_thread(), "Must be Java thread!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4109
   JavaThread *jt = (JavaThread *)THREAD;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4110
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4111
   guarantee(_owner != Self, "reenter already owner");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4112
   enter (THREAD);       // enter the monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4113
   guarantee (_recursions == 0, "reenter recursion");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4114
   _recursions = recursions;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4115
   return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4116
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4117
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4118
// Note: a subset of changes to ObjectMonitor::wait()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4119
// will need to be replicated in complete_exit above
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4120
void ObjectMonitor::wait(jlong millis, bool interruptible, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4121
   Thread * const Self = THREAD ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4122
   assert(Self->is_Java_thread(), "Must be Java thread!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4123
   JavaThread *jt = (JavaThread *)THREAD;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4124
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4125
   DeferredInitialize () ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4126
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4127
   // Throw IMSX or IEX.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4128
   CHECK_OWNER();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4129
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4130
   // check for a pending interrupt
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4131
   if (interruptible && Thread::is_interrupted(Self, true) && !HAS_PENDING_EXCEPTION) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4132
     // post monitor waited event.  Note that this is past-tense, we are done waiting.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4133
     if (JvmtiExport::should_post_monitor_waited()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4134
        // Note: 'false' parameter is passed here because the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4135
        // wait was not timed out due to thread interrupt.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4136
        JvmtiExport::post_monitor_waited(jt, this, false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4137
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4138
     TEVENT (Wait - Throw IEX) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4139
     THROW(vmSymbols::java_lang_InterruptedException());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4140
     return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4141
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4142
   TEVENT (Wait) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4143
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4144
   assert (Self->_Stalled == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4145
   Self->_Stalled = intptr_t(this) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4146
   jt->set_current_waiting_monitor(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4147
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4148
   // create a node to be put into the queue
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4149
   // Critically, after we reset() the event but prior to park(), we must check
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4150
   // for a pending interrupt.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4151
   ObjectWaiter node(Self);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4152
   node.TState = ObjectWaiter::TS_WAIT ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4153
   Self->_ParkEvent->reset() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4154
   OrderAccess::fence();          // ST into Event; membar ; LD interrupted-flag
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4155
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4156
   // Enter the waiting queue, which is a circular doubly linked list in this case
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4157
   // but it could be a priority queue or any data structure.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4158
   // _WaitSetLock protects the wait queue.  Normally the wait queue is accessed only
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4159
   // by the the owner of the monitor *except* in the case where park()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4160
   // returns because of a timeout of interrupt.  Contention is exceptionally rare
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4161
   // so we use a simple spin-lock instead of a heavier-weight blocking lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4162
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4163
   Thread::SpinAcquire (&_WaitSetLock, "WaitSet - add") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4164
   AddWaiter (&node) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4165
   Thread::SpinRelease (&_WaitSetLock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4166
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4167
   if ((SyncFlags & 4) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4168
      _Responsible = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4169
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4170
   intptr_t save = _recursions; // record the old recursion count
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4171
   _waiters++;                  // increment the number of waiters
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4172
   _recursions = 0;             // set the recursion level to be 1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4173
   exit (Self) ;                    // exit the monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4174
   guarantee (_owner != Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4175
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4176
   // As soon as the ObjectMonitor's ownership is dropped in the exit()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4177
   // call above, another thread can enter() the ObjectMonitor, do the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4178
   // notify(), and exit() the ObjectMonitor. If the other thread's
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4179
   // exit() call chooses this thread as the successor and the unpark()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4180
   // call happens to occur while this thread is posting a
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4181
   // MONITOR_CONTENDED_EXIT event, then we run the risk of the event
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4182
   // handler using RawMonitors and consuming the unpark().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4183
   //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4184
   // To avoid the problem, we re-post the event. This does no harm
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4185
   // even if the original unpark() was not consumed because we are the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4186
   // chosen successor for this monitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4187
   if (node._notified != 0 && _succ == Self) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4188
      node._event->unpark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4189
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4190
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4191
   // The thread is on the WaitSet list - now park() it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4192
   // On MP systems it's conceivable that a brief spin before we park
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4193
   // could be profitable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4194
   //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4195
   // TODO-FIXME: change the following logic to a loop of the form
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4196
   //   while (!timeout && !interrupted && _notified == 0) park()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4197
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4198
   int ret = OS_OK ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4199
   int WasNotified = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4200
   { // State transition wrappers
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4201
     OSThread* osthread = Self->osthread();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4202
     OSThreadWaitState osts(osthread, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4203
     {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4204
       ThreadBlockInVM tbivm(jt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4205
       // Thread is in thread_blocked state and oop access is unsafe.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4206
       jt->set_suspend_equivalent();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4207
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4208
       if (interruptible && (Thread::is_interrupted(THREAD, false) || HAS_PENDING_EXCEPTION)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4209
           // Intentionally empty
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4210
       } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4211
       if (node._notified == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4212
         if (millis <= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4213
            Self->_ParkEvent->park () ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4214
         } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4215
            ret = Self->_ParkEvent->park (millis) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4216
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4217
       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4218
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4219
       // were we externally suspended while we were waiting?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4220
       if (ExitSuspendEquivalent (jt)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4221
          // TODO-FIXME: add -- if succ == Self then succ = null.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4222
          jt->java_suspend_self();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4223
       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4224
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4225
     } // Exit thread safepoint: transition _thread_blocked -> _thread_in_vm
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4226
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4227
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4228
     // Node may be on the WaitSet, the EntryList (or cxq), or in transition
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4229
     // from the WaitSet to the EntryList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4230
     // See if we need to remove Node from the WaitSet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4231
     // We use double-checked locking to avoid grabbing _WaitSetLock
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4232
     // if the thread is not on the wait queue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4233
     //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4234
     // Note that we don't need a fence before the fetch of TState.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4235
     // In the worst case we'll fetch a old-stale value of TS_WAIT previously
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4236
     // written by the is thread. (perhaps the fetch might even be satisfied
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4237
     // by a look-aside into the processor's own store buffer, although given
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4238
     // the length of the code path between the prior ST and this load that's
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4239
     // highly unlikely).  If the following LD fetches a stale TS_WAIT value
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4240
     // then we'll acquire the lock and then re-fetch a fresh TState value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4241
     // That is, we fail toward safety.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4242
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4243
     if (node.TState == ObjectWaiter::TS_WAIT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4244
         Thread::SpinAcquire (&_WaitSetLock, "WaitSet - unlink") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4245
         if (node.TState == ObjectWaiter::TS_WAIT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4246
            DequeueSpecificWaiter (&node) ;       // unlink from WaitSet
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4247
            assert(node._notified == 0, "invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4248
            node.TState = ObjectWaiter::TS_RUN ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4249
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4250
         Thread::SpinRelease (&_WaitSetLock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4251
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4252
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4253
     // The thread is now either on off-list (TS_RUN),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4254
     // on the EntryList (TS_ENTER), or on the cxq (TS_CXQ).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4255
     // The Node's TState variable is stable from the perspective of this thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4256
     // No other threads will asynchronously modify TState.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4257
     guarantee (node.TState != ObjectWaiter::TS_WAIT, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4258
     OrderAccess::loadload() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4259
     if (_succ == Self) _succ = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4260
     WasNotified = node._notified ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4261
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4262
     // Reentry phase -- reacquire the monitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4263
     // re-enter contended monitor after object.wait().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4264
     // retain OBJECT_WAIT state until re-enter successfully completes
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4265
     // Thread state is thread_in_vm and oop access is again safe,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4266
     // although the raw address of the object may have changed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4267
     // (Don't cache naked oops over safepoints, of course).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4268
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4269
     // post monitor waited event. Note that this is past-tense, we are done waiting.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4270
     if (JvmtiExport::should_post_monitor_waited()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4271
       JvmtiExport::post_monitor_waited(jt, this, ret == OS_TIMEOUT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4272
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4273
     OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4274
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4275
     assert (Self->_Stalled != 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4276
     Self->_Stalled = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4277
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4278
     assert (_owner != Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4279
     ObjectWaiter::TStates v = node.TState ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4280
     if (v == ObjectWaiter::TS_RUN) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4281
         enter (Self) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4282
     } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4283
         guarantee (v == ObjectWaiter::TS_ENTER || v == ObjectWaiter::TS_CXQ, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4284
         ReenterI (Self, &node) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4285
         node.wait_reenter_end(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4286
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4287
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4288
     // Self has reacquired the lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4289
     // Lifecycle - the node representing Self must not appear on any queues.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4290
     // Node is about to go out-of-scope, but even if it were immortal we wouldn't
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4291
     // want residual elements associated with this thread left on any lists.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4292
     guarantee (node.TState == ObjectWaiter::TS_RUN, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4293
     assert    (_owner == Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4294
     assert    (_succ != Self , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4295
   } // OSThreadWaitState()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4296
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4297
   jt->set_current_waiting_monitor(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4298
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4299
   guarantee (_recursions == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4300
   _recursions = save;     // restore the old recursion count
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4301
   _waiters--;             // decrement the number of waiters
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4302
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4303
   // Verify a few postconditions
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4304
   assert (_owner == Self       , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4305
   assert (_succ  != Self       , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4306
   assert (((oop)(object()))->mark() == markOopDesc::encode(this), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4307
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4308
   if (SyncFlags & 32) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4309
      OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4310
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4311
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4312
   // check if the notification happened
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4313
   if (!WasNotified) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4314
     // no, it could be timeout or Thread.interrupt() or both
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4315
     // check for interrupt event, otherwise it is timeout
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4316
     if (interruptible && Thread::is_interrupted(Self, true) && !HAS_PENDING_EXCEPTION) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4317
       TEVENT (Wait - throw IEX from epilog) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4318
       THROW(vmSymbols::java_lang_InterruptedException());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4319
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4320
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4321
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4322
   // NOTE: Spurious wake up will be consider as timeout.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4323
   // Monitor notify has precedence over thread interrupt.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4324
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4325
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4326
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4327
// Consider:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4328
// If the lock is cool (cxq == null && succ == null) and we're on an MP system
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4329
// then instead of transferring a thread from the WaitSet to the EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4330
// we might just dequeue a thread from the WaitSet and directly unpark() it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4331
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4332
void ObjectMonitor::notify(TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4333
  CHECK_OWNER();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4334
  if (_WaitSet == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4335
     TEVENT (Empty-Notify) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4336
     return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4337
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4338
  DTRACE_MONITOR_PROBE(notify, this, object(), THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4339
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4340
  int Policy = Knob_MoveNotifyee ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4341
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4342
  Thread::SpinAcquire (&_WaitSetLock, "WaitSet - notify") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4343
  ObjectWaiter * iterator = DequeueWaiter() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4344
  if (iterator != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4345
     TEVENT (Notify1 - Transfer) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4346
     guarantee (iterator->TState == ObjectWaiter::TS_WAIT, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4347
     guarantee (iterator->_notified == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4348
     // Disposition - what might we do with iterator ?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4349
     // a.  add it directly to the EntryList - either tail or head.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4350
     // b.  push it onto the front of the _cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4351
     // For now we use (a).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4352
     if (Policy != 4) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4353
        iterator->TState = ObjectWaiter::TS_ENTER ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4354
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4355
     iterator->_notified = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4356
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4357
     ObjectWaiter * List = _EntryList ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4358
     if (List != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4359
        assert (List->_prev == NULL, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4360
        assert (List->TState == ObjectWaiter::TS_ENTER, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4361
        assert (List != iterator, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4362
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4363
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4364
     if (Policy == 0) {       // prepend to EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4365
         if (List == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4366
             iterator->_next = iterator->_prev = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4367
             _EntryList = iterator ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4368
         } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4369
             List->_prev = iterator ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4370
             iterator->_next = List ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4371
             iterator->_prev = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4372
             _EntryList = iterator ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4373
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4374
     } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4375
     if (Policy == 1) {      // append to EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4376
         if (List == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4377
             iterator->_next = iterator->_prev = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4378
             _EntryList = iterator ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4379
         } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4380
            // CONSIDER:  finding the tail currently requires a linear-time walk of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4381
            // the EntryList.  We can make tail access constant-time by converting to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4382
            // a CDLL instead of using our current DLL.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4383
            ObjectWaiter * Tail ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4384
            for (Tail = List ; Tail->_next != NULL ; Tail = Tail->_next) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4385
            assert (Tail != NULL && Tail->_next == NULL, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4386
            Tail->_next = iterator ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4387
            iterator->_prev = Tail ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4388
            iterator->_next = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4389
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4390
     } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4391
     if (Policy == 2) {      // prepend to cxq
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4392
         // prepend to cxq
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4393
         if (List == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4394
             iterator->_next = iterator->_prev = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4395
             _EntryList = iterator ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4396
         } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4397
            iterator->TState = ObjectWaiter::TS_CXQ ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4398
            for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4399
                ObjectWaiter * Front = _cxq ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4400
                iterator->_next = Front ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4401
                if (Atomic::cmpxchg_ptr (iterator, &_cxq, Front) == Front) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4402
                    break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4403
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4404
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4405
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4406
     } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4407
     if (Policy == 3) {      // append to cxq
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4408
        iterator->TState = ObjectWaiter::TS_CXQ ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4409
        for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4410
            ObjectWaiter * Tail ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4411
            Tail = _cxq ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4412
            if (Tail == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4413
                iterator->_next = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4414
                if (Atomic::cmpxchg_ptr (iterator, &_cxq, NULL) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4415
                   break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4416
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4417
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4418
                while (Tail->_next != NULL) Tail = Tail->_next ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4419
                Tail->_next = iterator ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4420
                iterator->_prev = Tail ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4421
                iterator->_next = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4422
                break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4423
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4424
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4425
     } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4426
        ParkEvent * ev = iterator->_event ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4427
        iterator->TState = ObjectWaiter::TS_RUN ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4428
        OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4429
        ev->unpark() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4430
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4431
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4432
     if (Policy < 4) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4433
       iterator->wait_reenter_begin(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4434
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4435
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4436
     // _WaitSetLock protects the wait queue, not the EntryList.  We could
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4437
     // move the add-to-EntryList operation, above, outside the critical section
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4438
     // protected by _WaitSetLock.  In practice that's not useful.  With the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4439
     // exception of  wait() timeouts and interrupts the monitor owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4440
     // is the only thread that grabs _WaitSetLock.  There's almost no contention
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4441
     // on _WaitSetLock so it's not profitable to reduce the length of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4442
     // critical section.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4443
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4444
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4445
  Thread::SpinRelease (&_WaitSetLock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4446
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4447
  if (iterator != NULL && ObjectSynchronizer::_sync_Notifications != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4448
     ObjectSynchronizer::_sync_Notifications->inc() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4449
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4450
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4451
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4452
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4453
void ObjectMonitor::notifyAll(TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4454
  CHECK_OWNER();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4455
  ObjectWaiter* iterator;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4456
  if (_WaitSet == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4457
      TEVENT (Empty-NotifyAll) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4458
      return ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4459
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4460
  DTRACE_MONITOR_PROBE(notifyAll, this, object(), THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4461
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4462
  int Policy = Knob_MoveNotifyee ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4463
  int Tally = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4464
  Thread::SpinAcquire (&_WaitSetLock, "WaitSet - notifyall") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4465
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4466
  for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4467
     iterator = DequeueWaiter () ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4468
     if (iterator == NULL) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4469
     TEVENT (NotifyAll - Transfer1) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4470
     ++Tally ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4471
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4472
     // Disposition - what might we do with iterator ?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4473
     // a.  add it directly to the EntryList - either tail or head.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4474
     // b.  push it onto the front of the _cxq.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4475
     // For now we use (a).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4476
     //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4477
     // TODO-FIXME: currently notifyAll() transfers the waiters one-at-a-time from the waitset
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4478
     // to the EntryList.  This could be done more efficiently with a single bulk transfer,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4479
     // but in practice it's not time-critical.  Beware too, that in prepend-mode we invert the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4480
     // order of the waiters.  Lets say that the waitset is "ABCD" and the EntryList is "XYZ".
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4481
     // After a notifyAll() in prepend mode the waitset will be empty and the EntryList will
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4482
     // be "DCBAXYZ".
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4483
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4484
     guarantee (iterator->TState == ObjectWaiter::TS_WAIT, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4485
     guarantee (iterator->_notified == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4486
     iterator->_notified = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4487
     if (Policy != 4) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4488
        iterator->TState = ObjectWaiter::TS_ENTER ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4489
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4490
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4491
     ObjectWaiter * List = _EntryList ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4492
     if (List != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4493
        assert (List->_prev == NULL, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4494
        assert (List->TState == ObjectWaiter::TS_ENTER, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4495
        assert (List != iterator, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4496
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4497
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4498
     if (Policy == 0) {       // prepend to EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4499
         if (List == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4500
             iterator->_next = iterator->_prev = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4501
             _EntryList = iterator ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4502
         } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4503
             List->_prev = iterator ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4504
             iterator->_next = List ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4505
             iterator->_prev = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4506
             _EntryList = iterator ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4507
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4508
     } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4509
     if (Policy == 1) {      // append to EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4510
         if (List == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4511
             iterator->_next = iterator->_prev = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4512
             _EntryList = iterator ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4513
         } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4514
            // CONSIDER:  finding the tail currently requires a linear-time walk of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4515
            // the EntryList.  We can make tail access constant-time by converting to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4516
            // a CDLL instead of using our current DLL.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4517
            ObjectWaiter * Tail ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4518
            for (Tail = List ; Tail->_next != NULL ; Tail = Tail->_next) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4519
            assert (Tail != NULL && Tail->_next == NULL, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4520
            Tail->_next = iterator ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4521
            iterator->_prev = Tail ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4522
            iterator->_next = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4523
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4524
     } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4525
     if (Policy == 2) {      // prepend to cxq
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4526
         // prepend to cxq
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4527
         iterator->TState = ObjectWaiter::TS_CXQ ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4528
         for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4529
             ObjectWaiter * Front = _cxq ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4530
             iterator->_next = Front ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4531
             if (Atomic::cmpxchg_ptr (iterator, &_cxq, Front) == Front) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4532
                 break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4533
             }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4534
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4535
     } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4536
     if (Policy == 3) {      // append to cxq
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4537
        iterator->TState = ObjectWaiter::TS_CXQ ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4538
        for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4539
            ObjectWaiter * Tail ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4540
            Tail = _cxq ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4541
            if (Tail == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4542
                iterator->_next = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4543
                if (Atomic::cmpxchg_ptr (iterator, &_cxq, NULL) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4544
                   break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4545
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4546
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4547
                while (Tail->_next != NULL) Tail = Tail->_next ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4548
                Tail->_next = iterator ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4549
                iterator->_prev = Tail ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4550
                iterator->_next = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4551
                break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4552
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4553
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4554
     } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4555
        ParkEvent * ev = iterator->_event ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4556
        iterator->TState = ObjectWaiter::TS_RUN ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4557
        OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4558
        ev->unpark() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4559
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4560
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4561
     if (Policy < 4) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4562
       iterator->wait_reenter_begin(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4563
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4564
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4565
     // _WaitSetLock protects the wait queue, not the EntryList.  We could
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4566
     // move the add-to-EntryList operation, above, outside the critical section
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4567
     // protected by _WaitSetLock.  In practice that's not useful.  With the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4568
     // exception of  wait() timeouts and interrupts the monitor owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4569
     // is the only thread that grabs _WaitSetLock.  There's almost no contention
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4570
     // on _WaitSetLock so it's not profitable to reduce the length of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4571
     // critical section.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4572
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4573
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4574
  Thread::SpinRelease (&_WaitSetLock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4575
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4576
  if (Tally != 0 && ObjectSynchronizer::_sync_Notifications != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4577
     ObjectSynchronizer::_sync_Notifications->inc(Tally) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4578
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4579
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4580
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4581
// check_slow() is a misnomer.  It's called to simply to throw an IMSX exception.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4582
// TODO-FIXME: remove check_slow() -- it's likely dead.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4583
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4584
void ObjectMonitor::check_slow(TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4585
  TEVENT (check_slow - throw IMSX) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4586
  assert(THREAD != _owner && !THREAD->is_lock_owned((address) _owner), "must not be owner");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4587
  THROW_MSG(vmSymbols::java_lang_IllegalMonitorStateException(), "current thread not owner");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4588
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4589
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4590
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4591
// -------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4592
// The raw monitor subsystem is entirely distinct from normal
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4593
// java-synchronization or jni-synchronization.  raw monitors are not
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4594
// associated with objects.  They can be implemented in any manner
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4595
// that makes sense.  The original implementors decided to piggy-back
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4596
// the raw-monitor implementation on the existing Java objectMonitor mechanism.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4597
// This flaw needs to fixed.  We should reimplement raw monitors as sui-generis.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4598
// Specifically, we should not implement raw monitors via java monitors.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4599
// Time permitting, we should disentangle and deconvolve the two implementations
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4600
// and move the resulting raw monitor implementation over to the JVMTI directories.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4601
// Ideally, the raw monitor implementation would be built on top of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4602
// park-unpark and nothing else.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4603
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4604
// raw monitors are used mainly by JVMTI
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4605
// The raw monitor implementation borrows the ObjectMonitor structure,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4606
// but the operators are degenerate and extremely simple.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4607
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4608
// Mixed use of a single objectMonitor instance -- as both a raw monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4609
// and a normal java monitor -- is not permissible.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4610
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4611
// Note that we use the single RawMonitor_lock to protect queue operations for
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4612
// _all_ raw monitors.  This is a scalability impediment, but since raw monitor usage
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4613
// is deprecated and rare, this is not of concern.  The RawMonitor_lock can not
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4614
// be held indefinitely.  The critical sections must be short and bounded.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4615
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4616
// -------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4617
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4618
int ObjectMonitor::SimpleEnter (Thread * Self) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4619
  for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4620
    if (Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4621
       return OS_OK ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4622
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4623
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4624
    ObjectWaiter Node (Self) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4625
    Self->_ParkEvent->reset() ;     // strictly optional
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4626
    Node.TState = ObjectWaiter::TS_ENTER ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4627
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4628
    RawMonitor_lock->lock_without_safepoint_check() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4629
    Node._next  = _EntryList ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4630
    _EntryList  = &Node ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4631
    OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4632
    if (_owner == NULL && Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4633
        _EntryList = Node._next ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4634
        RawMonitor_lock->unlock() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4635
        return OS_OK ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4636
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4637
    RawMonitor_lock->unlock() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4638
    while (Node.TState == ObjectWaiter::TS_ENTER) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4639
       Self->_ParkEvent->park() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4640
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4641
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4642
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4643
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4644
int ObjectMonitor::SimpleExit (Thread * Self) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4645
  guarantee (_owner == Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4646
  OrderAccess::release_store_ptr (&_owner, NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4647
  OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4648
  if (_EntryList == NULL) return OS_OK ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4649
  ObjectWaiter * w ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4650
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4651
  RawMonitor_lock->lock_without_safepoint_check() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4652
  w = _EntryList ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4653
  if (w != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4654
      _EntryList = w->_next ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4655
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4656
  RawMonitor_lock->unlock() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4657
  if (w != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4658
      guarantee (w ->TState == ObjectWaiter::TS_ENTER, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4659
      ParkEvent * ev = w->_event ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4660
      w->TState = ObjectWaiter::TS_RUN ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4661
      OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4662
      ev->unpark() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4663
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4664
  return OS_OK ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4665
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4666
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4667
int ObjectMonitor::SimpleWait (Thread * Self, jlong millis) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4668
  guarantee (_owner == Self  , "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4669
  guarantee (_recursions == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4670
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4671
  ObjectWaiter Node (Self) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4672
  Node._notified = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4673
  Node.TState    = ObjectWaiter::TS_WAIT ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4674
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4675
  RawMonitor_lock->lock_without_safepoint_check() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4676
  Node._next     = _WaitSet ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4677
  _WaitSet       = &Node ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4678
  RawMonitor_lock->unlock() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4679
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4680
  SimpleExit (Self) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4681
  guarantee (_owner != Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4682
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4683
  int ret = OS_OK ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4684
  if (millis <= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4685
    Self->_ParkEvent->park();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4686
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4687
    ret = Self->_ParkEvent->park(millis);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4688
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4689
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4690
  // If thread still resides on the waitset then unlink it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4691
  // Double-checked locking -- the usage is safe in this context
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4692
  // as we TState is volatile and the lock-unlock operators are
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4693
  // serializing (barrier-equivalent).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4694
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4695
  if (Node.TState == ObjectWaiter::TS_WAIT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4696
    RawMonitor_lock->lock_without_safepoint_check() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4697
    if (Node.TState == ObjectWaiter::TS_WAIT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4698
      // Simple O(n) unlink, but performance isn't critical here.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4699
      ObjectWaiter * p ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4700
      ObjectWaiter * q = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4701
      for (p = _WaitSet ; p != &Node; p = p->_next) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4702
         q = p ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4703
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4704
      guarantee (p == &Node, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4705
      if (q == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4706
        guarantee (p == _WaitSet, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4707
        _WaitSet = p->_next ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4708
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4709
        guarantee (p == q->_next, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4710
        q->_next = p->_next ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4711
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4712
      Node.TState = ObjectWaiter::TS_RUN ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4713
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4714
    RawMonitor_lock->unlock() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4715
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4716
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4717
  guarantee (Node.TState == ObjectWaiter::TS_RUN, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4718
  SimpleEnter (Self) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4719
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4720
  guarantee (_owner == Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4721
  guarantee (_recursions == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4722
  return ret ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4723
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4724
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4725
int ObjectMonitor::SimpleNotify (Thread * Self, bool All) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4726
  guarantee (_owner == Self, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4727
  if (_WaitSet == NULL) return OS_OK ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4728
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4729
  // We have two options:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4730
  // A. Transfer the threads from the WaitSet to the EntryList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4731
  // B. Remove the thread from the WaitSet and unpark() it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4732
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4733
  // We use (B), which is crude and results in lots of futile
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4734
  // context switching.  In particular (B) induces lots of contention.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4735
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4736
  ParkEvent * ev = NULL ;       // consider using a small auto array ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4737
  RawMonitor_lock->lock_without_safepoint_check() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4738
  for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4739
      ObjectWaiter * w = _WaitSet ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4740
      if (w == NULL) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4741
      _WaitSet = w->_next ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4742
      if (ev != NULL) { ev->unpark(); ev = NULL; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4743
      ev = w->_event ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4744
      OrderAccess::loadstore() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4745
      w->TState = ObjectWaiter::TS_RUN ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4746
      OrderAccess::storeload();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4747
      if (!All) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4748
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4749
  RawMonitor_lock->unlock() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4750
  if (ev != NULL) ev->unpark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4751
  return OS_OK ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4752
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4753
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4754
// Any JavaThread will enter here with state _thread_blocked
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4755
int ObjectMonitor::raw_enter(TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4756
  TEVENT (raw_enter) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4757
  void * Contended ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4758
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4759
  // don't enter raw monitor if thread is being externally suspended, it will
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4760
  // surprise the suspender if a "suspended" thread can still enter monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4761
  JavaThread * jt = (JavaThread *)THREAD;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4762
  if (THREAD->is_Java_thread()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4763
    jt->SR_lock()->lock_without_safepoint_check();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4764
    while (jt->is_external_suspend()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4765
      jt->SR_lock()->unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4766
      jt->java_suspend_self();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4767
      jt->SR_lock()->lock_without_safepoint_check();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4768
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4769
    // guarded by SR_lock to avoid racing with new external suspend requests.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4770
    Contended = Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4771
    jt->SR_lock()->unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4772
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4773
    Contended = Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4774
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4775
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4776
  if (Contended == THREAD) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4777
     _recursions ++ ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4778
     return OM_OK ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4779
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4780
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4781
  if (Contended == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4782
     guarantee (_owner == THREAD, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4783
     guarantee (_recursions == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4784
     return OM_OK ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4785
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4786
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4787
  THREAD->set_current_pending_monitor(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4788
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4789
  if (!THREAD->is_Java_thread()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4790
     // No other non-Java threads besides VM thread would acquire
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4791
     // a raw monitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4792
     assert(THREAD->is_VM_thread(), "must be VM thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4793
     SimpleEnter (THREAD) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4794
   } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4795
     guarantee (jt->thread_state() == _thread_blocked, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4796
     for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4797
       jt->set_suspend_equivalent();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4798
       // cleared by handle_special_suspend_equivalent_condition() or
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4799
       // java_suspend_self()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4800
       SimpleEnter (THREAD) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4801
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4802
       // were we externally suspended while we were waiting?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4803
       if (!jt->handle_special_suspend_equivalent_condition()) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4804
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4805
       // This thread was externally suspended
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4806
       //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4807
       // This logic isn't needed for JVMTI raw monitors,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4808
       // but doesn't hurt just in case the suspend rules change. This
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4809
           // logic is needed for the ObjectMonitor.wait() reentry phase.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4810
           // We have reentered the contended monitor, but while we were
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4811
           // waiting another thread suspended us. We don't want to reenter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4812
           // the monitor while suspended because that would surprise the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4813
           // thread that suspended us.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4814
           //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4815
           // Drop the lock -
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4816
       SimpleExit (THREAD) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4817
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4818
           jt->java_suspend_self();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4819
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4820
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4821
     assert(_owner == THREAD, "Fatal error with monitor owner!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4822
     assert(_recursions == 0, "Fatal error with monitor recursions!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4823
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4824
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4825
  THREAD->set_current_pending_monitor(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4826
  guarantee (_recursions == 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4827
  return OM_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4828
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4829
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4830
// Used mainly for JVMTI raw monitor implementation
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4831
// Also used for ObjectMonitor::wait().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4832
int ObjectMonitor::raw_exit(TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4833
  TEVENT (raw_exit) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4834
  if (THREAD != _owner) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4835
    return OM_ILLEGAL_MONITOR_STATE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4836
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4837
  if (_recursions > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4838
    --_recursions ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4839
    return OM_OK ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4840
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4841
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4842
  void * List = _EntryList ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4843
  SimpleExit (THREAD) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4844
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4845
  return OM_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4846
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4847
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4848
// Used for JVMTI raw monitor implementation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4849
// All JavaThreads will enter here with state _thread_blocked
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4850
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4851
int ObjectMonitor::raw_wait(jlong millis, bool interruptible, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4852
  TEVENT (raw_wait) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4853
  if (THREAD != _owner) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4854
    return OM_ILLEGAL_MONITOR_STATE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4855
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4856
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4857
  // To avoid spurious wakeups we reset the parkevent -- This is strictly optional.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4858
  // The caller must be able to tolerate spurious returns from raw_wait().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4859
  THREAD->_ParkEvent->reset() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4860
  OrderAccess::fence() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4861
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4862
  // check interrupt event
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4863
  if (interruptible && Thread::is_interrupted(THREAD, true)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4864
    return OM_INTERRUPTED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4865
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4866
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4867
  intptr_t save = _recursions ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4868
  _recursions = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4869
  _waiters ++ ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4870
  if (THREAD->is_Java_thread()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4871
    guarantee (((JavaThread *) THREAD)->thread_state() == _thread_blocked, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4872
    ((JavaThread *)THREAD)->set_suspend_equivalent();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4873
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4874
  int rv = SimpleWait (THREAD, millis) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4875
  _recursions = save ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4876
  _waiters -- ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4877
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4878
  guarantee (THREAD == _owner, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4879
  if (THREAD->is_Java_thread()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4880
     JavaThread * jSelf = (JavaThread *) THREAD ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4881
     for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4882
        if (!jSelf->handle_special_suspend_equivalent_condition()) break ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4883
        SimpleExit (THREAD) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4884
        jSelf->java_suspend_self();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4885
        SimpleEnter (THREAD) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4886
        jSelf->set_suspend_equivalent() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4887
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4888
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4889
  guarantee (THREAD == _owner, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4890
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4891
  if (interruptible && Thread::is_interrupted(THREAD, true)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4892
    return OM_INTERRUPTED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4893
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4894
  return OM_OK ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4895
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4896
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4897
int ObjectMonitor::raw_notify(TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4898
  TEVENT (raw_notify) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4899
  if (THREAD != _owner) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4900
    return OM_ILLEGAL_MONITOR_STATE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4901
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4902
  SimpleNotify (THREAD, false) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4903
  return OM_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4904
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4905
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4906
int ObjectMonitor::raw_notifyAll(TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4907
  TEVENT (raw_notifyAll) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4908
  if (THREAD != _owner) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4909
    return OM_ILLEGAL_MONITOR_STATE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4910
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4911
  SimpleNotify (THREAD, true) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4912
  return OM_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4913
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4914
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4915
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4916
void ObjectMonitor::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4917
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4918
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4919
void ObjectMonitor::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4920
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4921
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4922
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4923
//------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4924
// Non-product code
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4925
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4926
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4927
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4928
void ObjectSynchronizer::trace_locking(Handle locking_obj, bool is_compiled,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4929
                                       bool is_method, bool is_locking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4930
  // Don't know what to do here
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4931
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4932
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4933
// Verify all monitors in the monitor cache, the verification is weak.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4934
void ObjectSynchronizer::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4935
  ObjectMonitor* block = gBlockList;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4936
  ObjectMonitor* mid;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4937
  while (block) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4938
    assert(block->object() == CHAINMARKER, "must be a block header");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4939
    for (int i = 1; i < _BLOCKSIZE; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4940
      mid = block + i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4941
      oop object = (oop) mid->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4942
      if (object != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4943
        mid->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4944
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4945
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4946
    block = (ObjectMonitor*) block->FreeNext;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4947
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4948
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4949
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4950
// Check if monitor belongs to the monitor cache
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4951
// The list is grow-only so it's *relatively* safe to traverse
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4952
// the list of extant blocks without taking a lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4953
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4954
int ObjectSynchronizer::verify_objmon_isinpool(ObjectMonitor *monitor) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4955
  ObjectMonitor* block = gBlockList;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4956
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4957
  while (block) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4958
    assert(block->object() == CHAINMARKER, "must be a block header");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4959
    if (monitor > &block[0] && monitor < &block[_BLOCKSIZE]) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4960
      address mon = (address) monitor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4961
      address blk = (address) block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4962
      size_t diff = mon - blk;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4963
      assert((diff % sizeof(ObjectMonitor)) == 0, "check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4964
      return 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4965
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4966
    block = (ObjectMonitor*) block->FreeNext;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4967
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4968
  return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4969
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4970
489c9b5090e2 Initial load
duke
parents:
diff changeset
  4971
#endif