1 /*
   2  * Copyright (c) 1998, 2020, 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 "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(&_header, hdr);
  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 Atomic::load(&_owner) == DEFLATER_MARKER;
  66 }
  67 
  68 // Returns true if 'this' is being async deflated and false otherwise.
  69 inline bool ObjectMonitor::is_being_async_deflated() {
  70   return contentions() < 0;
  71 }
  72 
  73 inline void ObjectMonitor::clear() {
  74   assert(Atomic::load(&_header).value() != 0, "must be non-zero");
  75   assert(_owner == NULL, "must be NULL: owner=" INTPTR_FORMAT, p2i(_owner));
  76 
  77   Atomic::store(&_header, markWord::zero());
  78 
  79   clear_common();
  80 }
  81 
  82 inline void ObjectMonitor::clear_common() {
  83   // Async deflation protocol uses the header, owner and contentions
  84   // fields. While the ObjectMonitor being deflated is on the global
  85   // free list, we leave those three fields alone; contentions < 0
  86   // will force any racing threads to retry. The header field is used
  87   // by install_displaced_markword_in_object() to restore the object's
  88   // header so we cannot check its value here.
  89   guarantee(_owner == NULL || _owner == DEFLATER_MARKER,
  90             "must be NULL or DEFLATER_MARKER: owner=" INTPTR_FORMAT,
  91             p2i(_owner));
  92   assert(contentions() <= 0, "must not be positive: contentions=%d", contentions());
  93   assert(_waiters == 0, "must be 0: waiters=%d", _waiters);
  94   assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
  95   assert(_object != NULL, "must be non-NULL");
  96 
  97   set_allocation_state(Free);
  98   _object = NULL;
  99 }
 100 
 101 inline void* ObjectMonitor::object() const {
 102   return _object;
 103 }
 104 
 105 inline void* ObjectMonitor::object_addr() {
 106   return (void *)(&_object);
 107 }
 108 
 109 inline void ObjectMonitor::set_object(void* obj) {
 110   _object = obj;
 111 }
 112 
 113 // Return number of threads contending for this monitor.
 114 inline jint ObjectMonitor::contentions() const {
 115   return Atomic::load(&_contentions);
 116 }
 117 
 118 // Add value to the contentions field.
 119 inline void ObjectMonitor::add_to_contentions(jint value) {
 120   Atomic::add(&_contentions, value);
 121 }
 122 
 123 // Clear _owner field; current value must match old_value.
 124 inline void ObjectMonitor::release_clear_owner(void* old_value) {
 125 #ifdef ASSERT
 126   void* prev = Atomic::load(&_owner);
 127 #endif
 128   assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
 129          ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
 130   Atomic::release_store(&_owner, (void*)NULL);
 131   log_trace(monitorinflation, owner)("release_clear_owner(): mid="
 132                                      INTPTR_FORMAT ", old_value=" INTPTR_FORMAT,
 133                                      p2i(this), p2i(old_value));
 134 }
 135 
 136 // Simply set _owner field to new_value; current value must match old_value.
 137 // (Simple means no memory sync needed.)
 138 inline void ObjectMonitor::set_owner_from(void* old_value, void* new_value) {
 139 #ifdef ASSERT
 140   void* prev = Atomic::load(&_owner);
 141 #endif
 142   assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
 143          ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
 144   Atomic::store(&_owner, new_value);
 145   log_trace(monitorinflation, owner)("set_owner_from(): mid="
 146                                      INTPTR_FORMAT ", old_value=" INTPTR_FORMAT
 147                                      ", new_value=" INTPTR_FORMAT, p2i(this),
 148                                      p2i(old_value), p2i(new_value));
 149 }
 150 
 151 // Simply set _owner field to new_value; current value must match old_value1 or old_value2.
 152 // (Simple means no memory sync needed.)
 153 inline void ObjectMonitor::set_owner_from(void* old_value1, void* old_value2, void* new_value) {
 154   void* prev = Atomic::load(&_owner);
 155   assert(prev == old_value1 || prev == old_value2,
 156          "unexpected prev owner=" INTPTR_FORMAT ", expected1="
 157          INTPTR_FORMAT " or expected2=" INTPTR_FORMAT, p2i(prev),
 158          p2i(old_value1), p2i(old_value2));
 159   _owner = new_value;
 160   log_trace(monitorinflation, owner)("set_owner_from(old1=" INTPTR_FORMAT
 161                                      ", old2=" INTPTR_FORMAT "): mid="
 162                                      INTPTR_FORMAT ", prev=" INTPTR_FORMAT
 163                                      ", new=" INTPTR_FORMAT, p2i(old_value1),
 164                                      p2i(old_value2), p2i(this), p2i(prev),
 165                                      p2i(new_value));
 166 }
 167 
 168 // Simply set _owner field to self; current value must match basic_lock_p.
 169 inline void ObjectMonitor::set_owner_from_BasicLock(void* basic_lock_p, Thread* self) {
 170 #ifdef ASSERT
 171   void* prev = Atomic::load(&_owner);
 172 #endif
 173   assert(prev == basic_lock_p, "unexpected prev owner=" INTPTR_FORMAT
 174          ", expected=" INTPTR_FORMAT, p2i(prev), p2i(basic_lock_p));
 175   // Non-null owner field to non-null owner field is safe without
 176   // cmpxchg() as long as all readers can tolerate either flavor.
 177   Atomic::store(&_owner, self);
 178   log_trace(monitorinflation, owner)("set_owner_from_BasicLock(): mid="
 179                                      INTPTR_FORMAT ", basic_lock_p="
 180                                      INTPTR_FORMAT ", new_value=" INTPTR_FORMAT,
 181                                      p2i(this), p2i(basic_lock_p), p2i(self));
 182 }
 183 
 184 // Try to set _owner field to new_value if the current value matches
 185 // old_value. Otherwise, does not change the _owner field. Returns
 186 // the prior value of the _owner field.
 187 inline void* ObjectMonitor::try_set_owner_from(void* old_value, void* new_value) {
 188   void* prev = Atomic::cmpxchg(&_owner, old_value, new_value);
 189   if (prev == old_value) {
 190     log_trace(monitorinflation, owner)("try_set_owner_from(): mid="
 191                                        INTPTR_FORMAT ", prev=" INTPTR_FORMAT
 192                                        ", new=" INTPTR_FORMAT, p2i(this),
 193                                        p2i(prev), p2i(new_value));
 194   }
 195   return prev;
 196 }
 197 
 198 inline void ObjectMonitor::set_allocation_state(ObjectMonitor::AllocationState s) {
 199   _allocation_state = s;
 200 }
 201 
 202 inline ObjectMonitor::AllocationState ObjectMonitor::allocation_state() const {
 203   return _allocation_state;
 204 }
 205 
 206 inline bool ObjectMonitor::is_free() const {
 207   return _allocation_state == Free;
 208 }
 209 
 210 inline bool ObjectMonitor::is_old() const {
 211   return _allocation_state == Old;
 212 }
 213 
 214 inline bool ObjectMonitor::is_new() const {
 215   return _allocation_state == New;
 216 }
 217 
 218 // The _next_om field can be concurrently read and modified so we
 219 // use Atomic operations to disable compiler optimizations that
 220 // might try to elide loading and/or storing this field.
 221 
 222 inline ObjectMonitor* ObjectMonitor::next_om() const {
 223   return Atomic::load(&_next_om);
 224 }
 225 
 226 // Simply set _next_om field to new_value.
 227 inline void ObjectMonitor::set_next_om(ObjectMonitor* new_value) {
 228   Atomic::store(&_next_om, new_value);
 229 }
 230 
 231 // Try to set _next_om field to new_value if the current value matches
 232 // old_value. Otherwise, does not change the _next_om field. Returns
 233 // the prior value of the _next_om field.
 234 inline ObjectMonitor* ObjectMonitor::try_set_next_om(ObjectMonitor* old_value, ObjectMonitor* new_value) {
 235   return Atomic::cmpxchg(&_next_om, old_value, new_value);
 236 }
 237 
 238 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP