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(_owner == NULL, "must be NULL: owner=" INTPTR_FORMAT, p2i(_owner));
  61   assert(_ref_count == 0, "must be 0: ref_count=%d", _ref_count);
  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, owner and ref_count
  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 ref_count < 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 value here.
  81     guarantee(_owner == NULL || _owner == DEFLATER_MARKER,
  82               "must be NULL or DEFLATER_MARKER: owner=" INTPTR_FORMAT,
  83               p2i(_owner));
  84     guarantee(_ref_count <= 0, "must be <= 0: ref_count=%d", _ref_count);
  85   }
  86   assert(_contentions == 0, "must be 0: contentions=%d", _contentions);
  87   assert(_waiters == 0, "must be 0: waiters=%d", _waiters);
  88   assert(_recursions == 0, "must be 0: recursions=" INTPTR_FORMAT, _recursions);
  89   assert(_object != NULL, "must be non-NULL");
  90 
  91   set_allocation_state(Free);
  92   _object = NULL;
  93 }
  94 
  95 inline void* ObjectMonitor::object() const {
  96   return _object;
  97 }
  98 
  99 inline void* ObjectMonitor::object_addr() {
 100   return (void *)(&_object);
 101 }
 102 
 103 inline void ObjectMonitor::set_object(void* obj) {
 104   _object = obj;
 105 }
 106 
 107 inline bool ObjectMonitor::check(TRAPS) {
 108   if (THREAD != _owner) {
 109     if (THREAD->is_lock_owned((address) _owner)) {
 110       _owner = THREAD;  // regain ownership of inflated monitor
 111       assert (_recursions == 0, "invariant") ;
 112     } else {
 113       check_slow(THREAD);
 114       return false;
 115     }
 116   }
 117   return true;
 118 }
 119 
 120 // return number of threads contending for this monitor
 121 inline jint ObjectMonitor::contentions() const {
 122   return _contentions;
 123 }
 124 
 125 // Do NOT set _contentions = 0. There is a race such that _contentions could
 126 // be set while inflating prior to setting _owner
 127 // Just use Atomic::inc/dec and assert 0 when monitor put on free list
 128 inline void ObjectMonitor::set_owner(void* owner) {
 129   _owner = owner;
 130   _recursions = 0;
 131 }
 132 
 133 inline void ObjectMonitor::set_allocation_state(ObjectMonitor::AllocationState s) {
 134   _allocation_state = s;
 135 }
 136 
 137 inline ObjectMonitor::AllocationState ObjectMonitor::allocation_state() const {
 138   return _allocation_state;
 139 }
 140 
 141 inline bool ObjectMonitor::is_free() const {
 142   return _allocation_state == Free;
 143 }
 144 
 145 inline bool ObjectMonitor::is_active() const {
 146   return !is_free();
 147 }
 148 
 149 inline bool ObjectMonitor::is_old() const {
 150   return _allocation_state == Old;
 151 }
 152 
 153 inline bool ObjectMonitor::is_new() const {
 154   return _allocation_state == New;
 155 }
 156 
 157 inline void ObjectMonitor::dec_ref_count() {
 158   // The decrement only needs to be MO_ACQ_REL since the reference
 159   // counter is volatile.
 160   Atomic::dec(&_ref_count);
 161   // Can be negative as part of async deflation protocol.
 162   guarantee(AsyncDeflateIdleMonitors || _ref_count >= 0,
 163             "sanity check: ref_count=%d", _ref_count);
 164 }
 165 
 166 inline void ObjectMonitor::inc_ref_count() {
 167   // The increment needs to be MO_SEQ_CST so that the reference
 168   // counter update is seen as soon as possible in a race with the
 169   // async deflation protocol.
 170   Atomic::inc(&_ref_count);
 171   // Can be negative as part of async deflation protocol.
 172   guarantee(AsyncDeflateIdleMonitors || _ref_count > 0,
 173             "sanity check: ref_count=%d", _ref_count);
 174 }
 175 
 176 inline jint ObjectMonitor::ref_count() const {
 177   return OrderAccess::load_acquire(&_ref_count);
 178 }
 179 
 180 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP