1 /* 2 * Copyright (c) 1998, 2023, 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 #ifndef SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP 26 #define SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP 27 28 #include "runtime/objectMonitor.hpp" 29 30 #include "logging/log.hpp" 31 #include "oops/access.inline.hpp" 32 #include "runtime/atomic.hpp" 33 #include "runtime/lockStack.inline.hpp" 34 #include "runtime/synchronizer.hpp" 35 36 inline bool ObjectMonitor::is_entered(JavaThread* current) const { 37 if (LockingMode == LM_LIGHTWEIGHT) { 38 if (is_owner_anonymous()) { 39 return current->lock_stack().contains(object()); 40 } else { 41 return current == owner_raw(); 42 } 43 } else { 44 void* owner = owner_raw(); 45 if (current == owner || current->is_lock_owned((address)owner)) { 46 return true; 47 } 48 } 49 return false; 50 } 51 52 inline markWord ObjectMonitor::header() const { 53 return Atomic::load(&_header); 54 } 55 56 inline volatile markWord* ObjectMonitor::header_addr() { 57 return &_header; 58 } 59 60 inline void ObjectMonitor::set_header(markWord hdr) { 61 Atomic::store(&_header, hdr); 62 } 63 64 inline int ObjectMonitor::waiters() const { 65 return _waiters; 66 } 67 68 inline bool ObjectMonitor::has_owner() const { 69 void* owner = owner_raw(); 70 return owner != nullptr && owner != DEFLATER_MARKER; 71 } 72 73 // Returns null if DEFLATER_MARKER is observed. 74 inline void* ObjectMonitor::owner() const { 75 void* owner = owner_raw(); 76 return owner != DEFLATER_MARKER ? owner : nullptr; 77 } 78 79 inline void* ObjectMonitor::owner_raw() const { 80 return Atomic::load(&_owner); 81 } 82 83 // Returns true if owner field == DEFLATER_MARKER and false otherwise. 84 // This accessor is called when we really need to know if the owner 85 // field == DEFLATER_MARKER and any non-null value won't do the trick. 86 inline bool ObjectMonitor::owner_is_DEFLATER_MARKER() const { 87 return owner_raw() == DEFLATER_MARKER; 88 } 89 90 // Returns true if 'this' is being async deflated and false otherwise. 91 inline bool ObjectMonitor::is_being_async_deflated() { 92 return contentions() < 0; 93 } 94 95 // Return number of threads contending for this monitor. 96 inline int ObjectMonitor::contentions() const { 97 return Atomic::load(&_contentions); 98 } 99 100 // Add value to the contentions field. 101 inline void ObjectMonitor::add_to_contentions(int value) { 102 Atomic::add(&_contentions, value); 103 } 104 105 // Clear _owner field; current value must match old_value. 106 inline void ObjectMonitor::release_clear_owner(void* old_value) { 107 #ifdef ASSERT 108 void* prev = Atomic::load(&_owner); 109 assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT 110 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value)); 111 #endif 112 Atomic::release_store(&_owner, (void*)nullptr); 113 log_trace(monitorinflation, owner)("release_clear_owner(): mid=" 114 INTPTR_FORMAT ", old_value=" INTPTR_FORMAT, 115 p2i(this), p2i(old_value)); 116 } 117 118 // Simply set _owner field to new_value; current value must match old_value. 119 // (Simple means no memory sync needed.) 120 inline void ObjectMonitor::set_owner_from(void* old_value, void* new_value) { 121 #ifdef ASSERT 122 void* prev = Atomic::load(&_owner); 123 assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT 124 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value)); 125 #endif 126 Atomic::store(&_owner, new_value); 127 log_trace(monitorinflation, owner)("set_owner_from(): mid=" 128 INTPTR_FORMAT ", old_value=" INTPTR_FORMAT 129 ", new_value=" INTPTR_FORMAT, p2i(this), 130 p2i(old_value), p2i(new_value)); 131 } 132 133 // Simply set _owner field to self; current value must match basic_lock_p. 134 inline void ObjectMonitor::set_owner_from_BasicLock(void* basic_lock_p, JavaThread* current) { 135 #ifdef ASSERT 136 void* prev = Atomic::load(&_owner); 137 assert(prev == basic_lock_p, "unexpected prev owner=" INTPTR_FORMAT 138 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(basic_lock_p)); 139 #endif 140 // Non-null owner field to non-null owner field is safe without 141 // cmpxchg() as long as all readers can tolerate either flavor. 142 Atomic::store(&_owner, current); 143 log_trace(monitorinflation, owner)("set_owner_from_BasicLock(): mid=" 144 INTPTR_FORMAT ", basic_lock_p=" 145 INTPTR_FORMAT ", new_value=" INTPTR_FORMAT, 146 p2i(this), p2i(basic_lock_p), p2i(current)); 147 } 148 149 // Try to set _owner field to new_value if the current value matches 150 // old_value. Otherwise, does not change the _owner field. Returns 151 // the prior value of the _owner field. 152 inline void* ObjectMonitor::try_set_owner_from(void* old_value, void* new_value) { 153 void* prev = Atomic::cmpxchg(&_owner, old_value, new_value); 154 if (prev == old_value) { 155 log_trace(monitorinflation, owner)("try_set_owner_from(): mid=" 156 INTPTR_FORMAT ", prev=" INTPTR_FORMAT 157 ", new=" INTPTR_FORMAT, p2i(this), 158 p2i(prev), p2i(new_value)); 159 } 160 return prev; 161 } 162 163 // The _next_om field can be concurrently read and modified so we 164 // use Atomic operations to disable compiler optimizations that 165 // might try to elide loading and/or storing this field. 166 167 // Simply get _next_om field. 168 inline ObjectMonitor* ObjectMonitor::next_om() const { 169 return Atomic::load(&_next_om); 170 } 171 172 // Simply set _next_om field to new_value. 173 inline void ObjectMonitor::set_next_om(ObjectMonitor* new_value) { 174 Atomic::store(&_next_om, new_value); 175 } 176 177 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP