hotspot/src/share/vm/runtime/synchronizer.cpp
author dcubed
Thu, 18 Jul 2013 12:35:55 -0700
changeset 18945 1225c36dacd3
parent 18025 b7bcf7497f93
child 20282 7f9cbdf89af2
child 22819 f88b9c394e42
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
15233
c06b129cf6c7 6444286: Possible naked oop related to biased locking revocation safepoint in jni_exit()
dcubed
parents: 14583
diff changeset
     2
 * Copyright (c) 1998, 2013, 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
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    26
#include "classfile/vmSymbols.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    27
#include "memory/resourceArea.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    28
#include "oops/markOop.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    29
#include "oops/oop.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    30
#include "runtime/biasedLocking.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    31
#include "runtime/handles.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    32
#include "runtime/interfaceSupport.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    33
#include "runtime/mutexLocker.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    34
#include "runtime/objectMonitor.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    35
#include "runtime/objectMonitor.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    36
#include "runtime/osThread.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    37
#include "runtime/stubRoutines.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    38
#include "runtime/synchronizer.hpp"
14583
d70ee55535f4 8003935: Simplify the needed includes for using Thread::current()
stefank
parents: 14488
diff changeset
    39
#include "runtime/thread.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    40
#include "utilities/dtrace.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    41
#include "utilities/events.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    42
#include "utilities/preserveException.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    43
#ifdef TARGET_OS_FAMILY_linux
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    44
# include "os_linux.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    45
#endif
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    46
#ifdef TARGET_OS_FAMILY_solaris
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    47
# include "os_solaris.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    48
#endif
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    49
#ifdef TARGET_OS_FAMILY_windows
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    50
# include "os_windows.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    51
#endif
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents: 8921
diff changeset
    52
#ifdef TARGET_OS_FAMILY_bsd
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents: 8921
diff changeset
    53
# include "os_bsd.inline.hpp"
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents: 8921
diff changeset
    54
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
15475
73896d91270c 6518907: cleanup IA64 specific code in Hotspot
morris
parents: 15233
diff changeset
    56
#if defined(__GNUC__)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  // Need to inhibit inlining for older versions of GCC to avoid build-time failures
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  #define ATTR __attribute__((noinline))
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  #define ATTR
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
// The "core" versions of monitor enter and exit reside in this file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
// The interpreter and compilers contain specialized transliterated
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
// variants of the enter-exit fast-path operations.  See i486.ad fast_lock(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
// for instance.  If you make changes here, make sure to modify the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
// interpreter, and both C1 and C2 fast-path inline locking code emission.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
// -----------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
#ifdef DTRACE_ENABLED
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
// Only bother with this argument setup if dtrace is available
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
// TODO-FIXME: probes should not fire when caller is _blocked.  assert() accordingly.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 10739
diff changeset
    77
#define DTRACE_MONITOR_PROBE_COMMON(obj, thread)                           \
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  char* bytes = NULL;                                                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  int len = 0;                                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  jlong jtid = SharedRuntime::get_java_tid(thread);                        \
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 10739
diff changeset
    81
  Symbol* klassname = ((oop)(obj))->klass()->name();                       \
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  if (klassname != NULL) {                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
    bytes = (char*)klassname->bytes();                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
    len = klassname->utf8_length();                                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
    87
#ifndef USDT2
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
    88
HS_DTRACE_PROBE_DECL5(hotspot, monitor__wait,
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
    89
  jlong, uintptr_t, char*, int, long);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
    90
HS_DTRACE_PROBE_DECL4(hotspot, monitor__waited,
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
    91
  jlong, uintptr_t, char*, int);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
    92
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 10739
diff changeset
    93
#define DTRACE_MONITOR_WAIT_PROBE(monitor, obj, thread, millis)            \
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  {                                                                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    if (DTraceMonitorProbes) {                                            \
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 10739
diff changeset
    96
      DTRACE_MONITOR_PROBE_COMMON(obj, thread);                            \
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
      HS_DTRACE_PROBE5(hotspot, monitor__wait, jtid,                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
                       (monitor), bytes, len, (millis));                   \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    }                                                                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 10739
diff changeset
   102
#define DTRACE_MONITOR_PROBE(probe, monitor, obj, thread)                  \
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  {                                                                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    if (DTraceMonitorProbes) {                                            \
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 10739
diff changeset
   105
      DTRACE_MONITOR_PROBE_COMMON(obj, thread);                            \
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
      HS_DTRACE_PROBE4(hotspot, monitor__##probe, jtid,                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
                       (uintptr_t)(monitor), bytes, len);                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    }                                                                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   111
#else /* USDT2 */
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   112
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 10739
diff changeset
   113
#define DTRACE_MONITOR_WAIT_PROBE(monitor, obj, thread, millis)            \
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   114
  {                                                                        \
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   115
    if (DTraceMonitorProbes) {                                            \
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 10739
diff changeset
   116
      DTRACE_MONITOR_PROBE_COMMON(obj, thread);                            \
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   117
      HOTSPOT_MONITOR_WAIT(jtid,                                           \
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   118
                           (uintptr_t)(monitor), bytes, len, (millis));  \
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   119
    }                                                                      \
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   120
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   121
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   122
#define HOTSPOT_MONITOR_PROBE_waited HOTSPOT_MONITOR_PROBE_WAITED
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   123
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 10739
diff changeset
   124
#define DTRACE_MONITOR_PROBE(probe, monitor, obj, thread)                  \
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   125
  {                                                                        \
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   126
    if (DTraceMonitorProbes) {                                            \
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 10739
diff changeset
   127
      DTRACE_MONITOR_PROBE_COMMON(obj, thread);                            \
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   128
      HOTSPOT_MONITOR_PROBE_##probe(jtid, /* probe = waited */             \
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   129
                       (uintptr_t)(monitor), bytes, len);                  \
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   130
    }                                                                      \
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   131
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   132
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   133
#endif /* USDT2 */
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
#else //  ndef DTRACE_ENABLED
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 10739
diff changeset
   136
#define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon)    {;}
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 10739
diff changeset
   137
#define DTRACE_MONITOR_PROBE(probe, obj, thread, mon)          {;}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
#endif // ndef DTRACE_ENABLED
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   141
// This exists only as a workaround of dtrace bug 6254741
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   142
int dtrace_waited_probe(ObjectMonitor* monitor, Handle obj, Thread* thr) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   143
  DTRACE_MONITOR_PROBE(waited, monitor, obj(), thr);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   144
  return 0;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   145
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   146
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   147
#define NINFLATIONLOCKS 256
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   148
static volatile intptr_t InflationLocks [NINFLATIONLOCKS] ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   149
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   150
ObjectMonitor * ObjectSynchronizer::gBlockList = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   151
ObjectMonitor * volatile ObjectSynchronizer::gFreeList  = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   152
ObjectMonitor * volatile ObjectSynchronizer::gOmInUseList  = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   153
int ObjectSynchronizer::gOmInUseCount = 0;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   154
static volatile intptr_t ListLock = 0 ;      // protects global monitor free-list cache
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   155
static volatile int MonitorFreeCount  = 0 ;      // # on gFreeList
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   156
static volatile int MonitorPopulation = 0 ;      // # Extant -- in circulation
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   157
#define CHAINMARKER ((oop)-1)
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   158
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   159
// -----------------------------------------------------------------------------
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   160
//  Fast Monitor Enter/Exit
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   161
// This the fast monitor enter. The interpreter and compiler use
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   162
// some assembly copies of this code. Make sure update those code
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   163
// if the following function is changed. The implementation is
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   164
// extremely sensitive to race condition. Be careful.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   165
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   166
void ObjectSynchronizer::fast_enter(Handle obj, BasicLock* lock, bool attempt_rebias, TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   167
 if (UseBiasedLocking) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   168
    if (!SafepointSynchronize::is_at_safepoint()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   169
      BiasedLocking::Condition cond = BiasedLocking::revoke_and_rebias(obj, attempt_rebias, THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   170
      if (cond == BiasedLocking::BIAS_REVOKED_AND_REBIASED) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   171
        return;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   172
      }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   173
    } else {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   174
      assert(!attempt_rebias, "can not rebias toward VM thread");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   175
      BiasedLocking::revoke_at_safepoint(obj);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   176
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   177
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   178
 }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   179
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   180
 slow_enter (obj, lock, THREAD) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   181
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   182
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   183
void ObjectSynchronizer::fast_exit(oop object, BasicLock* lock, TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   184
  assert(!object->mark()->has_bias_pattern(), "should not see bias pattern here");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   185
  // if displaced header is null, the previous enter is recursive enter, no-op
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   186
  markOop dhw = lock->displaced_header();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   187
  markOop mark ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   188
  if (dhw == NULL) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   189
     // Recursive stack-lock.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   190
     // Diagnostics -- Could be: stack-locked, inflating, inflated.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   191
     mark = object->mark() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   192
     assert (!mark->is_neutral(), "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   193
     if (mark->has_locker() && mark != markOopDesc::INFLATING()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   194
        assert(THREAD->is_lock_owned((address)mark->locker()), "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   195
     }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   196
     if (mark->has_monitor()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   197
        ObjectMonitor * m = mark->monitor() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   198
        assert(((oop)(m->object()))->mark() == mark, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   199
        assert(m->is_entered(THREAD), "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   200
     }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   201
     return ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   202
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   203
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   204
  mark = object->mark() ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   206
  // If the object is stack-locked by the current thread, try to
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   207
  // swing the displaced header from the box back to the mark.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   208
  if (mark == (markOop) lock) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   209
     assert (dhw->is_neutral(), "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   210
     if ((markOop) Atomic::cmpxchg_ptr (dhw, object->mark_addr(), mark) == mark) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   211
        TEVENT (fast_exit: release stacklock) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   212
        return;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   213
     }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   214
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   215
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17087
diff changeset
   216
  ObjectSynchronizer::inflate(THREAD, object)->exit (true, THREAD) ;
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   217
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   218
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   219
// -----------------------------------------------------------------------------
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   220
// Interpreter/Compiler Slow Case
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   221
// This routine is used to handle interpreter/compiler slow case
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   222
// We don't need to use fast path here, because it must have been
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   223
// failed in the interpreter/compiler code.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   224
void ObjectSynchronizer::slow_enter(Handle obj, BasicLock* lock, TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   225
  markOop mark = obj->mark();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   226
  assert(!mark->has_bias_pattern(), "should not see bias pattern here");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   227
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   228
  if (mark->is_neutral()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   229
    // Anticipate successful CAS -- the ST of the displaced mark must
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   230
    // be visible <= the ST performed by the CAS.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   231
    lock->set_displaced_header(mark);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   232
    if (mark == (markOop) Atomic::cmpxchg_ptr(lock, obj()->mark_addr(), mark)) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   233
      TEVENT (slow_enter: release stacklock) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   234
      return ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   235
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   236
    // Fall through to inflate() ...
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   237
  } else
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   238
  if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   239
    assert(lock != mark->locker(), "must not re-lock the same lock");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   240
    assert(lock != (BasicLock*)obj->mark(), "don't relock with same BasicLock");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   241
    lock->set_displaced_header(NULL);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   242
    return;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   243
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   244
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   245
#if 0
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   246
  // The following optimization isn't particularly useful.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   247
  if (mark->has_monitor() && mark->monitor()->is_entered(THREAD)) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   248
    lock->set_displaced_header (NULL) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   249
    return ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   250
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   251
#endif
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   252
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   253
  // The object header will never be displaced to this lock,
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   254
  // so it does not matter what the value is, except that it
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   255
  // must be non-zero to avoid looking like a re-entrant lock,
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   256
  // and must not look locked either.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   257
  lock->set_displaced_header(markOopDesc::unused_mark());
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   258
  ObjectSynchronizer::inflate(THREAD, obj())->enter(THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   259
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   260
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   261
// This routine is used to handle interpreter/compiler slow case
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   262
// We don't need to use fast path here, because it must have
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   263
// failed in the interpreter/compiler code. Simply use the heavy
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   264
// weight monitor should be ok, unless someone find otherwise.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   265
void ObjectSynchronizer::slow_exit(oop object, BasicLock* lock, TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   266
  fast_exit (object, lock, THREAD) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   267
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   268
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   269
// -----------------------------------------------------------------------------
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   270
// Class Loader  support to workaround deadlocks on the class loader lock objects
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   271
// Also used by GC
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   272
// complete_exit()/reenter() are used to wait on a nested lock
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   273
// i.e. to give up an outer lock completely and then re-enter
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   274
// Used when holding nested locks - lock acquisition order: lock1 then lock2
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   275
//  1) complete_exit lock1 - saving recursion count
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   276
//  2) wait on lock2
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   277
//  3) when notified on lock2, unlock lock2
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   278
//  4) reenter lock1 with original recursion count
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   279
//  5) lock lock2
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   280
// NOTE: must use heavy weight monitor to handle complete_exit/reenter()
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   281
intptr_t ObjectSynchronizer::complete_exit(Handle obj, TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   282
  TEVENT (complete_exit) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   283
  if (UseBiasedLocking) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   284
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   285
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   288
  ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj());
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   289
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   290
  return monitor->complete_exit(THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   291
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   292
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   293
// NOTE: must use heavy weight monitor to handle complete_exit/reenter()
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   294
void ObjectSynchronizer::reenter(Handle obj, intptr_t recursion, TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   295
  TEVENT (reenter) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   296
  if (UseBiasedLocking) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   297
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   298
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   299
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   300
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   301
  ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj());
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   302
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   303
  monitor->reenter(recursion, THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   304
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   305
// -----------------------------------------------------------------------------
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   306
// JNI locks on java objects
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   307
// NOTE: must use heavy weight monitor to handle jni monitor enter
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   308
void ObjectSynchronizer::jni_enter(Handle obj, TRAPS) { // possible entry from jni enter
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   309
  // the current locking is from JNI instead of Java code
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   310
  TEVENT (jni_enter) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   311
  if (UseBiasedLocking) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   312
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   313
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   314
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   315
  THREAD->set_current_pending_monitor_is_from_java(false);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   316
  ObjectSynchronizer::inflate(THREAD, obj())->enter(THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   317
  THREAD->set_current_pending_monitor_is_from_java(true);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   318
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   319
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   320
// NOTE: must use heavy weight monitor to handle jni monitor enter
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   321
bool ObjectSynchronizer::jni_try_enter(Handle obj, Thread* THREAD) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   322
  if (UseBiasedLocking) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   323
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   324
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   327
  ObjectMonitor* monitor = ObjectSynchronizer::inflate_helper(obj());
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   328
  return monitor->try_enter(THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   329
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   330
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   331
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   332
// NOTE: must use heavy weight monitor to handle jni monitor exit
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   333
void ObjectSynchronizer::jni_exit(oop obj, Thread* THREAD) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   334
  TEVENT (jni_exit) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   335
  if (UseBiasedLocking) {
15233
c06b129cf6c7 6444286: Possible naked oop related to biased locking revocation safepoint in jni_exit()
dcubed
parents: 14583
diff changeset
   336
    Handle h_obj(THREAD, obj);
c06b129cf6c7 6444286: Possible naked oop related to biased locking revocation safepoint in jni_exit()
dcubed
parents: 14583
diff changeset
   337
    BiasedLocking::revoke_and_rebias(h_obj, false, THREAD);
c06b129cf6c7 6444286: Possible naked oop related to biased locking revocation safepoint in jni_exit()
dcubed
parents: 14583
diff changeset
   338
    obj = h_obj();
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   339
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   340
  assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   341
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   342
  ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   343
  // If this thread has locked the object, exit the monitor.  Note:  can't use
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   344
  // monitor->check(CHECK); must exit even if an exception is pending.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   345
  if (monitor->check(THREAD)) {
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17087
diff changeset
   346
     monitor->exit(true, THREAD);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  }
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   348
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   349
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   350
// -----------------------------------------------------------------------------
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   351
// Internal VM locks on java objects
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   352
// standard constructor, allows locking failures
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   353
ObjectLocker::ObjectLocker(Handle obj, Thread* thread, bool doLock) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   354
  _dolock = doLock;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   355
  _thread = thread;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   356
  debug_only(if (StrictSafepointChecks) _thread->check_for_valid_safepoint_state(false);)
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   357
  _obj = obj;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   359
  if (_dolock) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   360
    TEVENT (ObjectLocker) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   361
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   362
    ObjectSynchronizer::fast_enter(_obj, &_lock, false, _thread);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   363
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   364
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   365
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   366
ObjectLocker::~ObjectLocker() {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   367
  if (_dolock) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   368
    ObjectSynchronizer::fast_exit(_obj(), &_lock, _thread);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   369
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   370
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   373
// -----------------------------------------------------------------------------
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   374
//  Wait/Notify/NotifyAll
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   375
// NOTE: must use heavy weight monitor to handle wait()
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   376
void ObjectSynchronizer::wait(Handle obj, jlong millis, TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   377
  if (UseBiasedLocking) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   378
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   379
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   380
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   381
  if (millis < 0) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   382
    TEVENT (wait - throw IAX) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   383
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   384
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   385
  ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj());
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   386
  DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), THREAD, millis);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   387
  monitor->wait(millis, true, THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   388
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   389
  /* This dummy call is in place to get around dtrace bug 6254741.  Once
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   390
     that's fixed we can uncomment the following line and remove the call */
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   391
  // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   392
  dtrace_waited_probe(monitor, obj, THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   393
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   395
void ObjectSynchronizer::waitUninterruptibly (Handle obj, jlong millis, TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   396
  if (UseBiasedLocking) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   397
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   398
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   399
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   400
  if (millis < 0) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   401
    TEVENT (wait - throw IAX) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   402
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   403
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   404
  ObjectSynchronizer::inflate(THREAD, obj()) -> wait(millis, false, THREAD) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   405
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   406
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   407
void ObjectSynchronizer::notify(Handle obj, TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   408
 if (UseBiasedLocking) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   409
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   410
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   411
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   413
  markOop mark = obj->mark();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   414
  if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   415
    return;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   416
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   417
  ObjectSynchronizer::inflate(THREAD, obj())->notify(THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   418
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   420
// NOTE: see comment of notify()
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   421
void ObjectSynchronizer::notifyall(Handle obj, TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   422
  if (UseBiasedLocking) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   423
    BiasedLocking::revoke_and_rebias(obj, false, THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   424
    assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   425
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   426
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   427
  markOop mark = obj->mark();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   428
  if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   429
    return;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   430
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   431
  ObjectSynchronizer::inflate(THREAD, obj())->notifyAll(THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   432
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   433
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   434
// -----------------------------------------------------------------------------
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   435
// Hash Code handling
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   436
//
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
// Performance concern:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
// OrderAccess::storestore() calls release() which STs 0 into the global volatile
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
// OrderAccess::Dummy variable.  This store is unnecessary for correctness.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
// Many threads STing into a common location causes considerable cache migration
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
// or "sloshing" on large SMP system.  As such, I avoid using OrderAccess::storestore()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
// until it's repaired.  In some cases OrderAccess::fence() -- which incurs local
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
// latency on the executing processor -- is a better choice as it scales on SMP
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
// systems.  See http://blogs.sun.com/dave/entry/biased_locking_in_hotspot for a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
// discussion of coherency costs.  Note that all our current reference platforms
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
// provide strong ST-ST order, so the issue is moot on IA32, x64, and SPARC.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
// As a general policy we use "volatile" to control compiler-based reordering
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
// and explicit fences (barriers) to control for architectural reordering performed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
// by the CPU(s) or platform.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
struct SharedGlobals {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
    // These are highly shared mostly-read variables.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
    // To avoid false-sharing they need to be the sole occupants of a $ line.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
    double padPrefix [8];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
    volatile int stwRandom ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
    volatile int stwCycle ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
    // Hot RW variables -- Sequester to avoid false-sharing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
    double padSuffix [16];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
    volatile int hcSequence ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
    double padFinal [8] ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
} ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
static SharedGlobals GVars ;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   466
static int MonitorScavengeThreshold = 1000000 ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   467
static volatile int ForceMonitorScavenge = 0 ; // Scavenge required and pending
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   469
static markOop ReadStableMark (oop obj) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   470
  markOop mark = obj->mark() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   471
  if (!mark->is_being_inflated()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   472
    return mark ;       // normal fast-path return
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   473
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   475
  int its = 0 ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   476
  for (;;) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   477
    markOop mark = obj->mark() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   478
    if (!mark->is_being_inflated()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   479
      return mark ;    // normal fast-path return
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   480
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   481
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   482
    // The object is being inflated by some other thread.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   483
    // The caller of ReadStableMark() must wait for inflation to complete.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   484
    // Avoid live-lock
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   485
    // TODO: consider calling SafepointSynchronize::do_call_back() while
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   486
    // spinning to see if there's a safepoint pending.  If so, immediately
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   487
    // yielding or blocking would be appropriate.  Avoid spinning while
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   488
    // there is a safepoint pending.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   489
    // TODO: add inflation contention performance counters.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   490
    // TODO: restrict the aggregate number of spinners.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   492
    ++its ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   493
    if (its > 10000 || !os::is_MP()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   494
       if (its & 1) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   495
         os::NakedYield() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   496
         TEVENT (Inflate: INFLATING - yield) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   497
       } else {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   498
         // Note that the following code attenuates the livelock problem but is not
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   499
         // a complete remedy.  A more complete solution would require that the inflating
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   500
         // thread hold the associated inflation lock.  The following code simply restricts
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   501
         // the number of spinners to at most one.  We'll have N-2 threads blocked
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   502
         // on the inflationlock, 1 thread holding the inflation lock and using
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   503
         // a yield/park strategy, and 1 thread in the midst of inflation.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   504
         // A more refined approach would be to change the encoding of INFLATING
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   505
         // to allow encapsulation of a native thread pointer.  Threads waiting for
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   506
         // inflation to complete would use CAS to push themselves onto a singly linked
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   507
         // list rooted at the markword.  Once enqueued, they'd loop, checking a per-thread flag
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   508
         // and calling park().  When inflation was complete the thread that accomplished inflation
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   509
         // would detach the list and set the markword to inflated with a single CAS and
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   510
         // then for each thread on the list, set the flag and unpark() the thread.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   511
         // This is conceptually similar to muxAcquire-muxRelease, except that muxRelease
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   512
         // wakes at most one thread whereas we need to wake the entire list.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   513
         int ix = (intptr_t(obj) >> 5) & (NINFLATIONLOCKS-1) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   514
         int YieldThenBlock = 0 ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   515
         assert (ix >= 0 && ix < NINFLATIONLOCKS, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   516
         assert ((NINFLATIONLOCKS & (NINFLATIONLOCKS-1)) == 0, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   517
         Thread::muxAcquire (InflationLocks + ix, "InflationLock") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   518
         while (obj->mark() == markOopDesc::INFLATING()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   519
           // Beware: NakedYield() is advisory and has almost no effect on some platforms
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   520
           // so we periodically call Self->_ParkEvent->park(1).
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   521
           // We use a mixed spin/yield/block mechanism.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   522
           if ((YieldThenBlock++) >= 16) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   523
              Thread::current()->_ParkEvent->park(1) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   524
           } else {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   525
              os::NakedYield() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   526
           }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   527
         }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   528
         Thread::muxRelease (InflationLocks + ix ) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   529
         TEVENT (Inflate: INFLATING - yield/park) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   530
       }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   531
    } else {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   532
       SpinPause() ;       // SMP-polite spinning
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   533
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   534
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   535
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
// hashCode() generation :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
// Possibilities:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
// * MD5Digest of {obj,stwRandom}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
// * CRC32 of {obj,stwRandom} or any linear-feedback shift register function.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
// * A DES- or AES-style SBox[] mechanism
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
// * One of the Phi-based schemes, such as:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
//   2654435761 = 2^32 * Phi (golden ratio)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
//   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stwRandom ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
// * A variation of Marsaglia's shift-xor RNG scheme.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
// * (obj ^ stwRandom) is appealing, but can result
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
//   in undesirable regularity in the hashCode values of adjacent objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
//   (objects allocated back-to-back, in particular).  This could potentially
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
//   result in hashtable collisions and reduced hashtable efficiency.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
//   There are simple ways to "diffuse" the middle address bits over the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
//   generated hashCode values:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
static inline intptr_t get_next_hash(Thread * Self, oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
  intptr_t value = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
  if (hashCode == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
     // This form uses an unguarded global Park-Miller RNG,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
     // so it's possible for two threads to race and generate the same RNG.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
     // On MP system we'll have lots of RW access to a global, so the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
     // mechanism induces lots of coherency traffic.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
     value = os::random() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
  } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
  if (hashCode == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
     // This variation has the property of being stable (idempotent)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
     // between STW operations.  This can be useful in some of the 1-0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
     // synchronization schemes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
     intptr_t addrBits = intptr_t(obj) >> 3 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
     value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
  } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
  if (hashCode == 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
     value = 1 ;            // for sensitivity testing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
  } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
  if (hashCode == 3) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
     value = ++GVars.hcSequence ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
  } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
  if (hashCode == 4) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
     value = intptr_t(obj) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
     // Marsaglia's xor-shift scheme with thread-specific state
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
     // This is probably the best overall implementation -- we'll
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
     // likely make this the default in future releases.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
     unsigned t = Self->_hashStateX ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
     t ^= (t << 11) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
     Self->_hashStateX = Self->_hashStateY ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
     Self->_hashStateY = Self->_hashStateZ ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
     Self->_hashStateZ = Self->_hashStateW ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
     unsigned v = Self->_hashStateW ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
     v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
     Self->_hashStateW = v ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
     value = v ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
  value &= markOopDesc::hash_mask;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
  if (value == 0) value = 0xBAD ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
  assert (value != markOopDesc::no_hash, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
  TEVENT (hashCode: GENERATE) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
  return value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
}
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   600
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   601
intptr_t ObjectSynchronizer::FastHashCode (Thread * Self, oop obj) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   602
  if (UseBiasedLocking) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   603
    // NOTE: many places throughout the JVM do not expect a safepoint
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   604
    // to be taken here, in particular most operations on perm gen
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   605
    // objects. However, we only ever bias Java instances and all of
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   606
    // the call sites of identity_hash that might revoke biases have
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   607
    // been checked to make sure they can handle a safepoint. The
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   608
    // added check of the bias pattern is to avoid useless calls to
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   609
    // thread-local storage.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   610
    if (obj->mark()->has_bias_pattern()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   611
      // Box and unbox the raw reference just in case we cause a STW safepoint.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   612
      Handle hobj (Self, obj) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   613
      // Relaxing assertion for bug 6320749.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   614
      assert (Universe::verify_in_progress() ||
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   615
              !SafepointSynchronize::is_at_safepoint(),
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   616
             "biases should not be seen by VM thread here");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   617
      BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current());
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   618
      obj = hobj() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   619
      assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   620
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   621
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   623
  // hashCode() is a heap mutator ...
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   624
  // Relaxing assertion for bug 6320749.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   625
  assert (Universe::verify_in_progress() ||
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   626
          !SafepointSynchronize::is_at_safepoint(), "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   627
  assert (Universe::verify_in_progress() ||
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   628
          Self->is_Java_thread() , "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   629
  assert (Universe::verify_in_progress() ||
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   630
         ((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   631
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   632
  ObjectMonitor* monitor = NULL;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   633
  markOop temp, test;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   634
  intptr_t hash;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   635
  markOop mark = ReadStableMark (obj);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   636
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   637
  // object should remain ineligible for biased locking
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   638
  assert (!mark->has_bias_pattern(), "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   639
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   640
  if (mark->is_neutral()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   641
    hash = mark->hash();              // this is a normal header
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   642
    if (hash) {                       // if it has hash, just return it
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   643
      return hash;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   644
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   645
    hash = get_next_hash(Self, obj);  // allocate a new hash code
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   646
    temp = mark->copy_set_hash(hash); // merge the hash code into header
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   647
    // use (machine word version) atomic operation to install the hash
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   648
    test = (markOop) Atomic::cmpxchg_ptr(temp, obj->mark_addr(), mark);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   649
    if (test == mark) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   650
      return hash;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   651
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   652
    // If atomic operation failed, we must inflate the header
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   653
    // into heavy weight monitor. We could add more code here
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   654
    // for fast path, but it does not worth the complexity.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   655
  } else if (mark->has_monitor()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   656
    monitor = mark->monitor();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   657
    temp = monitor->header();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   658
    assert (temp->is_neutral(), "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   659
    hash = temp->hash();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   660
    if (hash) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   661
      return hash;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   662
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   663
    // Skip to the following code to reduce code size
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   664
  } else if (Self->is_lock_owned((address)mark->locker())) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   665
    temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   666
    assert (temp->is_neutral(), "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   667
    hash = temp->hash();              // by current thread, check if the displaced
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   668
    if (hash) {                       // header contains hash code
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   669
      return hash;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   670
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   671
    // WARNING:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   672
    //   The displaced header is strictly immutable.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   673
    // It can NOT be changed in ANY cases. So we have
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   674
    // to inflate the header into heavyweight monitor
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   675
    // even the current thread owns the lock. The reason
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   676
    // is the BasicLock (stack slot) will be asynchronously
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   677
    // read by other threads during the inflate() function.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   678
    // Any change to stack may not propagate to other threads
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   679
    // correctly.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   680
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   681
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   682
  // Inflate the monitor to set hash code
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   683
  monitor = ObjectSynchronizer::inflate(Self, obj);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   684
  // Load displaced header and check it has hash code
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   685
  mark = monitor->header();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   686
  assert (mark->is_neutral(), "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   687
  hash = mark->hash();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   688
  if (hash == 0) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   689
    hash = get_next_hash(Self, obj);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   690
    temp = mark->copy_set_hash(hash); // merge hash code into header
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   691
    assert (temp->is_neutral(), "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   692
    test = (markOop) Atomic::cmpxchg_ptr(temp, monitor, mark);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   693
    if (test != mark) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   694
      // The only update to the header in the monitor (outside GC)
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   695
      // is install the hash code. If someone add new usage of
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   696
      // displaced header, please update this code
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   697
      hash = test->hash();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   698
      assert (test->is_neutral(), "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   699
      assert (hash != 0, "Trivial unexpected object/monitor header usage.");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   700
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   701
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   702
  // We finally get the hash
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   703
  return hash;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   706
// Deprecated -- use FastHashCode() instead.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   708
intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   709
  return FastHashCode (Thread::current(), obj()) ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   713
bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* thread,
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   714
                                                   Handle h_obj) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   715
  if (UseBiasedLocking) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   716
    BiasedLocking::revoke_and_rebias(h_obj, false, thread);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   717
    assert(!h_obj->mark()->has_bias_pattern(), "biases should be revoked by now");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   718
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   719
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   720
  assert(thread == JavaThread::current(), "Can only be called on current thread");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   721
  oop obj = h_obj();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   722
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   723
  markOop mark = ReadStableMark (obj) ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   725
  // Uncontended case, header points to stack
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   726
  if (mark->has_locker()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   727
    return thread->is_lock_owned((address)mark->locker());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
  }
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   729
  // Contended case, header points to ObjectMonitor (tagged pointer)
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   730
  if (mark->has_monitor()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   731
    ObjectMonitor* monitor = mark->monitor();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   732
    return monitor->is_entered(thread) != 0 ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
  }
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   734
  // Unlocked case, header in place
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   735
  assert(mark->is_neutral(), "sanity check");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   736
  return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   739
// Be aware of this method could revoke bias of the lock object.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   740
// This method querys the ownership of the lock handle specified by 'h_obj'.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   741
// If the current thread owns the lock, it returns owner_self. If no
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   742
// thread owns the lock, it returns owner_none. Otherwise, it will return
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   743
// ower_other.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   744
ObjectSynchronizer::LockOwnership ObjectSynchronizer::query_lock_ownership
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   745
(JavaThread *self, Handle h_obj) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   746
  // The caller must beware this method can revoke bias, and
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   747
  // revocation can result in a safepoint.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   748
  assert (!SafepointSynchronize::is_at_safepoint(), "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   749
  assert (self->thread_state() != _thread_blocked , "invariant") ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   751
  // Possible mark states: neutral, biased, stack-locked, inflated
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   752
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   753
  if (UseBiasedLocking && h_obj()->mark()->has_bias_pattern()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   754
    // CASE: biased
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   755
    BiasedLocking::revoke_and_rebias(h_obj, false, self);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   756
    assert(!h_obj->mark()->has_bias_pattern(),
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   757
           "biases should be revoked by now");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   758
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   760
  assert(self == JavaThread::current(), "Can only be called on current thread");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   761
  oop obj = h_obj();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   762
  markOop mark = ReadStableMark (obj) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   763
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   764
  // CASE: stack-locked.  Mark points to a BasicLock on the owner's stack.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   765
  if (mark->has_locker()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   766
    return self->is_lock_owned((address)mark->locker()) ?
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   767
      owner_self : owner_other;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   768
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   770
  // CASE: inflated. Mark (tagged pointer) points to an objectMonitor.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   771
  // The Object:ObjectMonitor relationship is stable as long as we're
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   772
  // not at a safepoint.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   773
  if (mark->has_monitor()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   774
    void * owner = mark->monitor()->_owner ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   775
    if (owner == NULL) return owner_none ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   776
    return (owner == self ||
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   777
            self->is_lock_owned((address)owner)) ? owner_self : owner_other;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   778
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   779
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   780
  // CASE: neutral
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   781
  assert(mark->is_neutral(), "sanity check");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   782
  return owner_none ;           // it's unlocked
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   783
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   785
// FIXME: jvmti should call this
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   786
JavaThread* ObjectSynchronizer::get_lock_owner(Handle h_obj, bool doLock) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   787
  if (UseBiasedLocking) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   788
    if (SafepointSynchronize::is_at_safepoint()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   789
      BiasedLocking::revoke_at_safepoint(h_obj);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   790
    } else {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   791
      BiasedLocking::revoke_and_rebias(h_obj, false, JavaThread::current());
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   792
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   793
    assert(!h_obj->mark()->has_bias_pattern(), "biases should be revoked by now");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   794
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   795
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   796
  oop obj = h_obj();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   797
  address owner = NULL;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   798
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   799
  markOop mark = ReadStableMark (obj) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   800
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   801
  // Uncontended case, header points to stack
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   802
  if (mark->has_locker()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   803
    owner = (address) mark->locker();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   804
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   805
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   806
  // Contended case, header points to ObjectMonitor (tagged pointer)
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   807
  if (mark->has_monitor()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   808
    ObjectMonitor* monitor = mark->monitor();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   809
    assert(monitor != NULL, "monitor should be non-null");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   810
    owner = (address) monitor->owner();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   811
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   812
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   813
  if (owner != NULL) {
15853
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 15475
diff changeset
   814
    // owning_thread_from_monitor_owner() may also return NULL here
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   815
    return Threads::owning_thread_from_monitor_owner(owner, doLock);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   816
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   817
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   818
  // Unlocked case, header in place
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   819
  // Cannot have assertion since this object may have been
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   820
  // locked by another thread when reaching here.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   821
  // assert(mark->is_neutral(), "sanity check");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   822
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   823
  return NULL;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   824
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   825
// Visitors ...
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   826
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   827
void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   828
  ObjectMonitor* block = gBlockList;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   829
  ObjectMonitor* mid;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   830
  while (block) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   831
    assert(block->object() == CHAINMARKER, "must be a block header");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   832
    for (int i = _BLOCKSIZE - 1; i > 0; i--) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   833
      mid = block + i;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   834
      oop object = (oop) mid->object();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   835
      if (object != NULL) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   836
        closure->do_monitor(mid);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   837
      }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   838
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   839
    block = (ObjectMonitor*) block->FreeNext;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   843
// Get the next block in the block list.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   844
static inline ObjectMonitor* next(ObjectMonitor* block) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   845
  assert(block->object() == CHAINMARKER, "must be a block header");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   846
  block = block->FreeNext ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   847
  assert(block == NULL || block->object() == CHAINMARKER, "must be a block header");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   848
  return block;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   852
void ObjectSynchronizer::oops_do(OopClosure* f) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   853
  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   854
  for (ObjectMonitor* block = gBlockList; block != NULL; block = next(block)) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   855
    assert(block->object() == CHAINMARKER, "must be a block header");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   856
    for (int i = 1; i < _BLOCKSIZE; i++) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   857
      ObjectMonitor* mid = &block[i];
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   858
      if (mid->object() != NULL) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   859
        f->do_oop((oop*)mid->object_addr());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   866
// -----------------------------------------------------------------------------
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
// ObjectMonitor Lifecycle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
// -----------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
// Inflation unlinks monitors from the global gFreeList and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
// associates them with objects.  Deflation -- which occurs at
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
// STW-time -- disassociates idle monitors from objects.  Such
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
// scavenged monitors are returned to the gFreeList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
// The global list is protected by ListLock.  All the critical sections
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
// are short and operate in constant-time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
// ObjectMonitors reside in type-stable memory (TSM) and are immortal.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
// Lifecycle:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
// --   unassigned and on the global free list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
// --   unassigned and on a thread's private omFreeList
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
// --   assigned to an object.  The object is inflated and the mark refers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
//      to the objectmonitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   887
// Constraining monitor pool growth via MonitorBound ...
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   888
//
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   889
// 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
   890
// 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
   891
// an inordinate number of monitors in circulation.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   892
// To avoid that scenario we can artificially induce a STW safepoint
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   893
// if the pool appears to be growing past some reasonable bound.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   894
// Generally we favor time in space-time tradeoffs, but as there's no
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   895
// natural back-pressure on the # of extant monitors we need to impose some
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   896
// 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
   897
// 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
   898
// we'll incur more safepoints, which are harmful to performance.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   899
// See also: GuaranteedSafepointInterval
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   900
//
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   901
// The current implementation uses asynchronous VM operations.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   902
//
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   903
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   904
static void InduceScavenge (Thread * Self, const char * Whence) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   905
  // Induce STW safepoint to trim monitors
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   906
  // 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
   907
  // More precisely, trigger an asynchronous STW safepoint as the number
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   908
  // of active monitors passes the specified threshold.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   909
  // TODO: assert thread state is reasonable
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   910
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   911
  if (ForceMonitorScavenge == 0 && Atomic::xchg (1, &ForceMonitorScavenge) == 0) {
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   912
    if (ObjectMonitor::Knob_Verbose) {
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   913
      ::printf ("Monitor scavenge - Induced STW @%s (%d)\n", Whence, ForceMonitorScavenge) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   914
      ::fflush(stdout) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   915
    }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   916
    // Induce a 'null' safepoint to scavenge monitors
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   917
    // 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
   918
    // 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
   919
    // The VMThread will delete the op when completed.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   920
    VMThread::execute (new VM_ForceAsyncSafepoint()) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   921
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
   922
    if (ObjectMonitor::Knob_Verbose) {
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   923
      ::printf ("Monitor scavenge - STW posted @%s (%d)\n", Whence, ForceMonitorScavenge) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   924
      ::fflush(stdout) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   925
    }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   926
  }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   927
}
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   928
/* Too slow for general assert or debug
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   929
void ObjectSynchronizer::verifyInUse (Thread *Self) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   930
   ObjectMonitor* mid;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   931
   int inusetally = 0;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   932
   for (mid = Self->omInUseList; mid != NULL; mid = mid->FreeNext) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   933
     inusetally ++;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   934
   }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   935
   assert(inusetally == Self->omInUseCount, "inuse count off");
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   936
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   937
   int freetally = 0;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   938
   for (mid = Self->omFreeList; mid != NULL; mid = mid->FreeNext) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   939
     freetally ++;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   940
   }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   941
   assert(freetally == Self->omFreeCount, "free count off");
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   942
}
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   943
*/
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
ObjectMonitor * ATTR ObjectSynchronizer::omAlloc (Thread * Self) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
    // A large MAXPRIVATE value reduces both list lock contention
489c9b5090e2 Initial load
duke
parents:
diff changeset
   946
    // and list coherency traffic, but also tends to increase the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
    // number of objectMonitors in circulation as well as the STW
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
    // scavenge costs.  As usual, we lean toward time in space-time
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
    // tradeoffs.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
    const int MAXPRIVATE = 1024 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
    for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
        ObjectMonitor * m ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
489c9b5090e2 Initial load
duke
parents:
diff changeset
   954
        // 1: try to allocate from the thread's local omFreeList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
        // Threads will attempt to allocate first from their local list, then
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
        // from the global list, and only after those attempts fail will the thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
        // attempt to instantiate new monitors.   Thread-local free lists take
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
        // heat off the ListLock and improve allocation latency, as well as reducing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
        // coherency traffic on the shared global list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
        m = Self->omFreeList ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
        if (m != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
           Self->omFreeList = m->FreeNext ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
           Self->omFreeCount -- ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
           // CONSIDER: set m->FreeNext = BAD -- diagnostic hygiene
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
           guarantee (m->object() == NULL, "invariant") ;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   966
           if (MonitorInUseLists) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   967
             m->FreeNext = Self->omInUseList;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   968
             Self->omInUseList = m;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   969
             Self->omInUseCount ++;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   970
             // verifyInUse(Self);
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   971
           } else {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   972
             m->FreeNext = NULL;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   973
           }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
           return m ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
        // 2: try to allocate from the global gFreeList
489c9b5090e2 Initial load
duke
parents:
diff changeset
   978
        // CONSIDER: use muxTry() instead of muxAcquire().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
        // If the muxTry() fails then drop immediately into case 3.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
        // If we're using thread-local free lists then try
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
        // to reprovision the caller's free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
        if (gFreeList != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
            // Reprovision the thread's omFreeList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
            // Use bulk transfers to reduce the allocation rate and heat
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
            // on various locks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   986
            Thread::muxAcquire (&ListLock, "omAlloc") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   987
            for (int i = Self->omFreeProvision; --i >= 0 && gFreeList != NULL; ) {
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
   988
                MonitorFreeCount --;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
                ObjectMonitor * take = gFreeList ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   990
                gFreeList = take->FreeNext ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   991
                guarantee (take->object() == NULL, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   992
                guarantee (!take->is_busy(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   993
                take->Recycle() ;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
   994
                omRelease (Self, take, false) ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   995
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   996
            Thread::muxRelease (&ListLock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   997
            Self->omFreeProvision += 1 + (Self->omFreeProvision/2) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   998
            if (Self->omFreeProvision > MAXPRIVATE ) Self->omFreeProvision = MAXPRIVATE ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   999
            TEVENT (omFirst - reprovision) ;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1000
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1001
            const int mx = MonitorBound ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1002
            if (mx > 0 && (MonitorPopulation-MonitorFreeCount) > mx) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1003
              // We can't safely induce a STW safepoint from omAlloc() as our thread
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1004
              // state may not be appropriate for such activities and callers may hold
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1005
              // naked oops, so instead we defer the action.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1006
              InduceScavenge (Self, "omAlloc") ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1007
            }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1008
            continue;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1010
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1011
        // 3: allocate a block of new ObjectMonitors
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1012
        // Both the local and global free lists are empty -- resort to malloc().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1013
        // In the current implementation objectMonitors are TSM - immortal.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1014
        assert (_BLOCKSIZE > 1, "invariant") ;
17031
9a576749065b 8012907: anti-delta fix for 8010992
dcubed
parents: 17026
diff changeset
  1015
        ObjectMonitor * temp = new ObjectMonitor[_BLOCKSIZE];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1016
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1017
        // NOTE: (almost) no way to recover if allocation failed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1018
        // We might be able to induce a STW safepoint and scavenge enough
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1019
        // objectMonitors to permit progress.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1020
        if (temp == NULL) {
17087
f0b76c4c93a0 8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents: 17031
diff changeset
  1021
            vm_exit_out_of_memory (sizeof (ObjectMonitor[_BLOCKSIZE]), OOM_MALLOC_ERROR,
f0b76c4c93a0 8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents: 17031
diff changeset
  1022
                                   "Allocate ObjectMonitors");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1023
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1024
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1025
        // Format the block.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1026
        // initialize the linked list, each monitor points to its next
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1027
        // forming the single linked free list, the very first monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1028
        // will points to next block, which forms the block list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1029
        // The trick of using the 1st element in the block as gBlockList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1030
        // linkage should be reconsidered.  A better implementation would
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1031
        // look like: class Block { Block * next; int N; ObjectMonitor Body [N] ; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1032
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1033
        for (int i = 1; i < _BLOCKSIZE ; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1034
           temp[i].FreeNext = &temp[i+1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1035
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1036
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1037
        // terminate the last monitor as the end of list
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1038
        temp[_BLOCKSIZE - 1].FreeNext = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1039
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1040
        // Element [0] is reserved for global list linkage
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1041
        temp[0].set_object(CHAINMARKER);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1042
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1043
        // Consider carving out this thread's current request from the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1044
        // block in hand.  This avoids some lock traffic and redundant
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1045
        // list activity.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1046
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1047
        // Acquire the ListLock to manipulate BlockList and FreeList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1048
        // An Oyama-Taura-Yonezawa scheme might be more efficient.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1049
        Thread::muxAcquire (&ListLock, "omAlloc [2]") ;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1050
        MonitorPopulation += _BLOCKSIZE-1;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1051
        MonitorFreeCount += _BLOCKSIZE-1;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1052
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1053
        // Add the new block to the list of extant blocks (gBlockList).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1054
        // The very first objectMonitor in a block is reserved and dedicated.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1055
        // It serves as blocklist "next" linkage.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1056
        temp[0].FreeNext = gBlockList;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1057
        gBlockList = temp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1058
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1059
        // Add the new string of objectMonitors to the global free list
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1060
        temp[_BLOCKSIZE - 1].FreeNext = gFreeList ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1061
        gFreeList = temp + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1062
        Thread::muxRelease (&ListLock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1063
        TEVENT (Allocate block of monitors) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1064
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1065
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1066
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1067
// Place "m" on the caller's private per-thread omFreeList.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1068
// In practice there's no need to clamp or limit the number of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1069
// monitors on a thread's omFreeList as the only time we'll call
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1070
// omRelease is to return a monitor to the free list after a CAS
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1071
// attempt failed.  This doesn't allow unbounded #s of monitors to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1072
// accumulate on a thread's free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1073
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1074
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1075
void ObjectSynchronizer::omRelease (Thread * Self, ObjectMonitor * m, bool fromPerThreadAlloc) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1076
    guarantee (m->object() == NULL, "invariant") ;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1077
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1078
    // Remove from omInUseList
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1079
    if (MonitorInUseLists && fromPerThreadAlloc) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1080
      ObjectMonitor* curmidinuse = NULL;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1081
      for (ObjectMonitor* mid = Self->omInUseList; mid != NULL; ) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1082
       if (m == mid) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1083
         // extract from per-thread in-use-list
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1084
         if (mid == Self->omInUseList) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1085
           Self->omInUseList = mid->FreeNext;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1086
         } else if (curmidinuse != NULL) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1087
           curmidinuse->FreeNext = mid->FreeNext; // maintain the current thread inuselist
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1088
         }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1089
         Self->omInUseCount --;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1090
         // verifyInUse(Self);
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1091
         break;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1092
       } else {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1093
         curmidinuse = mid;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1094
         mid = mid->FreeNext;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1095
      }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1096
    }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1097
  }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1098
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1099
  // 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
  1100
  m->FreeNext = Self->omFreeList ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1101
  Self->omFreeList = m ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1102
  Self->omFreeCount ++ ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1103
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1104
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1105
// Return the monitors of a moribund thread's local free list to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1106
// the global free list.  Typically a thread calls omFlush() when
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1107
// it's dying.  We could also consider having the VM thread steal
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1108
// monitors from threads that have not run java code over a few
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1109
// consecutive STW safepoints.  Relatedly, we might decay
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1110
// omFreeProvision at STW safepoints.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1111
//
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1112
// Also return the monitors of a moribund thread"s omInUseList to
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1113
// a global gOmInUseList under the global list lock so these
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1114
// will continue to be scanned.
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1115
//
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1116
// We currently call omFlush() from the Thread:: dtor _after the thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1117
// has been excised from the thread list and is no longer a mutator.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1118
// That means that omFlush() can run concurrently with a safepoint and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1119
// the scavenge operator.  Calling omFlush() from JavaThread::exit() might
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1120
// be a better choice as we could safely reason that that the JVM is
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1121
// not at a safepoint at the time of the call, and thus there could
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1122
// be not inopportune interleavings between omFlush() and the scavenge
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1123
// operator.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1124
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1125
void ObjectSynchronizer::omFlush (Thread * Self) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1126
    ObjectMonitor * List = Self->omFreeList ;  // Null-terminated SLL
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1127
    Self->omFreeList = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1128
    ObjectMonitor * Tail = NULL ;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1129
    int Tally = 0;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1130
    if (List != NULL) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1131
      ObjectMonitor * s ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1132
      for (s = List ; s != NULL ; s = s->FreeNext) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1133
          Tally ++ ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1134
          Tail = s ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1135
          guarantee (s->object() == NULL, "invariant") ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1136
          guarantee (!s->is_busy(), "invariant") ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1137
          s->set_owner (NULL) ;   // redundant but good hygiene
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1138
          TEVENT (omFlush - Move one) ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1139
      }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1140
      guarantee (Tail != NULL && List != NULL, "invariant") ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1141
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1142
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1143
    ObjectMonitor * InUseList = Self->omInUseList;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1144
    ObjectMonitor * InUseTail = NULL ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1145
    int InUseTally = 0;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1146
    if (InUseList != NULL) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1147
      Self->omInUseList = NULL;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1148
      ObjectMonitor *curom;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1149
      for (curom = InUseList; curom != NULL; curom = curom->FreeNext) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1150
        InUseTail = curom;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1151
        InUseTally++;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1152
      }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1153
// TODO debug
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1154
      assert(Self->omInUseCount == InUseTally, "inuse count off");
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1155
      Self->omInUseCount = 0;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1156
      guarantee (InUseTail != NULL && InUseList != NULL, "invariant");
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1157
    }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1158
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1159
    Thread::muxAcquire (&ListLock, "omFlush") ;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1160
    if (Tail != NULL) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1161
      Tail->FreeNext = gFreeList ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1162
      gFreeList = List ;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1163
      MonitorFreeCount += Tally;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1164
    }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1165
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1166
    if (InUseTail != NULL) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1167
      InUseTail->FreeNext = gOmInUseList;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1168
      gOmInUseList = InUseList;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1169
      gOmInUseCount += InUseTally;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1170
    }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1171
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1172
    Thread::muxRelease (&ListLock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1173
    TEVENT (omFlush) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1174
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1175
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1176
// Fast path code shared by multiple functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1177
ObjectMonitor* ObjectSynchronizer::inflate_helper(oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1178
  markOop mark = obj->mark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1179
  if (mark->has_monitor()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1180
    assert(ObjectSynchronizer::verify_objmon_isinpool(mark->monitor()), "monitor is invalid");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1181
    assert(mark->monitor()->header()->is_neutral(), "monitor must record a good object header");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1182
    return mark->monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1183
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1184
  return ObjectSynchronizer::inflate(Thread::current(), obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1185
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1186
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1187
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1188
// Note that we could encounter some performance loss through false-sharing as
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1189
// multiple locks occupy the same $ line.  Padding might be appropriate.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1190
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1191
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1192
ObjectMonitor * ATTR ObjectSynchronizer::inflate (Thread * Self, oop object) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1193
  // Inflate mutates the heap ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1194
  // Relaxing assertion for bug 6320749.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1195
  assert (Universe::verify_in_progress() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1196
          !SafepointSynchronize::is_at_safepoint(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1197
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1198
  for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1199
      const markOop mark = object->mark() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1200
      assert (!mark->has_bias_pattern(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1201
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1202
      // The mark can be in one of the following states:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1203
      // *  Inflated     - just return
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1204
      // *  Stack-locked - coerce it to inflated
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1205
      // *  INFLATING    - busy wait for conversion to complete
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1206
      // *  Neutral      - aggressively inflate the object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1207
      // *  BIASED       - Illegal.  We should never see this
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1208
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1209
      // CASE: inflated
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1210
      if (mark->has_monitor()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1211
          ObjectMonitor * inf = mark->monitor() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1212
          assert (inf->header()->is_neutral(), "invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1213
          assert (inf->object() == object, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1214
          assert (ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1215
          return inf ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1216
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1217
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1218
      // CASE: inflation in progress - inflating over a stack-lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1219
      // Some other thread is converting from stack-locked to inflated.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1220
      // Only that thread can complete inflation -- other threads must wait.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1221
      // The INFLATING value is transient.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1222
      // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1223
      // We could always eliminate polling by parking the thread on some auxiliary list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1224
      if (mark == markOopDesc::INFLATING()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1225
         TEVENT (Inflate: spin while INFLATING) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1226
         ReadStableMark(object) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1227
         continue ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1228
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1229
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1230
      // CASE: stack-locked
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1231
      // Could be stack-locked either by this thread or by some other thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1232
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1233
      // Note that we allocate the objectmonitor speculatively, _before_ attempting
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1234
      // to install INFLATING into the mark word.  We originally installed INFLATING,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1235
      // allocated the objectmonitor, and then finally STed the address of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1236
      // objectmonitor into the mark.  This was correct, but artificially lengthened
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1237
      // the interval in which INFLATED appeared in the mark, thus increasing
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1238
      // the odds of inflation contention.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1239
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1240
      // We now use per-thread private objectmonitor free lists.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1241
      // These list are reprovisioned from the global free list outside the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1242
      // critical INFLATING...ST interval.  A thread can transfer
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1243
      // multiple objectmonitors en-mass from the global free list to its local free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1244
      // This reduces coherency traffic and lock contention on the global free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1245
      // Using such local free lists, it doesn't matter if the omAlloc() call appears
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1246
      // before or after the CAS(INFLATING) operation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1247
      // See the comments in omAlloc().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1248
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1249
      if (mark->has_locker()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1250
          ObjectMonitor * m = omAlloc (Self) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1251
          // Optimistically prepare the objectmonitor - anticipate successful CAS
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1252
          // We do this before the CAS in order to minimize the length of time
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1253
          // in which INFLATING appears in the mark.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1254
          m->Recycle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1255
          m->_Responsible  = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1256
          m->OwnerIsThread = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1257
          m->_recursions   = 0 ;
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1258
          m->_SpinDuration = ObjectMonitor::Knob_SpinLimit ;   // Consider: maintain by type/class
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1259
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1260
          markOop cmp = (markOop) Atomic::cmpxchg_ptr (markOopDesc::INFLATING(), object->mark_addr(), mark) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1261
          if (cmp != mark) {
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1262
             omRelease (Self, m, true) ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1263
             continue ;       // Interference -- just retry
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1264
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1265
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1266
          // We've successfully installed INFLATING (0) into the mark-word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1267
          // This is the only case where 0 will appear in a mark-work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1268
          // Only the singular thread that successfully swings the mark-word
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1269
          // to 0 can perform (or more precisely, complete) inflation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1270
          //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1271
          // Why do we CAS a 0 into the mark-word instead of just CASing the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1272
          // mark-word from the stack-locked value directly to the new inflated state?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1273
          // Consider what happens when a thread unlocks a stack-locked object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1274
          // It attempts to use CAS to swing the displaced header value from the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1275
          // on-stack basiclock back into the object header.  Recall also that the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1276
          // header value (hashcode, etc) can reside in (a) the object header, or
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1277
          // (b) a displaced header associated with the stack-lock, or (c) a displaced
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1278
          // header in an objectMonitor.  The inflate() routine must copy the header
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1279
          // value from the basiclock on the owner's stack to the objectMonitor, all
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1280
          // the while preserving the hashCode stability invariants.  If the owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1281
          // decides to release the lock while the value is 0, the unlock will fail
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1282
          // and control will eventually pass from slow_exit() to inflate.  The owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1283
          // will then spin, waiting for the 0 value to disappear.   Put another way,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1284
          // the 0 causes the owner to stall if the owner happens to try to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1285
          // drop the lock (restoring the header from the basiclock to the object)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1286
          // while inflation is in-progress.  This protocol avoids races that might
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1287
          // would otherwise permit hashCode values to change or "flicker" for an object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1288
          // Critically, while object->mark is 0 mark->displaced_mark_helper() is stable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1289
          // 0 serves as a "BUSY" inflate-in-progress indicator.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1290
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1291
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1292
          // fetch the displaced mark from the owner's stack.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1293
          // The owner can't die or unwind past the lock while our INFLATING
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1294
          // object is in the mark.  Furthermore the owner can't complete
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1295
          // an unlock on the object, either.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1296
          markOop dmw = mark->displaced_mark_helper() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1297
          assert (dmw->is_neutral(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1298
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1299
          // Setup monitor fields to proper values -- prepare the monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1300
          m->set_header(dmw) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1301
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1302
          // Optimization: if the mark->locker stack address is associated
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1303
          // 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
  1304
          // m->OwnerIsThread = 1. Note that a thread can inflate an object
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1305
          // that it has stack-locked -- as might happen in wait() -- directly
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1306
          // 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
  1307
          m->set_owner(mark->locker());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1308
          m->set_object(object);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1309
          // TODO-FIXME: assert BasicLock->dhw != 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1310
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1311
          // Must preserve store ordering. The monitor state must
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1312
          // be stable at the time of publishing the monitor address.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1313
          guarantee (object->mark() == markOopDesc::INFLATING(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1314
          object->release_set_mark(markOopDesc::encode(m));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1315
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1316
          // Hopefully the performance counters are allocated on distinct cache lines
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1317
          // to avoid false sharing on MP systems ...
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1318
          if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1319
          TEVENT(Inflate: overwrite stacklock) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1320
          if (TraceMonitorInflation) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1321
            if (object->is_instance()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1322
              ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1323
              tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1324
                (intptr_t) object, (intptr_t) object->mark(),
14488
ab48109f7d1b 8001471: Klass::cast() does nothing
hseigel
parents: 13728
diff changeset
  1325
                object->klass()->external_name());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1326
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1327
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1328
          return m ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1329
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1330
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1331
      // CASE: neutral
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1332
      // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1333
      // If we know we're inflating for entry it's better to inflate by swinging a
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1334
      // pre-locked objectMonitor pointer into the object header.   A successful
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1335
      // CAS inflates the object *and* confers ownership to the inflating thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1336
      // In the current implementation we use a 2-step mechanism where we CAS()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1337
      // to inflate and then CAS() again to try to swing _owner from NULL to Self.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1338
      // An inflateTry() method that we could call from fast_enter() and slow_enter()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1339
      // would be useful.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1340
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1341
      assert (mark->is_neutral(), "invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1342
      ObjectMonitor * m = omAlloc (Self) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1343
      // prepare m for installation - set monitor to initial state
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1344
      m->Recycle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1345
      m->set_header(mark);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1346
      m->set_owner(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1347
      m->set_object(object);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1348
      m->OwnerIsThread = 1 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1349
      m->_recursions   = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1350
      m->_Responsible  = NULL ;
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1351
      m->_SpinDuration = ObjectMonitor::Knob_SpinLimit ;       // consider: keep metastats by type/class
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1352
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1353
      if (Atomic::cmpxchg_ptr (markOopDesc::encode(m), object->mark_addr(), mark) != mark) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1354
          m->set_object (NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1355
          m->set_owner  (NULL) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1356
          m->OwnerIsThread = 0 ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1357
          m->Recycle() ;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1358
          omRelease (Self, m, true) ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1359
          m = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1360
          continue ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1361
          // interference - the markword changed - just retry.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1362
          // The state-transitions are one-way, so there's no chance of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1363
          // live-lock -- "Inflated" is an absorbing state.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1364
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1365
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1366
      // Hopefully the performance counters are allocated on distinct
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1367
      // cache lines to avoid false sharing on MP systems ...
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1368
      if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1369
      TEVENT(Inflate: overwrite neutral) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1370
      if (TraceMonitorInflation) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1371
        if (object->is_instance()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1372
          ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1373
          tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1374
            (intptr_t) object, (intptr_t) object->mark(),
14488
ab48109f7d1b 8001471: Klass::cast() does nothing
hseigel
parents: 13728
diff changeset
  1375
            object->klass()->external_name());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1376
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1377
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1378
      return m ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1379
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1380
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1381
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1382
// Note that we could encounter some performance loss through false-sharing as
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1383
// multiple locks occupy the same $ line.  Padding might be appropriate.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1384
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1385
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1386
// Deflate_idle_monitors() is called at all safepoints, immediately
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1387
// after all mutators are stopped, but before any objects have moved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1388
// It traverses the list of known monitors, deflating where possible.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1389
// The scavenged monitor are returned to the monitor free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1390
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1391
// Beware that we scavenge at *every* stop-the-world point.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1392
// Having a large number of monitors in-circulation negatively
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1393
// impacts the performance of some applications (e.g., PointBase).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1394
// Broadly, we want to minimize the # of monitors in circulation.
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1395
//
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1396
// We have added a flag, MonitorInUseLists, which creates a list
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1397
// of active monitors for each thread. deflate_idle_monitors()
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1398
// only scans the per-thread inuse lists. omAlloc() puts all
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1399
// assigned monitors on the per-thread list. deflate_idle_monitors()
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1400
// returns the non-busy monitors to the global free list.
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1401
// When a thread dies, omFlush() adds the list of active monitors for
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1402
// that thread to a global gOmInUseList acquiring the
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1403
// global list lock. deflate_idle_monitors() acquires the global
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1404
// 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
  1405
// An alternative could have used a single global inuse list. The
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1406
// downside would have been the additional cost of acquiring the global list lock
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1407
// for every omAlloc().
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1408
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1409
// Perversely, the heap size -- and thus the STW safepoint rate --
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1410
// typically drives the scavenge rate.  Large heaps can mean infrequent GC,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1411
// which in turn can mean large(r) numbers of objectmonitors in circulation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1412
// This is an unfortunate aspect of this design.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1413
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1414
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1415
enum ManifestConstants {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1416
    ClearResponsibleAtSTW   = 0,
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1417
    MaximumRecheckInterval  = 1000
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1418
} ;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1419
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1420
// Deflate a single monitor if not in use
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1421
// Return true if deflated, false if in use
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1422
bool ObjectSynchronizer::deflate_monitor(ObjectMonitor* mid, oop obj,
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1423
                                         ObjectMonitor** FreeHeadp, ObjectMonitor** FreeTailp) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1424
  bool deflated;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1425
  // Normal case ... The monitor is associated with obj.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1426
  guarantee (obj->mark() == markOopDesc::encode(mid), "invariant") ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1427
  guarantee (mid == obj->mark()->monitor(), "invariant");
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1428
  guarantee (mid->header()->is_neutral(), "invariant");
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1429
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1430
  if (mid->is_busy()) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1431
     if (ClearResponsibleAtSTW) mid->_Responsible = NULL ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1432
     deflated = false;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1433
  } else {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1434
     // Deflate the monitor if it is no longer being used
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1435
     // It's idle - scavenge and return to the global free list
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1436
     // plain old deflation ...
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1437
     TEVENT (deflate_idle_monitors - scavenge1) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1438
     if (TraceMonitorInflation) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1439
       if (obj->is_instance()) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1440
         ResourceMark rm;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1441
           tty->print_cr("Deflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
14488
ab48109f7d1b 8001471: Klass::cast() does nothing
hseigel
parents: 13728
diff changeset
  1442
                (intptr_t) obj, (intptr_t) obj->mark(), obj->klass()->external_name());
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1443
       }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1444
     }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1445
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1446
     // Restore the header back to obj
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1447
     obj->release_set_mark(mid->header());
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1448
     mid->clear();
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1449
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1450
     assert (mid->object() == NULL, "invariant") ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1451
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1452
     // Move the object to the working free list defined by FreeHead,FreeTail.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1453
     if (*FreeHeadp == NULL) *FreeHeadp = mid;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1454
     if (*FreeTailp != NULL) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1455
       ObjectMonitor * prevtail = *FreeTailp;
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1456
       assert(prevtail->FreeNext == NULL, "cleaned up deflated?"); // TODO KK
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1457
       prevtail->FreeNext = mid;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1458
      }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1459
     *FreeTailp = mid;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1460
     deflated = true;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1461
  }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1462
  return deflated;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1463
}
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1464
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1465
// Caller acquires ListLock
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1466
int ObjectSynchronizer::walk_monitor_list(ObjectMonitor** listheadp,
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1467
                                          ObjectMonitor** FreeHeadp, ObjectMonitor** FreeTailp) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1468
  ObjectMonitor* mid;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1469
  ObjectMonitor* next;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1470
  ObjectMonitor* curmidinuse = NULL;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1471
  int deflatedcount = 0;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1472
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1473
  for (mid = *listheadp; mid != NULL; ) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1474
     oop obj = (oop) mid->object();
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1475
     bool deflated = false;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1476
     if (obj != NULL) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1477
       deflated = deflate_monitor(mid, obj, FreeHeadp, FreeTailp);
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1478
     }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1479
     if (deflated) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1480
       // extract from per-thread in-use-list
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1481
       if (mid == *listheadp) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1482
         *listheadp = mid->FreeNext;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1483
       } else if (curmidinuse != NULL) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1484
         curmidinuse->FreeNext = mid->FreeNext; // maintain the current thread inuselist
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1485
       }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1486
       next = mid->FreeNext;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1487
       mid->FreeNext = NULL;  // This mid is current tail in the FreeHead list
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1488
       mid = next;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1489
       deflatedcount++;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1490
     } else {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1491
       curmidinuse = mid;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1492
       mid = mid->FreeNext;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1493
    }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1494
  }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1495
  return deflatedcount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1496
}
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1497
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1498
void ObjectSynchronizer::deflate_idle_monitors() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1499
  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1500
  int nInuse = 0 ;              // currently associated with objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1501
  int nInCirculation = 0 ;      // extant
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1502
  int nScavenged = 0 ;          // reclaimed
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1503
  bool deflated = false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1504
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1505
  ObjectMonitor * FreeHead = NULL ;  // Local SLL of scavenged monitors
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1506
  ObjectMonitor * FreeTail = NULL ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1507
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1508
  TEVENT (deflate_idle_monitors) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1509
  // Prevent omFlush from changing mids in Thread dtor's during deflation
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1510
  // And in case the vm thread is acquiring a lock during a safepoint
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1511
  // See e.g. 6320749
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1512
  Thread::muxAcquire (&ListLock, "scavenge - return") ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1513
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1514
  if (MonitorInUseLists) {
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1515
    int inUse = 0;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1516
    for (JavaThread* cur = Threads::first(); cur != NULL; cur = cur->next()) {
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1517
      nInCirculation+= cur->omInUseCount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1518
      int deflatedcount = walk_monitor_list(cur->omInUseList_addr(), &FreeHead, &FreeTail);
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1519
      cur->omInUseCount-= deflatedcount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1520
      // verifyInUse(cur);
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1521
      nScavenged += deflatedcount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1522
      nInuse += cur->omInUseCount;
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1523
     }
5920
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1524
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1525
   // For moribund threads, scan gOmInUseList
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1526
   if (gOmInUseList) {
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1527
     nInCirculation += gOmInUseCount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1528
     int deflatedcount = walk_monitor_list((ObjectMonitor **)&gOmInUseList, &FreeHead, &FreeTail);
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1529
     gOmInUseCount-= deflatedcount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1530
     nScavenged += deflatedcount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1531
     nInuse += gOmInUseCount;
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1532
    }
8fdbb85e62d3 6964164: MonitorInUseLists leak of contended objects
acorn
parents: 5712
diff changeset
  1533
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1534
  } else for (ObjectMonitor* block = gBlockList; block != NULL; block = next(block)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1535
  // Iterate over all extant monitors - Scavenge all idle monitors.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1536
    assert(block->object() == CHAINMARKER, "must be a block header");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1537
    nInCirculation += _BLOCKSIZE ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1538
    for (int i = 1 ; i < _BLOCKSIZE; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1539
      ObjectMonitor* mid = &block[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1540
      oop obj = (oop) mid->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1541
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1542
      if (obj == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1543
        // The monitor is not associated with an object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1544
        // The monitor should either be a thread-specific private
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1545
        // free list or the global free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1546
        // obj == NULL IMPLIES mid->is_busy() == 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1547
        guarantee (!mid->is_busy(), "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1548
        continue ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1549
      }
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1550
      deflated = deflate_monitor(mid, obj, &FreeHead, &FreeTail);
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1551
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1552
      if (deflated) {
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1553
        mid->FreeNext = NULL ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1554
        nScavenged ++ ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1555
      } else {
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1556
        nInuse ++;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1557
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1558
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1559
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1560
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1561
  MonitorFreeCount += nScavenged;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1562
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1563
  // Consider: audit gFreeList to ensure that MonitorFreeCount and list agree.
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1564
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1565
  if (ObjectMonitor::Knob_Verbose) {
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1566
    ::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
  1567
        nInCirculation, nInuse, nScavenged, ForceMonitorScavenge,
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1568
        MonitorPopulation, MonitorFreeCount) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1569
    ::fflush(stdout) ;
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1570
  }
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1571
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1572
  ForceMonitorScavenge = 0;    // Reset
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1573
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1574
  // Move the scavenged monitors back to the global free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1575
  if (FreeHead != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1576
     guarantee (FreeTail != NULL && nScavenged > 0, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1577
     assert (FreeTail->FreeNext == NULL, "invariant") ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1578
     // constant-time list splice - prepend scavenged segment to gFreeList
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1579
     FreeTail->FreeNext = gFreeList ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1580
     gFreeList = FreeHead ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1581
  }
5710
d664086c0add 6852873: Reduce safepoint cleanup time
acorn
parents: 2526
diff changeset
  1582
  Thread::muxRelease (&ListLock) ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1583
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1584
  if (ObjectMonitor::_sync_Deflations != NULL) ObjectMonitor::_sync_Deflations->inc(nScavenged) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1585
  if (ObjectMonitor::_sync_MonExtant  != NULL) ObjectMonitor::_sync_MonExtant ->set_value(nInCirculation);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1586
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1587
  // TODO: Add objectMonitor leak detection.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1588
  // Audit/inventory the objectMonitors -- make sure they're all accounted for.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1589
  GVars.stwRandom = os::random() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1590
  GVars.stwCycle ++ ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1591
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1592
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1593
// Monitor cleanup on JavaThread::exit
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1594
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1595
// Iterate through monitor cache and attempt to release thread's monitors
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1596
// Gives up on a particular monitor if an exception occurs, but continues
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1597
// the overall iteration, swallowing the exception.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1598
class ReleaseJavaMonitorsClosure: public MonitorClosure {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1599
private:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1600
  TRAPS;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1601
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1602
public:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1603
  ReleaseJavaMonitorsClosure(Thread* thread) : THREAD(thread) {}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1604
  void do_monitor(ObjectMonitor* mid) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1605
    if (mid->owner() == THREAD) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1606
      (void)mid->complete_exit(CHECK);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1607
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1608
  }
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1609
};
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1610
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1611
// Release all inflated monitors owned by THREAD.  Lightweight monitors are
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1612
// ignored.  This is meant to be called during JNI thread detach which assumes
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1613
// all remaining monitors are heavyweight.  All exceptions are swallowed.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1614
// Scanning the extant monitor list can be time consuming.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1615
// A simple optimization is to add a per-thread flag that indicates a thread
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1616
// called jni_monitorenter() during its lifetime.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1617
//
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1618
// Instead of No_Savepoint_Verifier it might be cheaper to
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1619
// use an idiom of the form:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1620
//   auto int tmp = SafepointSynchronize::_safepoint_counter ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1621
//   <code that must not run at safepoint>
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1622
//   guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1623
// Since the tests are extremely cheap we could leave them enabled
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1624
// for normal product builds.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1625
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1626
void ObjectSynchronizer::release_monitors_owned_by_thread(TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1627
  assert(THREAD == JavaThread::current(), "must be current Java thread");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1628
  No_Safepoint_Verifier nsv ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1629
  ReleaseJavaMonitorsClosure rjmc(THREAD);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1630
  Thread::muxAcquire(&ListLock, "release_monitors_owned_by_thread");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1631
  ObjectSynchronizer::monitors_iterate(&rjmc);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1632
  Thread::muxRelease(&ListLock);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents: 5920
diff changeset
  1633
  THREAD->clear_pending_exception();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1634
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1635
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1636
//------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1637
// Non-product code
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1638
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1639
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1640
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1641
// Verify all monitors in the monitor cache, the verification is weak.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1642
void ObjectSynchronizer::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1643
  ObjectMonitor* block = gBlockList;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1644
  ObjectMonitor* mid;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1645
  while (block) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1646
    assert(block->object() == CHAINMARKER, "must be a block header");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1647
    for (int i = 1; i < _BLOCKSIZE; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1648
      mid = block + i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1649
      oop object = (oop) mid->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1650
      if (object != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1651
        mid->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1652
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1653
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1654
    block = (ObjectMonitor*) block->FreeNext;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1655
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1656
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1657
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1658
// Check if monitor belongs to the monitor cache
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1659
// The list is grow-only so it's *relatively* safe to traverse
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1660
// the list of extant blocks without taking a lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1661
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1662
int ObjectSynchronizer::verify_objmon_isinpool(ObjectMonitor *monitor) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1663
  ObjectMonitor* block = gBlockList;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1664
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1665
  while (block) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1666
    assert(block->object() == CHAINMARKER, "must be a block header");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1667
    if (monitor > &block[0] && monitor < &block[_BLOCKSIZE]) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1668
      address mon = (address) monitor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1669
      address blk = (address) block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1670
      size_t diff = mon - blk;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1671
      assert((diff % sizeof(ObjectMonitor)) == 0, "check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1672
      return 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1673
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1674
    block = (ObjectMonitor*) block->FreeNext;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1675
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1676
  return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1677
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1678
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1679
#endif