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/lockStack.inline.hpp"
34 #include "runtime/synchronizer.hpp"
35
36 inline bool ObjectMonitor::is_entered(JavaThread* current) const {
37 if (LockingMode == LM_LIGHTWEIGHT) {
38 if (is_owner_anonymous()) {
39 return current->lock_stack().contains(object());
40 } else {
41 return current == owner_raw();
42 }
43 } else {
44 void* owner = owner_raw();
45 if (current == owner || current->is_lock_owned((address)owner)) {
46 return true;
47 }
48 }
49 return false;
50 }
51
52 inline markWord ObjectMonitor::header() const {
53 return Atomic::load(&_header);
54 }
55
56 inline volatile markWord* ObjectMonitor::header_addr() {
57 return &_header;
58 }
59
60 inline void ObjectMonitor::set_header(markWord hdr) {
61 Atomic::store(&_header, hdr);
62 }
63
64 inline int ObjectMonitor::waiters() const {
65 return _waiters;
66 }
67
68 inline bool ObjectMonitor::has_owner() const {
69 void* owner = owner_raw();
70 return owner != nullptr && owner != DEFLATER_MARKER;
71 }
72
73 // Returns null if DEFLATER_MARKER is observed.
74 inline void* ObjectMonitor::owner() const {
75 void* owner = owner_raw();
76 return owner != DEFLATER_MARKER ? owner : nullptr;
77 }
78
79 inline void* ObjectMonitor::owner_raw() const {
80 return Atomic::load(&_owner);
81 }
82
83 // Returns true if owner field == DEFLATER_MARKER and false otherwise.
84 // This accessor is called when we really need to know if the owner
85 // field == DEFLATER_MARKER and any non-null value won't do the trick.
86 inline bool ObjectMonitor::owner_is_DEFLATER_MARKER() const {
87 return owner_raw() == DEFLATER_MARKER;
88 }
89
90 // Returns true if 'this' is being async deflated and false otherwise.
91 inline bool ObjectMonitor::is_being_async_deflated() {
92 return contentions() < 0;
93 }
94
95 // Return number of threads contending for this monitor.
96 inline int ObjectMonitor::contentions() const {
97 return Atomic::load(&_contentions);
98 }
99
100 // Add value to the contentions field.
101 inline void ObjectMonitor::add_to_contentions(int value) {
102 Atomic::add(&_contentions, value);
103 }
104
105 inline void ObjectMonitor::set_recursions(size_t recursions) {
106 assert(_recursions == 0, "must be");
107 assert(has_owner(), "must be owned");
108 _recursions = checked_cast<intx>(recursions);
109 }
110
111 // Clear _owner field; current value must match old_value.
112 inline void ObjectMonitor::release_clear_owner(void* old_value) {
113 #ifdef ASSERT
114 void* prev = Atomic::load(&_owner);
115 assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
116 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
117 #endif
118 Atomic::release_store(&_owner, (void*)nullptr);
119 log_trace(monitorinflation, owner)("release_clear_owner(): mid="
120 INTPTR_FORMAT ", old_value=" INTPTR_FORMAT,
121 p2i(this), p2i(old_value));
122 }
123
124 // Simply set _owner field to new_value; current value must match old_value.
125 // (Simple means no memory sync needed.)
126 inline void ObjectMonitor::set_owner_from(void* old_value, void* new_value) {
127 #ifdef ASSERT
128 void* prev = Atomic::load(&_owner);
129 assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
130 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
131 #endif
132 Atomic::store(&_owner, new_value);
133 log_trace(monitorinflation, owner)("set_owner_from(): mid="
134 INTPTR_FORMAT ", old_value=" INTPTR_FORMAT
135 ", new_value=" INTPTR_FORMAT, p2i(this),
136 p2i(old_value), p2i(new_value));
137 }
138
139 // Simply set _owner field to self; current value must match basic_lock_p.
140 inline void ObjectMonitor::set_owner_from_BasicLock(void* basic_lock_p, JavaThread* current) {
141 #ifdef ASSERT
142 void* prev = Atomic::load(&_owner);
143 assert(prev == basic_lock_p, "unexpected prev owner=" INTPTR_FORMAT
144 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(basic_lock_p));
145 #endif
146 // Non-null owner field to non-null owner field is safe without
147 // cmpxchg() as long as all readers can tolerate either flavor.
148 Atomic::store(&_owner, current);
149 log_trace(monitorinflation, owner)("set_owner_from_BasicLock(): mid="
150 INTPTR_FORMAT ", basic_lock_p="
151 INTPTR_FORMAT ", new_value=" INTPTR_FORMAT,
152 p2i(this), p2i(basic_lock_p), p2i(current));
153 }
154
155 // Try to set _owner field to new_value if the current value matches
156 // old_value. Otherwise, does not change the _owner field. Returns
157 // the prior value of the _owner field.
158 inline void* ObjectMonitor::try_set_owner_from(void* old_value, void* new_value) {
159 void* prev = Atomic::cmpxchg(&_owner, old_value, new_value);
160 if (prev == old_value) {
161 log_trace(monitorinflation, owner)("try_set_owner_from(): mid="
162 INTPTR_FORMAT ", prev=" INTPTR_FORMAT
163 ", new=" INTPTR_FORMAT, p2i(this),
164 p2i(prev), p2i(new_value));
165 }
166 return prev;
167 }
168
169 // The _next_om field can be concurrently read and modified so we
170 // use Atomic operations to disable compiler optimizations that
171 // might try to elide loading and/or storing this field.
172
173 // Simply get _next_om field.
174 inline ObjectMonitor* ObjectMonitor::next_om() const {
175 return Atomic::load(&_next_om);
176 }
177
178 // Simply set _next_om field to new_value.
179 inline void ObjectMonitor::set_next_om(ObjectMonitor* new_value) {
180 Atomic::store(&_next_om, new_value);
181 }
182
183 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP
|
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/javaThread.inline.hpp"
34 #include "runtime/lockStack.inline.hpp"
35 #include "runtime/synchronizer.hpp"
36 #include "runtime/threadIdentifier.hpp"
37
38 inline void* ObjectMonitor::owner_for(JavaThread* thread) const {
39 int64_t tid = thread->lock_id();
40 assert(tid >= 3 && tid < ThreadIdentifier::current(), "must be reasonable");
41 return (void*)tid;
42 }
43
44 inline bool ObjectMonitor::is_entered(JavaThread* current) const {
45 if (is_owner_anonymous()) {
46 if (LockingMode == LM_LIGHTWEIGHT) {
47 return current->lock_stack().contains(object());
48 } else {
49 return current->is_lock_owned((address)stack_locker());
50 }
51 } else {
52 return is_owner(current);
53 }
54 return false;
55 }
56
57 inline markWord ObjectMonitor::header() const {
58 return Atomic::load(&_header);
59 }
60
61 inline volatile markWord* ObjectMonitor::header_addr() {
62 return &_header;
63 }
64
65 inline void ObjectMonitor::set_header(markWord hdr) {
66 Atomic::store(&_header, hdr);
67 }
68
69 inline int ObjectMonitor::waiters() const {
70 return _waiters;
71 }
72
73 inline bool ObjectMonitor::has_owner() const {
74 void* owner = owner_raw();
75 return owner != nullptr && owner != DEFLATER_MARKER;
76 }
77
78 // Returns null if DEFLATER_MARKER is observed.
79 inline void* ObjectMonitor::owner() const {
80 void* owner = owner_raw();
81 return owner != DEFLATER_MARKER ? owner : nullptr;
82 }
83
84 inline void* ObjectMonitor::owner_raw() const {
85 return Atomic::load(&_owner);
86 }
87
88 inline BasicLock* ObjectMonitor::stack_locker() const {
89 return Atomic::load(&_stack_locker);
90 }
91
92 inline void ObjectMonitor::set_stack_locker(BasicLock* locker) {
93 Atomic::store(&_stack_locker, locker);
94 }
95
96 inline bool ObjectMonitor::is_stack_locker(JavaThread* current) {
97 return is_owner_anonymous() && current->is_lock_owned((address)stack_locker());
98 }
99
100 // Returns true if owner field == DEFLATER_MARKER and false otherwise.
101 // This accessor is called when we really need to know if the owner
102 // field == DEFLATER_MARKER and any non-null value won't do the trick.
103 inline bool ObjectMonitor::owner_is_DEFLATER_MARKER() const {
104 return owner_raw() == DEFLATER_MARKER;
105 }
106
107 // Returns true if 'this' is being async deflated and false otherwise.
108 inline bool ObjectMonitor::is_being_async_deflated() {
109 return contentions() < 0;
110 }
111
112 // Return number of threads contending for this monitor.
113 inline int ObjectMonitor::contentions() const {
114 return Atomic::load(&_contentions);
115 }
116
117 // Add value to the contentions field.
118 inline void ObjectMonitor::add_to_contentions(int value) {
119 Atomic::add(&_contentions, value);
120 }
121
122 inline void ObjectMonitor::set_recursions(size_t recursions) {
123 assert(_recursions == 0, "must be");
124 assert(has_owner(), "must be owned");
125 _recursions = checked_cast<intx>(recursions);
126 }
127
128 // Clear _owner field; current value must match old_value.
129 inline void ObjectMonitor::release_clear_owner(JavaThread* old_owner) {
130 void* old_value = owner_for(old_owner);
131 #ifdef ASSERT
132 void* prev = Atomic::load(&_owner);
133 assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
134 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
135 #endif
136 Atomic::release_store(&_owner, (void*)nullptr);
137 log_trace(monitorinflation, owner)("release_clear_owner(): mid="
138 INTPTR_FORMAT ", old_value=" INTPTR_FORMAT,
139 p2i(this), p2i(old_value));
140 }
141
142 // Simply set _owner field to new_value; current value must match old_value.
143 // (Simple means no memory sync needed.)
144 inline void ObjectMonitor::set_owner_from_raw(void* old_value, void* new_value) {
145 #ifdef ASSERT
146 void* prev = Atomic::load(&_owner);
147 assert((int64_t)prev < ThreadIdentifier::current(), "must be reasonable");
148 assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
149 ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
150 #endif
151 Atomic::store(&_owner, new_value);
152 log_trace(monitorinflation, owner)("set_owner_from(): mid="
153 INTPTR_FORMAT ", old_value=" INTPTR_FORMAT
154 ", new_value=" INTPTR_FORMAT, p2i(this),
155 p2i(old_value), p2i(new_value));
156 }
157
158 inline void ObjectMonitor::set_owner_from(void* old_value, JavaThread* current) {
159 set_owner_from_raw(old_value, owner_for(current));
160 }
161
162 // Simply set _owner field to self; current value must match basic_lock_p.
163 inline void ObjectMonitor::set_owner_from_BasicLock(JavaThread* current) {
164 BasicLock* basic_lock_p = stack_locker();
165
166 set_stack_locker(nullptr); // first
167 assert(is_owner_anonymous(), "should be anon for now");
168
169 // Non-null owner field to non-null owner field is safe without
170 // cmpxchg() as long as all readers can tolerate either flavor.
171 Atomic::store(&_owner, owner_for(current));
172 log_trace(monitorinflation, owner)("set_owner_from_BasicLock(): mid="
173 INTPTR_FORMAT ", basic_lock_p="
174 INTPTR_FORMAT ", new_value=" INTPTR_FORMAT,
175 p2i(this), p2i(basic_lock_p), p2i(current));
176 }
177
178 // Try to set _owner field to new_value if the current value matches
179 // old_value. Otherwise, does not change the _owner field. Returns
180 // the prior value of the _owner field.
181 inline void* ObjectMonitor::try_set_owner_from_raw(void* old_value, void* new_value) {
182 assert((int64_t)new_value < ThreadIdentifier::current(), "must be reasonable");
183 void* prev = Atomic::cmpxchg(&_owner, old_value, new_value);
184 if (prev == old_value) {
185 log_trace(monitorinflation, owner)("try_set_owner_from(): mid="
186 INTPTR_FORMAT ", prev=" INTPTR_FORMAT
187 ", new=" INTPTR_FORMAT, p2i(this),
188 p2i(prev), p2i(new_value));
189 }
190 return prev;
191 }
192
193 inline void* ObjectMonitor::try_set_owner_from(void* old_value, JavaThread* current) {
194 return try_set_owner_from_raw(old_value, owner_for(current));
195 }
196
197 // The _next_om field can be concurrently read and modified so we
198 // use Atomic operations to disable compiler optimizations that
199 // might try to elide loading and/or storing this field.
200
201 // Simply get _next_om field.
202 inline ObjectMonitor* ObjectMonitor::next_om() const {
203 return Atomic::load(&_next_om);
204 }
205
206 // Simply set _next_om field to new_value.
207 inline void ObjectMonitor::set_next_om(ObjectMonitor* new_value) {
208 Atomic::store(&_next_om, new_value);
209 }
210
211 #endif // SHARE_RUNTIME_OBJECTMONITOR_INLINE_HPP
|