< prev index next >

src/hotspot/share/runtime/objectMonitor.cpp

Print this page
rev 55936 : [mq]: 8229212.cr0
rev 55937 : 8229212: clear up CHECK_OWNER confusion in objectMonitor.cpp
Reviewed-by: dholmes


1126   _recursions = 0;        // set the recursion level to be 0
1127   exit(true, Self);           // exit the monitor
1128   guarantee(_owner != Self, "invariant");
1129   return save;
1130 }
1131 
1132 // reenter() enters a lock and sets recursion count
1133 // complete_exit/reenter operate as a wait without waiting
1134 void ObjectMonitor::reenter(intptr_t recursions, TRAPS) {
1135   Thread * const Self = THREAD;
1136   assert(Self->is_Java_thread(), "Must be Java thread!");
1137   JavaThread *jt = (JavaThread *)THREAD;
1138 
1139   guarantee(_owner != Self, "reenter already owner");
1140   enter(THREAD);       // enter the monitor
1141   guarantee(_recursions == 0, "reenter recursion");
1142   _recursions = recursions;
1143   return;
1144 }
1145 
1146 
1147 // -----------------------------------------------------------------------------
1148 // A macro is used below because there may already be a pending
1149 // exception which should not abort the execution of the routines
1150 // which use this (which is why we don't put this into check_slow and
1151 // call it with a CHECK argument).
1152 
1153 #define CHECK_OWNER()                                                       \
1154   do {                                                                      \
1155     if (THREAD != _owner) {                                                 \
1156       if (THREAD->is_lock_owned((address) _owner)) {                        \
1157         _owner = THREAD;  /* Convert from basiclock addr to Thread addr */  \
1158         _recursions = 0;                                                    \
1159       } else {                                                              \
1160         THROW(vmSymbols::java_lang_IllegalMonitorStateException());         \
1161       }                                                                     \
1162     }                                                                       \
1163   } while (false)
1164 
1165 // check_slow() is a misnomer.  It's called to simply to throw an IMSX exception.
1166 // TODO-FIXME: remove check_slow() -- it's likely dead.
1167 
1168 void ObjectMonitor::check_slow(TRAPS) {
1169   assert(THREAD != _owner && !THREAD->is_lock_owned((address) _owner), "must not be owner");
1170   THROW_MSG(vmSymbols::java_lang_IllegalMonitorStateException(), "current thread not owner");









1171 }
1172 
1173 static void post_monitor_wait_event(EventJavaMonitorWait* event,
1174                                     ObjectMonitor* monitor,
1175                                     jlong notifier_tid,
1176                                     jlong timeout,
1177                                     bool timedout) {
1178   assert(event != NULL, "invariant");
1179   assert(monitor != NULL, "invariant");
1180   event->set_monitorClass(((oop)monitor->object())->klass());
1181   event->set_timeout(timeout);
1182   event->set_address((uintptr_t)monitor->object_addr());
1183   event->set_notifier(notifier_tid);
1184   event->set_timedOut(timedout);
1185   event->commit();
1186 }
1187 
1188 // -----------------------------------------------------------------------------
1189 // Wait/Notify/NotifyAll
1190 //
1191 // Note: a subset of changes to ObjectMonitor::wait()
1192 // will need to be replicated in complete_exit
1193 void ObjectMonitor::wait(jlong millis, bool interruptible, TRAPS) {
1194   Thread * const Self = THREAD;
1195   assert(Self->is_Java_thread(), "Must be Java thread!");
1196   JavaThread *jt = (JavaThread *)THREAD;
1197 
1198   assert(InitDone, "Unexpectedly not initialized");
1199 
1200   // Throw IMSX or IEX.
1201   CHECK_OWNER();
1202 
1203   EventJavaMonitorWait event;
1204 
1205   // check for a pending interrupt
1206   if (interruptible && Thread::is_interrupted(Self, true) && !HAS_PENDING_EXCEPTION) {
1207     // post monitor waited event.  Note that this is past-tense, we are done waiting.
1208     if (JvmtiExport::should_post_monitor_waited()) {
1209       // Note: 'false' parameter is passed here because the
1210       // wait was not timed out due to thread interrupt.
1211       JvmtiExport::post_monitor_waited(jt, this, false);
1212 
1213       // In this short circuit of the monitor wait protocol, the
1214       // current thread never drops ownership of the monitor and
1215       // never gets added to the wait queue so the current thread
1216       // cannot be made the successor. This means that the
1217       // JVMTI_EVENT_MONITOR_WAITED event handler cannot accidentally
1218       // consume an unpark() meant for the ParkEvent associated with
1219       // this ObjectMonitor.
1220     }
1221     if (event.should_commit()) {


1460     // is the only thread that grabs _WaitSetLock.  There's almost no contention
1461     // on _WaitSetLock so it's not profitable to reduce the length of the
1462     // critical section.
1463 
1464     iterator->wait_reenter_begin(this);
1465   }
1466   Thread::SpinRelease(&_WaitSetLock);
1467 }
1468 
1469 // Consider: a not-uncommon synchronization bug is to use notify() when
1470 // notifyAll() is more appropriate, potentially resulting in stranded
1471 // threads; this is one example of a lost wakeup. A useful diagnostic
1472 // option is to force all notify() operations to behave as notifyAll().
1473 //
1474 // Note: We can also detect many such problems with a "minimum wait".
1475 // When the "minimum wait" is set to a small non-zero timeout value
1476 // and the program does not hang whereas it did absent "minimum wait",
1477 // that suggests a lost wakeup bug.
1478 
1479 void ObjectMonitor::notify(TRAPS) {
1480   CHECK_OWNER();
1481   if (_WaitSet == NULL) {
1482     return;
1483   }
1484   DTRACE_MONITOR_PROBE(notify, this, object(), THREAD);
1485   INotify(THREAD);
1486   OM_PERFDATA_OP(Notifications, inc(1));
1487 }
1488 
1489 
1490 // The current implementation of notifyAll() transfers the waiters one-at-a-time
1491 // from the waitset to the EntryList. This could be done more efficiently with a
1492 // single bulk transfer but in practice it's not time-critical. Beware too,
1493 // that in prepend-mode we invert the order of the waiters. Let's say that the
1494 // waitset is "ABCD" and the EntryList is "XYZ". After a notifyAll() in prepend
1495 // mode the waitset will be empty and the EntryList will be "DCBAXYZ".
1496 
1497 void ObjectMonitor::notifyAll(TRAPS) {
1498   CHECK_OWNER();
1499   if (_WaitSet == NULL) {
1500     return;
1501   }
1502 
1503   DTRACE_MONITOR_PROBE(notifyAll, this, object(), THREAD);
1504   int tally = 0;
1505   while (_WaitSet != NULL) {
1506     tally++;
1507     INotify(THREAD);
1508   }
1509 
1510   OM_PERFDATA_OP(Notifications, inc(tally));
1511 }
1512 
1513 // -----------------------------------------------------------------------------
1514 // Adaptive Spinning Support
1515 //
1516 // Adaptive spin-then-block - rational spinning
1517 //
1518 // Note that we spin "globally" on _owner with a classic SMP-polite TATAS




1126   _recursions = 0;        // set the recursion level to be 0
1127   exit(true, Self);           // exit the monitor
1128   guarantee(_owner != Self, "invariant");
1129   return save;
1130 }
1131 
1132 // reenter() enters a lock and sets recursion count
1133 // complete_exit/reenter operate as a wait without waiting
1134 void ObjectMonitor::reenter(intptr_t recursions, TRAPS) {
1135   Thread * const Self = THREAD;
1136   assert(Self->is_Java_thread(), "Must be Java thread!");
1137   JavaThread *jt = (JavaThread *)THREAD;
1138 
1139   guarantee(_owner != Self, "reenter already owner");
1140   enter(THREAD);       // enter the monitor
1141   guarantee(_recursions == 0, "reenter recursion");
1142   _recursions = recursions;
1143   return;
1144 }
1145 
1146 // Checks that the current THREAD owns this monitor and causes an
1147 // immediate return if it doesn't. We don't use the CHECK macro
1148 // because we want the IMSE to be the only exception that is thrown
1149 // from the call site when false is returned. Any other pending
1150 // exception is ignored.


1151 #define CHECK_OWNER()                                                  \
1152   do {                                                                 \
1153     if (!check_owner(THREAD)) {                                        \
1154        assert(HAS_PENDING_EXCEPTION, "expected a pending IMSE here."); \
1155        return;                                                         \




1156      }                                                                 \
1157   } while (false)
1158 
1159 // Returns true if the specified thread owns the ObjectMonitor.
1160 // Otherwise returns false and throws IllegalMonitorStateException
1161 // (IMSE). If there is a pending exception and the specified thread
1162 // is not the owner, that exception will be replaced by the IMSE.
1163 bool ObjectMonitor::check_owner(Thread* THREAD) {
1164   if (_owner == THREAD) {
1165     return true;
1166   }
1167   if (THREAD->is_lock_owned((address)_owner)) {
1168     _owner = THREAD;  // convert from BasicLock addr to Thread addr
1169     _recursions = 0;
1170     return true;
1171   }
1172   THROW_MSG_(vmSymbols::java_lang_IllegalMonitorStateException(),
1173              "current thread is not owner", false);
1174 }
1175 
1176 static void post_monitor_wait_event(EventJavaMonitorWait* event,
1177                                     ObjectMonitor* monitor,
1178                                     jlong notifier_tid,
1179                                     jlong timeout,
1180                                     bool timedout) {
1181   assert(event != NULL, "invariant");
1182   assert(monitor != NULL, "invariant");
1183   event->set_monitorClass(((oop)monitor->object())->klass());
1184   event->set_timeout(timeout);
1185   event->set_address((uintptr_t)monitor->object_addr());
1186   event->set_notifier(notifier_tid);
1187   event->set_timedOut(timedout);
1188   event->commit();
1189 }
1190 
1191 // -----------------------------------------------------------------------------
1192 // Wait/Notify/NotifyAll
1193 //
1194 // Note: a subset of changes to ObjectMonitor::wait()
1195 // will need to be replicated in complete_exit
1196 void ObjectMonitor::wait(jlong millis, bool interruptible, TRAPS) {
1197   Thread * const Self = THREAD;
1198   assert(Self->is_Java_thread(), "Must be Java thread!");
1199   JavaThread *jt = (JavaThread *)THREAD;
1200 
1201   assert(InitDone, "Unexpectedly not initialized");
1202 
1203   CHECK_OWNER();  // Throws IMSE if not owner.

1204 
1205   EventJavaMonitorWait event;
1206 
1207   // check for a pending interrupt
1208   if (interruptible && Thread::is_interrupted(Self, true) && !HAS_PENDING_EXCEPTION) {
1209     // post monitor waited event.  Note that this is past-tense, we are done waiting.
1210     if (JvmtiExport::should_post_monitor_waited()) {
1211       // Note: 'false' parameter is passed here because the
1212       // wait was not timed out due to thread interrupt.
1213       JvmtiExport::post_monitor_waited(jt, this, false);
1214 
1215       // In this short circuit of the monitor wait protocol, the
1216       // current thread never drops ownership of the monitor and
1217       // never gets added to the wait queue so the current thread
1218       // cannot be made the successor. This means that the
1219       // JVMTI_EVENT_MONITOR_WAITED event handler cannot accidentally
1220       // consume an unpark() meant for the ParkEvent associated with
1221       // this ObjectMonitor.
1222     }
1223     if (event.should_commit()) {


1462     // is the only thread that grabs _WaitSetLock.  There's almost no contention
1463     // on _WaitSetLock so it's not profitable to reduce the length of the
1464     // critical section.
1465 
1466     iterator->wait_reenter_begin(this);
1467   }
1468   Thread::SpinRelease(&_WaitSetLock);
1469 }
1470 
1471 // Consider: a not-uncommon synchronization bug is to use notify() when
1472 // notifyAll() is more appropriate, potentially resulting in stranded
1473 // threads; this is one example of a lost wakeup. A useful diagnostic
1474 // option is to force all notify() operations to behave as notifyAll().
1475 //
1476 // Note: We can also detect many such problems with a "minimum wait".
1477 // When the "minimum wait" is set to a small non-zero timeout value
1478 // and the program does not hang whereas it did absent "minimum wait",
1479 // that suggests a lost wakeup bug.
1480 
1481 void ObjectMonitor::notify(TRAPS) {
1482   CHECK_OWNER();  // Throws IMSE if not owner.
1483   if (_WaitSet == NULL) {
1484     return;
1485   }
1486   DTRACE_MONITOR_PROBE(notify, this, object(), THREAD);
1487   INotify(THREAD);
1488   OM_PERFDATA_OP(Notifications, inc(1));
1489 }
1490 
1491 
1492 // The current implementation of notifyAll() transfers the waiters one-at-a-time
1493 // from the waitset to the EntryList. This could be done more efficiently with a
1494 // single bulk transfer but in practice it's not time-critical. Beware too,
1495 // that in prepend-mode we invert the order of the waiters. Let's say that the
1496 // waitset is "ABCD" and the EntryList is "XYZ". After a notifyAll() in prepend
1497 // mode the waitset will be empty and the EntryList will be "DCBAXYZ".
1498 
1499 void ObjectMonitor::notifyAll(TRAPS) {
1500   CHECK_OWNER();  // Throws IMSE if not owner.
1501   if (_WaitSet == NULL) {
1502     return;
1503   }
1504 
1505   DTRACE_MONITOR_PROBE(notifyAll, this, object(), THREAD);
1506   int tally = 0;
1507   while (_WaitSet != NULL) {
1508     tally++;
1509     INotify(THREAD);
1510   }
1511 
1512   OM_PERFDATA_OP(Notifications, inc(tally));
1513 }
1514 
1515 // -----------------------------------------------------------------------------
1516 // Adaptive Spinning Support
1517 //
1518 // Adaptive spin-then-block - rational spinning
1519 //
1520 // Note that we spin "globally" on _owner with a classic SMP-polite TATAS


< prev index next >