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(_contentions == 0, "must be 0: contentions=%d", _contentions);
  60   assert(_owner == NULL, "must be NULL: owner=" INTPTR_FORMAT, p2i(_owner));
  61 
  62   clear_using_JT();
  63 }
  64 
  65 inline void ObjectMonitor::clear_using_JT() {
  66   // When clearing using a JavaThread, we leave _owner == DEFLATER_MARKER
  67   // and _contentions < 0 to force any racing threads to retry. Unlike other
  68   // *_using_JT() functions, we cannot assert AsyncDeflateIdleMonitors
  69   // or Thread::current()->is_Java_thread() because clear() calls this
  70   // function for the rest of its checks.
  71 
  72   assert(_header != NULL, "must be non-NULL");
  73   assert(_waiters == 0, "must be 0: waiters=%d", _waiters);
  74   assert(_recursions == 0, "must be 0: recursions=" INTPTR_FORMAT, _recursions);
  75   assert(_object != NULL, "must be non-NULL");
  76   // Do not assert _ref_count == 0 here because a racing thread could
  77   // increment _ref_count, observe _owner == DEFLATER_MARKER and then
  78   // decrement _ref_count.
  79 
  80   set_allocation_state(Free);
  81   _header = NULL;
  82   _object = NULL;
  83   // Do not clear _ref_count here because _ref_count is for indicating
  84   // that the ObjectMonitor* is in use which is orthogonal to whether
  85   // the ObjectMonitor itself is in use for a locking operation.
  86 }
  87 
  88 inline void* ObjectMonitor::object() const {
  89   return _object;
  90 }
  91 
  92 inline void* ObjectMonitor::object_addr() {
  93   return (void *)(&_object);
  94 }
  95 
  96 inline void ObjectMonitor::set_object(void* obj) {
  97   _object = obj;
  98 }
  99 
 100 inline bool ObjectMonitor::check(TRAPS) {
 101   if (THREAD != _owner) {
 102     if (THREAD->is_lock_owned((address) _owner)) {
 103       _owner = THREAD;  // regain ownership of inflated monitor
 104       assert (_recursions == 0, "invariant") ;
 105     } else {
 106       check_slow(THREAD);
 107       return false;
 108     }
 109   }
 110   return true;
 111 }
 112 
 113 // return number of threads contending for this monitor
 114 inline jint ObjectMonitor::contentions() const {
 115   return _contentions;
 116 }
 117 
 118 // Do NOT set _contentions = 0. There is a race such that _contentions could
 119 // be set while inflating prior to setting _owner
 120 // Just use Atomic::inc/dec and assert 0 when monitor put on free list
 121 inline void ObjectMonitor::set_owner(void* owner) {
 122   _owner = owner;
 123   _recursions = 0;
 124 }
 125 
 126 inline void ObjectMonitor::set_allocation_state(ObjectMonitor::AllocationState s) {
 127   _allocation_state = s;
 128 }
 129 
 130 inline ObjectMonitor::AllocationState ObjectMonitor::allocation_state() const {
 131   return _allocation_state;
 132 }
 133 
 134 inline bool ObjectMonitor::is_free() const {
 135   return _allocation_state == Free;
 136 }
 137 
 138 inline bool ObjectMonitor::is_active() const {
 139   return !is_free();
 140 }
 141 
 142 inline bool ObjectMonitor::is_old() const {
 143   return _allocation_state == Old;
 144 }
 145 
 146 inline bool ObjectMonitor::is_new() const {
 147   return _allocation_state == New;
 148 }
 149 
 150 inline void ObjectMonitor::dec_ref_count() {
 151   // The decrement needs to be MO_ACQ_REL. At the moment, the Atomic::dec
 152   // backend on PPC does not yet conform to these requirements. Therefore
 153   // the decrement is simulated with an Atomic::sub(1, &addr). Without
 154   // this MO_ACQ_REL Atomic::dec simulation, AsyncDeflateIdleMonitors is
 155   // not safe.
 156   Atomic::sub((jint)1, &_ref_count);
 157   guarantee(_ref_count >= 0, "sanity check: ref_count=%d", _ref_count);
 158 }
 159 
 160 inline void ObjectMonitor::inc_ref_count() {
 161   // The increment needs to be MO_SEQ_CST. At the moment, the Atomic::inc
 162   // backend on PPC does not yet conform to these requirements. Therefore
 163   // the increment is simulated with a load phi; cas phi + 1; loop.
 164   // Without this MO_SEQ_CST Atomic::inc simulation, AsyncDeflateIdleMonitors
 165   // is not safe.
 166   for (;;) {
 167     jint sample = OrderAccess::load_acquire(&_ref_count);
 168     guarantee(sample >= 0, "sanity check: sample=%d", (int)sample);
 169     if (Atomic::cmpxchg(sample + 1, &_ref_count, sample) == sample) {
 170       // Incremented _ref_count without interference.
 171       return;
 172     }
 173     // Implied else: Saw interference so loop and try again.
 174   }
 175 }
 176 
 177 inline jint ObjectMonitor::ref_count() const {
 178   return OrderAccess::load_acquire(&_ref_count);
 179 }
 180 
 181 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP