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 #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 "oops/markWord.hpp"
 33 #include "runtime/atomic.hpp"
 34 #include "runtime/globals.hpp"
 35 #include "runtime/lockStack.inline.hpp"
 36 #include "runtime/synchronizer.hpp"
 37 #include "utilities/checkedCast.hpp"
 38 #include "utilities/globalDefinitions.hpp"
 39 
 40 inline bool ObjectMonitor::is_entered(JavaThread* current) const {
 41   if (LockingMode == LM_LIGHTWEIGHT) {
 42     if (is_owner_anonymous()) {
 43       return current->lock_stack().contains(object());
 44     } else {
 45       return current == owner_raw();
 46     }
 47   } else {
 48     void* owner = owner_raw();
 49     if (current == owner || current->is_lock_owned((address)owner)) {
 50       return true;
 51     }
 52   }
 53   return false;
 54 }
 55 
 56 inline uintptr_t ObjectMonitor::metadata() const {
 57   return Atomic::load(&_metadata);
 58 }
 59 
 60 inline void ObjectMonitor::set_metadata(uintptr_t value) {
 61   Atomic::store(&_metadata, value);
 62 }
 63 
 64 inline volatile uintptr_t* ObjectMonitor::metadata_addr() {
 65   STATIC_ASSERT(std::is_standard_layout<ObjectMonitor>::value);
 66   STATIC_ASSERT(offsetof(ObjectMonitor, _metadata) == 0);
 67   return &_metadata;
 68 }
 69 
 70 inline markWord ObjectMonitor::header() const {
 71   assert(LockingMode != LM_LIGHTWEIGHT, "Lightweight locking does not use header");
 72   return markWord(metadata());
 73 }
 74 
 75 inline void ObjectMonitor::set_header(markWord hdr) {
 76   assert(LockingMode != LM_LIGHTWEIGHT, "Lightweight locking does not use header");
 77   set_metadata(hdr.value());
 78 }
 79 
 80 inline intptr_t ObjectMonitor::hash() const {
 81   assert(LockingMode == LM_LIGHTWEIGHT, "Only used by lightweight locking");
 82   return metadata();
 83 }
 84 
 85 inline void ObjectMonitor::set_hash(intptr_t hash) {
 86   assert(LockingMode == LM_LIGHTWEIGHT, "Only used by lightweight locking");
 87   set_metadata(hash);
 88 }
 89 
 90 inline int ObjectMonitor::waiters() const {
 91   return _waiters;
 92 }
 93 
 94 inline bool ObjectMonitor::has_owner() const {
 95   void* owner = owner_raw();
 96   return owner != nullptr && owner != DEFLATER_MARKER;
 97 }
 98 
 99 // Returns null if DEFLATER_MARKER is observed.
100 inline void* ObjectMonitor::owner() const {
101   void* owner = owner_raw();
102   return owner != DEFLATER_MARKER ? owner : nullptr;
103 }
104 
105 inline void* ObjectMonitor::owner_raw() const {
106   return Atomic::load(&_owner);
107 }
108 
109 // Returns true if owner field == DEFLATER_MARKER and false otherwise.
110 // This accessor is called when we really need to know if the owner
111 // field == DEFLATER_MARKER and any non-null value won't do the trick.
112 inline bool ObjectMonitor::owner_is_DEFLATER_MARKER() const {
113   return owner_raw() == DEFLATER_MARKER;
114 }
115 
116 // Returns true if 'this' is being async deflated and false otherwise.
117 inline bool ObjectMonitor::is_being_async_deflated() {
118   return contentions() < 0;
119 }
120 
121 // Return number of threads contending for this monitor.
122 inline int ObjectMonitor::contentions() const {
123   return Atomic::load(&_contentions);
124 }
125 
126 // Add value to the contentions field.
127 inline void ObjectMonitor::add_to_contentions(int value) {
128   Atomic::add(&_contentions, value);
129 }
130 
131 inline void ObjectMonitor::set_recursions(size_t recursions) {
132   assert(_recursions == 0, "must be");
133   assert(has_owner(), "must be owned");
134   _recursions = checked_cast<intx>(recursions);
135 }
136 
137 // Clear _owner field; current value must match old_value.
138 inline void ObjectMonitor::release_clear_owner(void* old_value) {
139 #ifdef ASSERT
140   void* prev = Atomic::load(&_owner);
141   assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
142          ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
143 #endif
144   Atomic::release_store(&_owner, (void*)nullptr);
145   log_trace(monitorinflation, owner)("release_clear_owner(): mid="
146                                      INTPTR_FORMAT ", old_value=" INTPTR_FORMAT,
147                                      p2i(this), p2i(old_value));
148 }
149 
150 // Simply set _owner field to new_value; current value must match old_value.
151 // (Simple means no memory sync needed.)
152 inline void ObjectMonitor::set_owner_from(void* old_value, void* new_value) {
153 #ifdef ASSERT
154   void* prev = Atomic::load(&_owner);
155   assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
156          ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
157 #endif
158   Atomic::store(&_owner, new_value);
159   log_trace(monitorinflation, owner)("set_owner_from(): mid="
160                                      INTPTR_FORMAT ", old_value=" INTPTR_FORMAT
161                                      ", new_value=" INTPTR_FORMAT, p2i(this),
162                                      p2i(old_value), p2i(new_value));
163 }
164 
165 // Simply set _owner field to self; current value must match basic_lock_p.
166 inline void ObjectMonitor::set_owner_from_BasicLock(void* basic_lock_p, JavaThread* current) {
167 #ifdef ASSERT
168   void* prev = Atomic::load(&_owner);
169   assert(prev == basic_lock_p, "unexpected prev owner=" INTPTR_FORMAT
170          ", expected=" INTPTR_FORMAT, p2i(prev), p2i(basic_lock_p));
171 #endif
172   // Non-null owner field to non-null owner field is safe without
173   // cmpxchg() as long as all readers can tolerate either flavor.
174   Atomic::store(&_owner, current);
175   log_trace(monitorinflation, owner)("set_owner_from_BasicLock(): mid="
176                                      INTPTR_FORMAT ", basic_lock_p="
177                                      INTPTR_FORMAT ", new_value=" INTPTR_FORMAT,
178                                      p2i(this), p2i(basic_lock_p), p2i(current));
179 }
180 
181 // Try to set _owner field to new_value if the current value matches
182 // old_value. Otherwise, does not change the _owner field. Returns
183 // the prior value of the _owner field.
184 inline void* ObjectMonitor::try_set_owner_from(void* old_value, void* new_value) {
185   void* prev = Atomic::cmpxchg(&_owner, old_value, new_value);
186   if (prev == old_value) {
187     log_trace(monitorinflation, owner)("try_set_owner_from(): mid="
188                                        INTPTR_FORMAT ", prev=" INTPTR_FORMAT
189                                        ", new=" INTPTR_FORMAT, p2i(this),
190                                        p2i(prev), p2i(new_value));
191   }
192   return prev;
193 }
194 
195 // The _next_om field can be concurrently read and modified so we
196 // use Atomic operations to disable compiler optimizations that
197 // might try to elide loading and/or storing this field.
198 
199 // Simply get _next_om field.
200 inline ObjectMonitor* ObjectMonitor::next_om() const {
201   return Atomic::load(&_next_om);
202 }
203 
204 // Simply set _next_om field to new_value.
205 inline void ObjectMonitor::set_next_om(ObjectMonitor* new_value) {
206   Atomic::store(&_next_om, new_value);
207 }
208 
209 inline ObjectMonitorContentionMark::ObjectMonitorContentionMark(ObjectMonitor* monitor)
210   : _monitor(monitor) {
211   _monitor->add_to_contentions(1);
212 }
213 
214 inline ObjectMonitorContentionMark::~ObjectMonitorContentionMark() {
215   _monitor->add_to_contentions(-1);
216 }
217 
218 inline oop ObjectMonitor::object_peek() const {
219   if (_object.is_null()) {
220     return nullptr;
221   }
222   return _object.peek();
223 }
224 
225 inline bool ObjectMonitor::object_is_dead() const {
226   return object_peek() == nullptr;
227 }
228 
229 inline bool ObjectMonitor::object_is_cleared() const {
230   return _object.is_null();
231 }
232 
233 inline bool ObjectMonitor::object_refers_to(oop obj) const {
234   if (_object.is_null()) {
235     return false;
236   }
237   return _object.peek() == obj;
238 }
239 
240 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP