< prev index next >

src/hotspot/share/runtime/objectMonitor.cpp

Print this page

 317 
 318 // -----------------------------------------------------------------------------
 319 // Enter support
 320 
 321 bool ObjectMonitor::enter(JavaThread* current) {
 322   // The following code is ordered to check the most common cases first
 323   // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
 324 
 325   void* cur = try_set_owner_from(NULL, current);
 326   if (cur == NULL) {
 327     assert(_recursions == 0, "invariant");
 328     return true;
 329   }
 330 
 331   if (cur == current) {
 332     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
 333     _recursions++;
 334     return true;
 335   }
 336 
 337   if (current->is_lock_owned((address)cur)) {
 338     assert(_recursions == 0, "internal state error");
 339     _recursions = 1;
 340     set_owner_from_BasicLock(cur, current);  // Convert from BasicLock* to Thread*.
 341     return true;
 342   }
 343 
 344   // We've encountered genuine contention.
 345   assert(current->_Stalled == 0, "invariant");
 346   current->_Stalled = intptr_t(this);
 347 
 348   // Try one round of spinning *before* enqueueing current
 349   // and before going through the awkward and expensive state
 350   // transitions.  The following spin is strictly optional ...
 351   // Note that if we acquire the monitor from an initial spin
 352   // we forgo posting JVMTI events and firing DTRACE probes.
 353   if (TrySpin(current) > 0) {
 354     assert(owner_raw() == current, "must be current: owner=" INTPTR_FORMAT, p2i(owner_raw()));
 355     assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
 356     assert(object()->mark() == markWord::encode(this),
 357            "object mark must match encoded this: mark=" INTPTR_FORMAT

 583             p2i(_EntryList));
 584 
 585   if (obj != NULL) {
 586     if (log_is_enabled(Trace, monitorinflation)) {
 587       ResourceMark rm;
 588       log_trace(monitorinflation)("deflate_monitor: object=" INTPTR_FORMAT
 589                                   ", mark=" INTPTR_FORMAT ", type='%s'",
 590                                   p2i(obj), obj->mark().value(),
 591                                   obj->klass()->external_name());
 592     }
 593 
 594     // Install the old mark word if nobody else has already done it.
 595     install_displaced_markword_in_object(obj);
 596   }
 597 
 598   // We leave owner == DEFLATER_MARKER and contentions < 0
 599   // to force any racing threads to retry.
 600   return true;  // Success, ObjectMonitor has been deflated.
 601 }
 602 
















 603 // Install the displaced mark word (dmw) of a deflating ObjectMonitor
 604 // into the header of the object associated with the monitor. This
 605 // idempotent method is called by a thread that is deflating a
 606 // monitor and by other threads that have detected a race with the
 607 // deflation process.
 608 void ObjectMonitor::install_displaced_markword_in_object(const oop obj) {
 609   // This function must only be called when (owner == DEFLATER_MARKER
 610   // && contentions <= 0), but we can't guarantee that here because
 611   // those values could change when the ObjectMonitor gets moved from
 612   // the global free list to a per-thread free list.
 613 
 614   guarantee(obj != NULL, "must be non-NULL");
 615 
 616   // Separate loads in is_being_async_deflated(), which is almost always
 617   // called before this function, from the load of dmw/header below.
 618 
 619   // _contentions and dmw/header may get written by different threads.
 620   // Make sure to observe them in the same order when having several observers.
 621   OrderAccess::loadload_for_IRIW();
 622 

1118 // the timer expires.  If the lock is high traffic then the stranding latency
1119 // will be low due to (a).  If the lock is low traffic then the odds of
1120 // stranding are lower, although the worst-case stranding latency
1121 // is longer.  Critically, we don't want to put excessive load in the
1122 // platform's timer subsystem.  We want to minimize both the timer injection
1123 // rate (timers created/sec) as well as the number of timers active at
1124 // any one time.  (more precisely, we want to minimize timer-seconds, which is
1125 // the integral of the # of active timers at any instant over time).
1126 // Both impinge on OS scalability.  Given that, at most one thread parked on
1127 // a monitor will use a timer.
1128 //
1129 // There is also the risk of a futile wake-up. If we drop the lock
1130 // another thread can reacquire the lock immediately, and we can
1131 // then wake a thread unnecessarily. This is benign, and we've
1132 // structured the code so the windows are short and the frequency
1133 // of such futile wakups is low.
1134 
1135 void ObjectMonitor::exit(JavaThread* current, bool not_suspended) {
1136   void* cur = owner_raw();
1137   if (current != cur) {
1138     if (current->is_lock_owned((address)cur)) {
1139       assert(_recursions == 0, "invariant");
1140       set_owner_from_BasicLock(cur, current);  // Convert from BasicLock* to Thread*.
1141       _recursions = 0;
1142     } else {
1143       // Apparent unbalanced locking ...
1144       // Naively we'd like to throw IllegalMonitorStateException.
1145       // As a practical matter we can neither allocate nor throw an
1146       // exception as ::exit() can be called from leaf routines.
1147       // see x86_32.ad Fast_Unlock() and the I1 and I2 properties.
1148       // Upon deeper reflection, however, in a properly run JVM the only
1149       // way we should encounter this situation is in the presence of
1150       // unbalanced JNI locking. TODO: CheckJNICalls.
1151       // See also: CR4414101
1152 #ifdef ASSERT
1153       LogStreamHandle(Error, monitorinflation) lsh;
1154       lsh.print_cr("ERROR: ObjectMonitor::exit(): thread=" INTPTR_FORMAT
1155                     " is exiting an ObjectMonitor it does not own.", p2i(current));
1156       lsh.print_cr("The imbalance is possibly caused by JNI locking.");
1157       print_debug_style_on(&lsh);
1158       assert(false, "Non-balanced monitor enter/exit!");

1338 
1339   // Maintain stats and report events to JVMTI
1340   OM_PERFDATA_OP(Parks, inc());
1341 }
1342 
1343 
1344 // -----------------------------------------------------------------------------
1345 // Class Loader deadlock handling.
1346 //
1347 // complete_exit exits a lock returning recursion count
1348 // complete_exit/reenter operate as a wait without waiting
1349 // complete_exit requires an inflated monitor
1350 // The _owner field is not always the Thread addr even with an
1351 // inflated monitor, e.g. the monitor can be inflated by a non-owning
1352 // thread due to contention.
1353 intx ObjectMonitor::complete_exit(JavaThread* current) {
1354   assert(InitDone, "Unexpectedly not initialized");
1355 
1356   void* cur = owner_raw();
1357   if (current != cur) {
1358     if (current->is_lock_owned((address)cur)) {
1359       assert(_recursions == 0, "internal state error");
1360       set_owner_from_BasicLock(cur, current);  // Convert from BasicLock* to Thread*.
1361       _recursions = 0;
1362     }
1363   }
1364 
1365   guarantee(current == owner_raw(), "complete_exit not owner");
1366   intx save = _recursions; // record the old recursion count
1367   _recursions = 0;         // set the recursion level to be 0
1368   exit(current);           // exit the monitor
1369   guarantee(owner_raw() != current, "invariant");
1370   return save;
1371 }
1372 
1373 // reenter() enters a lock and sets recursion count
1374 // complete_exit/reenter operate as a wait without waiting
1375 bool ObjectMonitor::reenter(intx recursions, JavaThread* current) {
1376 
1377   guarantee(owner_raw() != current, "reenter already owner");
1378   if (!enter(current)) {

1387 // Checks that the current THREAD owns this monitor and causes an
1388 // immediate return if it doesn't. We don't use the CHECK macro
1389 // because we want the IMSE to be the only exception that is thrown
1390 // from the call site when false is returned. Any other pending
1391 // exception is ignored.
1392 #define CHECK_OWNER()                                                  \
1393   do {                                                                 \
1394     if (!check_owner(THREAD)) {                                        \
1395        assert(HAS_PENDING_EXCEPTION, "expected a pending IMSE here."); \
1396        return;                                                         \
1397      }                                                                 \
1398   } while (false)
1399 
1400 // Returns true if the specified thread owns the ObjectMonitor.
1401 // Otherwise returns false and throws IllegalMonitorStateException
1402 // (IMSE). If there is a pending exception and the specified thread
1403 // is not the owner, that exception will be replaced by the IMSE.
1404 bool ObjectMonitor::check_owner(TRAPS) {
1405   JavaThread* current = THREAD;
1406   void* cur = owner_raw();

1407   if (cur == current) {
1408     return true;
1409   }
1410   if (current->is_lock_owned((address)cur)) {
1411     set_owner_from_BasicLock(cur, current);  // Convert from BasicLock* to Thread*.
1412     _recursions = 0;
1413     return true;
1414   }
1415   THROW_MSG_(vmSymbols::java_lang_IllegalMonitorStateException(),
1416              "current thread is not owner", false);
1417 }
1418 
1419 static inline bool is_excluded(const Klass* monitor_klass) {
1420   assert(monitor_klass != nullptr, "invariant");
1421   NOT_JFR_RETURN_(false);
1422   JFR_ONLY(return vmSymbols::jfr_chunk_rotation_monitor() == monitor_klass->name());
1423 }
1424 
1425 static void post_monitor_wait_event(EventJavaMonitorWait* event,
1426                                     ObjectMonitor* monitor,
1427                                     jlong notifier_tid,
1428                                     jlong timeout,
1429                                     bool timedout) {
1430   assert(event != NULL, "invariant");

 317 
 318 // -----------------------------------------------------------------------------
 319 // Enter support
 320 
 321 bool ObjectMonitor::enter(JavaThread* current) {
 322   // The following code is ordered to check the most common cases first
 323   // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
 324 
 325   void* cur = try_set_owner_from(NULL, current);
 326   if (cur == NULL) {
 327     assert(_recursions == 0, "invariant");
 328     return true;
 329   }
 330 
 331   if (cur == current) {
 332     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
 333     _recursions++;
 334     return true;
 335   }
 336 
 337   if (LockingMode != LM_LIGHTWEIGHT && current->is_lock_owned((address)cur)) {
 338     assert(_recursions == 0, "internal state error");
 339     _recursions = 1;
 340     set_owner_from_BasicLock(cur, current);  // Convert from BasicLock* to Thread*.
 341     return true;
 342   }
 343 
 344   // We've encountered genuine contention.
 345   assert(current->_Stalled == 0, "invariant");
 346   current->_Stalled = intptr_t(this);
 347 
 348   // Try one round of spinning *before* enqueueing current
 349   // and before going through the awkward and expensive state
 350   // transitions.  The following spin is strictly optional ...
 351   // Note that if we acquire the monitor from an initial spin
 352   // we forgo posting JVMTI events and firing DTRACE probes.
 353   if (TrySpin(current) > 0) {
 354     assert(owner_raw() == current, "must be current: owner=" INTPTR_FORMAT, p2i(owner_raw()));
 355     assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
 356     assert(object()->mark() == markWord::encode(this),
 357            "object mark must match encoded this: mark=" INTPTR_FORMAT

 583             p2i(_EntryList));
 584 
 585   if (obj != NULL) {
 586     if (log_is_enabled(Trace, monitorinflation)) {
 587       ResourceMark rm;
 588       log_trace(monitorinflation)("deflate_monitor: object=" INTPTR_FORMAT
 589                                   ", mark=" INTPTR_FORMAT ", type='%s'",
 590                                   p2i(obj), obj->mark().value(),
 591                                   obj->klass()->external_name());
 592     }
 593 
 594     // Install the old mark word if nobody else has already done it.
 595     install_displaced_markword_in_object(obj);
 596   }
 597 
 598   // We leave owner == DEFLATER_MARKER and contentions < 0
 599   // to force any racing threads to retry.
 600   return true;  // Success, ObjectMonitor has been deflated.
 601 }
 602 
 603 // We might access the dead object headers for parsable heap walk, make sure
 604 // headers are in correct shape, e.g. monitors deflated.
 605 void ObjectMonitor::maybe_deflate_dead(oop* p) {
 606   oop obj = *p;
 607   assert(obj != NULL, "must not yet been cleared");
 608   markWord mark = obj->mark();
 609   if (mark.has_monitor()) {
 610     ObjectMonitor* monitor = mark.monitor();
 611     if (p == monitor->_object.ptr_raw()) {
 612       assert(monitor->object_peek() == obj, "lock object must match");
 613       markWord dmw = monitor->header();
 614       obj->set_mark(dmw);
 615     }
 616   }
 617 }
 618 
 619 // Install the displaced mark word (dmw) of a deflating ObjectMonitor
 620 // into the header of the object associated with the monitor. This
 621 // idempotent method is called by a thread that is deflating a
 622 // monitor and by other threads that have detected a race with the
 623 // deflation process.
 624 void ObjectMonitor::install_displaced_markword_in_object(const oop obj) {
 625   // This function must only be called when (owner == DEFLATER_MARKER
 626   // && contentions <= 0), but we can't guarantee that here because
 627   // those values could change when the ObjectMonitor gets moved from
 628   // the global free list to a per-thread free list.
 629 
 630   guarantee(obj != NULL, "must be non-NULL");
 631 
 632   // Separate loads in is_being_async_deflated(), which is almost always
 633   // called before this function, from the load of dmw/header below.
 634 
 635   // _contentions and dmw/header may get written by different threads.
 636   // Make sure to observe them in the same order when having several observers.
 637   OrderAccess::loadload_for_IRIW();
 638 

1134 // the timer expires.  If the lock is high traffic then the stranding latency
1135 // will be low due to (a).  If the lock is low traffic then the odds of
1136 // stranding are lower, although the worst-case stranding latency
1137 // is longer.  Critically, we don't want to put excessive load in the
1138 // platform's timer subsystem.  We want to minimize both the timer injection
1139 // rate (timers created/sec) as well as the number of timers active at
1140 // any one time.  (more precisely, we want to minimize timer-seconds, which is
1141 // the integral of the # of active timers at any instant over time).
1142 // Both impinge on OS scalability.  Given that, at most one thread parked on
1143 // a monitor will use a timer.
1144 //
1145 // There is also the risk of a futile wake-up. If we drop the lock
1146 // another thread can reacquire the lock immediately, and we can
1147 // then wake a thread unnecessarily. This is benign, and we've
1148 // structured the code so the windows are short and the frequency
1149 // of such futile wakups is low.
1150 
1151 void ObjectMonitor::exit(JavaThread* current, bool not_suspended) {
1152   void* cur = owner_raw();
1153   if (current != cur) {
1154     if (LockingMode != LM_LIGHTWEIGHT && current->is_lock_owned((address)cur)) {
1155       assert(_recursions == 0, "invariant");
1156       set_owner_from_BasicLock(cur, current);  // Convert from BasicLock* to Thread*.
1157       _recursions = 0;
1158     } else {
1159       // Apparent unbalanced locking ...
1160       // Naively we'd like to throw IllegalMonitorStateException.
1161       // As a practical matter we can neither allocate nor throw an
1162       // exception as ::exit() can be called from leaf routines.
1163       // see x86_32.ad Fast_Unlock() and the I1 and I2 properties.
1164       // Upon deeper reflection, however, in a properly run JVM the only
1165       // way we should encounter this situation is in the presence of
1166       // unbalanced JNI locking. TODO: CheckJNICalls.
1167       // See also: CR4414101
1168 #ifdef ASSERT
1169       LogStreamHandle(Error, monitorinflation) lsh;
1170       lsh.print_cr("ERROR: ObjectMonitor::exit(): thread=" INTPTR_FORMAT
1171                     " is exiting an ObjectMonitor it does not own.", p2i(current));
1172       lsh.print_cr("The imbalance is possibly caused by JNI locking.");
1173       print_debug_style_on(&lsh);
1174       assert(false, "Non-balanced monitor enter/exit!");

1354 
1355   // Maintain stats and report events to JVMTI
1356   OM_PERFDATA_OP(Parks, inc());
1357 }
1358 
1359 
1360 // -----------------------------------------------------------------------------
1361 // Class Loader deadlock handling.
1362 //
1363 // complete_exit exits a lock returning recursion count
1364 // complete_exit/reenter operate as a wait without waiting
1365 // complete_exit requires an inflated monitor
1366 // The _owner field is not always the Thread addr even with an
1367 // inflated monitor, e.g. the monitor can be inflated by a non-owning
1368 // thread due to contention.
1369 intx ObjectMonitor::complete_exit(JavaThread* current) {
1370   assert(InitDone, "Unexpectedly not initialized");
1371 
1372   void* cur = owner_raw();
1373   if (current != cur) {
1374     if (LockingMode != LM_LIGHTWEIGHT && current->is_lock_owned((address)cur)) {
1375       assert(_recursions == 0, "internal state error");
1376       set_owner_from_BasicLock(cur, current);  // Convert from BasicLock* to Thread*.
1377       _recursions = 0;
1378     }
1379   }
1380 
1381   guarantee(current == owner_raw(), "complete_exit not owner");
1382   intx save = _recursions; // record the old recursion count
1383   _recursions = 0;         // set the recursion level to be 0
1384   exit(current);           // exit the monitor
1385   guarantee(owner_raw() != current, "invariant");
1386   return save;
1387 }
1388 
1389 // reenter() enters a lock and sets recursion count
1390 // complete_exit/reenter operate as a wait without waiting
1391 bool ObjectMonitor::reenter(intx recursions, JavaThread* current) {
1392 
1393   guarantee(owner_raw() != current, "reenter already owner");
1394   if (!enter(current)) {

1403 // Checks that the current THREAD owns this monitor and causes an
1404 // immediate return if it doesn't. We don't use the CHECK macro
1405 // because we want the IMSE to be the only exception that is thrown
1406 // from the call site when false is returned. Any other pending
1407 // exception is ignored.
1408 #define CHECK_OWNER()                                                  \
1409   do {                                                                 \
1410     if (!check_owner(THREAD)) {                                        \
1411        assert(HAS_PENDING_EXCEPTION, "expected a pending IMSE here."); \
1412        return;                                                         \
1413      }                                                                 \
1414   } while (false)
1415 
1416 // Returns true if the specified thread owns the ObjectMonitor.
1417 // Otherwise returns false and throws IllegalMonitorStateException
1418 // (IMSE). If there is a pending exception and the specified thread
1419 // is not the owner, that exception will be replaced by the IMSE.
1420 bool ObjectMonitor::check_owner(TRAPS) {
1421   JavaThread* current = THREAD;
1422   void* cur = owner_raw();
1423   assert(cur != anon_owner_ptr(), "no anon owner here");
1424   if (cur == current) {
1425     return true;
1426   }
1427   if (LockingMode != LM_LIGHTWEIGHT && current->is_lock_owned((address)cur)) {
1428     set_owner_from_BasicLock(cur, current);  // Convert from BasicLock* to Thread*.
1429     _recursions = 0;
1430     return true;
1431   }
1432   THROW_MSG_(vmSymbols::java_lang_IllegalMonitorStateException(),
1433              "current thread is not owner", false);
1434 }
1435 
1436 static inline bool is_excluded(const Klass* monitor_klass) {
1437   assert(monitor_klass != nullptr, "invariant");
1438   NOT_JFR_RETURN_(false);
1439   JFR_ONLY(return vmSymbols::jfr_chunk_rotation_monitor() == monitor_klass->name());
1440 }
1441 
1442 static void post_monitor_wait_event(EventJavaMonitorWait* event,
1443                                     ObjectMonitor* monitor,
1444                                     jlong notifier_tid,
1445                                     jlong timeout,
1446                                     bool timedout) {
1447   assert(event != NULL, "invariant");
< prev index next >