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 inline void* ObjectMonitor::owner() const { 56 return _owner; 57 } 58 59 inline void ObjectMonitor::clear() { 60 assert(Atomic::load(&_header).value() != 0, "must be non-zero"); 61 assert(_contentions == 0, "must be 0: contentions=%d", _contentions); 62 assert(_waiters == 0, "must be 0: waiters=%d", _waiters); 63 assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions); 64 assert(_object != NULL, "must be non-NULL"); 65 assert(_owner == NULL, "must be NULL: owner=" INTPTR_FORMAT, p2i(_owner)); 66 67 Atomic::store(&_header, markWord::zero()); 68 _object = NULL; 69 } 70 71 inline void* ObjectMonitor::object() const { 72 return _object; 73 } 74 75 inline void* ObjectMonitor::object_addr() { 76 return (void *)(&_object); 77 } 78 79 inline void ObjectMonitor::set_object(void* obj) { 80 _object = obj; 81 } 82 83 // return number of threads contending for this monitor 84 inline jint ObjectMonitor::contentions() const { 85 return _contentions; 86 } 87 88 // Clear _owner field; current value must match old_value. 89 // If needs_fence is true, we issue a fence() after the release_store(). 90 // Otherwise, a storeload() is good enough. See the callers for more info. 91 inline void ObjectMonitor::release_clear_owner_with_barrier(void* old_value, 92 bool needs_fence) { 93 void* prev = _owner; 94 assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT 95 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value)); 96 Atomic::release_store(&_owner, (void*)NULL); 97 if (needs_fence) { 98 OrderAccess::fence(); 99 } else { 100 OrderAccess::storeload(); 101 } 102 log_trace(monitorinflation, owner)("release_clear_owner_with_barrier(): mid=" 103 INTPTR_FORMAT ", prev=" INTPTR_FORMAT 104 ", needs_fence=%d", p2i(this), p2i(prev), 105 needs_fence); 106 } 107 108 // Simply set _owner field to new_value; current value must match old_value. 109 // (Simple means no memory sync needed.) 110 inline void ObjectMonitor::simply_set_owner_from(void* old_value, void* new_value) { 111 void* prev = _owner; 112 assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT 113 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value)); 114 _owner = new_value; 115 log_trace(monitorinflation, owner)("simply_set_owner_from(): mid=" 116 INTPTR_FORMAT ", prev=" INTPTR_FORMAT 117 ", new=" INTPTR_FORMAT, p2i(this), 118 p2i(prev), p2i(new_value)); 119 } 120 121 // Simply set _owner field to self; current value must match basic_lock_p. 122 inline void ObjectMonitor::simply_set_owner_from_BasicLock(void* basic_lock_p, Thread* self) { 123 void* prev = _owner; 124 assert(prev == basic_lock_p, "unexpected prev owner=" INTPTR_FORMAT 125 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(basic_lock_p)); 126 // Non-null owner field to non-null owner field is safe without 127 // cmpxchg() as long as all readers can tolerate either flavor. 128 _owner = self; 129 log_trace(monitorinflation, owner)("simply_set_owner_from_BasicLock(): mid=" 130 INTPTR_FORMAT ", prev=" INTPTR_FORMAT 131 ", new=" INTPTR_FORMAT ", basic_lock_p=" 132 INTPTR_FORMAT, p2i(this), p2i(prev), 133 p2i(self), p2i(basic_lock_p)); 134 } 135 136 // Try to set _owner field to new_value if the current value matches 137 // old_value. Otherwise, does not change the _owner field. 138 inline void* ObjectMonitor::try_set_owner_from(void* old_value, void* new_value) { 139 void* prev = Atomic::cmpxchg(&_owner, old_value, new_value); 140 if (prev == old_value) { 141 log_trace(monitorinflation, owner)("try_set_owner_from(): mid=" 142 INTPTR_FORMAT ", prev=" INTPTR_FORMAT 143 ", new=" INTPTR_FORMAT, p2i(this), 144 p2i(prev), p2i(new_value)); 145 } 146 return prev; 147 } 148 149 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP