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 inline intptr_t ObjectMonitor::is_entered(TRAPS) const { 29 if (THREAD == _owner || THREAD->is_lock_owned((address) _owner)) { 30 return 1; 31 } 32 return 0; 33 } 34 35 inline markOop ObjectMonitor::header() const { 36 return _header; 37 } 38 39 inline volatile markOop* ObjectMonitor::header_addr() { 40 assert((intptr_t)this == (intptr_t)&_header, "sync code expects this"); 41 return &_header; 42 } 43 44 inline void ObjectMonitor::set_header(markOop hdr) { 45 _header = hdr; 46 } 47 48 inline jint ObjectMonitor::waiters() const { 49 return _waiters; 50 } 51 52 // Returns NULL if DEFLATER_MARKER is observed. 53 inline void* ObjectMonitor::owner() const { 54 void* owner = _owner; 55 return owner != DEFLATER_MARKER ? owner : NULL; 56 } 57 58 inline void ObjectMonitor::clear() { 59 assert(_header != NULL, "must be non-NULL"); 60 assert(_contentions == 0, "must be 0: contentions=%d", _contentions); 61 assert(_owner == NULL, "must be NULL: owner=" INTPTR_FORMAT, p2i(_owner)); 62 63 _header = NULL; 64 65 clear_using_JT(); 66 } 67 68 inline void ObjectMonitor::clear_using_JT() { 69 // Unlike other *_using_JT() functions, we cannot assert 70 // AsyncDeflateIdleMonitors or Thread::current()->is_Java_thread() 71 // because clear() calls this function for the rest of its checks. 72 73 if (AsyncDeflateIdleMonitors) { 74 // Async deflation protocol uses the _header, _contentions and _owner 75 // fields. While the ObjectMonitor being deflated is on the global free 76 // list, we leave those three fields alone; _owner == DEFLATER_MARKER 77 // and _contentions < 0 will force any racing threads to retry. The 78 // _header field is used by install_displaced_markword_in_object() 79 // in the last part of the deflation protocol so we cannot check 80 // its values here. 81 guarantee(_owner == NULL || _owner == DEFLATER_MARKER, 82 "must be NULL or DEFLATER_MARKER: owner=" INTPTR_FORMAT, 83 p2i(_owner)); 84 guarantee(_contentions <= 0, "must be <= 0: contentions=%d", _contentions); 85 } 86 assert(_waiters == 0, "must be 0: waiters=%d", _waiters); 87 assert(_recursions == 0, "must be 0: recursions=" INTPTR_FORMAT, _recursions); 88 assert(_object != NULL, "must be non-NULL"); 89 // Do not assert _ref_count == 0 here because a racing thread could 90 // increment _ref_count, observe _owner == DEFLATER_MARKER and then 91 // decrement _ref_count. 92 93 set_allocation_state(Free); 94 _object = NULL; 95 // Do not clear _ref_count here because _ref_count is for indicating 96 // that the ObjectMonitor* is in use which is orthogonal to whether 97 // the ObjectMonitor itself is in use for a locking operation. 98 } 99 100 inline void* ObjectMonitor::object() const { 101 return _object; 102 } 103 104 inline void* ObjectMonitor::object_addr() { 105 return (void *)(&_object); 106 } 107 108 inline void ObjectMonitor::set_object(void* obj) { 109 _object = obj; 110 } 111 112 inline bool ObjectMonitor::check(TRAPS) { 113 if (THREAD != _owner) { 114 if (THREAD->is_lock_owned((address) _owner)) { 115 _owner = THREAD; // regain ownership of inflated monitor 116 assert (_recursions == 0, "invariant") ; 117 } else { 118 check_slow(THREAD); 119 return false; 120 } 121 } 122 return true; 123 } 124 125 // return number of threads contending for this monitor 126 inline jint ObjectMonitor::contentions() const { 127 return _contentions; 128 } 129 130 // Do NOT set _contentions = 0. There is a race such that _contentions could 131 // be set while inflating prior to setting _owner 132 // Just use Atomic::inc/dec and assert 0 when monitor put on free list 133 inline void ObjectMonitor::set_owner(void* owner) { 134 _owner = owner; 135 _recursions = 0; 136 } 137 138 inline void ObjectMonitor::set_allocation_state(ObjectMonitor::AllocationState s) { 139 _allocation_state = s; 140 } 141 142 inline ObjectMonitor::AllocationState ObjectMonitor::allocation_state() const { 143 return _allocation_state; 144 } 145 146 inline bool ObjectMonitor::is_free() const { 147 return _allocation_state == Free; 148 } 149 150 inline bool ObjectMonitor::is_active() const { 151 return !is_free(); 152 } 153 154 inline bool ObjectMonitor::is_old() const { 155 return _allocation_state == Old; 156 } 157 158 inline bool ObjectMonitor::is_new() const { 159 return _allocation_state == New; 160 } 161 162 inline void ObjectMonitor::dec_ref_count() { 163 // The decrement only needs to be MO_ACQ_REL since the reference 164 // counter is volatile. 165 Atomic::dec(&_ref_count); 166 // Can be negative as part of async deflation protocol. 167 guarantee(AsyncDeflateIdleMonitors || _ref_count >= 0, 168 "sanity check: ref_count=%d", _ref_count); 169 } 170 171 inline void ObjectMonitor::inc_ref_count() { 172 // The increment needs to be MO_SEQ_CST so that the reference 173 // counter update is seen as soon as possible in a race with the 174 // async deflation protocol. 175 Atomic::inc(&_ref_count); 176 // Can be negative as part of async deflation protocol. 177 guarantee(AsyncDeflateIdleMonitors || _ref_count > 0, 178 "sanity check: ref_count=%d", _ref_count); 179 } 180 181 inline jint ObjectMonitor::ref_count() const { 182 return OrderAccess::load_acquire(&_ref_count); 183 } 184 185 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP