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 // Returns true if the specified thread owns the ObjectMonitor. Otherwise
1147 // returns false and throws IllegalMonitorStateException (IMSE). This
1148 // function is not called with CHECK because we want the IMSE to be the
1149 // only exception that is thrown from the call site when false is returned.
1150 // If an IMSE needs to be thrown, then it takes precedence over any pending
1151 // exception at the call sites. If true is returned, then we don't throw
1152 // any exception (pending or otherwise) at the call site.
1153 bool ObjectMonitor::check_owner_and_throw_IMSE_if_not(Thread* THREAD) {
1154 if (_owner == THREAD) {
1155 return true;
1156 }
1157 if (THREAD->is_lock_owned((address)_owner)) {
1158 _owner = THREAD; // convert from BasicLock addr to Thread addr
1159 _recursions = 0;
1160 return true;
1161 }
1162 THROW_MSG_(vmSymbols::java_lang_IllegalMonitorStateException(),
1163 "current thread is not owner", false);
1164 }
1165
1166 static void post_monitor_wait_event(EventJavaMonitorWait* event,
1167 ObjectMonitor* monitor,
1168 jlong notifier_tid,
1169 jlong timeout,
1170 bool timedout) {
1171 assert(event != NULL, "invariant");
1172 assert(monitor != NULL, "invariant");
1173 event->set_monitorClass(((oop)monitor->object())->klass());
1174 event->set_timeout(timeout);
1175 event->set_address((uintptr_t)monitor->object_addr());
1176 event->set_notifier(notifier_tid);
1177 event->set_timedOut(timedout);
1178 event->commit();
1179 }
1180
1181 // -----------------------------------------------------------------------------
1182 // Wait/Notify/NotifyAll
1183 //
1184 // Note: a subset of changes to ObjectMonitor::wait()
1185 // will need to be replicated in complete_exit
1186 void ObjectMonitor::wait(jlong millis, bool interruptible, TRAPS) {
1187 Thread * const Self = THREAD;
1188 assert(Self->is_Java_thread(), "Must be Java thread!");
1189 JavaThread *jt = (JavaThread *)THREAD;
1190
1191 assert(InitDone, "Unexpectedly not initialized");
1192
1193 if (!check_owner_and_throw_IMSE_if_not(THREAD)) {
1194 assert(HAS_PENDING_EXCEPTION, "expected a pending exception here.");
1195 return;
1196 }
1197
1198 EventJavaMonitorWait event;
1199
1200 // check for a pending interrupt
1201 if (interruptible && Thread::is_interrupted(Self, true) && !HAS_PENDING_EXCEPTION) {
1202 // post monitor waited event. Note that this is past-tense, we are done waiting.
1203 if (JvmtiExport::should_post_monitor_waited()) {
1204 // Note: 'false' parameter is passed here because the
1205 // wait was not timed out due to thread interrupt.
1206 JvmtiExport::post_monitor_waited(jt, this, false);
1207
1208 // In this short circuit of the monitor wait protocol, the
1209 // current thread never drops ownership of the monitor and
1210 // never gets added to the wait queue so the current thread
1211 // cannot be made the successor. This means that the
1212 // JVMTI_EVENT_MONITOR_WAITED event handler cannot accidentally
1213 // consume an unpark() meant for the ParkEvent associated with
1214 // this ObjectMonitor.
1215 }
1216 if (event.should_commit()) {
1455 // is the only thread that grabs _WaitSetLock. There's almost no contention
1456 // on _WaitSetLock so it's not profitable to reduce the length of the
1457 // critical section.
1458
1459 iterator->wait_reenter_begin(this);
1460 }
1461 Thread::SpinRelease(&_WaitSetLock);
1462 }
1463
1464 // Consider: a not-uncommon synchronization bug is to use notify() when
1465 // notifyAll() is more appropriate, potentially resulting in stranded
1466 // threads; this is one example of a lost wakeup. A useful diagnostic
1467 // option is to force all notify() operations to behave as notifyAll().
1468 //
1469 // Note: We can also detect many such problems with a "minimum wait".
1470 // When the "minimum wait" is set to a small non-zero timeout value
1471 // and the program does not hang whereas it did absent "minimum wait",
1472 // that suggests a lost wakeup bug.
1473
1474 void ObjectMonitor::notify(TRAPS) {
1475 if (!check_owner_and_throw_IMSE_if_not(THREAD)) {
1476 assert(HAS_PENDING_EXCEPTION, "expected a pending exception here.");
1477 return;
1478 }
1479 if (_WaitSet == NULL) {
1480 return;
1481 }
1482 DTRACE_MONITOR_PROBE(notify, this, object(), THREAD);
1483 INotify(THREAD);
1484 OM_PERFDATA_OP(Notifications, inc(1));
1485 }
1486
1487
1488 // The current implementation of notifyAll() transfers the waiters one-at-a-time
1489 // from the waitset to the EntryList. This could be done more efficiently with a
1490 // single bulk transfer but in practice it's not time-critical. Beware too,
1491 // that in prepend-mode we invert the order of the waiters. Let's say that the
1492 // waitset is "ABCD" and the EntryList is "XYZ". After a notifyAll() in prepend
1493 // mode the waitset will be empty and the EntryList will be "DCBAXYZ".
1494
1495 void ObjectMonitor::notifyAll(TRAPS) {
1496 if (!check_owner_and_throw_IMSE_if_not(THREAD)) {
1497 assert(HAS_PENDING_EXCEPTION, "expected a pending exception here.");
1498 return;
1499 }
1500 if (_WaitSet == NULL) {
1501 return;
1502 }
1503
1504 DTRACE_MONITOR_PROBE(notifyAll, this, object(), THREAD);
1505 int tally = 0;
1506 while (_WaitSet != NULL) {
1507 tally++;
1508 INotify(THREAD);
1509 }
1510
1511 OM_PERFDATA_OP(Notifications, inc(tally));
1512 }
1513
1514 // -----------------------------------------------------------------------------
1515 // Adaptive Spinning Support
1516 //
1517 // Adaptive spin-then-block - rational spinning
1518 //
1519 // 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
|