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