1 /*
2 * Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/vmSymbols.hpp"
27 #include "gc/shared/oopStorage.hpp"
28 #include "gc/shared/oopStorageSet.hpp"
29 #include "jfr/jfrEvents.hpp"
30 #include "jfr/support/jfrThreadId.hpp"
31 #include "logging/log.hpp"
32 #include "logging/logStream.hpp"
33 #include "memory/allocation.inline.hpp"
34 #include "memory/resourceArea.hpp"
35 #include "oops/markWord.hpp"
36 #include "oops/oop.inline.hpp"
37 #include "oops/oopHandle.inline.hpp"
38 #include "oops/weakHandle.inline.hpp"
39 #include "prims/jvmtiDeferredUpdates.hpp"
40 #include "prims/jvmtiExport.hpp"
41 #include "runtime/atomic.hpp"
42 #include "runtime/handles.inline.hpp"
43 #include "runtime/interfaceSupport.inline.hpp"
44 #include "runtime/mutexLocker.hpp"
45 #include "runtime/objectMonitor.hpp"
46 #include "runtime/objectMonitor.inline.hpp"
47 #include "runtime/orderAccess.hpp"
48 #include "runtime/osThread.hpp"
49 #include "runtime/perfData.hpp"
50 #include "runtime/safefetch.hpp"
51 #include "runtime/safepointMechanism.inline.hpp"
52 #include "runtime/sharedRuntime.hpp"
53 #include "runtime/thread.inline.hpp"
54 #include "services/threadService.hpp"
55 #include "utilities/dtrace.hpp"
56 #include "utilities/macros.hpp"
57 #include "utilities/preserveException.hpp"
58 #if INCLUDE_JFR
59 #include "jfr/support/jfrFlush.hpp"
60 #endif
61
62 #ifdef DTRACE_ENABLED
63
64 // Only bother with this argument setup if dtrace is available
65 // TODO-FIXME: probes should not fire when caller is _blocked. assert() accordingly.
66
67
68 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread) \
69 char* bytes = NULL; \
70 int len = 0; \
71 jlong jtid = SharedRuntime::get_java_tid(thread); \
72 Symbol* klassname = obj->klass()->name(); \
73 if (klassname != NULL) { \
74 bytes = (char*)klassname->bytes(); \
75 len = klassname->utf8_length(); \
301 // Don't need a full fence after clearing successor here because of the call to exit().
302 _om->exit(current, false /* not_suspended */);
303 _om_exited = true;
304
305 current->set_current_pending_monitor(_om);
306 }
307 }
308
309 void ObjectMonitor::ClearSuccOnSuspend::operator()(JavaThread* current) {
310 if (current->is_suspended()) {
311 if (_om->_succ == current) {
312 _om->_succ = NULL;
313 OrderAccess::fence(); // always do a full fence when successor is cleared
314 }
315 }
316 }
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");
|
1 /*
2 * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/vmSymbols.hpp"
27 #include "gc/shared/oopStorage.hpp"
28 #include "gc/shared/oopStorageSet.hpp"
29 #include "jfr/jfrEvents.hpp"
30 #include "jfr/support/jfrThreadId.hpp"
31 #include "logging/log.hpp"
32 #include "logging/logStream.hpp"
33 #include "memory/allocation.inline.hpp"
34 #include "memory/resourceArea.hpp"
35 #include "oops/markWord.hpp"
36 #include "oops/oop.inline.hpp"
37 #include "oops/oopHandle.inline.hpp"
38 #include "oops/weakHandle.inline.hpp"
39 #include "prims/jvmtiDeferredUpdates.hpp"
40 #include "prims/jvmtiExport.hpp"
41 #include "runtime/atomic.hpp"
42 #include "runtime/globals.hpp"
43 #include "runtime/handles.inline.hpp"
44 #include "runtime/interfaceSupport.inline.hpp"
45 #include "runtime/mutexLocker.hpp"
46 #include "runtime/objectMonitor.hpp"
47 #include "runtime/objectMonitor.inline.hpp"
48 #include "runtime/orderAccess.hpp"
49 #include "runtime/osThread.hpp"
50 #include "runtime/perfData.hpp"
51 #include "runtime/safefetch.hpp"
52 #include "runtime/safepointMechanism.inline.hpp"
53 #include "runtime/sharedRuntime.hpp"
54 #include "runtime/thread.inline.hpp"
55 #include "services/threadService.hpp"
56 #include "utilities/dtrace.hpp"
57 #include "utilities/globalDefinitions.hpp"
58 #include "utilities/macros.hpp"
59 #include "utilities/preserveException.hpp"
60 #if INCLUDE_JFR
61 #include "jfr/support/jfrFlush.hpp"
62 #endif
63
64 #ifdef DTRACE_ENABLED
65
66 // Only bother with this argument setup if dtrace is available
67 // TODO-FIXME: probes should not fire when caller is _blocked. assert() accordingly.
68
69
70 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread) \
71 char* bytes = NULL; \
72 int len = 0; \
73 jlong jtid = SharedRuntime::get_java_tid(thread); \
74 Symbol* klassname = obj->klass()->name(); \
75 if (klassname != NULL) { \
76 bytes = (char*)klassname->bytes(); \
77 len = klassname->utf8_length(); \
303 // Don't need a full fence after clearing successor here because of the call to exit().
304 _om->exit(current, false /* not_suspended */);
305 _om_exited = true;
306
307 current->set_current_pending_monitor(_om);
308 }
309 }
310
311 void ObjectMonitor::ClearSuccOnSuspend::operator()(JavaThread* current) {
312 if (current->is_suspended()) {
313 if (_om->_succ == current) {
314 _om->_succ = NULL;
315 OrderAccess::fence(); // always do a full fence when successor is cleared
316 }
317 }
318 }
319
320 // -----------------------------------------------------------------------------
321 // Enter support
322
323 bool ObjectMonitor::enter_for(JavaThread* locking_thread) {
324 // Used by ObjectSynchronizer::enter_for to enter for another thread.
325 // The monitor is private to or already owned by locking_thread which must be suspended.
326 // So this code may only contend with deflation.
327 assert(locking_thread == Thread::current() || locking_thread->is_obj_deopt_suspend(), "must be");
328
329 // Block out deflation as soon as possible.
330 add_to_contentions(1);
331
332 bool success = false;
333 if (!is_being_async_deflated()) {
334 void* prev_owner = try_set_owner_from(nullptr, locking_thread);
335
336 if (prev_owner == nullptr) {
337 assert(_recursions == 0, "invariant");
338 success = true;
339 } else if (prev_owner == locking_thread) {
340 _recursions++;
341 success = true;
342 } else if (prev_owner == DEFLATER_MARKER) {
343 // Racing with deflation.
344 prev_owner = try_set_owner_from(DEFLATER_MARKER, locking_thread);
345 if (prev_owner == DEFLATER_MARKER) {
346 // Cancelled deflation. Increment contentions as part of the deflation protocol.
347 add_to_contentions(1);
348 success = true;
349 } else if (prev_owner == nullptr) {
350 // At this point we cannot race with deflation as we have both incremented
351 // contentions, seen contention > 0 and seen a DEFLATER_MARKER.
352 // success will only be false if this races with something other than
353 // deflation.
354 prev_owner = try_set_owner_from(nullptr, locking_thread);
355 success = prev_owner == nullptr;
356 }
357 } else if (LockingMode == LM_LEGACY && locking_thread->is_lock_owned((address)prev_owner)) {
358 assert(_recursions == 0, "must be");
359 _recursions = 1;
360 set_owner_from_BasicLock(prev_owner, locking_thread);
361 success = true;
362 }
363 assert(success, "Failed to enter_for: locking_thread=" INTPTR_FORMAT
364 ", this=" INTPTR_FORMAT "{owner=" INTPTR_FORMAT "}, observed owner: " INTPTR_FORMAT,
365 p2i(locking_thread), p2i(this), p2i(owner_raw()), p2i(prev_owner));
366 } else {
367 // Async deflation is in progress and our contentions increment
368 // above lost the race to async deflation. Undo the work and
369 // force the caller to retry.
370 const oop l_object = object();
371 if (l_object != nullptr) {
372 // Attempt to restore the header/dmw to the object's header so that
373 // we only retry once if the deflater thread happens to be slow.
374 install_displaced_markword_in_object(l_object);
375 }
376 }
377
378 add_to_contentions(-1);
379
380 assert(!success || owner_raw() == locking_thread, "must be");
381
382 return success;
383 }
384
385 bool ObjectMonitor::enter(JavaThread* current) {
386 assert(current == JavaThread::current(), "must be");
387 // The following code is ordered to check the most common cases first
388 // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
389
390 void* cur = try_set_owner_from(NULL, current);
391 if (cur == NULL) {
392 assert(_recursions == 0, "invariant");
393 return true;
394 }
395
396 if (cur == current) {
397 // TODO-FIXME: check for integer overflow! BUGID 6557169.
398 _recursions++;
399 return true;
400 }
401
402 if (LockingMode != LM_LIGHTWEIGHT && current->is_lock_owned((address)cur)) {
403 assert(_recursions == 0, "internal state error");
404 _recursions = 1;
405 set_owner_from_BasicLock(cur, current); // Convert from BasicLock* to Thread*.
406 return true;
407 }
408
409 // We've encountered genuine contention.
410 assert(current->_Stalled == 0, "invariant");
411 current->_Stalled = intptr_t(this);
412
413 // Try one round of spinning *before* enqueueing current
414 // and before going through the awkward and expensive state
415 // transitions. The following spin is strictly optional ...
416 // Note that if we acquire the monitor from an initial spin
417 // we forgo posting JVMTI events and firing DTRACE probes.
418 if (TrySpin(current) > 0) {
419 assert(owner_raw() == current, "must be current: owner=" INTPTR_FORMAT, p2i(owner_raw()));
420 assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
421 assert(object()->mark() == markWord::encode(this),
422 "object mark must match encoded this: mark=" INTPTR_FORMAT
648 p2i(_EntryList));
649
650 if (obj != NULL) {
651 if (log_is_enabled(Trace, monitorinflation)) {
652 ResourceMark rm;
653 log_trace(monitorinflation)("deflate_monitor: object=" INTPTR_FORMAT
654 ", mark=" INTPTR_FORMAT ", type='%s'",
655 p2i(obj), obj->mark().value(),
656 obj->klass()->external_name());
657 }
658
659 // Install the old mark word if nobody else has already done it.
660 install_displaced_markword_in_object(obj);
661 }
662
663 // We leave owner == DEFLATER_MARKER and contentions < 0
664 // to force any racing threads to retry.
665 return true; // Success, ObjectMonitor has been deflated.
666 }
667
668 // We might access the dead object headers for parsable heap walk, make sure
669 // headers are in correct shape, e.g. monitors deflated.
670 void ObjectMonitor::maybe_deflate_dead(oop* p) {
671 oop obj = *p;
672 assert(obj != NULL, "must not yet been cleared");
673 markWord mark = obj->mark();
674 if (mark.has_monitor()) {
675 ObjectMonitor* monitor = mark.monitor();
676 if (p == monitor->_object.ptr_raw()) {
677 assert(monitor->object_peek() == obj, "lock object must match");
678 markWord dmw = monitor->header();
679 obj->set_mark(dmw);
680 }
681 }
682 }
683
684 // Install the displaced mark word (dmw) of a deflating ObjectMonitor
685 // into the header of the object associated with the monitor. This
686 // idempotent method is called by a thread that is deflating a
687 // monitor and by other threads that have detected a race with the
688 // deflation process.
689 void ObjectMonitor::install_displaced_markword_in_object(const oop obj) {
690 // This function must only be called when (owner == DEFLATER_MARKER
691 // && contentions <= 0), but we can't guarantee that here because
692 // those values could change when the ObjectMonitor gets moved from
693 // the global free list to a per-thread free list.
694
695 guarantee(obj != NULL, "must be non-NULL");
696
697 // Separate loads in is_being_async_deflated(), which is almost always
698 // called before this function, from the load of dmw/header below.
699
700 // _contentions and dmw/header may get written by different threads.
701 // Make sure to observe them in the same order when having several observers.
702 OrderAccess::loadload_for_IRIW();
703
1199 // the timer expires. If the lock is high traffic then the stranding latency
1200 // will be low due to (a). If the lock is low traffic then the odds of
1201 // stranding are lower, although the worst-case stranding latency
1202 // is longer. Critically, we don't want to put excessive load in the
1203 // platform's timer subsystem. We want to minimize both the timer injection
1204 // rate (timers created/sec) as well as the number of timers active at
1205 // any one time. (more precisely, we want to minimize timer-seconds, which is
1206 // the integral of the # of active timers at any instant over time).
1207 // Both impinge on OS scalability. Given that, at most one thread parked on
1208 // a monitor will use a timer.
1209 //
1210 // There is also the risk of a futile wake-up. If we drop the lock
1211 // another thread can reacquire the lock immediately, and we can
1212 // then wake a thread unnecessarily. This is benign, and we've
1213 // structured the code so the windows are short and the frequency
1214 // of such futile wakups is low.
1215
1216 void ObjectMonitor::exit(JavaThread* current, bool not_suspended) {
1217 void* cur = owner_raw();
1218 if (current != cur) {
1219 if (LockingMode != LM_LIGHTWEIGHT && current->is_lock_owned((address)cur)) {
1220 assert(_recursions == 0, "invariant");
1221 set_owner_from_BasicLock(cur, current); // Convert from BasicLock* to Thread*.
1222 _recursions = 0;
1223 } else {
1224 // Apparent unbalanced locking ...
1225 // Naively we'd like to throw IllegalMonitorStateException.
1226 // As a practical matter we can neither allocate nor throw an
1227 // exception as ::exit() can be called from leaf routines.
1228 // see x86_32.ad Fast_Unlock() and the I1 and I2 properties.
1229 // Upon deeper reflection, however, in a properly run JVM the only
1230 // way we should encounter this situation is in the presence of
1231 // unbalanced JNI locking. TODO: CheckJNICalls.
1232 // See also: CR4414101
1233 #ifdef ASSERT
1234 LogStreamHandle(Error, monitorinflation) lsh;
1235 lsh.print_cr("ERROR: ObjectMonitor::exit(): thread=" INTPTR_FORMAT
1236 " is exiting an ObjectMonitor it does not own.", p2i(current));
1237 lsh.print_cr("The imbalance is possibly caused by JNI locking.");
1238 print_debug_style_on(&lsh);
1239 assert(false, "Non-balanced monitor enter/exit!");
1419
1420 // Maintain stats and report events to JVMTI
1421 OM_PERFDATA_OP(Parks, inc());
1422 }
1423
1424
1425 // -----------------------------------------------------------------------------
1426 // Class Loader deadlock handling.
1427 //
1428 // complete_exit exits a lock returning recursion count
1429 // complete_exit/reenter operate as a wait without waiting
1430 // complete_exit requires an inflated monitor
1431 // The _owner field is not always the Thread addr even with an
1432 // inflated monitor, e.g. the monitor can be inflated by a non-owning
1433 // thread due to contention.
1434 intx ObjectMonitor::complete_exit(JavaThread* current) {
1435 assert(InitDone, "Unexpectedly not initialized");
1436
1437 void* cur = owner_raw();
1438 if (current != cur) {
1439 if (LockingMode != LM_LIGHTWEIGHT && current->is_lock_owned((address)cur)) {
1440 assert(_recursions == 0, "internal state error");
1441 set_owner_from_BasicLock(cur, current); // Convert from BasicLock* to Thread*.
1442 _recursions = 0;
1443 }
1444 }
1445
1446 guarantee(current == owner_raw(), "complete_exit not owner");
1447 intx save = _recursions; // record the old recursion count
1448 _recursions = 0; // set the recursion level to be 0
1449 exit(current); // exit the monitor
1450 guarantee(owner_raw() != current, "invariant");
1451 return save;
1452 }
1453
1454 // reenter() enters a lock and sets recursion count
1455 // complete_exit/reenter operate as a wait without waiting
1456 bool ObjectMonitor::reenter(intx recursions, JavaThread* current) {
1457
1458 guarantee(owner_raw() != current, "reenter already owner");
1459 if (!enter(current)) {
1468 // Checks that the current THREAD owns this monitor and causes an
1469 // immediate return if it doesn't. We don't use the CHECK macro
1470 // because we want the IMSE to be the only exception that is thrown
1471 // from the call site when false is returned. Any other pending
1472 // exception is ignored.
1473 #define CHECK_OWNER() \
1474 do { \
1475 if (!check_owner(THREAD)) { \
1476 assert(HAS_PENDING_EXCEPTION, "expected a pending IMSE here."); \
1477 return; \
1478 } \
1479 } while (false)
1480
1481 // Returns true if the specified thread owns the ObjectMonitor.
1482 // Otherwise returns false and throws IllegalMonitorStateException
1483 // (IMSE). If there is a pending exception and the specified thread
1484 // is not the owner, that exception will be replaced by the IMSE.
1485 bool ObjectMonitor::check_owner(TRAPS) {
1486 JavaThread* current = THREAD;
1487 void* cur = owner_raw();
1488 assert(cur != anon_owner_ptr(), "no anon owner here");
1489 if (cur == current) {
1490 return true;
1491 }
1492 if (LockingMode != LM_LIGHTWEIGHT && current->is_lock_owned((address)cur)) {
1493 set_owner_from_BasicLock(cur, current); // Convert from BasicLock* to Thread*.
1494 _recursions = 0;
1495 return true;
1496 }
1497 THROW_MSG_(vmSymbols::java_lang_IllegalMonitorStateException(),
1498 "current thread is not owner", false);
1499 }
1500
1501 static inline bool is_excluded(const Klass* monitor_klass) {
1502 assert(monitor_klass != nullptr, "invariant");
1503 NOT_JFR_RETURN_(false);
1504 JFR_ONLY(return vmSymbols::jfr_chunk_rotation_monitor() == monitor_klass->name());
1505 }
1506
1507 static void post_monitor_wait_event(EventJavaMonitorWait* event,
1508 ObjectMonitor* monitor,
1509 jlong notifier_tid,
1510 jlong timeout,
1511 bool timedout) {
1512 assert(event != NULL, "invariant");
|