39 class ParkEvent;
40 class BasicLock;
41 class ContinuationWrapper;
42
43
44 class ObjectWaiter : public CHeapObj<mtThread> {
45 public:
46 enum TStates : uint8_t { TS_UNDEF, TS_READY, TS_RUN, TS_WAIT, TS_ENTER };
47 ObjectWaiter* volatile _next;
48 ObjectWaiter* volatile _prev;
49 JavaThread* _thread;
50 OopHandle _vthread;
51 ObjectMonitor* _monitor;
52 uint64_t _notifier_tid;
53 int _recursions;
54 volatile TStates TState;
55 volatile bool _notified;
56 bool _is_wait;
57 bool _at_reenter;
58 bool _interrupted;
59 bool _active; // Contention monitoring is enabled
60 public:
61 ObjectWaiter(JavaThread* current);
62 ObjectWaiter(oop vthread, ObjectMonitor* mon);
63 ~ObjectWaiter();
64 JavaThread* thread() const { return _thread; }
65 bool is_vthread() const { return _thread == nullptr; }
66 uint8_t state() const { return TState; }
67 ObjectMonitor* monitor() const { return _monitor; }
68 bool is_wait() const { return _is_wait; }
69 bool notified() const { return _notified; }
70 bool at_reenter() const { return _at_reenter; }
71 bool at_monitorenter() const { return !_is_wait || _at_reenter || _notified; }
72 oop vthread() const;
73 void wait_reenter_begin(ObjectMonitor *mon);
74 void wait_reenter_end(ObjectMonitor *mon);
75
76 void set_bad_pointers() {
77 #ifdef ASSERT
78 this->_prev = (ObjectWaiter*) badAddressVal;
182 // Separate _owner and _next_om on different cache lines since
183 // both can have busy multi-threaded access. _previous_owner_tid is only
184 // changed by ObjectMonitor::exit() so it is a good choice to share the
185 // cache line with _owner.
186 DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(void* volatile) +
187 sizeof(volatile uint64_t));
188 ObjectMonitor* _next_om; // Next ObjectMonitor* linkage
189 volatile intx _recursions; // recursion count, 0 for first entry
190 ObjectWaiter* volatile _entry_list; // Threads blocked on entry or reentry.
191 // The list is actually composed of wait-nodes,
192 // acting as proxies for Threads.
193 ObjectWaiter* volatile _entry_list_tail; // _entry_list is the head, this is the tail.
194 int64_t volatile _succ; // Heir presumptive thread - used for futile wakeup throttling
195
196 volatile int _SpinDuration;
197
198 int _contentions; // Number of active contentions in enter(). It is used by is_busy()
199 // along with other fields to determine if an ObjectMonitor can be
200 // deflated. It is also used by the async deflation protocol. See
201 // ObjectMonitor::deflate_monitor().
202
203 ObjectWaiter* volatile _wait_set; // LL of threads waiting on the monitor - wait()
204 volatile int _waiters; // number of waiting threads
205 volatile int _wait_set_lock; // protects wait set queue - simple spinlock
206
207 public:
208
209 static void Initialize();
210 static void Initialize2();
211
212 static OopHandle& vthread_list_head() { return _vthread_list_head; }
213 static ParkEvent* vthread_unparker_ParkEvent() { return _vthread_unparker_ParkEvent; }
214
215 static int Knob_SpinLimit;
216
217 static ByteSize metadata_offset() { return byte_offset_of(ObjectMonitor, _metadata); }
218 static ByteSize owner_offset() { return byte_offset_of(ObjectMonitor, _owner); }
219 static ByteSize recursions_offset() { return byte_offset_of(ObjectMonitor, _recursions); }
220 static ByteSize succ_offset() { return byte_offset_of(ObjectMonitor, _succ); }
221 static ByteSize entry_list_offset() { return byte_offset_of(ObjectMonitor, _entry_list); }
222
223 // ObjectMonitor references can be ORed with markWord::monitor_value
224 // as part of the ObjectMonitor tagging mechanism. When we combine an
225 // ObjectMonitor reference with an offset, we need to remove the tag
308 }
309
310 bool has_anonymous_owner() const { return owner_raw() == ANONYMOUS_OWNER; }
311 void set_anonymous_owner() {
312 set_owner_from_raw(NO_OWNER, ANONYMOUS_OWNER);
313 }
314 void set_owner_from_anonymous(JavaThread* owner) {
315 set_owner_from(ANONYMOUS_OWNER, owner);
316 }
317
318 // Simply get _next_om field.
319 ObjectMonitor* next_om() const;
320 // Simply set _next_om field to new_value.
321 void set_next_om(ObjectMonitor* new_value);
322
323 int contentions() const;
324 void add_to_contentions(int value);
325 intx recursions() const { return _recursions; }
326 void set_recursions(size_t recursions);
327 void increment_recursions(JavaThread* current);
328
329 // JVM/TI GetObjectMonitorUsage() needs this:
330 int waiters() const;
331 ObjectWaiter* first_waiter() { return _wait_set; }
332 ObjectWaiter* next_waiter(ObjectWaiter* o) { return o->_next; }
333 JavaThread* thread_of_waiter(ObjectWaiter* o) { return o->_thread; }
334
335 ObjectMonitor(oop object);
336 ~ObjectMonitor();
337
338 oop object() const;
339 oop object_peek() const;
340 bool object_is_dead() const;
341 bool object_refers_to(oop obj) const;
342
343 // Returns true if the specified thread owns the ObjectMonitor. Otherwise
344 // returns false and throws IllegalMonitorStateException (IMSE).
345 bool check_owner(TRAPS);
346
347 private:
348 class ExitOnSuspend {
349 protected:
350 ObjectMonitor* _om;
351 bool _om_exited;
352 public:
353 ExitOnSuspend(ObjectMonitor* om) : _om(om), _om_exited(false) {}
354 void operator()(JavaThread* current);
355 bool exited() { return _om_exited; }
356 };
357 class ClearSuccOnSuspend {
358 protected:
359 ObjectMonitor* _om;
360 public:
361 ClearSuccOnSuspend(ObjectMonitor* om) : _om(om) {}
384 void print_debug_style_on(outputStream* st) const;
385 #endif
386 void print_on(outputStream* st) const;
387
388 // Use the following at your own risk
389 intx complete_exit(JavaThread* current);
390
391 private:
392 void add_to_entry_list(JavaThread* current, ObjectWaiter* node);
393 void add_waiter(ObjectWaiter* waiter);
394 bool notify_internal(JavaThread* current);
395 ObjectWaiter* dequeue_waiter();
396 void dequeue_specific_waiter(ObjectWaiter* waiter);
397 void enter_internal(JavaThread* current);
398 void reenter_internal(JavaThread* current, ObjectWaiter* current_node);
399 void entry_list_build_dll(JavaThread* current);
400 void unlink_after_acquire(JavaThread* current, ObjectWaiter* current_node);
401 ObjectWaiter* entry_list_tail(JavaThread* current);
402
403 bool vthread_monitor_enter(JavaThread* current, ObjectWaiter* node = nullptr);
404 void vthread_wait(JavaThread* current, jlong millis);
405 bool vthread_wait_reenter(JavaThread* current, ObjectWaiter* node, ContinuationWrapper& cont);
406 void vthread_epilog(JavaThread* current, ObjectWaiter* node);
407
408 enum class TryLockResult { Interference = -1, HasOwner = 0, Success = 1 };
409
410 bool try_lock_with_contention_mark(JavaThread* locking_thread, ObjectMonitorContentionMark& contention_mark);
411 bool try_lock_or_add_to_entry_list(JavaThread* current, ObjectWaiter* node);
412 TryLockResult try_lock(JavaThread* current);
413
414 bool try_spin(JavaThread* current);
415 bool short_fixed_spin(JavaThread* current, int spin_count, bool adapt);
416 void exit_epilog(JavaThread* current, ObjectWaiter* Wakee);
417
418 public:
419 // Deflation support
420 bool deflate_monitor(Thread* current);
421 void install_displaced_markword_in_object(const oop obj);
422
423 // JFR support
424 static bool is_jfr_excluded(const Klass* monitor_klass);
|
39 class ParkEvent;
40 class BasicLock;
41 class ContinuationWrapper;
42
43
44 class ObjectWaiter : public CHeapObj<mtThread> {
45 public:
46 enum TStates : uint8_t { TS_UNDEF, TS_READY, TS_RUN, TS_WAIT, TS_ENTER };
47 ObjectWaiter* volatile _next;
48 ObjectWaiter* volatile _prev;
49 JavaThread* _thread;
50 OopHandle _vthread;
51 ObjectMonitor* _monitor;
52 uint64_t _notifier_tid;
53 int _recursions;
54 volatile TStates TState;
55 volatile bool _notified;
56 bool _is_wait;
57 bool _at_reenter;
58 bool _interrupted;
59 bool _interruptible;
60 bool _do_timed_park;
61 bool _active; // Contention monitoring is enabled
62 public:
63 ObjectWaiter(JavaThread* current);
64 ObjectWaiter(oop vthread, ObjectMonitor* mon);
65 ~ObjectWaiter();
66 JavaThread* thread() const { return _thread; }
67 bool is_vthread() const { return _thread == nullptr; }
68 uint8_t state() const { return TState; }
69 ObjectMonitor* monitor() const { return _monitor; }
70 bool is_wait() const { return _is_wait; }
71 bool notified() const { return _notified; }
72 bool at_reenter() const { return _at_reenter; }
73 bool at_monitorenter() const { return !_is_wait || _at_reenter || _notified; }
74 oop vthread() const;
75 void wait_reenter_begin(ObjectMonitor *mon);
76 void wait_reenter_end(ObjectMonitor *mon);
77
78 void set_bad_pointers() {
79 #ifdef ASSERT
80 this->_prev = (ObjectWaiter*) badAddressVal;
184 // Separate _owner and _next_om on different cache lines since
185 // both can have busy multi-threaded access. _previous_owner_tid is only
186 // changed by ObjectMonitor::exit() so it is a good choice to share the
187 // cache line with _owner.
188 DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(void* volatile) +
189 sizeof(volatile uint64_t));
190 ObjectMonitor* _next_om; // Next ObjectMonitor* linkage
191 volatile intx _recursions; // recursion count, 0 for first entry
192 ObjectWaiter* volatile _entry_list; // Threads blocked on entry or reentry.
193 // The list is actually composed of wait-nodes,
194 // acting as proxies for Threads.
195 ObjectWaiter* volatile _entry_list_tail; // _entry_list is the head, this is the tail.
196 int64_t volatile _succ; // Heir presumptive thread - used for futile wakeup throttling
197
198 volatile int _SpinDuration;
199
200 int _contentions; // Number of active contentions in enter(). It is used by is_busy()
201 // along with other fields to determine if an ObjectMonitor can be
202 // deflated. It is also used by the async deflation protocol. See
203 // ObjectMonitor::deflate_monitor().
204 int64_t _unmounted_vthreads; // Number of nodes in the _entry_list associated with unmounted vthreads.
205 // It might be temporarily more than the actual number but never less.
206 OopHandle _object_strong; // Used to protect object during preemption on class initialization
207
208 ObjectWaiter* volatile _wait_set; // LL of threads waiting on the monitor - wait()
209 volatile int _waiters; // number of waiting threads
210 volatile int _wait_set_lock; // protects wait set queue - simple spinlock
211 volatile int _object_strong_lock; // protects setting of _object_strong
212
213 public:
214
215 static void Initialize();
216 static void Initialize2();
217
218 static OopHandle& vthread_list_head() { return _vthread_list_head; }
219 static ParkEvent* vthread_unparker_ParkEvent() { return _vthread_unparker_ParkEvent; }
220
221 static int Knob_SpinLimit;
222
223 static ByteSize metadata_offset() { return byte_offset_of(ObjectMonitor, _metadata); }
224 static ByteSize owner_offset() { return byte_offset_of(ObjectMonitor, _owner); }
225 static ByteSize recursions_offset() { return byte_offset_of(ObjectMonitor, _recursions); }
226 static ByteSize succ_offset() { return byte_offset_of(ObjectMonitor, _succ); }
227 static ByteSize entry_list_offset() { return byte_offset_of(ObjectMonitor, _entry_list); }
228
229 // ObjectMonitor references can be ORed with markWord::monitor_value
230 // as part of the ObjectMonitor tagging mechanism. When we combine an
231 // ObjectMonitor reference with an offset, we need to remove the tag
314 }
315
316 bool has_anonymous_owner() const { return owner_raw() == ANONYMOUS_OWNER; }
317 void set_anonymous_owner() {
318 set_owner_from_raw(NO_OWNER, ANONYMOUS_OWNER);
319 }
320 void set_owner_from_anonymous(JavaThread* owner) {
321 set_owner_from(ANONYMOUS_OWNER, owner);
322 }
323
324 // Simply get _next_om field.
325 ObjectMonitor* next_om() const;
326 // Simply set _next_om field to new_value.
327 void set_next_om(ObjectMonitor* new_value);
328
329 int contentions() const;
330 void add_to_contentions(int value);
331 intx recursions() const { return _recursions; }
332 void set_recursions(size_t recursions);
333 void increment_recursions(JavaThread* current);
334 void inc_unmounted_vthreads();
335 void dec_unmounted_vthreads();
336 bool has_unmounted_vthreads() const;
337
338 // JVM/TI GetObjectMonitorUsage() needs this:
339 int waiters() const;
340 ObjectWaiter* first_waiter() { return _wait_set; }
341 ObjectWaiter* next_waiter(ObjectWaiter* o) { return o->_next; }
342 JavaThread* thread_of_waiter(ObjectWaiter* o) { return o->_thread; }
343
344 ObjectMonitor(oop object);
345 ~ObjectMonitor();
346
347 oop object() const;
348 oop object_peek() const;
349 bool object_is_dead() const;
350 bool object_refers_to(oop obj) const;
351 void set_object_strong();
352
353 // Returns true if the specified thread owns the ObjectMonitor. Otherwise
354 // returns false and throws IllegalMonitorStateException (IMSE).
355 bool check_owner(TRAPS);
356
357 private:
358 class ExitOnSuspend {
359 protected:
360 ObjectMonitor* _om;
361 bool _om_exited;
362 public:
363 ExitOnSuspend(ObjectMonitor* om) : _om(om), _om_exited(false) {}
364 void operator()(JavaThread* current);
365 bool exited() { return _om_exited; }
366 };
367 class ClearSuccOnSuspend {
368 protected:
369 ObjectMonitor* _om;
370 public:
371 ClearSuccOnSuspend(ObjectMonitor* om) : _om(om) {}
394 void print_debug_style_on(outputStream* st) const;
395 #endif
396 void print_on(outputStream* st) const;
397
398 // Use the following at your own risk
399 intx complete_exit(JavaThread* current);
400
401 private:
402 void add_to_entry_list(JavaThread* current, ObjectWaiter* node);
403 void add_waiter(ObjectWaiter* waiter);
404 bool notify_internal(JavaThread* current);
405 ObjectWaiter* dequeue_waiter();
406 void dequeue_specific_waiter(ObjectWaiter* waiter);
407 void enter_internal(JavaThread* current);
408 void reenter_internal(JavaThread* current, ObjectWaiter* current_node);
409 void entry_list_build_dll(JavaThread* current);
410 void unlink_after_acquire(JavaThread* current, ObjectWaiter* current_node);
411 ObjectWaiter* entry_list_tail(JavaThread* current);
412
413 bool vthread_monitor_enter(JavaThread* current, ObjectWaiter* node = nullptr);
414 void vthread_wait(JavaThread* current, jlong millis, bool interruptible);
415 bool vthread_wait_reenter(JavaThread* current, ObjectWaiter* node, ContinuationWrapper& cont);
416 void vthread_epilog(JavaThread* current, ObjectWaiter* node);
417
418 enum class TryLockResult { Interference = -1, HasOwner = 0, Success = 1 };
419
420 bool try_lock_with_contention_mark(JavaThread* locking_thread, ObjectMonitorContentionMark& contention_mark);
421 bool try_lock_or_add_to_entry_list(JavaThread* current, ObjectWaiter* node);
422 TryLockResult try_lock(JavaThread* current);
423
424 bool try_spin(JavaThread* current);
425 bool short_fixed_spin(JavaThread* current, int spin_count, bool adapt);
426 void exit_epilog(JavaThread* current, ObjectWaiter* Wakee);
427
428 public:
429 // Deflation support
430 bool deflate_monitor(Thread* current);
431 void install_displaced_markword_in_object(const oop obj);
432
433 // JFR support
434 static bool is_jfr_excluded(const Klass* monitor_klass);
|