src/hotspot/share/runtime/objectMonitor.cpp
changeset 59156 14fa9e70ae71
parent 59105 76ae9aa0e794
child 59247 56bf71d64d51
equal deleted inserted replaced
59154:0c2e1808f800 59156:14fa9e70ae71
    24 
    24 
    25 #include "precompiled.hpp"
    25 #include "precompiled.hpp"
    26 #include "classfile/vmSymbols.hpp"
    26 #include "classfile/vmSymbols.hpp"
    27 #include "jfr/jfrEvents.hpp"
    27 #include "jfr/jfrEvents.hpp"
    28 #include "jfr/support/jfrThreadId.hpp"
    28 #include "jfr/support/jfrThreadId.hpp"
       
    29 #include "logging/log.hpp"
       
    30 #include "logging/logStream.hpp"
    29 #include "memory/allocation.inline.hpp"
    31 #include "memory/allocation.inline.hpp"
    30 #include "memory/resourceArea.hpp"
    32 #include "memory/resourceArea.hpp"
    31 #include "oops/markWord.hpp"
    33 #include "oops/markWord.hpp"
    32 #include "oops/oop.inline.hpp"
    34 #include "oops/oop.inline.hpp"
    33 #include "runtime/atomic.hpp"
    35 #include "runtime/atomic.hpp"
   253     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
   255     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
   254     _recursions++;
   256     _recursions++;
   255     return;
   257     return;
   256   }
   258   }
   257 
   259 
   258   if (Self->is_lock_owned ((address)cur)) {
   260   if (Self->is_lock_owned((address)cur)) {
   259     assert(_recursions == 0, "internal state error");
   261     assert(_recursions == 0, "internal state error");
   260     _recursions = 1;
   262     _recursions = 1;
   261     // Commute owner from a thread-specific on-stack BasicLockObject address to
   263     // Commute owner from a thread-specific on-stack BasicLockObject address to
   262     // a full-fledged "Thread *".
   264     // a full-fledged "Thread *".
   263     _owner = Self;
   265     _owner = Self;
   273   // transitions.  The following spin is strictly optional ...
   275   // transitions.  The following spin is strictly optional ...
   274   // Note that if we acquire the monitor from an initial spin
   276   // Note that if we acquire the monitor from an initial spin
   275   // we forgo posting JVMTI events and firing DTRACE probes.
   277   // we forgo posting JVMTI events and firing DTRACE probes.
   276   if (TrySpin(Self) > 0) {
   278   if (TrySpin(Self) > 0) {
   277     assert(_owner == Self, "must be Self: owner=" INTPTR_FORMAT, p2i(_owner));
   279     assert(_owner == Self, "must be Self: owner=" INTPTR_FORMAT, p2i(_owner));
   278     assert(_recursions == 0, "must be 0: recursions=" INTPTR_FORMAT,
   280     assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
   279            _recursions);
       
   280     assert(((oop)object())->mark() == markWord::encode(this),
   281     assert(((oop)object())->mark() == markWord::encode(this),
   281            "object mark must match encoded this: mark=" INTPTR_FORMAT
   282            "object mark must match encoded this: mark=" INTPTR_FORMAT
   282            ", encoded this=" INTPTR_FORMAT, ((oop)object())->mark().value(),
   283            ", encoded this=" INTPTR_FORMAT, ((oop)object())->mark().value(),
   283            markWord::encode(this).value());
   284            markWord::encode(this).value());
   284     Self->_Stalled = 0;
   285     Self->_Stalled = 0;
   879       // see x86_32.ad Fast_Unlock() and the I1 and I2 properties.
   880       // see x86_32.ad Fast_Unlock() and the I1 and I2 properties.
   880       // Upon deeper reflection, however, in a properly run JVM the only
   881       // Upon deeper reflection, however, in a properly run JVM the only
   881       // way we should encounter this situation is in the presence of
   882       // way we should encounter this situation is in the presence of
   882       // unbalanced JNI locking. TODO: CheckJNICalls.
   883       // unbalanced JNI locking. TODO: CheckJNICalls.
   883       // See also: CR4414101
   884       // See also: CR4414101
   884       assert(false, "Non-balanced monitor enter/exit! Likely JNI locking");
   885 #ifdef ASSERT
       
   886       LogStreamHandle(Error, monitorinflation) lsh;
       
   887       lsh.print_cr("ERROR: ObjectMonitor::exit(): thread=" INTPTR_FORMAT
       
   888                     " is exiting an ObjectMonitor it does not own.", p2i(THREAD));
       
   889       lsh.print_cr("The imbalance is possibly caused by JNI locking.");
       
   890       print_debug_style_on(&lsh);
       
   891 #endif
       
   892       assert(false, "Non-balanced monitor enter/exit!");
   885       return;
   893       return;
   886     }
   894     }
   887   }
   895   }
   888 
   896 
   889   if (_recursions != 0) {
   897   if (_recursions != 0) {
   906   for (;;) {
   914   for (;;) {
   907     assert(THREAD == _owner, "invariant");
   915     assert(THREAD == _owner, "invariant");
   908 
   916 
   909     // release semantics: prior loads and stores from within the critical section
   917     // release semantics: prior loads and stores from within the critical section
   910     // must not float (reorder) past the following store that drops the lock.
   918     // must not float (reorder) past the following store that drops the lock.
   911     // On SPARC that requires MEMBAR #loadstore|#storestore.
       
   912     // But of course in TSO #loadstore|#storestore is not required.
       
   913     OrderAccess::release_store(&_owner, (void*)NULL);   // drop the lock
   919     OrderAccess::release_store(&_owner, (void*)NULL);   // drop the lock
   914     OrderAccess::storeload();                        // See if we need to wake a successor
   920     OrderAccess::storeload();                        // See if we need to wake a successor
   915     if ((intptr_t(_EntryList)|intptr_t(_cxq)) == 0 || _succ != NULL) {
   921     if ((intptr_t(_EntryList)|intptr_t(_cxq)) == 0 || _succ != NULL) {
   916       return;
   922       return;
   917     }
   923     }
  1104 // complete_exit/reenter operate as a wait without waiting
  1110 // complete_exit/reenter operate as a wait without waiting
  1105 // complete_exit requires an inflated monitor
  1111 // complete_exit requires an inflated monitor
  1106 // The _owner field is not always the Thread addr even with an
  1112 // The _owner field is not always the Thread addr even with an
  1107 // inflated monitor, e.g. the monitor can be inflated by a non-owning
  1113 // inflated monitor, e.g. the monitor can be inflated by a non-owning
  1108 // thread due to contention.
  1114 // thread due to contention.
  1109 intptr_t ObjectMonitor::complete_exit(TRAPS) {
  1115 intx ObjectMonitor::complete_exit(TRAPS) {
  1110   Thread * const Self = THREAD;
  1116   Thread * const Self = THREAD;
  1111   assert(Self->is_Java_thread(), "Must be Java thread!");
  1117   assert(Self->is_Java_thread(), "Must be Java thread!");
  1112   JavaThread *jt = (JavaThread *)THREAD;
  1118   JavaThread *jt = (JavaThread *)THREAD;
  1113 
  1119 
  1114   assert(InitDone, "Unexpectedly not initialized");
  1120   assert(InitDone, "Unexpectedly not initialized");
  1120       _recursions = 0;
  1126       _recursions = 0;
  1121     }
  1127     }
  1122   }
  1128   }
  1123 
  1129 
  1124   guarantee(Self == _owner, "complete_exit not owner");
  1130   guarantee(Self == _owner, "complete_exit not owner");
  1125   intptr_t save = _recursions; // record the old recursion count
  1131   intx save = _recursions; // record the old recursion count
  1126   _recursions = 0;        // set the recursion level to be 0
  1132   _recursions = 0;        // set the recursion level to be 0
  1127   exit(true, Self);           // exit the monitor
  1133   exit(true, Self);           // exit the monitor
  1128   guarantee(_owner != Self, "invariant");
  1134   guarantee(_owner != Self, "invariant");
  1129   return save;
  1135   return save;
  1130 }
  1136 }
  1131 
  1137 
  1132 // reenter() enters a lock and sets recursion count
  1138 // reenter() enters a lock and sets recursion count
  1133 // complete_exit/reenter operate as a wait without waiting
  1139 // complete_exit/reenter operate as a wait without waiting
  1134 void ObjectMonitor::reenter(intptr_t recursions, TRAPS) {
  1140 void ObjectMonitor::reenter(intx recursions, TRAPS) {
  1135   Thread * const Self = THREAD;
  1141   Thread * const Self = THREAD;
  1136   assert(Self->is_Java_thread(), "Must be Java thread!");
  1142   assert(Self->is_Java_thread(), "Must be Java thread!");
  1137   JavaThread *jt = (JavaThread *)THREAD;
  1143   JavaThread *jt = (JavaThread *)THREAD;
  1138 
  1144 
  1139   guarantee(_owner != Self, "reenter already owner");
  1145   guarantee(_owner != Self, "reenter already owner");
  1250   AddWaiter(&node);
  1256   AddWaiter(&node);
  1251   Thread::SpinRelease(&_WaitSetLock);
  1257   Thread::SpinRelease(&_WaitSetLock);
  1252 
  1258 
  1253   _Responsible = NULL;
  1259   _Responsible = NULL;
  1254 
  1260 
  1255   intptr_t save = _recursions; // record the old recursion count
  1261   intx save = _recursions;     // record the old recursion count
  1256   _waiters++;                  // increment the number of waiters
  1262   _waiters++;                  // increment the number of waiters
  1257   _recursions = 0;             // set the recursion level to be 1
  1263   _recursions = 0;             // set the recursion level to be 1
  1258   exit(true, Self);                    // exit the monitor
  1264   exit(true, Self);                    // exit the monitor
  1259   guarantee(_owner != Self, "invariant");
  1265   guarantee(_owner != Self, "invariant");
  1260 
  1266 
  1939 }
  1945 }
  1940 
  1946 
  1941 void ObjectMonitor::print_on(outputStream* st) const {
  1947 void ObjectMonitor::print_on(outputStream* st) const {
  1942   // The minimal things to print for markWord printing, more can be added for debugging and logging.
  1948   // The minimal things to print for markWord printing, more can be added for debugging and logging.
  1943   st->print("{contentions=0x%08x,waiters=0x%08x"
  1949   st->print("{contentions=0x%08x,waiters=0x%08x"
  1944             ",recursions=" INTPTR_FORMAT ",owner=" INTPTR_FORMAT "}",
  1950             ",recursions=" INTX_FORMAT ",owner=" INTPTR_FORMAT "}",
  1945             contentions(), waiters(), recursions(),
  1951             contentions(), waiters(), recursions(),
  1946             p2i(owner()));
  1952             p2i(owner()));
  1947 }
  1953 }
  1948 void ObjectMonitor::print() const { print_on(tty); }
  1954 void ObjectMonitor::print() const { print_on(tty); }
       
  1955 
       
  1956 #ifdef ASSERT
       
  1957 // Print the ObjectMonitor like a debugger would:
       
  1958 //
       
  1959 // (ObjectMonitor) 0x00007fdfb6012e40 = {
       
  1960 //   _header = 0x0000000000000001
       
  1961 //   _object = 0x000000070ff45fd0
       
  1962 //   _next_om = 0x0000000000000000
       
  1963 //   _pad_buf0 = {
       
  1964 //     [0] = '\0'
       
  1965 //     ...
       
  1966 //     [103] = '\0'
       
  1967 //   }
       
  1968 //   _owner = 0x0000000000000000
       
  1969 //   _previous_owner_tid = 0
       
  1970 //   _recursions = 0
       
  1971 //   _EntryList = 0x0000000000000000
       
  1972 //   _cxq = 0x0000000000000000
       
  1973 //   _succ = 0x0000000000000000
       
  1974 //   _Responsible = 0x0000000000000000
       
  1975 //   _Spinner = 0
       
  1976 //   _SpinDuration = 5000
       
  1977 //   _contentions = 0
       
  1978 //   _WaitSet = 0x0000700009756248
       
  1979 //   _waiters = 1
       
  1980 //   _WaitSetLock = 0
       
  1981 // }
       
  1982 //
       
  1983 void ObjectMonitor::print_debug_style_on(outputStream* st) const {
       
  1984   st->print_cr("(ObjectMonitor*) " INTPTR_FORMAT " = {", p2i(this));
       
  1985   st->print_cr("  _header = " INTPTR_FORMAT, header().value());
       
  1986   st->print_cr("  _object = " INTPTR_FORMAT, p2i(_object));
       
  1987   st->print_cr("  _next_om = " INTPTR_FORMAT, p2i(_next_om));
       
  1988   st->print_cr("  _pad_buf0 = {");
       
  1989   st->print_cr("    [0] = '\\0'");
       
  1990   st->print_cr("    ...");
       
  1991   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf0) - 1);
       
  1992   st->print_cr("  }");
       
  1993   st->print_cr("  _owner = " INTPTR_FORMAT, p2i(_owner));
       
  1994   st->print_cr("  _previous_owner_tid = " JLONG_FORMAT, _previous_owner_tid);
       
  1995   st->print_cr("  _recursions = " INTX_FORMAT, _recursions);
       
  1996   st->print_cr("  _EntryList = " INTPTR_FORMAT, p2i(_EntryList));
       
  1997   st->print_cr("  _cxq = " INTPTR_FORMAT, p2i(_cxq));
       
  1998   st->print_cr("  _succ = " INTPTR_FORMAT, p2i(_succ));
       
  1999   st->print_cr("  _Responsible = " INTPTR_FORMAT, p2i(_Responsible));
       
  2000   st->print_cr("  _Spinner = %d", _Spinner);
       
  2001   st->print_cr("  _SpinDuration = %d", _SpinDuration);
       
  2002   st->print_cr("  _contentions = %d", _contentions);
       
  2003   st->print_cr("  _WaitSet = " INTPTR_FORMAT, p2i(_WaitSet));
       
  2004   st->print_cr("  _waiters = %d", _waiters);
       
  2005   st->print_cr("  _WaitSetLock = %d", _WaitSetLock);
       
  2006   st->print_cr("}");
       
  2007 }
       
  2008 #endif