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 // Returns true if owner field == DEFLATER_MARKER and false otherwise. 59 // This accessor is called when we really need to know if the owner 60 // field == DEFLATER_MARKER and any non-NULL value won't do the trick. 61 inline bool ObjectMonitor::owner_is_DEFLATER_MARKER() { 62 return OrderAccess::load_acquire(&_owner) == DEFLATER_MARKER; 63 } 64 65 inline void ObjectMonitor::clear() { 66 assert(_header != NULL, "must be non-NULL"); 67 assert(_owner == NULL, "must be NULL: owner=" INTPTR_FORMAT, p2i(_owner)); 68 assert(ref_count() == 0, "must be 0: ref_count=%d", ref_count()); 69 70 _header = NULL; 71 72 clear_using_JT(); 73 } 74 75 inline void ObjectMonitor::clear_using_JT() { 76 // Unlike other *_using_JT() functions, we cannot assert 77 // AsyncDeflateIdleMonitors or Thread::current()->is_Java_thread() 78 // because clear() calls this function for the rest of its checks. 79 80 if (AsyncDeflateIdleMonitors) { 81 // Async deflation protocol uses the header, owner and ref_count 82 // fields. While the ObjectMonitor being deflated is on the global free 83 // list, we leave those three fields alone; owner == DEFLATER_MARKER 84 // and ref_count < 0 will force any racing threads to retry. The 85 // header field is used by install_displaced_markword_in_object() 86 // in the last part of the deflation protocol so we cannot check 87 // its value here. 88 guarantee(_owner == NULL || _owner == DEFLATER_MARKER, 89 "must be NULL or DEFLATER_MARKER: owner=" INTPTR_FORMAT, 90 p2i(_owner)); 91 guarantee(ref_count() <= 0, "must be <= 0: ref_count=%d", ref_count()); 92 } 93 assert(_contentions == 0, "must be 0: contentions=%d", _contentions); 94 assert(_waiters == 0, "must be 0: waiters=%d", _waiters); 95 assert(_recursions == 0, "must be 0: recursions=" INTPTR_FORMAT, _recursions); 96 assert(_object != NULL, "must be non-NULL"); 97 98 set_allocation_state(Free); 99 _object = NULL; 100 } 101 102 inline void* ObjectMonitor::object() const { 103 return _object; 104 } 105 106 inline void* ObjectMonitor::object_addr() { 107 return (void *)(&_object); 108 } 109 110 inline void ObjectMonitor::set_object(void* obj) { 111 _object = obj; 112 } 113 114 inline bool ObjectMonitor::check(TRAPS) { 115 if (THREAD != _owner) { 116 if (THREAD->is_lock_owned((address) _owner)) { 117 _owner = THREAD; // regain ownership of inflated monitor 118 assert (_recursions == 0, "invariant") ; 119 } else { 120 check_slow(THREAD); 121 return false; 122 } 123 } 124 return true; 125 } 126 127 // return number of threads contending for this monitor 128 inline jint ObjectMonitor::contentions() const { 129 return _contentions; 130 } 131 132 inline void ObjectMonitor::set_owner(void* owner) { 133 _owner = owner; 134 } 135 136 inline void ObjectMonitor::set_allocation_state(ObjectMonitor::AllocationState s) { 137 _allocation_state = s; 138 } 139 140 inline ObjectMonitor::AllocationState ObjectMonitor::allocation_state() const { 141 return _allocation_state; 142 } 143 144 inline bool ObjectMonitor::is_free() const { 145 return _allocation_state == Free; 146 } 147 148 inline bool ObjectMonitor::is_active() const { 149 return !is_free(); 150 } 151 152 inline bool ObjectMonitor::is_old() const { 153 return _allocation_state == Old; 154 } 155 156 inline bool ObjectMonitor::is_new() const { 157 return _allocation_state == New; 158 } 159 160 inline void ObjectMonitor::dec_ref_count() { 161 // The decrement only needs to be MO_ACQ_REL since the reference 162 // counter is volatile. 163 Atomic::dec(&_ref_count); 164 // Can be negative as part of async deflation protocol. 165 ADIM_guarantee(AsyncDeflateIdleMonitors || ref_count() >= 0, 166 "sanity check: ref_count=%d", ref_count()); 167 } 168 169 inline void ObjectMonitor::inc_ref_count() { 170 // The increment needs to be MO_SEQ_CST so that the reference 171 // counter update is seen as soon as possible in a race with the 172 // async deflation protocol. 173 Atomic::inc(&_ref_count); 174 // Can be negative as part of async deflation protocol. 175 ADIM_guarantee(AsyncDeflateIdleMonitors || ref_count() > 0, 176 "sanity check: ref_count=%d", ref_count()); 177 } 178 179 inline jint ObjectMonitor::ref_count() const { 180 return OrderAccess::load_acquire(&_ref_count); 181 } 182 183 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP