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_HPP
26 #define SHARE_RUNTIME_OBJECTMONITOR_HPP
27
28 #include "memory/allocation.hpp"
29 #include "memory/padded.hpp"
30 #include "oops/markWord.hpp"
31 #include "oops/weakHandle.hpp"
32 #include "runtime/perfDataTypes.hpp"
33 #include "utilities/checkedCast.hpp"
34
35 class ObjectMonitor;
36 class ObjectMonitorContentionMark;
37 class ParkEvent;
38
39 // ObjectWaiter serves as a "proxy" or surrogate thread.
40 // TODO-FIXME: Eliminate ObjectWaiter and use the thread-specific
41 // ParkEvent instead. Beware, however, that the JVMTI code
42 // knows about ObjectWaiters, so we'll have to reconcile that code.
43 // See next_waiter(), first_waiter(), etc.
44
45 class ObjectWaiter : public StackObj {
46 public:
47 enum TStates { TS_UNDEF, TS_READY, TS_RUN, TS_WAIT, TS_ENTER, TS_CXQ };
48 ObjectWaiter* volatile _next;
49 ObjectWaiter* volatile _prev;
50 JavaThread* _thread;
51 uint64_t _notifier_tid;
52 ParkEvent * _event;
53 volatile int _notified;
54 volatile TStates TState;
55 bool _active; // Contention monitoring is enabled
56 public:
57 ObjectWaiter(JavaThread* current);
58
59 void wait_reenter_begin(ObjectMonitor *mon);
60 void wait_reenter_end(ObjectMonitor *mon);
61 };
62
63 // The ObjectMonitor class implements the heavyweight version of a
64 // JavaMonitor. The lightweight BasicLock/stack lock version has been
65 // inflated into an ObjectMonitor. This inflation is typically due to
66 // contention or use of Object.wait().
67 //
68 // WARNING: This is a very sensitive and fragile class. DO NOT make any
69 // changes unless you are fully aware of the underlying semantics.
70 //
71 // ObjectMonitor Layout Overview/Highlights/Restrictions:
72 //
73 // - The _metadata field must be at offset 0 because the displaced header
74 // from markWord is stored there. We do not want markWord.hpp to include
75 // ObjectMonitor.hpp to avoid exposing ObjectMonitor everywhere. This
76 // means that ObjectMonitor cannot inherit from any other class nor can
77 // it use any virtual member functions. This restriction is critical to
78 // the proper functioning of the VM.
111 // - Separating _owner from the <remaining_fields> by enough space to
112 // avoid false sharing might be profitable. Given that the CAS in
113 // monitorenter will invalidate the line underlying _owner. We want
114 // to avoid an L1 data cache miss on that same line for monitorexit.
115 // Putting these <remaining_fields>:
116 // _recursions, _EntryList, _cxq, and _succ, all of which may be
117 // fetched in the inflated unlock path, on a different cache line
118 // would make them immune to CAS-based invalidation from the _owner
119 // field.
120 //
121 // - The _recursions field should be of type int, or int32_t but not
122 // intptr_t. There's no reason to use a 64-bit type for this field
123 // in a 64-bit JVM.
124
125 #define OM_CACHE_LINE_SIZE DEFAULT_CACHE_LINE_SIZE
126
127 class ObjectMonitor : public CHeapObj<mtObjectMonitor> {
128 friend class ObjectSynchronizer;
129 friend class ObjectWaiter;
130 friend class VMStructs;
131 JVMCI_ONLY(friend class JVMCIVMStructs;)
132
133 static OopStorage* _oop_storage;
134
135 // The sync code expects the metadata field to be at offset zero (0).
136 // Enforced by the assert() in metadata_addr().
137 // * LM_LIGHTWEIGHT with UseObjectMonitorTable:
138 // Contains the _object's hashCode.
139 // * LM_LEGACY, LM_MONITOR, LM_LIGHTWEIGHT without UseObjectMonitorTable:
140 // Contains the displaced object header word - mark
141 volatile uintptr_t _metadata; // metadata
142 WeakHandle _object; // backward object pointer
143 // Separate _metadata and _owner on different cache lines since both can
144 // have busy multi-threaded access. _metadata and _object are set at initial
145 // inflation. The _object does not change, so it is a good choice to share
146 // its cache line with _metadata.
147 DEFINE_PAD_MINUS_SIZE(0, OM_CACHE_LINE_SIZE, sizeof(_metadata) +
148 sizeof(WeakHandle));
149 // Used by async deflation as a marker in the _owner field.
150 // Note that the choice of the two markers is peculiar:
151 // - They need to represent values that cannot be pointers. In particular,
152 // we achieve this by using the lowest two bits.
153 // - ANONYMOUS_OWNER should be a small value, it is used in generated code
154 // and small values encode much better.
155 // - We test for anonymous owner by testing for the lowest bit, therefore
156 // DEFLATER_MARKER must *not* have that bit set.
157 static const uintptr_t DEFLATER_MARKER_VALUE = 2;
158 #define DEFLATER_MARKER reinterpret_cast<void*>(DEFLATER_MARKER_VALUE)
159 public:
160 // NOTE: Typed as uintptr_t so that we can pick it up in SA, via vmStructs.
161 static const uintptr_t ANONYMOUS_OWNER = 1;
162
163 private:
164 static void* anon_owner_ptr() { return reinterpret_cast<void*>(ANONYMOUS_OWNER); }
165
166 void* volatile _owner; // pointer to owning thread OR BasicLock
167 volatile uint64_t _previous_owner_tid; // thread id of the previous owner of the monitor
168 // Separate _owner and _next_om on different cache lines since
169 // both can have busy multi-threaded access. _previous_owner_tid is only
170 // changed by ObjectMonitor::exit() so it is a good choice to share the
171 // cache line with _owner.
172 DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(void* volatile) +
173 sizeof(volatile uint64_t));
174 ObjectMonitor* _next_om; // Next ObjectMonitor* linkage
175 volatile intx _recursions; // recursion count, 0 for first entry
176 ObjectWaiter* volatile _EntryList; // Threads blocked on entry or reentry.
177 // The list is actually composed of WaitNodes,
178 // acting as proxies for Threads.
179
180 ObjectWaiter* volatile _cxq; // LL of recently-arrived threads blocked on entry.
181 JavaThread* volatile _succ; // Heir presumptive thread - used for futile wakeup throttling
182
183 volatile int _SpinDuration;
184
185 int _contentions; // Number of active contentions in enter(). It is used by is_busy()
186 // along with other fields to determine if an ObjectMonitor can be
187 // deflated. It is also used by the async deflation protocol. See
188 // ObjectMonitor::deflate_monitor().
189
190 ObjectWaiter* volatile _WaitSet; // LL of threads wait()ing on the monitor
191 volatile int _waiters; // number of waiting threads
192 volatile int _WaitSetLock; // protects Wait Queue - simple spinlock
193
194 public:
195
196 static void Initialize();
197
198 // Only perform a PerfData operation if the PerfData object has been
199 // allocated and if the PerfDataManager has not freed the PerfData
200 // objects which can happen at normal VM shutdown.
201 //
202 #define OM_PERFDATA_OP(f, op_str) \
203 do { \
204 if (ObjectMonitor::_sync_ ## f != nullptr && \
205 PerfDataManager::has_PerfData()) { \
206 ObjectMonitor::_sync_ ## f->op_str; \
207 } \
208 } while (0)
209
210 static PerfCounter * _sync_ContendedLockAttempts;
211 static PerfCounter * _sync_FutileWakeups;
212 static PerfCounter * _sync_Parks;
213 static PerfCounter * _sync_Notifications;
214 static PerfCounter * _sync_Inflations;
215 static PerfCounter * _sync_Deflations;
216 static PerfLongVariable * _sync_MonExtant;
250
251 bool is_busy() const {
252 // TODO-FIXME: assert _owner == null implies _recursions = 0
253 intptr_t ret_code = intptr_t(_waiters) | intptr_t(_cxq) | intptr_t(_EntryList);
254 int cnts = contentions(); // read once
255 if (cnts > 0) {
256 ret_code |= intptr_t(cnts);
257 }
258 if (!owner_is_DEFLATER_MARKER()) {
259 ret_code |= intptr_t(owner_raw());
260 }
261 return ret_code != 0;
262 }
263 const char* is_busy_to_string(stringStream* ss);
264
265 bool is_entered(JavaThread* current) const;
266
267 // Returns true if this OM has an owner, false otherwise.
268 bool has_owner() const;
269 void* owner() const; // Returns null if DEFLATER_MARKER is observed.
270 void* owner_raw() const;
271 // Returns true if owner field == DEFLATER_MARKER and false otherwise.
272 bool owner_is_DEFLATER_MARKER() const;
273 // Returns true if 'this' is being async deflated and false otherwise.
274 bool is_being_async_deflated();
275 // Clear _owner field; current value must match old_value.
276 void release_clear_owner(void* old_value);
277 // Simply set _owner field to new_value; current value must match old_value.
278 void set_owner_from(void* old_value, void* new_value);
279 // Simply set _owner field to current; current value must match basic_lock_p.
280 void set_owner_from_BasicLock(void* basic_lock_p, JavaThread* current);
281 // Try to set _owner field to new_value if the current value matches
282 // old_value, using Atomic::cmpxchg(). Otherwise, does not change the
283 // _owner field. Returns the prior value of the _owner field.
284 void* try_set_owner_from(void* old_value, void* new_value);
285
286 void set_owner_anonymous() {
287 set_owner_from(nullptr, anon_owner_ptr());
288 }
289
290 bool is_owner_anonymous() const {
291 return owner_raw() == anon_owner_ptr();
292 }
293
294 void set_owner_from_anonymous(Thread* owner) {
295 set_owner_from(anon_owner_ptr(), owner);
296 }
297
298 // Simply get _next_om field.
299 ObjectMonitor* next_om() const;
300 // Simply set _next_om field to new_value.
301 void set_next_om(ObjectMonitor* new_value);
302
303 int waiters() const;
304
305 int contentions() const;
306 void add_to_contentions(int value);
307 intx recursions() const { return _recursions; }
308 void set_recursions(size_t recursions);
309
310 // JVM/TI GetObjectMonitorUsage() needs this:
311 ObjectWaiter* first_waiter() { return _WaitSet; }
312 ObjectWaiter* next_waiter(ObjectWaiter* o) { return o->_next; }
313 JavaThread* thread_of_waiter(ObjectWaiter* o) { return o->_thread; }
314
315 ObjectMonitor(oop object);
316 ~ObjectMonitor();
317
318 oop object() const;
319 oop object_peek() const;
320 bool object_is_dead() const;
321 bool object_refers_to(oop obj) const;
322
323 // Returns true if the specified thread owns the ObjectMonitor. Otherwise
324 // returns false and throws IllegalMonitorStateException (IMSE).
325 bool check_owner(TRAPS);
326
327 private:
328 class ExitOnSuspend {
329 protected:
330 ObjectMonitor* _om;
334 void operator()(JavaThread* current);
335 bool exited() { return _om_exited; }
336 };
337 class ClearSuccOnSuspend {
338 protected:
339 ObjectMonitor* _om;
340 public:
341 ClearSuccOnSuspend(ObjectMonitor* om) : _om(om) {}
342 void operator()(JavaThread* current);
343 };
344
345 bool enter_is_async_deflating();
346 public:
347 void enter_for_with_contention_mark(JavaThread* locking_thread, ObjectMonitorContentionMark& contention_mark);
348 bool enter_for(JavaThread* locking_thread);
349 bool enter(JavaThread* current);
350 bool try_enter(JavaThread* current, bool check_for_recursion = true);
351 bool spin_enter(JavaThread* current);
352 void enter_with_contention_mark(JavaThread* current, ObjectMonitorContentionMark& contention_mark);
353 void exit(JavaThread* current, bool not_suspended = true);
354 void wait(jlong millis, bool interruptible, TRAPS);
355 void notify(TRAPS);
356 void notifyAll(TRAPS);
357
358 void print() const;
359 #ifdef ASSERT
360 void print_debug_style_on(outputStream* st) const;
361 #endif
362 void print_on(outputStream* st) const;
363
364 // Use the following at your own risk
365 intx complete_exit(JavaThread* current);
366
367 private:
368 void AddWaiter(ObjectWaiter* waiter);
369 void INotify(JavaThread* current);
370 ObjectWaiter* DequeueWaiter();
371 void DequeueSpecificWaiter(ObjectWaiter* waiter);
372 void EnterI(JavaThread* current);
373 void ReenterI(JavaThread* current, ObjectWaiter* current_node);
374 void UnlinkAfterAcquire(JavaThread* current, ObjectWaiter* current_node);
375
376
377 enum class TryLockResult { Interference = -1, HasOwner = 0, Success = 1 };
378
379 bool TryLockWithContentionMark(JavaThread* locking_thread, ObjectMonitorContentionMark& contention_mark);
380 TryLockResult TryLock(JavaThread* current);
381
382 bool TrySpin(JavaThread* current);
383 bool short_fixed_spin(JavaThread* current, int spin_count, bool adapt);
384 void ExitEpilog(JavaThread* current, ObjectWaiter* Wakee);
385
386 // Deflation support
387 bool deflate_monitor(Thread* current);
388 private:
389 void install_displaced_markword_in_object(const oop obj);
390 };
391
392 // RAII object to ensure that ObjectMonitor::is_being_async_deflated() is
393 // stable within the context of this mark.
394 class ObjectMonitorContentionMark : StackObj {
395 DEBUG_ONLY(friend class ObjectMonitor;)
|
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_HPP
26 #define SHARE_RUNTIME_OBJECTMONITOR_HPP
27
28 #include "memory/allocation.hpp"
29 #include "memory/padded.hpp"
30 #include "oops/markWord.hpp"
31 #include "oops/oopHandle.hpp"
32 #include "oops/weakHandle.hpp"
33 #include "runtime/javaThread.hpp"
34 #include "runtime/perfDataTypes.hpp"
35 #include "utilities/checkedCast.hpp"
36
37 class ObjectMonitor;
38 class ObjectMonitorContentionMark;
39 class ParkEvent;
40 class BasicLock;
41 class ContinuationWrapper;
42
43 // ObjectWaiter serves as a "proxy" or surrogate thread.
44 // TODO-FIXME: Eliminate ObjectWaiter and use the thread-specific
45 // ParkEvent instead. Beware, however, that the JVMTI code
46 // knows about ObjectWaiters, so we'll have to reconcile that code.
47 // See next_waiter(), first_waiter(), etc.
48
49 class ObjectWaiter : public CHeapObj<mtThread> {
50 public:
51 enum TStates : uint8_t { TS_UNDEF, TS_READY, TS_RUN, TS_WAIT, TS_ENTER, TS_CXQ };
52 ObjectWaiter* volatile _next;
53 ObjectWaiter* volatile _prev;
54 JavaThread* _thread;
55 OopHandle _vthread;
56 ObjectMonitor* _monitor;
57 uint64_t _notifier_tid;
58 int _recursions;
59 volatile TStates TState;
60 volatile bool _notified;
61 bool _is_wait;
62 bool _at_reenter;
63 bool _interrupted;
64 bool _active; // Contention monitoring is enabled
65 public:
66 ObjectWaiter(JavaThread* current);
67 ObjectWaiter(oop vthread, ObjectMonitor* mon);
68 ~ObjectWaiter();
69 JavaThread* thread() { return _thread; }
70 bool is_vthread() { return _thread == nullptr; }
71 uint8_t state() { return TState; }
72 ObjectMonitor* monitor() { return _monitor; }
73 bool is_monitorenter() { return !_is_wait; }
74 bool is_wait() { return _is_wait; }
75 bool notified() { return _notified; }
76 bool at_reenter() { return _at_reenter; }
77 oop vthread();
78 void wait_reenter_begin(ObjectMonitor *mon);
79 void wait_reenter_end(ObjectMonitor *mon);
80 };
81
82 // The ObjectMonitor class implements the heavyweight version of a
83 // JavaMonitor. The lightweight BasicLock/stack lock version has been
84 // inflated into an ObjectMonitor. This inflation is typically due to
85 // contention or use of Object.wait().
86 //
87 // WARNING: This is a very sensitive and fragile class. DO NOT make any
88 // changes unless you are fully aware of the underlying semantics.
89 //
90 // ObjectMonitor Layout Overview/Highlights/Restrictions:
91 //
92 // - The _metadata field must be at offset 0 because the displaced header
93 // from markWord is stored there. We do not want markWord.hpp to include
94 // ObjectMonitor.hpp to avoid exposing ObjectMonitor everywhere. This
95 // means that ObjectMonitor cannot inherit from any other class nor can
96 // it use any virtual member functions. This restriction is critical to
97 // the proper functioning of the VM.
130 // - Separating _owner from the <remaining_fields> by enough space to
131 // avoid false sharing might be profitable. Given that the CAS in
132 // monitorenter will invalidate the line underlying _owner. We want
133 // to avoid an L1 data cache miss on that same line for monitorexit.
134 // Putting these <remaining_fields>:
135 // _recursions, _EntryList, _cxq, and _succ, all of which may be
136 // fetched in the inflated unlock path, on a different cache line
137 // would make them immune to CAS-based invalidation from the _owner
138 // field.
139 //
140 // - The _recursions field should be of type int, or int32_t but not
141 // intptr_t. There's no reason to use a 64-bit type for this field
142 // in a 64-bit JVM.
143
144 #define OM_CACHE_LINE_SIZE DEFAULT_CACHE_LINE_SIZE
145
146 class ObjectMonitor : public CHeapObj<mtObjectMonitor> {
147 friend class ObjectSynchronizer;
148 friend class ObjectWaiter;
149 friend class VMStructs;
150 friend class MonitorList;
151 JVMCI_ONLY(friend class JVMCIVMStructs;)
152
153 static OopStorage* _oop_storage;
154
155 static OopHandle _vthread_cxq_head;
156 static ParkEvent* _vthread_unparker_ParkEvent;
157
158 // The sync code expects the metadata field to be at offset zero (0).
159 // Enforced by the assert() in metadata_addr().
160 // * LM_LIGHTWEIGHT with UseObjectMonitorTable:
161 // Contains the _object's hashCode.
162 // * LM_LEGACY, LM_MONITOR, LM_LIGHTWEIGHT without UseObjectMonitorTable:
163 // Contains the displaced object header word - mark
164 volatile uintptr_t _metadata; // metadata
165 WeakHandle _object; // backward object pointer
166 // Separate _metadata and _owner on different cache lines since both can
167 // have busy multi-threaded access. _metadata and _object are set at initial
168 // inflation. The _object does not change, so it is a good choice to share
169 // its cache line with _metadata.
170 DEFINE_PAD_MINUS_SIZE(0, OM_CACHE_LINE_SIZE, sizeof(_metadata) +
171 sizeof(WeakHandle));
172 // Used by async deflation as a marker in the _owner field.
173 // Note that the choice of the two markers is peculiar:
174 // - They need to represent values that cannot be pointers. In particular,
175 // we achieve this by using the lowest two bits.
176 // - ANONYMOUS_OWNER should be a small value, it is used in generated code
177 // and small values encode much better.
178 // - We test for anonymous owner by testing for the lowest bit, therefore
179 // DEFLATER_MARKER must *not* have that bit set.
180 static const uintptr_t DEFLATER_MARKER_VALUE = 2;
181 #define DEFLATER_MARKER reinterpret_cast<void*>(DEFLATER_MARKER_VALUE)
182 public:
183 // NOTE: Typed as uintptr_t so that we can pick it up in SA, via vmStructs.
184 static const uintptr_t ANONYMOUS_OWNER = 1;
185
186 private:
187 static void* anon_owner_ptr() { return reinterpret_cast<void*>(ANONYMOUS_OWNER); }
188
189 void* volatile _owner; // Either tid of owner, ANONYMOUS_OWNER or DEFLATER_MARKER_VALUE.
190 volatile uint64_t _previous_owner_tid; // thread id of the previous owner of the monitor
191 // Separate _owner and _next_om on different cache lines since
192 // both can have busy multi-threaded access. _previous_owner_tid is only
193 // changed by ObjectMonitor::exit() so it is a good choice to share the
194 // cache line with _owner.
195 DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(void* volatile) +
196 sizeof(volatile uint64_t));
197 ObjectMonitor* _next_om; // Next ObjectMonitor* linkage
198 volatile intx _recursions; // recursion count, 0 for first entry
199 ObjectWaiter* volatile _EntryList; // Threads blocked on entry or reentry.
200 // The list is actually composed of WaitNodes,
201 // acting as proxies for Threads.
202
203 ObjectWaiter* volatile _cxq; // LL of recently-arrived threads blocked on entry.
204 JavaThread* volatile _succ; // Heir presumptive thread - used for futile wakeup throttling
205
206 volatile int _SpinDuration;
207
208 int _contentions; // Number of active contentions in enter(). It is used by is_busy()
209 // along with other fields to determine if an ObjectMonitor can be
210 // deflated. It is also used by the async deflation protocol. See
211 // ObjectMonitor::deflate_monitor().
212
213 ObjectWaiter* volatile _WaitSet; // LL of threads wait()ing on the monitor
214 volatile int _waiters; // number of waiting threads
215 volatile int _WaitSetLock; // protects Wait Queue - simple spinlock
216
217 // used in LM_LEGACY mode to store BasicLock* in case of inflation by contending thread
218 BasicLock* volatile _stack_locker;
219
220 public:
221
222 static void Initialize();
223 static void Initialize2();
224
225 static OopHandle& vthread_cxq_head() { return _vthread_cxq_head; }
226 static ParkEvent* vthread_unparker_ParkEvent() { return _vthread_unparker_ParkEvent; }
227
228 // Only perform a PerfData operation if the PerfData object has been
229 // allocated and if the PerfDataManager has not freed the PerfData
230 // objects which can happen at normal VM shutdown.
231 //
232 #define OM_PERFDATA_OP(f, op_str) \
233 do { \
234 if (ObjectMonitor::_sync_ ## f != nullptr && \
235 PerfDataManager::has_PerfData()) { \
236 ObjectMonitor::_sync_ ## f->op_str; \
237 } \
238 } while (0)
239
240 static PerfCounter * _sync_ContendedLockAttempts;
241 static PerfCounter * _sync_FutileWakeups;
242 static PerfCounter * _sync_Parks;
243 static PerfCounter * _sync_Notifications;
244 static PerfCounter * _sync_Inflations;
245 static PerfCounter * _sync_Deflations;
246 static PerfLongVariable * _sync_MonExtant;
280
281 bool is_busy() const {
282 // TODO-FIXME: assert _owner == null implies _recursions = 0
283 intptr_t ret_code = intptr_t(_waiters) | intptr_t(_cxq) | intptr_t(_EntryList);
284 int cnts = contentions(); // read once
285 if (cnts > 0) {
286 ret_code |= intptr_t(cnts);
287 }
288 if (!owner_is_DEFLATER_MARKER()) {
289 ret_code |= intptr_t(owner_raw());
290 }
291 return ret_code != 0;
292 }
293 const char* is_busy_to_string(stringStream* ss);
294
295 bool is_entered(JavaThread* current) const;
296
297 // Returns true if this OM has an owner, false otherwise.
298 bool has_owner() const;
299 void* owner() const; // Returns null if DEFLATER_MARKER is observed.
300 bool is_owner(JavaThread* thread) const { return owner() == owner_for(thread); }
301 bool is_owner_anonymous() const { return owner_raw() == anon_owner_ptr(); }
302 bool is_stack_locker(JavaThread* current);
303 BasicLock* stack_locker() const;
304
305 static void* owner_for(JavaThread* thread);
306
307 void* owner_raw() const;
308 // Returns true if owner field == DEFLATER_MARKER and false otherwise.
309 bool owner_is_DEFLATER_MARKER() const;
310 // Returns true if 'this' is being async deflated and false otherwise.
311 bool is_being_async_deflated();
312 // Clear _owner field; current value must match old_value.
313 void release_clear_owner(JavaThread* old_value);
314 // Simply set _owner field to new_value; current value must match old_value.
315 void set_owner_from_raw(void* old_value, void* new_value);
316 void set_owner_from(void* old_value, JavaThread* current);
317 // Simply set _owner field to current; current value must match basic_lock_p.
318 void set_owner_from_BasicLock(JavaThread* current);
319 // Try to set _owner field to new_value if the current value matches
320 // old_value, using Atomic::cmpxchg(). Otherwise, does not change the
321 // _owner field. Returns the prior value of the _owner field.
322 void* try_set_owner_from_raw(void* old_value, void* new_value);
323 void* try_set_owner_from(void* old_value, JavaThread* current);
324
325 void set_owner_anonymous() {
326 set_owner_from_raw(nullptr, anon_owner_ptr());
327 }
328
329 void set_owner_from_anonymous(JavaThread* owner) {
330 set_owner_from(anon_owner_ptr(), owner);
331 }
332
333 void set_stack_locker(BasicLock* locker);
334
335 // Simply get _next_om field.
336 ObjectMonitor* next_om() const;
337 // Simply set _next_om field to new_value.
338 void set_next_om(ObjectMonitor* new_value);
339
340 int contentions() const;
341 void add_to_contentions(int value);
342 intx recursions() const { return _recursions; }
343 void set_recursions(size_t recursions);
344
345 // JVM/TI GetObjectMonitorUsage() needs this:
346 int waiters() const;
347 ObjectWaiter* first_waiter() { return _WaitSet; }
348 ObjectWaiter* next_waiter(ObjectWaiter* o) { return o->_next; }
349 JavaThread* thread_of_waiter(ObjectWaiter* o) { return o->_thread; }
350
351 ObjectMonitor(oop object);
352 ~ObjectMonitor();
353
354 oop object() const;
355 oop object_peek() const;
356 bool object_is_dead() const;
357 bool object_refers_to(oop obj) const;
358
359 // Returns true if the specified thread owns the ObjectMonitor. Otherwise
360 // returns false and throws IllegalMonitorStateException (IMSE).
361 bool check_owner(TRAPS);
362
363 private:
364 class ExitOnSuspend {
365 protected:
366 ObjectMonitor* _om;
370 void operator()(JavaThread* current);
371 bool exited() { return _om_exited; }
372 };
373 class ClearSuccOnSuspend {
374 protected:
375 ObjectMonitor* _om;
376 public:
377 ClearSuccOnSuspend(ObjectMonitor* om) : _om(om) {}
378 void operator()(JavaThread* current);
379 };
380
381 bool enter_is_async_deflating();
382 public:
383 void enter_for_with_contention_mark(JavaThread* locking_thread, ObjectMonitorContentionMark& contention_mark);
384 bool enter_for(JavaThread* locking_thread);
385 bool enter(JavaThread* current);
386 bool try_enter(JavaThread* current, bool check_for_recursion = true);
387 bool spin_enter(JavaThread* current);
388 void enter_with_contention_mark(JavaThread* current, ObjectMonitorContentionMark& contention_mark);
389 void exit(JavaThread* current, bool not_suspended = true);
390 bool resume_operation(JavaThread* current, ObjectWaiter* node, ContinuationWrapper& cont);
391 void wait(jlong millis, bool interruptible, TRAPS);
392 void notify(TRAPS);
393 void notifyAll(TRAPS);
394
395 void print() const;
396 #ifdef ASSERT
397 void print_debug_style_on(outputStream* st) const;
398 #endif
399 void print_on(outputStream* st) const;
400
401 // Use the following at your own risk
402 intx complete_exit(JavaThread* current);
403
404 private:
405 void AddWaiter(ObjectWaiter* waiter);
406 void INotify(JavaThread* current);
407 ObjectWaiter* DequeueWaiter();
408 void DequeueSpecificWaiter(ObjectWaiter* waiter);
409 void EnterI(JavaThread* current);
410 void ReenterI(JavaThread* current, ObjectWaiter* current_node);
411 void UnlinkAfterAcquire(JavaThread* current, ObjectWaiter* current_node);
412
413 bool VThreadMonitorEnter(JavaThread* current, ObjectWaiter* node = nullptr);
414 void VThreadWait(JavaThread* current, jlong millis);
415 bool VThreadWaitReenter(JavaThread* current, ObjectWaiter* node, ContinuationWrapper& cont);
416 void VThreadEpilog(JavaThread* current, ObjectWaiter* node);
417
418 enum class TryLockResult { Interference = -1, HasOwner = 0, Success = 1 };
419
420 bool TryLockWithContentionMark(JavaThread* locking_thread, ObjectMonitorContentionMark& contention_mark);
421 TryLockResult TryLock(JavaThread* current);
422
423 bool TrySpin(JavaThread* current);
424 bool short_fixed_spin(JavaThread* current, int spin_count, bool adapt);
425 void ExitEpilog(JavaThread* current, ObjectWaiter* Wakee);
426
427 // Deflation support
428 bool deflate_monitor(Thread* current);
429 private:
430 void install_displaced_markword_in_object(const oop obj);
431 };
432
433 // RAII object to ensure that ObjectMonitor::is_being_async_deflated() is
434 // stable within the context of this mark.
435 class ObjectMonitorContentionMark : StackObj {
436 DEBUG_ONLY(friend class ObjectMonitor;)
|