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 _do_timed_park;
60 bool _active; // Contention monitoring is enabled
61 public:
62 ObjectWaiter(JavaThread* current);
63 ObjectWaiter(oop vthread, ObjectMonitor* mon);
64 ~ObjectWaiter();
65 JavaThread* thread() const { return _thread; }
66 bool is_vthread() const { return _thread == nullptr; }
67 uint8_t state() const { return TState; }
68 ObjectMonitor* monitor() const { return _monitor; }
69 bool is_wait() const { return _is_wait; }
70 bool notified() const { return _notified; }
71 bool at_reenter() const { return _at_reenter; }
72 bool at_monitorenter() const { return !_is_wait || _at_reenter || _notified; }
73 oop vthread() const;
74 void wait_reenter_begin(ObjectMonitor *mon);
75 void wait_reenter_end(ObjectMonitor *mon);
76
77 void set_bad_pointers() {
78 #ifdef ASSERT
185 // changed by ObjectMonitor::exit() so it is a good choice to share the
186 // cache line with _owner.
187 DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(void* volatile) +
188 sizeof(volatile uint64_t));
189 ObjectMonitor* _next_om; // Next ObjectMonitor* linkage
190 volatile intx _recursions; // recursion count, 0 for first entry
191 ObjectWaiter* volatile _entry_list; // Threads blocked on entry or reentry.
192 // The list is actually composed of wait-nodes,
193 // acting as proxies for Threads.
194 ObjectWaiter* volatile _entry_list_tail; // _entry_list is the head, this is the tail.
195 int64_t volatile _succ; // Heir presumptive thread - used for futile wakeup throttling
196
197 volatile int _SpinDuration;
198
199 int _contentions; // Number of active contentions in enter(). It is used by is_busy()
200 // along with other fields to determine if an ObjectMonitor can be
201 // deflated. It is also used by the async deflation protocol. See
202 // ObjectMonitor::deflate_monitor().
203 int64_t _unmounted_vthreads; // Number of nodes in the _entry_list associated with unmounted vthreads.
204 // It might be temporarily more than the actual number but never less.
205
206 ObjectWaiter* volatile _wait_set; // LL of threads waiting on the monitor - wait()
207 volatile int _waiters; // number of waiting threads
208 volatile int _wait_set_lock; // protects wait set queue - simple spinlock
209
210 public:
211
212 static void Initialize();
213 static void Initialize2();
214
215 static OopHandle& vthread_list_head() { return _vthread_list_head; }
216 static ParkEvent* vthread_unparker_ParkEvent() { return _vthread_unparker_ParkEvent; }
217
218 static int Knob_SpinLimit;
219
220 static ByteSize metadata_offset() { return byte_offset_of(ObjectMonitor, _metadata); }
221 static ByteSize owner_offset() { return byte_offset_of(ObjectMonitor, _owner); }
222 static ByteSize recursions_offset() { return byte_offset_of(ObjectMonitor, _recursions); }
223 static ByteSize succ_offset() { return byte_offset_of(ObjectMonitor, _succ); }
224 static ByteSize entry_list_offset() { return byte_offset_of(ObjectMonitor, _entry_list); }
225
226 // ObjectMonitor references can be ORed with markWord::monitor_value
227 // as part of the ObjectMonitor tagging mechanism. When we combine an
228 // ObjectMonitor reference with an offset, we need to remove the tag
328 intx recursions() const { return _recursions; }
329 void set_recursions(size_t recursions);
330 void increment_recursions(JavaThread* current);
331 void inc_unmounted_vthreads();
332 void dec_unmounted_vthreads();
333 bool has_unmounted_vthreads() const;
334
335 // JVM/TI GetObjectMonitorUsage() needs this:
336 int waiters() const;
337 ObjectWaiter* first_waiter() { return _wait_set; }
338 ObjectWaiter* next_waiter(ObjectWaiter* o) { return o->_next; }
339 JavaThread* thread_of_waiter(ObjectWaiter* o) { return o->_thread; }
340
341 ObjectMonitor(oop object);
342 ~ObjectMonitor();
343
344 oop object() const;
345 oop object_peek() const;
346 bool object_is_dead() const;
347 bool object_refers_to(oop obj) const;
348
349 // Returns true if the specified thread owns the ObjectMonitor. Otherwise
350 // returns false and throws IllegalMonitorStateException (IMSE).
351 bool check_owner(TRAPS);
352
353 private:
354 class ExitOnSuspend {
355 protected:
356 ObjectMonitor* _om;
357 bool _om_exited;
358 public:
359 ExitOnSuspend(ObjectMonitor* om) : _om(om), _om_exited(false) {}
360 void operator()(JavaThread* current);
361 bool exited() { return _om_exited; }
362 };
363 class ClearSuccOnSuspend {
364 protected:
365 ObjectMonitor* _om;
366 public:
367 ClearSuccOnSuspend(ObjectMonitor* om) : _om(om) {}
390 void print_debug_style_on(outputStream* st) const;
391 #endif
392 void print_on(outputStream* st) const;
393
394 // Use the following at your own risk
395 intx complete_exit(JavaThread* current);
396
397 private:
398 void add_to_entry_list(JavaThread* current, ObjectWaiter* node);
399 void add_waiter(ObjectWaiter* waiter);
400 bool notify_internal(JavaThread* current);
401 ObjectWaiter* dequeue_waiter();
402 void dequeue_specific_waiter(ObjectWaiter* waiter);
403 void enter_internal(JavaThread* current);
404 void reenter_internal(JavaThread* current, ObjectWaiter* current_node);
405 void entry_list_build_dll(JavaThread* current);
406 void unlink_after_acquire(JavaThread* current, ObjectWaiter* current_node);
407 ObjectWaiter* entry_list_tail(JavaThread* current);
408
409 bool vthread_monitor_enter(JavaThread* current, ObjectWaiter* node = nullptr);
410 void vthread_wait(JavaThread* current, jlong millis);
411 bool vthread_wait_reenter(JavaThread* current, ObjectWaiter* node, ContinuationWrapper& cont);
412 void vthread_epilog(JavaThread* current, ObjectWaiter* node);
413
414 enum class TryLockResult { Interference = -1, HasOwner = 0, Success = 1 };
415
416 bool try_lock_with_contention_mark(JavaThread* locking_thread, ObjectMonitorContentionMark& contention_mark);
417 bool try_lock_or_add_to_entry_list(JavaThread* current, ObjectWaiter* node);
418 TryLockResult try_lock(JavaThread* current);
419
420 bool try_spin(JavaThread* current);
421 bool short_fixed_spin(JavaThread* current, int spin_count, bool adapt);
422 void exit_epilog(JavaThread* current, ObjectWaiter* Wakee);
423
424 public:
425 // Deflation support
426 bool deflate_monitor(Thread* current);
427 void install_displaced_markword_in_object(const oop obj);
428
429 // JFR support
430 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
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
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);
|