< prev index next >

src/hotspot/share/runtime/objectMonitor.inline.hpp

Print this page
rev 56634 : imported patch 8230876.patch
rev 56635 : v2.00 -> v2.05 (CR5/v2.05/8-for-jdk13) patches combined into one; merge with 8229212.patch; merge with jdk-14+11; merge with 8230184.patch; merge with 8230876.patch; merge with jdk-14+15; merge with jdk-14+18.
rev 56639 : loosen a couple more counter checks due to races observed in testing; simplify om_release() extraction of mid since list head or cur_mid_in_use is marked; simplify deflate_monitor_list() extraction of mid since there are no parallel deleters due to the safepoint; simplify deflate_monitor_list_using_JT() extraction of mid since list head or cur_mid_in_use is marked; prepend_block_to_lists() - simplify based on David H's comments; does not need load_acquire() or release_store() because of the cmpxchg(); prepend_to_common() - simplify to use mark_next_loop() for m and use mark_list_head() and release_store() for the non-empty list case; add more debugging for "Non-balanced monitor enter/exit" failure mode; fix race in inflate() in the "CASE: neutral" code path; install_displaced_markword_in_object() does not need to clear the header field since that is handled when the ObjectMonitor is moved from the global free list; LSuccess should clear boxReg to set ICC.ZF=1 to avoid depending on existing boxReg contents; update fast_unlock() to detect when object no longer refers to the same ObjectMonitor and take fast path exit instead; clarify fast_lock() code where we detect when object no longer refers to the same ObjectMonitor; add/update comments for movptr() calls where we move a literal into an Address; remove set_owner(); refactor setting of owner field into set_owner_from(2 versions), set_owner_from_BasicLock(), and try_set_owner_from(); the new functions include monitorinflation+owner logging; extract debug code from v2.06 and v2.07 and move to v2.07.debug; change 'jccb' -> 'jcc' and 'jmpb' -> 'jmp' as needed; checkpoint initial version of MacroAssembler::inc_om_ref_count(); update LP64 MacroAssembler::fast_lock() and fast_unlock() to use inc_om_ref_count(); fast_lock() return flag setting logic can use 'testptr(tmpReg, tmpReg)' instead of 'cmpptr(tmpReg, 0)' since that's more efficient; fast_unlock() LSuccess return flag setting logic can use 'testl (boxReg, 0)' instead of 'xorptr(boxReg, boxReg)' since that's more efficient; cleanup "fast-path" vs "fast path" and "slow-path" vs "slow path"; update MacroAssembler::rtm_inflated_locking() to use inc_om_ref_count(); update MacroAssembler::fast_lock() to preserve the flags before decrementing ref_count and restore the flags afterwards; this is more clean than depending on the contents of rax/tmpReg; coleenp CR - refactor async monitor deflation work from ServiceThread::service_thread_entry() to ObjectSynchronizer::deflate_idle_monitors_using_JT(); rehn,eosterlund CR - add support for HandshakeAfterDeflateIdleMonitors for platforms that don't have ObjectMonitor ref_count support implemented in C2 fast_lock() and fast_unlock().


   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 "runtime/atomic.hpp"
  29 
  30 inline intptr_t ObjectMonitor::is_entered(TRAPS) const {
  31   if (THREAD == _owner || THREAD->is_lock_owned((address) _owner)) {
  32     return 1;
  33   }
  34   return 0;
  35 }
  36 
  37 inline markWord ObjectMonitor::header() const {
  38   return Atomic::load(&_header);
  39 }
  40 
  41 inline volatile markWord* ObjectMonitor::header_addr() {
  42   assert((intptr_t)this == (intptr_t)&_header, "sync code expects this");
  43   return &_header;
  44 }
  45 
  46 inline void ObjectMonitor::set_header(markWord hdr) {
  47   Atomic::store(hdr, &_header);
  48 }
  49 
  50 inline jint ObjectMonitor::waiters() const {
  51   return _waiters;
  52 }
  53 

  54 inline void* ObjectMonitor::owner() const {
  55   return _owner;








  56 }
  57 
  58 inline void ObjectMonitor::clear() {
  59   assert(Atomic::load(&_header).value() != 0, "must be non-zero");


























  60   assert(_contentions == 0, "must be 0: contentions=%d", _contentions);
  61   assert(_waiters == 0, "must be 0: waiters=%d", _waiters);
  62   assert(_recursions == 0, "must be 0: recursions=" INTPTR_FORMAT, _recursions);
  63   assert(_object != NULL, "must be non-NULL");
  64   assert(_owner == NULL, "must be NULL: owner=" INTPTR_FORMAT, p2i(_owner));
  65 
  66   Atomic::store(markWord::zero(), &_header);
  67   _object = NULL;
  68 }
  69 
  70 inline void* ObjectMonitor::object() const {
  71   return _object;
  72 }
  73 
  74 inline void* ObjectMonitor::object_addr() {
  75   return (void *)(&_object);
  76 }
  77 
  78 inline void ObjectMonitor::set_object(void* obj) {
  79   _object = obj;
  80 }
  81 
  82 // return number of threads contending for this monitor
  83 inline jint ObjectMonitor::contentions() const {
  84   return _contentions;
  85 }
  86 
  87 inline void ObjectMonitor::set_owner(void* owner) {
  88   _owner = owner;































































































  89 }
  90 
  91 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP


   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(hdr, &_header);
  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 OrderAccess::load_acquire(&_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   assert(ref_count() == 0, "must be 0: ref_count=%d", ref_count());
  72 
  73   Atomic::store(markWord::zero(), &_header);
  74 
  75   clear_using_JT();
  76 }
  77 
  78 inline void ObjectMonitor::clear_using_JT() {
  79   // Unlike other *_using_JT() functions, we cannot assert
  80   // AsyncDeflateIdleMonitors or Thread::current()->is_Java_thread()
  81   // because clear() calls this function for the rest of its checks.
  82 
  83   if (AsyncDeflateIdleMonitors) {
  84     // Async deflation protocol uses the header, owner and ref_count
  85     // fields. While the ObjectMonitor being deflated is on the global free
  86     // list, we leave those three fields alone; owner == DEFLATER_MARKER
  87     // and ref_count < 0 will force any racing threads to retry. The
  88     // header field is used by install_displaced_markword_in_object()
  89     // in the last part of the deflation protocol so we cannot check
  90     // its value here.
  91     guarantee(_owner == NULL || _owner == DEFLATER_MARKER,
  92               "must be NULL or DEFLATER_MARKER: owner=" INTPTR_FORMAT,
  93               p2i(_owner));
  94     guarantee(ref_count() <= 0, "must be <= 0: ref_count=%d", ref_count());
  95   }
  96   assert(_contentions == 0, "must be 0: contentions=%d", _contentions);
  97   assert(_waiters == 0, "must be 0: waiters=%d", _waiters);
  98   assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
  99   assert(_object != NULL, "must be non-NULL");

 100  
 101   set_allocation_state(Free);
 102   _object = NULL;
 103 }
 104 
 105 inline void* ObjectMonitor::object() const {
 106   return _object;
 107 }
 108 
 109 inline void* ObjectMonitor::object_addr() {
 110   return (void *)(&_object);
 111 }
 112 
 113 inline void ObjectMonitor::set_object(void* obj) {
 114   _object = obj;
 115 }
 116 
 117 // return number of threads contending for this monitor
 118 inline jint ObjectMonitor::contentions() const {
 119   return _contentions;
 120 }
 121 
 122 // Set _owner field to new_value; current value must match old_value.
 123 inline void ObjectMonitor::set_owner_from(void* new_value, void* old_value) {
 124   void* prev = Atomic::cmpxchg(new_value, &_owner, old_value);
 125   ADIM_guarantee(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
 126                  ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
 127   log_trace(monitorinflation, owner)("mid=" INTPTR_FORMAT ", prev="
 128                                      INTPTR_FORMAT ", new=" INTPTR_FORMAT,
 129                                      p2i(this), p2i(prev), p2i(new_value));
 130 }
 131 
 132 // Set _owner field to new_value; current value must match old_value1 or old_value2.
 133 inline void ObjectMonitor::set_owner_from(void* new_value, void* old_value1, void* old_value2) {
 134   void* prev = Atomic::cmpxchg(new_value, &_owner, old_value1);
 135   if (prev != old_value1) {
 136     prev = Atomic::cmpxchg(new_value, &_owner, old_value2);
 137   }
 138   ADIM_guarantee(prev == old_value1 || prev == old_value2,
 139                  "unexpected prev owner=" INTPTR_FORMAT ", expected1="
 140                  INTPTR_FORMAT " or expected2=" INTPTR_FORMAT, p2i(prev),
 141                  p2i(old_value1), p2i(old_value2));
 142   log_trace(monitorinflation, owner)("mid=" INTPTR_FORMAT ", prev="
 143                                      INTPTR_FORMAT ", new=" INTPTR_FORMAT,
 144                                      p2i(this), p2i(prev), p2i(new_value));
 145 }
 146 
 147 // Set _owner field to self; current value must match basic_lock_p.
 148 inline void ObjectMonitor::set_owner_from_BasicLock(Thread* self, void* basic_lock_p) {
 149   assert(self->is_lock_owned((address)basic_lock_p), "self=" INTPTR_FORMAT
 150          " must own basic_lock_p=" INTPTR_FORMAT, p2i(self), p2i(basic_lock_p));
 151   void* prev = _owner;
 152   ADIM_guarantee(prev == basic_lock_p, "unexpected prev owner=" INTPTR_FORMAT
 153                  ", expected=" INTPTR_FORMAT, p2i(prev), p2i(basic_lock_p));
 154   // Non-null owner field to non-null owner field is safe without
 155   // cmpxchg() as long as all readers can tolerate either flavor.
 156   _owner = self;
 157   log_trace(monitorinflation, owner)("mid=" INTPTR_FORMAT ", prev="
 158                                      INTPTR_FORMAT ", new=" INTPTR_FORMAT,
 159                                      p2i(this), p2i(prev), p2i(self));
 160 }
 161 
 162 // Try to set _owner field to new_value if the current value matches
 163 // old_value. Otherwise, does not change the _owner field.
 164 inline void* ObjectMonitor::try_set_owner_from(void* new_value, void* old_value) {
 165   void* prev = Atomic::cmpxchg(new_value, &_owner, old_value);
 166   if (prev == old_value) {
 167     log_trace(monitorinflation, owner)("mid=" INTPTR_FORMAT ", prev="
 168                                        INTPTR_FORMAT ", new=" INTPTR_FORMAT,
 169                                        p2i(this), p2i(prev), p2i(new_value));
 170   }
 171   return prev;
 172 }
 173 
 174 inline void ObjectMonitor::set_allocation_state(ObjectMonitor::AllocationState s) {
 175   _allocation_state = s;
 176 }
 177 
 178 inline ObjectMonitor::AllocationState ObjectMonitor::allocation_state() const {
 179   return _allocation_state;
 180 }
 181 
 182 inline bool ObjectMonitor::is_free() const {
 183   return _allocation_state == Free;
 184 }
 185 
 186 inline bool ObjectMonitor::is_active() const {
 187   return !is_free();
 188 }
 189 
 190 inline bool ObjectMonitor::is_old() const {
 191   return _allocation_state == Old;
 192 }
 193 
 194 inline bool ObjectMonitor::is_new() const {
 195   return _allocation_state == New;
 196 }
 197 
 198 inline void ObjectMonitor::dec_ref_count() {
 199   // The decrement only needs to be MO_ACQ_REL since the reference
 200   // counter is volatile.
 201   Atomic::dec(&_ref_count);
 202   // Can be negative as part of async deflation protocol.
 203   ADIM_guarantee(AsyncDeflateIdleMonitors || ref_count() >= 0,
 204                  "sanity check: ref_count=%d", ref_count());
 205 }
 206 
 207 inline void ObjectMonitor::inc_ref_count() {
 208   // The increment needs to be MO_SEQ_CST so that the reference
 209   // counter update is seen as soon as possible in a race with the
 210   // async deflation protocol.
 211   Atomic::inc(&_ref_count);
 212   // Can be negative as part of async deflation protocol.
 213   ADIM_guarantee(AsyncDeflateIdleMonitors || ref_count() > 0,
 214                  "sanity check: ref_count=%d", ref_count());
 215 }
 216 
 217 inline jint ObjectMonitor::ref_count() const {
 218   return OrderAccess::load_acquire(&_ref_count);
 219 }
 220 
 221 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP
< prev index next >