222 // * See also http://blogs.sun.com/dave
223
224
225 void* ObjectMonitor::operator new (size_t size) throw() {
226 return AllocateHeap(size, mtInternal);
227 }
228 void* ObjectMonitor::operator new[] (size_t size) throw() {
229 return operator new (size);
230 }
231 void ObjectMonitor::operator delete(void* p) {
232 FreeHeap(p);
233 }
234 void ObjectMonitor::operator delete[] (void *p) {
235 operator delete(p);
236 }
237
238 // -----------------------------------------------------------------------------
239 // Enter support
240
241 void ObjectMonitor::enter(TRAPS) {
242 ADIM_guarantee(ref_count() > 0, "must be positive: ref_count=%d", ref_count());
243
244 // The following code is ordered to check the most common cases first
245 // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
246 Thread * const Self = THREAD;
247
248 void* cur = try_set_owner_from(Self, NULL);
249 if (cur == NULL) {
250 assert(_recursions == 0, "invariant");
251 return;
252 }
253
254 if (cur == Self) {
255 // TODO-FIXME: check for integer overflow! BUGID 6557169.
256 _recursions++;
257 return;
258 }
259
260 if (Self->is_lock_owned ((address)cur)) {
261 assert(_recursions == 0, "internal state error");
262 _recursions = 1;
263 set_owner_from_BasicLock(Self, cur); // Convert from BasicLock* to Thread*.
264 return;
265 }
266
267 if (AsyncDeflateIdleMonitors &&
268 try_set_owner_from(Self, DEFLATER_MARKER) == DEFLATER_MARKER) {
269 // The deflation protocol finished the first part (setting owner),
270 // but it failed the second part (making ref_count negative) and
271 // bailed. Or the ObjectMonitor was async deflated and reused.
272 // Acquired the monitor.
273 assert(_recursions == 0, "invariant");
274 return;
275 }
276
277 // We've encountered genuine contention.
278 assert(Self->_Stalled == 0, "invariant");
279 Self->_Stalled = intptr_t(this);
280
281 // Try one round of spinning *before* enqueueing Self
282 // and before going through the awkward and expensive state
283 // transitions. The following spin is strictly optional ...
284 // Note that if we acquire the monitor from an initial spin
285 // we forgo posting JVMTI events and firing DTRACE probes.
286 if (TrySpin(Self) > 0) {
287 assert(_owner == Self, "must be Self: owner=" INTPTR_FORMAT, p2i(_owner));
288 assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
289 assert(((oop)object())->mark() == markWord::encode(this),
290 "object mark must match encoded this: mark=" INTPTR_FORMAT
291 ", encoded this=" INTPTR_FORMAT, ((oop)object())->mark().value(),
292 markWord::encode(this).value());
293 Self->_Stalled = 0;
294 return;
295 }
296
297 assert(_owner != Self, "invariant");
298 assert(_succ != Self, "invariant");
299 assert(Self->is_Java_thread(), "invariant");
300 JavaThread * jt = (JavaThread *) Self;
301 assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
302 assert(jt->thread_state() != _thread_blocked, "invariant");
303 assert(AsyncDeflateIdleMonitors || this->object() != NULL, "invariant");
304 assert(_contentions >= 0, "must not be negative: contentions=%d", _contentions);
305
306 // Prevent deflation. See ObjectSynchronizer::deflate_monitor(),
307 // ObjectSynchronizer::deflate_monitor_using_JT() and is_busy().
308 // Ensure the object <-> monitor relationship remains stable while
309 // there's contention.
310 Atomic::add(1, &_contentions);
311
312 JFR_ONLY(JfrConditionalFlushWithStacktrace<EventJavaMonitorEnter> flush(jt);)
313 EventJavaMonitorEnter event;
314 if (event.should_commit()) {
315 event.set_monitorClass(((oop)this->object())->klass());
316 event.set_address((uintptr_t)(this->object_addr()));
317 }
318
319 { // Change java thread status to indicate blocked on monitor enter.
320 JavaThreadBlockedOnMonitorEnterState jtbmes(jt, this);
321
322 Self->set_current_pending_monitor(this);
323
489 // reach this point, but only one can win.
490 obj->cas_set_mark(dmw, markWord::encode(this));
491
492 // Note: It does not matter which thread restored the header/dmw
493 // into the object's header. The thread deflating the monitor just
494 // wanted the object's header restored and it is. The threads that
495 // detected a race with the deflation process also wanted the
496 // object's header restored before they retry their operation and
497 // because it is restored they will only retry once.
498 }
499
500 // Convert the fields used by is_busy() to a string that can be
501 // used for diagnostic output.
502 const char* ObjectMonitor::is_busy_to_string(stringStream* ss) {
503 ss->print("is_busy: contentions=%d, waiters=%d, ", _contentions, _waiters);
504 if (!AsyncDeflateIdleMonitors) {
505 ss->print("owner=" INTPTR_FORMAT, p2i(_owner));
506 } else if (_owner != DEFLATER_MARKER) {
507 ss->print("owner=" INTPTR_FORMAT, p2i(_owner));
508 } else {
509 ss->print("owner=" INTPTR_FORMAT, NULL);
510 }
511 ss->print(", cxq=" INTPTR_FORMAT ", EntryList=" INTPTR_FORMAT, p2i(_cxq),
512 p2i(_EntryList));
513 return ss->base();
514 }
515
516 #define MAX_RECHECK_INTERVAL 1000
517
518 void ObjectMonitor::EnterI(TRAPS) {
519 ADIM_guarantee(ref_count() > 0, "must be positive: ref_count=%d", ref_count());
520
521 Thread * const Self = THREAD;
522 assert(Self->is_Java_thread(), "invariant");
523 assert(((JavaThread *) Self)->thread_state() == _thread_blocked, "invariant");
524
525 // Try the lock - TATAS
526 if (TryLock (Self) > 0) {
527 assert(_succ != Self, "invariant");
528 assert(_owner == Self, "invariant");
529 assert(_Responsible != Self, "invariant");
530 return;
531 }
532
533 if (AsyncDeflateIdleMonitors &&
534 try_set_owner_from(Self, DEFLATER_MARKER) == DEFLATER_MARKER) {
535 // The deflation protocol finished the first part (setting owner),
536 // but it failed the second part (making ref_count negative) and
537 // bailed. Or the ObjectMonitor was async deflated and reused.
538 // Acquired the monitor.
539 assert(_succ != Self, "invariant");
753 // the lock. The barrier ensures that changes to monitor meta-data and data
754 // protected by the lock will be visible before we release the lock, and
755 // therefore before some other thread (CPU) has a chance to acquire the lock.
756 // See also: http://gee.cs.oswego.edu/dl/jmm/cookbook.html.
757 //
758 // Critically, any prior STs to _succ or EntryList must be visible before
759 // the ST of null into _owner in the *subsequent* (following) corresponding
760 // monitorexit. Recall too, that in 1-0 mode monitorexit does not necessarily
761 // execute a serializing instruction.
762
763 return;
764 }
765
766 // ReenterI() is a specialized inline form of the latter half of the
767 // contended slow-path from EnterI(). We use ReenterI() only for
768 // monitor reentry in wait().
769 //
770 // In the future we should reconcile EnterI() and ReenterI().
771
772 void ObjectMonitor::ReenterI(Thread * Self, ObjectWaiter * SelfNode) {
773 ADIM_guarantee(ref_count() > 0, "must be positive: ref_count=%d", ref_count());
774
775 assert(Self != NULL, "invariant");
776 assert(SelfNode != NULL, "invariant");
777 assert(SelfNode->_thread == Self, "invariant");
778 assert(_waiters > 0, "invariant");
779 assert(((oop)(object()))->mark() == markWord::encode(this), "invariant");
780 assert(((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant");
781 JavaThread * jt = (JavaThread *) Self;
782
783 int nWakeups = 0;
784 for (;;) {
785 ObjectWaiter::TStates v = SelfNode->TState;
786 guarantee(v == ObjectWaiter::TS_ENTER || v == ObjectWaiter::TS_CXQ, "invariant");
787 assert(_owner != Self, "invariant");
788
789 if (TryLock(Self) > 0) break;
790 if (TrySpin(Self) > 0) break;
791
792 if (AsyncDeflateIdleMonitors &&
793 try_set_owner_from(Self, DEFLATER_MARKER) == DEFLATER_MARKER) {
973 // is longer. Critically, we don't want to put excessive load in the
974 // platform's timer subsystem. We want to minimize both the timer injection
975 // rate (timers created/sec) as well as the number of timers active at
976 // any one time. (more precisely, we want to minimize timer-seconds, which is
977 // the integral of the # of active timers at any instant over time).
978 // Both impinge on OS scalability. Given that, at most one thread parked on
979 // a monitor will use a timer.
980 //
981 // There is also the risk of a futile wake-up. If we drop the lock
982 // another thread can reacquire the lock immediately, and we can
983 // then wake a thread unnecessarily. This is benign, and we've
984 // structured the code so the windows are short and the frequency
985 // of such futile wakups is low.
986
987 void ObjectMonitor::exit(bool not_suspended, TRAPS) {
988 Thread * const Self = THREAD;
989 if (THREAD != _owner) {
990 void* cur = _owner;
991 if (THREAD->is_lock_owned((address)cur)) {
992 assert(_recursions == 0, "invariant");
993 set_owner_from_BasicLock(Self, cur); // Convert from BasicLock* to Thread*.
994 _recursions = 0;
995 } else {
996 // Apparent unbalanced locking ...
997 // Naively we'd like to throw IllegalMonitorStateException.
998 // As a practical matter we can neither allocate nor throw an
999 // exception as ::exit() can be called from leaf routines.
1000 // see x86_32.ad Fast_Unlock() and the I1 and I2 properties.
1001 // Upon deeper reflection, however, in a properly run JVM the only
1002 // way we should encounter this situation is in the presence of
1003 // unbalanced JNI locking. TODO: CheckJNICalls.
1004 // See also: CR4414101
1005 tty->print_cr("ERROR: ObjectMonitor::exit(): thread=" INTPTR_FORMAT
1006 " is exiting an ObjectMonitor it does not own.",
1007 p2i(THREAD));
1008 tty->print_cr("The imbalance is possibly caused by JNI locking.");
1009 print_debug_style_on(tty);
1010 // Changing this from an assert() to ADIM_guarantee() may run
1011 // afoul of any test that is inducing non-balanced JNI locking.
1012 ADIM_guarantee(false, "Non-balanced monitor enter/exit!");
1013 return;
1019 return;
1020 }
1021
1022 // Invariant: after setting Responsible=null an thread must execute
1023 // a MEMBAR or other serializing instruction before fetching EntryList|cxq.
1024 _Responsible = NULL;
1025
1026 #if INCLUDE_JFR
1027 // get the owner's thread id for the MonitorEnter event
1028 // if it is enabled and the thread isn't suspended
1029 if (not_suspended && EventJavaMonitorEnter::is_enabled()) {
1030 _previous_owner_tid = JFR_THREAD_ID(Self);
1031 }
1032 #endif
1033
1034 for (;;) {
1035 assert(THREAD == _owner, "invariant");
1036
1037 // release semantics: prior loads and stores from within the critical section
1038 // must not float (reorder) past the following store that drops the lock.
1039 // On SPARC that requires MEMBAR #loadstore|#storestore.
1040 // But of course in TSO #loadstore|#storestore is not required.
1041 if (AsyncDeflateIdleMonitors) {
1042 set_owner_from(NULL, Self);
1043 } else {
1044 OrderAccess::release_store(&_owner, (void*)NULL); // drop the lock
1045 OrderAccess::storeload(); // See if we need to wake a successor
1046 }
1047 if ((intptr_t(_EntryList)|intptr_t(_cxq)) == 0 || _succ != NULL) {
1048 return;
1049 }
1050 // Other threads are blocked trying to acquire the lock.
1051
1052 // Normally the exiting thread is responsible for ensuring succession,
1053 // but if other successors are ready or other entering threads are spinning
1054 // then this thread can simply store NULL into _owner and exit without
1055 // waking a successor. The existence of spinners or ready successors
1056 // guarantees proper succession (liveness). Responsibility passes to the
1057 // ready or running successors. The exiting thread delegates the duty.
1058 // More precisely, if a successor already exists this thread is absolved
1059 // of the responsibility of waking (unparking) one.
1060 //
1236 // -----------------------------------------------------------------------------
1237 // Class Loader deadlock handling.
1238 //
1239 // complete_exit exits a lock returning recursion count
1240 // complete_exit/reenter operate as a wait without waiting
1241 // complete_exit requires an inflated monitor
1242 // The _owner field is not always the Thread addr even with an
1243 // inflated monitor, e.g. the monitor can be inflated by a non-owning
1244 // thread due to contention.
1245 intptr_t ObjectMonitor::complete_exit(TRAPS) {
1246 Thread * const Self = THREAD;
1247 assert(Self->is_Java_thread(), "Must be Java thread!");
1248 JavaThread *jt = (JavaThread *)THREAD;
1249
1250 assert(InitDone, "Unexpectedly not initialized");
1251
1252 if (THREAD != _owner) {
1253 void* cur = _owner;
1254 if (THREAD->is_lock_owned((address)cur)) {
1255 assert(_recursions == 0, "internal state error");
1256 set_owner_from_BasicLock(Self, cur); // Convert from BasicLock* to Thread*.
1257 _recursions = 0;
1258 }
1259 }
1260
1261 guarantee(Self == _owner, "complete_exit not owner");
1262 intptr_t save = _recursions; // record the old recursion count
1263 _recursions = 0; // set the recursion level to be 0
1264 exit(true, Self); // exit the monitor
1265 guarantee(_owner != Self, "invariant");
1266 return save;
1267 }
1268
1269 // reenter() enters a lock and sets recursion count
1270 // complete_exit/reenter operate as a wait without waiting
1271 void ObjectMonitor::reenter(intptr_t recursions, TRAPS) {
1272 Thread * const Self = THREAD;
1273 assert(Self->is_Java_thread(), "Must be Java thread!");
1274 JavaThread *jt = (JavaThread *)THREAD;
1275
1276 guarantee(_owner != Self, "reenter already owner");
1286 // from the call site when false is returned. Any other pending
1287 // exception is ignored.
1288 #define CHECK_OWNER() \
1289 do { \
1290 if (!check_owner(THREAD)) { \
1291 assert(HAS_PENDING_EXCEPTION, "expected a pending IMSE here."); \
1292 return; \
1293 } \
1294 } while (false)
1295
1296 // Returns true if the specified thread owns the ObjectMonitor.
1297 // Otherwise returns false and throws IllegalMonitorStateException
1298 // (IMSE). If there is a pending exception and the specified thread
1299 // is not the owner, that exception will be replaced by the IMSE.
1300 bool ObjectMonitor::check_owner(Thread* THREAD) {
1301 if (_owner == THREAD) {
1302 return true;
1303 }
1304 void* cur = _owner;
1305 if (THREAD->is_lock_owned((address)cur)) {
1306 set_owner_from_BasicLock(THREAD, cur); // Convert from BasicLock* to Thread*.
1307 _recursions = 0;
1308 return true;
1309 }
1310 THROW_MSG_(vmSymbols::java_lang_IllegalMonitorStateException(),
1311 "current thread is not owner", false);
1312 }
1313
1314 static void post_monitor_wait_event(EventJavaMonitorWait* event,
1315 ObjectMonitor* monitor,
1316 jlong notifier_tid,
1317 jlong timeout,
1318 bool timedout) {
1319 assert(event != NULL, "invariant");
1320 assert(monitor != NULL, "invariant");
1321 event->set_monitorClass(((oop)monitor->object())->klass());
1322 event->set_timeout(timeout);
1323 event->set_address((uintptr_t)monitor->object_addr());
1324 event->set_notifier(notifier_tid);
1325 event->set_timedOut(timedout);
1326 event->commit();
2055 }
2056 #define NEWPERFVARIABLE(n) \
2057 { \
2058 n = PerfDataManager::create_variable(SUN_RT, #n, PerfData::U_Events, \
2059 CHECK); \
2060 }
2061 NEWPERFCOUNTER(_sync_Inflations);
2062 NEWPERFCOUNTER(_sync_Deflations);
2063 NEWPERFCOUNTER(_sync_ContendedLockAttempts);
2064 NEWPERFCOUNTER(_sync_FutileWakeups);
2065 NEWPERFCOUNTER(_sync_Parks);
2066 NEWPERFCOUNTER(_sync_Notifications);
2067 NEWPERFVARIABLE(_sync_MonExtant);
2068 #undef NEWPERFCOUNTER
2069 #undef NEWPERFVARIABLE
2070 }
2071
2072 DEBUG_ONLY(InitDone = true;)
2073 }
2074
2075 // For internal use by ObjectSynchronizer::monitors_iterate().
2076 ObjectMonitorHandle::ObjectMonitorHandle(ObjectMonitor * om_ptr) {
2077 om_ptr->inc_ref_count();
2078 _om_ptr = om_ptr;
2079 }
2080
2081 ObjectMonitorHandle::~ObjectMonitorHandle() {
2082 if (_om_ptr != NULL) {
2083 _om_ptr->dec_ref_count();
2084 _om_ptr = NULL;
2085 }
2086 }
2087
2088 // Save the ObjectMonitor* associated with the specified markWord and
2089 // increment the ref_count. This function should only be called if
2090 // the caller has verified mark.has_monitor() == true. The object
2091 // parameter is needed to verify that ObjectMonitor* has not been
2092 // deflated and reused for another object.
2093 //
2094 // This function returns true if the ObjectMonitor* has been safely
2095 // saved. This function returns false if we have lost a race with
2096 // async deflation; the caller should retry as appropriate.
2097 //
2098 bool ObjectMonitorHandle::save_om_ptr(oop object, markWord mark) {
2099 guarantee(mark.has_monitor(), "sanity check: mark=" INTPTR_FORMAT,
2100 mark.value());
2101
2102 ObjectMonitor * om_ptr = mark.monitor();
2103 om_ptr->inc_ref_count();
2104
2105 if (AsyncDeflateIdleMonitors) {
2106 // Race here if monitor is not owned! The above ref_count bump
2107 // will cause subsequent async deflation to skip it. However,
2108 // previous or concurrent async deflation is a race.
2109 if (om_ptr->owner_is_DEFLATER_MARKER() && om_ptr->ref_count() <= 0) {
2110 // Async deflation is in progress and our ref_count increment
2111 // above lost the race to async deflation. Attempt to restore
2112 // the header/dmw to the object's header so that we only retry
2113 // once if the deflater thread happens to be slow.
2114 om_ptr->install_displaced_markword_in_object(object);
2115 om_ptr->dec_ref_count();
2116 return false;
2117 }
2118 if (om_ptr->ref_count() <= 0) {
2119 // Async deflation is in the process of bailing out, but has not
2120 // yet restored the ref_count field so we return false to force
2121 // a retry. We want a positive ref_count value for a true return.
2122 om_ptr->dec_ref_count();
2124 }
2125 // The ObjectMonitor could have been deflated and reused for
2126 // another object before we bumped the ref_count so make sure
2127 // our object still refers to this ObjectMonitor.
2128 const markWord tmp = object->mark();
2129 if (!tmp.has_monitor() || tmp.monitor() != om_ptr) {
2130 // Async deflation and reuse won the race so we have to retry.
2131 // Skip object header restoration since that's already done.
2132 om_ptr->dec_ref_count();
2133 return false;
2134 }
2135 }
2136
2137 ADIM_guarantee(_om_ptr == NULL, "sanity check: _om_ptr=" INTPTR_FORMAT,
2138 p2i(_om_ptr));
2139 _om_ptr = om_ptr;
2140 return true;
2141 }
2142
2143 // For internal use by ObjectSynchronizer::inflate().
2144 void ObjectMonitorHandle::set_om_ptr(ObjectMonitor * om_ptr) {
2145 if (_om_ptr == NULL) {
2146 ADIM_guarantee(om_ptr != NULL, "cannot clear an unset om_ptr");
2147 om_ptr->inc_ref_count();
2148 _om_ptr = om_ptr;
2149 } else {
2150 ADIM_guarantee(om_ptr == NULL, "can only clear a set om_ptr");
2151 _om_ptr->dec_ref_count();
2152 _om_ptr = NULL;
2153 }
2154 }
2155
2156 void ObjectMonitor::print_on(outputStream* st) const {
2157 // The minimal things to print for markWord printing, more can be added for debugging and logging.
2158 st->print("{contentions=0x%08x,waiters=0x%08x"
2159 ",recursions=" INTX_FORMAT ",owner=" INTPTR_FORMAT "}",
2160 contentions(), waiters(), recursions(),
2161 p2i(owner()));
2162 }
2163 void ObjectMonitor::print() const { print_on(tty); }
2164
2165 // Print the ObjectMonitor like a debugger would:
2166 //
2167 // (ObjectMonitor) 0x00007fdfb6012e40 = {
2168 // _header = (_value = 1)
2169 // _object = 0x000000070ff45fd0
2170 // _allocation_state = Old
2171 // _pad_buf0 = {
2172 // [0] = '\0'
2173 // ...
2174 // [43] = '\0'
2175 // }
2184 // _pad_buf2 = {
2185 // [0] = '\0'
2186 // ...
2187 // [59] = '\0'
2188 // }
2189 // _next_om = 0x0000000000000000
2190 // _recursions = 0
2191 // _EntryList = 0x0000000000000000
2192 // _cxq = 0x0000000000000000
2193 // _succ = 0x0000000000000000
2194 // _Responsible = 0x0000000000000000
2195 // _Spinner = 0
2196 // _SpinDuration = 5000
2197 // _contentions = 0
2198 // _WaitSet = 0x0000700009756248
2199 // _waiters = 1
2200 // _WaitSetLock = 0
2201 // }
2202 //
2203 void ObjectMonitor::print_debug_style_on(outputStream* st) const {
2204 st->print_cr("(ObjectMonitor *) " INTPTR_FORMAT " = {", p2i(this));
2205 st->print_cr(" _header = " INTPTR_FORMAT, header().value());
2206 st->print_cr(" _object = " INTPTR_FORMAT, p2i(_object));
2207 st->print(" _allocation_state = ");
2208 if (is_free()) {
2209 st->print("Free");
2210 } else if (is_old()) {
2211 st->print("Old");
2212 } else if (is_new()) {
2213 st->print("New");
2214 } else {
2215 st->print("unknown=%d", _allocation_state);
2216 }
2217 st->cr();
2218 st->print_cr(" _pad_buf0 = {");
2219 st->print_cr(" [0] = '\\0'");
2220 st->print_cr(" ...");
2221 st->print_cr(" [%d] = '\\0'", (int)sizeof(_pad_buf0) - 1);
2222 st->print_cr(" }");
2223 st->print_cr(" _owner = " INTPTR_FORMAT, p2i(_owner));
2224 st->print_cr(" _previous_owner_tid = " JLONG_FORMAT, _previous_owner_tid);
|
222 // * See also http://blogs.sun.com/dave
223
224
225 void* ObjectMonitor::operator new (size_t size) throw() {
226 return AllocateHeap(size, mtInternal);
227 }
228 void* ObjectMonitor::operator new[] (size_t size) throw() {
229 return operator new (size);
230 }
231 void ObjectMonitor::operator delete(void* p) {
232 FreeHeap(p);
233 }
234 void ObjectMonitor::operator delete[] (void *p) {
235 operator delete(p);
236 }
237
238 // -----------------------------------------------------------------------------
239 // Enter support
240
241 void ObjectMonitor::enter(TRAPS) {
242 jint l_ref_count = ref_count();
243 ADIM_guarantee(l_ref_count > 0, "must be positive: l_ref_count=%d, ref_count=%d", l_ref_count, ref_count());
244
245 // The following code is ordered to check the most common cases first
246 // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
247 Thread * const Self = THREAD;
248
249 void* cur = try_set_owner_from(Self, NULL);
250 if (cur == NULL) {
251 assert(_recursions == 0, "invariant");
252 return;
253 }
254
255 if (cur == Self) {
256 // TODO-FIXME: check for integer overflow! BUGID 6557169.
257 _recursions++;
258 return;
259 }
260
261 if (Self->is_lock_owned((address)cur)) {
262 assert(_recursions == 0, "internal state error");
263 _recursions = 1;
264 simply_set_owner_from_BasicLock(Self, cur); // Convert from BasicLock* to Thread*.
265 return;
266 }
267
268 if (AsyncDeflateIdleMonitors &&
269 try_set_owner_from(Self, DEFLATER_MARKER) == DEFLATER_MARKER) {
270 // The deflation protocol finished the first part (setting owner),
271 // but it failed the second part (making ref_count negative) and
272 // bailed. Or the ObjectMonitor was async deflated and reused.
273 // Acquired the monitor.
274 assert(_recursions == 0, "invariant");
275 return;
276 }
277
278 // We've encountered genuine contention.
279 assert(Self->_Stalled == 0, "invariant");
280 Self->_Stalled = intptr_t(this);
281
282 // Try one round of spinning *before* enqueueing Self
283 // and before going through the awkward and expensive state
284 // transitions. The following spin is strictly optional ...
285 // Note that if we acquire the monitor from an initial spin
286 // we forgo posting JVMTI events and firing DTRACE probes.
287 if (TrySpin(Self) > 0) {
288 assert(_owner == Self, "must be Self: owner=" INTPTR_FORMAT, p2i(_owner));
289 assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
290 assert(((oop)object())->mark() == markWord::encode(this),
291 "object mark must match encoded this: mark=" INTPTR_FORMAT
292 ", encoded this=" INTPTR_FORMAT, ((oop)object())->mark().value(),
293 markWord::encode(this).value());
294 Self->_Stalled = 0;
295 return;
296 }
297
298 assert(_owner != Self, "invariant");
299 assert(_succ != Self, "invariant");
300 assert(Self->is_Java_thread(), "invariant");
301 JavaThread * jt = (JavaThread *) Self;
302 assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
303 assert(jt->thread_state() != _thread_blocked, "invariant");
304 assert(this->object() != NULL, "invariant");
305 assert(_contentions >= 0, "must not be negative: contentions=%d", _contentions);
306
307 // Prevent deflation. See ObjectSynchronizer::deflate_monitor(),
308 // ObjectSynchronizer::deflate_monitor_using_JT() and is_busy().
309 // Ensure the object <-> monitor relationship remains stable while
310 // there's contention.
311 Atomic::add(1, &_contentions);
312
313 JFR_ONLY(JfrConditionalFlushWithStacktrace<EventJavaMonitorEnter> flush(jt);)
314 EventJavaMonitorEnter event;
315 if (event.should_commit()) {
316 event.set_monitorClass(((oop)this->object())->klass());
317 event.set_address((uintptr_t)(this->object_addr()));
318 }
319
320 { // Change java thread status to indicate blocked on monitor enter.
321 JavaThreadBlockedOnMonitorEnterState jtbmes(jt, this);
322
323 Self->set_current_pending_monitor(this);
324
490 // reach this point, but only one can win.
491 obj->cas_set_mark(dmw, markWord::encode(this));
492
493 // Note: It does not matter which thread restored the header/dmw
494 // into the object's header. The thread deflating the monitor just
495 // wanted the object's header restored and it is. The threads that
496 // detected a race with the deflation process also wanted the
497 // object's header restored before they retry their operation and
498 // because it is restored they will only retry once.
499 }
500
501 // Convert the fields used by is_busy() to a string that can be
502 // used for diagnostic output.
503 const char* ObjectMonitor::is_busy_to_string(stringStream* ss) {
504 ss->print("is_busy: contentions=%d, waiters=%d, ", _contentions, _waiters);
505 if (!AsyncDeflateIdleMonitors) {
506 ss->print("owner=" INTPTR_FORMAT, p2i(_owner));
507 } else if (_owner != DEFLATER_MARKER) {
508 ss->print("owner=" INTPTR_FORMAT, p2i(_owner));
509 } else {
510 // We report NULL instead of DEFLATER_MARKER here because is_busy()
511 // ignores DEFLATER_MARKER values.
512 ss->print("owner=" INTPTR_FORMAT, NULL);
513 }
514 ss->print(", cxq=" INTPTR_FORMAT ", EntryList=" INTPTR_FORMAT, p2i(_cxq),
515 p2i(_EntryList));
516 return ss->base();
517 }
518
519 #define MAX_RECHECK_INTERVAL 1000
520
521 void ObjectMonitor::EnterI(TRAPS) {
522 jint l_ref_count = ref_count();
523 ADIM_guarantee(l_ref_count > 0, "must be positive: l_ref_count=%d, ref_count=%d", l_ref_count, ref_count());
524
525 Thread * const Self = THREAD;
526 assert(Self->is_Java_thread(), "invariant");
527 assert(((JavaThread *) Self)->thread_state() == _thread_blocked, "invariant");
528
529 // Try the lock - TATAS
530 if (TryLock (Self) > 0) {
531 assert(_succ != Self, "invariant");
532 assert(_owner == Self, "invariant");
533 assert(_Responsible != Self, "invariant");
534 return;
535 }
536
537 if (AsyncDeflateIdleMonitors &&
538 try_set_owner_from(Self, DEFLATER_MARKER) == DEFLATER_MARKER) {
539 // The deflation protocol finished the first part (setting owner),
540 // but it failed the second part (making ref_count negative) and
541 // bailed. Or the ObjectMonitor was async deflated and reused.
542 // Acquired the monitor.
543 assert(_succ != Self, "invariant");
757 // the lock. The barrier ensures that changes to monitor meta-data and data
758 // protected by the lock will be visible before we release the lock, and
759 // therefore before some other thread (CPU) has a chance to acquire the lock.
760 // See also: http://gee.cs.oswego.edu/dl/jmm/cookbook.html.
761 //
762 // Critically, any prior STs to _succ or EntryList must be visible before
763 // the ST of null into _owner in the *subsequent* (following) corresponding
764 // monitorexit. Recall too, that in 1-0 mode monitorexit does not necessarily
765 // execute a serializing instruction.
766
767 return;
768 }
769
770 // ReenterI() is a specialized inline form of the latter half of the
771 // contended slow-path from EnterI(). We use ReenterI() only for
772 // monitor reentry in wait().
773 //
774 // In the future we should reconcile EnterI() and ReenterI().
775
776 void ObjectMonitor::ReenterI(Thread * Self, ObjectWaiter * SelfNode) {
777 jint l_ref_count = ref_count();
778 ADIM_guarantee(l_ref_count > 0, "must be positive: l_ref_count=%d, ref_count=%d", l_ref_count, ref_count());
779
780 assert(Self != NULL, "invariant");
781 assert(SelfNode != NULL, "invariant");
782 assert(SelfNode->_thread == Self, "invariant");
783 assert(_waiters > 0, "invariant");
784 assert(((oop)(object()))->mark() == markWord::encode(this), "invariant");
785 assert(((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant");
786 JavaThread * jt = (JavaThread *) Self;
787
788 int nWakeups = 0;
789 for (;;) {
790 ObjectWaiter::TStates v = SelfNode->TState;
791 guarantee(v == ObjectWaiter::TS_ENTER || v == ObjectWaiter::TS_CXQ, "invariant");
792 assert(_owner != Self, "invariant");
793
794 if (TryLock(Self) > 0) break;
795 if (TrySpin(Self) > 0) break;
796
797 if (AsyncDeflateIdleMonitors &&
798 try_set_owner_from(Self, DEFLATER_MARKER) == DEFLATER_MARKER) {
978 // is longer. Critically, we don't want to put excessive load in the
979 // platform's timer subsystem. We want to minimize both the timer injection
980 // rate (timers created/sec) as well as the number of timers active at
981 // any one time. (more precisely, we want to minimize timer-seconds, which is
982 // the integral of the # of active timers at any instant over time).
983 // Both impinge on OS scalability. Given that, at most one thread parked on
984 // a monitor will use a timer.
985 //
986 // There is also the risk of a futile wake-up. If we drop the lock
987 // another thread can reacquire the lock immediately, and we can
988 // then wake a thread unnecessarily. This is benign, and we've
989 // structured the code so the windows are short and the frequency
990 // of such futile wakups is low.
991
992 void ObjectMonitor::exit(bool not_suspended, TRAPS) {
993 Thread * const Self = THREAD;
994 if (THREAD != _owner) {
995 void* cur = _owner;
996 if (THREAD->is_lock_owned((address)cur)) {
997 assert(_recursions == 0, "invariant");
998 simply_set_owner_from_BasicLock(Self, cur); // Convert from BasicLock* to Thread*.
999 _recursions = 0;
1000 } else {
1001 // Apparent unbalanced locking ...
1002 // Naively we'd like to throw IllegalMonitorStateException.
1003 // As a practical matter we can neither allocate nor throw an
1004 // exception as ::exit() can be called from leaf routines.
1005 // see x86_32.ad Fast_Unlock() and the I1 and I2 properties.
1006 // Upon deeper reflection, however, in a properly run JVM the only
1007 // way we should encounter this situation is in the presence of
1008 // unbalanced JNI locking. TODO: CheckJNICalls.
1009 // See also: CR4414101
1010 tty->print_cr("ERROR: ObjectMonitor::exit(): thread=" INTPTR_FORMAT
1011 " is exiting an ObjectMonitor it does not own.",
1012 p2i(THREAD));
1013 tty->print_cr("The imbalance is possibly caused by JNI locking.");
1014 print_debug_style_on(tty);
1015 // Changing this from an assert() to ADIM_guarantee() may run
1016 // afoul of any test that is inducing non-balanced JNI locking.
1017 ADIM_guarantee(false, "Non-balanced monitor enter/exit!");
1018 return;
1024 return;
1025 }
1026
1027 // Invariant: after setting Responsible=null an thread must execute
1028 // a MEMBAR or other serializing instruction before fetching EntryList|cxq.
1029 _Responsible = NULL;
1030
1031 #if INCLUDE_JFR
1032 // get the owner's thread id for the MonitorEnter event
1033 // if it is enabled and the thread isn't suspended
1034 if (not_suspended && EventJavaMonitorEnter::is_enabled()) {
1035 _previous_owner_tid = JFR_THREAD_ID(Self);
1036 }
1037 #endif
1038
1039 for (;;) {
1040 assert(THREAD == _owner, "invariant");
1041
1042 // release semantics: prior loads and stores from within the critical section
1043 // must not float (reorder) past the following store that drops the lock.
1044 if (AsyncDeflateIdleMonitors) {
1045 set_owner_from(NULL, Self);
1046 } else {
1047 OrderAccess::release_store(&_owner, (void*)NULL); // drop the lock
1048 OrderAccess::storeload(); // See if we need to wake a successor
1049 }
1050 if ((intptr_t(_EntryList)|intptr_t(_cxq)) == 0 || _succ != NULL) {
1051 return;
1052 }
1053 // Other threads are blocked trying to acquire the lock.
1054
1055 // Normally the exiting thread is responsible for ensuring succession,
1056 // but if other successors are ready or other entering threads are spinning
1057 // then this thread can simply store NULL into _owner and exit without
1058 // waking a successor. The existence of spinners or ready successors
1059 // guarantees proper succession (liveness). Responsibility passes to the
1060 // ready or running successors. The exiting thread delegates the duty.
1061 // More precisely, if a successor already exists this thread is absolved
1062 // of the responsibility of waking (unparking) one.
1063 //
1239 // -----------------------------------------------------------------------------
1240 // Class Loader deadlock handling.
1241 //
1242 // complete_exit exits a lock returning recursion count
1243 // complete_exit/reenter operate as a wait without waiting
1244 // complete_exit requires an inflated monitor
1245 // The _owner field is not always the Thread addr even with an
1246 // inflated monitor, e.g. the monitor can be inflated by a non-owning
1247 // thread due to contention.
1248 intptr_t ObjectMonitor::complete_exit(TRAPS) {
1249 Thread * const Self = THREAD;
1250 assert(Self->is_Java_thread(), "Must be Java thread!");
1251 JavaThread *jt = (JavaThread *)THREAD;
1252
1253 assert(InitDone, "Unexpectedly not initialized");
1254
1255 if (THREAD != _owner) {
1256 void* cur = _owner;
1257 if (THREAD->is_lock_owned((address)cur)) {
1258 assert(_recursions == 0, "internal state error");
1259 simply_set_owner_from_BasicLock(Self, cur); // Convert from BasicLock* to Thread*.
1260 _recursions = 0;
1261 }
1262 }
1263
1264 guarantee(Self == _owner, "complete_exit not owner");
1265 intptr_t save = _recursions; // record the old recursion count
1266 _recursions = 0; // set the recursion level to be 0
1267 exit(true, Self); // exit the monitor
1268 guarantee(_owner != Self, "invariant");
1269 return save;
1270 }
1271
1272 // reenter() enters a lock and sets recursion count
1273 // complete_exit/reenter operate as a wait without waiting
1274 void ObjectMonitor::reenter(intptr_t recursions, TRAPS) {
1275 Thread * const Self = THREAD;
1276 assert(Self->is_Java_thread(), "Must be Java thread!");
1277 JavaThread *jt = (JavaThread *)THREAD;
1278
1279 guarantee(_owner != Self, "reenter already owner");
1289 // from the call site when false is returned. Any other pending
1290 // exception is ignored.
1291 #define CHECK_OWNER() \
1292 do { \
1293 if (!check_owner(THREAD)) { \
1294 assert(HAS_PENDING_EXCEPTION, "expected a pending IMSE here."); \
1295 return; \
1296 } \
1297 } while (false)
1298
1299 // Returns true if the specified thread owns the ObjectMonitor.
1300 // Otherwise returns false and throws IllegalMonitorStateException
1301 // (IMSE). If there is a pending exception and the specified thread
1302 // is not the owner, that exception will be replaced by the IMSE.
1303 bool ObjectMonitor::check_owner(Thread* THREAD) {
1304 if (_owner == THREAD) {
1305 return true;
1306 }
1307 void* cur = _owner;
1308 if (THREAD->is_lock_owned((address)cur)) {
1309 simply_set_owner_from_BasicLock(THREAD, cur); // Convert from BasicLock* to Thread*.
1310 _recursions = 0;
1311 return true;
1312 }
1313 THROW_MSG_(vmSymbols::java_lang_IllegalMonitorStateException(),
1314 "current thread is not owner", false);
1315 }
1316
1317 static void post_monitor_wait_event(EventJavaMonitorWait* event,
1318 ObjectMonitor* monitor,
1319 jlong notifier_tid,
1320 jlong timeout,
1321 bool timedout) {
1322 assert(event != NULL, "invariant");
1323 assert(monitor != NULL, "invariant");
1324 event->set_monitorClass(((oop)monitor->object())->klass());
1325 event->set_timeout(timeout);
1326 event->set_address((uintptr_t)monitor->object_addr());
1327 event->set_notifier(notifier_tid);
1328 event->set_timedOut(timedout);
1329 event->commit();
2058 }
2059 #define NEWPERFVARIABLE(n) \
2060 { \
2061 n = PerfDataManager::create_variable(SUN_RT, #n, PerfData::U_Events, \
2062 CHECK); \
2063 }
2064 NEWPERFCOUNTER(_sync_Inflations);
2065 NEWPERFCOUNTER(_sync_Deflations);
2066 NEWPERFCOUNTER(_sync_ContendedLockAttempts);
2067 NEWPERFCOUNTER(_sync_FutileWakeups);
2068 NEWPERFCOUNTER(_sync_Parks);
2069 NEWPERFCOUNTER(_sync_Notifications);
2070 NEWPERFVARIABLE(_sync_MonExtant);
2071 #undef NEWPERFCOUNTER
2072 #undef NEWPERFVARIABLE
2073 }
2074
2075 DEBUG_ONLY(InitDone = true;)
2076 }
2077
2078 ObjectMonitorHandle::~ObjectMonitorHandle() {
2079 if (_om_ptr != NULL) {
2080 _om_ptr->dec_ref_count();
2081 _om_ptr = NULL;
2082 }
2083 }
2084
2085 // Save the ObjectMonitor* associated with the specified markWord and
2086 // increment the ref_count. This function should only be called if
2087 // the caller has verified mark.has_monitor() == true. The object
2088 // parameter is needed to verify that ObjectMonitor* has not been
2089 // deflated and reused for another object.
2090 //
2091 // This function returns true if the ObjectMonitor* has been safely
2092 // saved. This function returns false if we have lost a race with
2093 // async deflation; the caller should retry as appropriate.
2094 //
2095 bool ObjectMonitorHandle::save_om_ptr(oop object, markWord mark) {
2096 guarantee(mark.has_monitor(), "sanity check: mark=" INTPTR_FORMAT,
2097 mark.value());
2098
2099 ObjectMonitor* om_ptr = mark.monitor();
2100 om_ptr->inc_ref_count();
2101
2102 if (AsyncDeflateIdleMonitors) {
2103 // Race here if monitor is not owned! The above ref_count bump
2104 // will cause subsequent async deflation to skip it. However,
2105 // previous or concurrent async deflation is a race.
2106 if (om_ptr->owner_is_DEFLATER_MARKER() && om_ptr->ref_count() <= 0) {
2107 // Async deflation is in progress and our ref_count increment
2108 // above lost the race to async deflation. Attempt to restore
2109 // the header/dmw to the object's header so that we only retry
2110 // once if the deflater thread happens to be slow.
2111 om_ptr->install_displaced_markword_in_object(object);
2112 om_ptr->dec_ref_count();
2113 return false;
2114 }
2115 if (om_ptr->ref_count() <= 0) {
2116 // Async deflation is in the process of bailing out, but has not
2117 // yet restored the ref_count field so we return false to force
2118 // a retry. We want a positive ref_count value for a true return.
2119 om_ptr->dec_ref_count();
2121 }
2122 // The ObjectMonitor could have been deflated and reused for
2123 // another object before we bumped the ref_count so make sure
2124 // our object still refers to this ObjectMonitor.
2125 const markWord tmp = object->mark();
2126 if (!tmp.has_monitor() || tmp.monitor() != om_ptr) {
2127 // Async deflation and reuse won the race so we have to retry.
2128 // Skip object header restoration since that's already done.
2129 om_ptr->dec_ref_count();
2130 return false;
2131 }
2132 }
2133
2134 ADIM_guarantee(_om_ptr == NULL, "sanity check: _om_ptr=" INTPTR_FORMAT,
2135 p2i(_om_ptr));
2136 _om_ptr = om_ptr;
2137 return true;
2138 }
2139
2140 // For internal use by ObjectSynchronizer::inflate().
2141 // This function is only used when we don't have to worry about async
2142 // deflation of the specified ObjectMonitor*.
2143 void ObjectMonitorHandle::set_om_ptr(ObjectMonitor* om_ptr) {
2144 if (_om_ptr == NULL) {
2145 ADIM_guarantee(om_ptr != NULL, "cannot clear an unset om_ptr");
2146 om_ptr->inc_ref_count();
2147 _om_ptr = om_ptr;
2148 } else {
2149 ADIM_guarantee(om_ptr == NULL, "can only clear a set om_ptr");
2150 _om_ptr->dec_ref_count();
2151 _om_ptr = NULL;
2152 }
2153 }
2154
2155 // Save the specified ObjectMonitor* if it is safe, i.e., not being
2156 // async deflated.
2157 //
2158 // This function returns true if the ObjectMonitor* has been safely
2159 // saved. This function returns false if the specified ObjectMonitor*
2160 // is NULL or if we have lost a race with async deflation; the caller
2161 // can retry as appropriate.
2162 bool ObjectMonitorHandle::set_om_ptr_if_safe(ObjectMonitor* om_ptr) {
2163 if (om_ptr == NULL) {
2164 return false; // Nothing to save if input is NULL
2165 }
2166
2167 om_ptr->inc_ref_count();
2168
2169 if (AsyncDeflateIdleMonitors) {
2170 if (om_ptr->owner_is_DEFLATER_MARKER() && om_ptr->ref_count() <= 0) {
2171 // Async deflation is in progress and our ref_count increment
2172 // above lost the race to async deflation.
2173 om_ptr->dec_ref_count();
2174 return false;
2175 }
2176 if (om_ptr->ref_count() <= 0) {
2177 // Async deflation is in the process of bailing out, but has not
2178 // yet restored the ref_count field so we return false to force
2179 // a retry. We want a positive ref_count value for a true return.
2180 om_ptr->dec_ref_count();
2181 return false;
2182 }
2183 // Unlike save_om_ptr(), we don't have context to determine if
2184 // the ObjectMonitor has been deflated and reused for another
2185 // object.
2186 }
2187
2188 ADIM_guarantee(_om_ptr == NULL, "sanity check: _om_ptr=" INTPTR_FORMAT,
2189 p2i(_om_ptr));
2190 _om_ptr = om_ptr;
2191 return true;
2192 }
2193
2194 // Unset the _om_ptr field and decrement the ref_count field.
2195 void ObjectMonitorHandle::unset_om_ptr() {
2196 ADIM_guarantee(_om_ptr != NULL, "_om_ptr must not be NULL");
2197 _om_ptr->dec_ref_count();
2198 _om_ptr = NULL;
2199 }
2200
2201 void ObjectMonitor::print_on(outputStream* st) const {
2202 // The minimal things to print for markWord printing, more can be added for debugging and logging.
2203 st->print("{contentions=0x%08x,waiters=0x%08x"
2204 ",recursions=" INTX_FORMAT ",owner=" INTPTR_FORMAT "}",
2205 contentions(), waiters(), recursions(),
2206 p2i(owner()));
2207 }
2208 void ObjectMonitor::print() const { print_on(tty); }
2209
2210 // Print the ObjectMonitor like a debugger would:
2211 //
2212 // (ObjectMonitor) 0x00007fdfb6012e40 = {
2213 // _header = (_value = 1)
2214 // _object = 0x000000070ff45fd0
2215 // _allocation_state = Old
2216 // _pad_buf0 = {
2217 // [0] = '\0'
2218 // ...
2219 // [43] = '\0'
2220 // }
2229 // _pad_buf2 = {
2230 // [0] = '\0'
2231 // ...
2232 // [59] = '\0'
2233 // }
2234 // _next_om = 0x0000000000000000
2235 // _recursions = 0
2236 // _EntryList = 0x0000000000000000
2237 // _cxq = 0x0000000000000000
2238 // _succ = 0x0000000000000000
2239 // _Responsible = 0x0000000000000000
2240 // _Spinner = 0
2241 // _SpinDuration = 5000
2242 // _contentions = 0
2243 // _WaitSet = 0x0000700009756248
2244 // _waiters = 1
2245 // _WaitSetLock = 0
2246 // }
2247 //
2248 void ObjectMonitor::print_debug_style_on(outputStream* st) const {
2249 st->print_cr("(ObjectMonitor*) " INTPTR_FORMAT " = {", p2i(this));
2250 st->print_cr(" _header = " INTPTR_FORMAT, header().value());
2251 st->print_cr(" _object = " INTPTR_FORMAT, p2i(_object));
2252 st->print(" _allocation_state = ");
2253 if (is_free()) {
2254 st->print("Free");
2255 } else if (is_old()) {
2256 st->print("Old");
2257 } else if (is_new()) {
2258 st->print("New");
2259 } else {
2260 st->print("unknown=%d", _allocation_state);
2261 }
2262 st->cr();
2263 st->print_cr(" _pad_buf0 = {");
2264 st->print_cr(" [0] = '\\0'");
2265 st->print_cr(" ...");
2266 st->print_cr(" [%d] = '\\0'", (int)sizeof(_pad_buf0) - 1);
2267 st->print_cr(" }");
2268 st->print_cr(" _owner = " INTPTR_FORMAT, p2i(_owner));
2269 st->print_cr(" _previous_owner_tid = " JLONG_FORMAT, _previous_owner_tid);
|