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