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 inline void ObjectMonitor::release_clear_owner(void* old_value) { 90 void* prev = _owner; 91 assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT 92 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value)); 93 Atomic::release_store(&_owner, (void*)NULL); 94 log_trace(monitorinflation, owner)("release_clear_owner(): mid=" 95 INTPTR_FORMAT ", prev=" INTPTR_FORMAT, 96 p2i(this), p2i(prev)); 97 } 98 99 // Simply set _owner field to new_value; current value must match old_value. 100 // (Simple means no memory sync needed.) 101 inline void ObjectMonitor::set_owner_from(void* old_value, void* new_value) { 102 void* prev = _owner; 103 assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT 104 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value)); 105 _owner = new_value; 106 log_trace(monitorinflation, owner)("set_owner_from(): mid=" 107 INTPTR_FORMAT ", prev=" INTPTR_FORMAT 108 ", new=" INTPTR_FORMAT, p2i(this), 109 p2i(prev), p2i(new_value)); 110 } 111 112 // Simply set _owner field to self; current value must match basic_lock_p. 113 inline void ObjectMonitor::set_owner_from_BasicLock(void* basic_lock_p, Thread* self) { 114 void* prev = _owner; 115 assert(prev == basic_lock_p, "unexpected prev owner=" INTPTR_FORMAT 116 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(basic_lock_p)); 117 // Non-null owner field to non-null owner field is safe without 118 // cmpxchg() as long as all readers can tolerate either flavor. 119 _owner = self; 120 log_trace(monitorinflation, owner)("set_owner_from_BasicLock(): mid=" 121 INTPTR_FORMAT ", prev=" INTPTR_FORMAT 122 ", new=" INTPTR_FORMAT ", basic_lock_p=" 123 INTPTR_FORMAT, p2i(this), p2i(prev), 124 p2i(self), p2i(basic_lock_p)); 125 } 126 127 // Try to set _owner field to new_value if the current value matches 128 // old_value. Otherwise, does not change the _owner field. 129 inline void* ObjectMonitor::try_set_owner_from(void* old_value, void* new_value) { 130 void* prev = Atomic::cmpxchg(&_owner, old_value, new_value); 131 if (prev == old_value) { 132 log_trace(monitorinflation, owner)("try_set_owner_from(): mid=" 133 INTPTR_FORMAT ", prev=" INTPTR_FORMAT 134 ", new=" INTPTR_FORMAT, p2i(this), 135 p2i(prev), p2i(new_value)); 136 } 137 return prev; 138 } 139 140 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP