1 /* 2 * Copyright (c) 1998, 2019, 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 "logging/log.hpp" 29 #include "runtime/atomic.hpp" 30 31 inline intptr_t ObjectMonitor::is_entered(TRAPS) const { 32 if (THREAD == _owner || THREAD->is_lock_owned((address) _owner)) { 33 return 1; 34 } 35 return 0; 36 } 37 38 inline markWord ObjectMonitor::header() const { 39 return Atomic::load(&_header); 40 } 41 42 inline volatile markWord* ObjectMonitor::header_addr() { 43 assert((intptr_t)this == (intptr_t)&_header, "sync code expects this"); 44 return &_header; 45 } 46 47 inline void ObjectMonitor::set_header(markWord hdr) { 48 Atomic::store(&_header, hdr); 49 } 50 51 inline jint ObjectMonitor::waiters() const { 52 return _waiters; 53 } 54 55 // Returns NULL if DEFLATER_MARKER is observed. 56 inline void* ObjectMonitor::owner() const { 57 void* owner = _owner; 58 return owner != DEFLATER_MARKER ? owner : NULL; 59 } 60 61 // Returns true if owner field == DEFLATER_MARKER and false otherwise. 62 // This accessor is called when we really need to know if the owner 63 // field == DEFLATER_MARKER and any non-NULL value won't do the trick. 64 inline bool ObjectMonitor::owner_is_DEFLATER_MARKER() { 65 return _owner == DEFLATER_MARKER; 66 } 67 68 inline void ObjectMonitor::clear() { 69 assert(Atomic::load(&_header).value() != 0, "must be non-zero"); 70 assert(_owner == NULL, "must be NULL: owner=" INTPTR_FORMAT, p2i(_owner)); 71 #ifdef ASSERT 72 jint l_ref_count = ref_count(); 73 #endif 74 assert(l_ref_count == 0, "must be 0: l_ref_count=%d, ref_count=%d", l_ref_count, ref_count()); 75 76 Atomic::store(&_header, markWord::zero()); 77 78 clear_using_JT(); 79 } 80 81 inline void ObjectMonitor::clear_using_JT() { 82 // Unlike other *_using_JT() functions, we cannot assert 83 // AsyncDeflateIdleMonitors or Thread::current()->is_Java_thread() 84 // because clear() calls this function for the rest of its checks. 85 86 if (AsyncDeflateIdleMonitors) { 87 // Async deflation protocol uses the header, owner and ref_count 88 // fields. While the ObjectMonitor being deflated is on the global free 89 // list, we leave those three fields alone; owner == DEFLATER_MARKER 90 // and ref_count < 0 will force any racing threads to retry. The 91 // header field is used by install_displaced_markword_in_object() 92 // in the last part of the deflation protocol so we cannot check 93 // its value here. 94 guarantee(_owner == NULL || _owner == DEFLATER_MARKER, 95 "must be NULL or DEFLATER_MARKER: owner=" INTPTR_FORMAT, 96 p2i(_owner)); 97 jint l_ref_count = ref_count(); 98 guarantee(l_ref_count <= 0, "must be <= 0: l_ref_count=%d, ref_count=%d", l_ref_count, ref_count()); 99 } 100 assert(_contentions == 0, "must be 0: contentions=%d", _contentions); 101 assert(_waiters == 0, "must be 0: waiters=%d", _waiters); 102 assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions); 103 assert(_object != NULL, "must be non-NULL"); 104 105 set_allocation_state(Free); 106 _object = NULL; 107 } 108 109 inline void* ObjectMonitor::object() const { 110 return _object; 111 } 112 113 inline void* ObjectMonitor::object_addr() { 114 return (void *)(&_object); 115 } 116 117 inline void ObjectMonitor::set_object(void* obj) { 118 _object = obj; 119 } 120 121 // return number of threads contending for this monitor 122 inline jint ObjectMonitor::contentions() const { 123 return _contentions; 124 } 125 126 // Clear _owner field; current value must match old_value. 127 // If needs_fence is true, we issue a fence() after the release_store(). 128 // Otherwise, a storeload() is good enough. See the callers for more info. 129 inline void ObjectMonitor::release_clear_owner_with_barrier(void* old_value, 130 bool needs_fence) { 131 void* prev = _owner; 132 ADIM_guarantee(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT 133 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value)); 134 Atomic::release_store(&_owner, (void*)NULL); 135 if (needs_fence) { 136 OrderAccess::fence(); 137 } else { 138 OrderAccess::storeload(); 139 } 140 log_trace(monitorinflation, owner)("release_clear_owner_with_barrier(): mid=" 141 INTPTR_FORMAT ", prev=" INTPTR_FORMAT 142 ", needs_fence=%d", p2i(this), p2i(prev), 143 needs_fence); 144 } 145 146 // Simply set _owner field to new_value; current value must match old_value. 147 // (Simple means no memory sync needed.) 148 inline void ObjectMonitor::simply_set_owner_from(void* new_value, void* old_value) { 149 void* prev = _owner; 150 ADIM_guarantee(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT 151 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value)); 152 _owner = new_value; 153 log_trace(monitorinflation, owner)("simply_set_owner_from(): mid=" 154 INTPTR_FORMAT ", prev=" INTPTR_FORMAT 155 ", new=" INTPTR_FORMAT, p2i(this), 156 p2i(prev), p2i(new_value)); 157 } 158 159 // Simply set _owner field to new_value; current value must match old_value1 or old_value2. 160 // (Simple means no memory sync needed.) 161 inline void ObjectMonitor::simply_set_owner_from(void* new_value, void* old_value1, void* old_value2) { 162 void* prev = _owner; 163 ADIM_guarantee(prev == old_value1 || prev == old_value2, 164 "unexpected prev owner=" INTPTR_FORMAT ", expected1=" 165 INTPTR_FORMAT " or expected2=" INTPTR_FORMAT, p2i(prev), 166 p2i(old_value1), p2i(old_value2)); 167 _owner = new_value; 168 log_trace(monitorinflation, owner)("simply_set_owner_from(old1=" INTPTR_FORMAT 169 ", old2=" INTPTR_FORMAT "): mid=" 170 INTPTR_FORMAT ", prev=" INTPTR_FORMAT 171 ", new=" INTPTR_FORMAT, p2i(old_value1), 172 p2i(old_value2), p2i(this), p2i(prev), 173 p2i(new_value)); 174 } 175 176 // Simply set _owner field to self; current value must match basic_lock_p. 177 inline void ObjectMonitor::simply_set_owner_from_BasicLock(Thread* self, void* basic_lock_p) { 178 void* prev = _owner; 179 ADIM_guarantee(prev == basic_lock_p, "unexpected prev owner=" INTPTR_FORMAT 180 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(basic_lock_p)); 181 // Non-null owner field to non-null owner field is safe without 182 // cmpxchg() as long as all readers can tolerate either flavor. 183 _owner = self; 184 log_trace(monitorinflation, owner)("simply_set_owner_from_BasicLock(): mid=" 185 INTPTR_FORMAT ", prev=" INTPTR_FORMAT 186 ", new=" INTPTR_FORMAT ", basic_lock_p=" 187 INTPTR_FORMAT, p2i(this), p2i(prev), 188 p2i(self), p2i(basic_lock_p)); 189 } 190 191 // Try to set _owner field to new_value if the current value matches 192 // old_value. Otherwise, does not change the _owner field. 193 inline void* ObjectMonitor::try_set_owner_from(void* new_value, void* old_value) { 194 void* prev = Atomic::cmpxchg(&_owner, old_value, new_value); 195 if (prev == old_value) { 196 log_trace(monitorinflation, owner)("try_set_owner_from(): mid=" 197 INTPTR_FORMAT ", prev=" INTPTR_FORMAT 198 ", new=" INTPTR_FORMAT, p2i(this), 199 p2i(prev), p2i(new_value)); 200 } 201 return prev; 202 } 203 204 inline void ObjectMonitor::set_allocation_state(ObjectMonitor::AllocationState s) { 205 _allocation_state = s; 206 } 207 208 inline ObjectMonitor::AllocationState ObjectMonitor::allocation_state() const { 209 return _allocation_state; 210 } 211 212 inline bool ObjectMonitor::is_free() const { 213 return _allocation_state == Free; 214 } 215 216 inline bool ObjectMonitor::is_old() const { 217 return _allocation_state == Old; 218 } 219 220 inline bool ObjectMonitor::is_new() const { 221 return _allocation_state == New; 222 } 223 224 inline void ObjectMonitor::dec_ref_count() { 225 Atomic::dec(&_ref_count); 226 // Can be negative as part of async deflation protocol. 227 jint l_ref_count = ref_count(); 228 ADIM_guarantee(AsyncDeflateIdleMonitors || l_ref_count >= 0, 229 "sanity check: l_ref_count=%d, ref_count=%d", l_ref_count, ref_count()); 230 } 231 232 inline void ObjectMonitor::inc_ref_count() { 233 Atomic::inc(&_ref_count); 234 // Can be negative as part of async deflation protocol. 235 jint l_ref_count = ref_count(); 236 ADIM_guarantee(AsyncDeflateIdleMonitors || l_ref_count > 0, 237 "sanity check: l_ref_count=%d, ref_count=%d", l_ref_count, ref_count()); 238 } 239 240 inline jint ObjectMonitor::ref_count() const { 241 return Atomic::load(&_ref_count); 242 } 243 244 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP