1 /*
  2  * Copyright (c) 1998, 2021, 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/objectMonitor.hpp"
 29 
 30 #include "logging/log.hpp"
 31 #include "oops/access.inline.hpp"
 32 #include "runtime/atomic.hpp"
 33 #include "runtime/synchronizer.hpp"
 34 
 35 inline intptr_t ObjectMonitor::is_entered(JavaThread* current) const {
 36   void* owner = owner_raw();
 37   if (current == owner || current->is_lock_owned((address)owner)) {
 38     return 1;
 39   }
 40   return 0;
 41 }
 42 
 43 inline markWord ObjectMonitor::header() const {
 44   return Atomic::load(&_header);
 45 }
 46 
 47 inline volatile markWord* ObjectMonitor::header_addr() {
 48   return &_header;
 49 }
 50 
 51 inline void ObjectMonitor::set_header(markWord hdr) {
 52   Atomic::store(&_header, hdr);
 53 }
 54 
 55 inline int ObjectMonitor::waiters() const {
 56   return _waiters;
 57 }
 58 
 59 // Returns NULL if DEFLATER_MARKER is observed.
 60 inline void* ObjectMonitor::owner() const {
 61   void* owner = owner_raw();
 62   return owner != DEFLATER_MARKER ? owner : NULL;
 63 }
 64 
 65 inline void* ObjectMonitor::owner_raw() const {
 66   return Atomic::load(&_owner);
 67 }
 68 
 69 // Returns true if owner field == DEFLATER_MARKER and false otherwise.
 70 // This accessor is called when we really need to know if the owner
 71 // field == DEFLATER_MARKER and any non-NULL value won't do the trick.
 72 inline bool ObjectMonitor::owner_is_DEFLATER_MARKER() const {
 73   return owner_raw() == DEFLATER_MARKER;
 74 }
 75 
 76 // Returns true if 'this' is being async deflated and false otherwise.
 77 inline bool ObjectMonitor::is_being_async_deflated() {
 78   return contentions() < 0;
 79 }
 80 
 81 // Return number of threads contending for this monitor.
 82 inline int ObjectMonitor::contentions() const {
 83   return Atomic::load(&_contentions);
 84 }
 85 
 86 // Add value to the contentions field.
 87 inline void ObjectMonitor::add_to_contentions(int value) {
 88   Atomic::add(&_contentions, value);
 89 }
 90 
 91 // Clear _owner field; current value must match old_value.
 92 inline void ObjectMonitor::release_clear_owner(void* old_value) {
 93 #ifdef ASSERT
 94   void* prev = Atomic::load(&_owner);
 95   assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
 96          ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
 97 #endif
 98   Atomic::release_store(&_owner, (void*)NULL);
 99   log_trace(monitorinflation, owner)("release_clear_owner(): mid="
100                                      INTPTR_FORMAT ", old_value=" INTPTR_FORMAT,
101                                      p2i(this), p2i(old_value));
102 }
103 
104 // Simply set _owner field to new_value; current value must match old_value.
105 // (Simple means no memory sync needed.)
106 inline void ObjectMonitor::set_owner_from(void* old_value, void* new_value) {
107 #ifdef ASSERT
108   void* prev = Atomic::load(&_owner);
109   assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
110          ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
111 #endif
112   Atomic::store(&_owner, new_value);
113   log_trace(monitorinflation, owner)("set_owner_from(): mid="
114                                      INTPTR_FORMAT ", old_value=" INTPTR_FORMAT
115                                      ", new_value=" INTPTR_FORMAT, p2i(this),
116                                      p2i(old_value), p2i(new_value));
117 }
118 
119 // Try to set _owner field to new_value if the current value matches
120 // old_value. Otherwise, does not change the _owner field. Returns
121 // the prior value of the _owner field.
122 inline void* ObjectMonitor::try_set_owner_from(void* old_value, void* new_value) {
123   void* prev = Atomic::cmpxchg(&_owner, old_value, new_value);
124   if (prev == old_value) {
125     log_trace(monitorinflation, owner)("try_set_owner_from(): mid="
126                                        INTPTR_FORMAT ", prev=" INTPTR_FORMAT
127                                        ", new=" INTPTR_FORMAT, p2i(this),
128                                        p2i(prev), p2i(new_value));
129   }
130   return prev;
131 }
132 
133 // The _next_om field can be concurrently read and modified so we
134 // use Atomic operations to disable compiler optimizations that
135 // might try to elide loading and/or storing this field.
136 
137 // Simply get _next_om field.
138 inline ObjectMonitor* ObjectMonitor::next_om() const {
139   return Atomic::load(&_next_om);
140 }
141 
142 // Get _next_om field with acquire semantics.
143 inline ObjectMonitor* ObjectMonitor::next_om_acquire() const {
144   return Atomic::load_acquire(&_next_om);
145 }
146 
147 // Simply set _next_om field to new_value.
148 inline void ObjectMonitor::set_next_om(ObjectMonitor* new_value) {
149   Atomic::store(&_next_om, new_value);
150 }
151 
152 // Set _next_om field to new_value with release semantics.
153 inline void ObjectMonitor::release_set_next_om(ObjectMonitor* new_value) {
154   Atomic::release_store(&_next_om, new_value);
155 }
156 
157 // Try to set _next_om field to new_value if the current value matches
158 // old_value. Otherwise, does not change the _next_om field. Returns
159 // the prior value of the _next_om field.
160 inline ObjectMonitor* ObjectMonitor::try_set_next_om(ObjectMonitor* old_value, ObjectMonitor* new_value) {
161   return Atomic::cmpxchg(&_next_om, old_value, new_value);
162 }
163 
164 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP