38 class ParkEvent;
39 class BasicLock;
40 class ContinuationWrapper;
41
42
43 class ObjectWaiter : public CHeapObj<mtThread> {
44 public:
45 enum TStates : uint8_t { TS_UNDEF, TS_READY, TS_RUN, TS_WAIT, TS_ENTER };
46 ObjectWaiter* volatile _next;
47 ObjectWaiter* volatile _prev;
48 JavaThread* _thread;
49 OopHandle _vthread;
50 ObjectMonitor* _monitor;
51 uint64_t _notifier_tid;
52 int _recursions;
53 volatile TStates TState;
54 volatile bool _notified;
55 bool _is_wait;
56 bool _at_reenter;
57 bool _interrupted;
58 bool _active; // Contention monitoring is enabled
59 public:
60 ObjectWaiter(JavaThread* current);
61 ObjectWaiter(oop vthread, ObjectMonitor* mon);
62 ~ObjectWaiter();
63 JavaThread* thread() const { return _thread; }
64 bool is_vthread() const { return _thread == nullptr; }
65 uint8_t state() const { return TState; }
66 ObjectMonitor* monitor() const { return _monitor; }
67 bool is_wait() const { return _is_wait; }
68 bool notified() const { return _notified; }
69 bool at_reenter() const { return _at_reenter; }
70 bool at_monitorenter() const { return !_is_wait || _at_reenter || _notified; }
71 oop vthread() const;
72 void wait_reenter_begin(ObjectMonitor *mon);
73 void wait_reenter_end(ObjectMonitor *mon);
74
75 ObjectWaiter* const badObjectWaiterPtr = (ObjectWaiter*) 0xBAD;
76 void set_bad_pointers() {
77 #ifdef ASSERT
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); }
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:
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);
|
38 class ParkEvent;
39 class BasicLock;
40 class ContinuationWrapper;
41
42
43 class ObjectWaiter : public CHeapObj<mtThread> {
44 public:
45 enum TStates : uint8_t { TS_UNDEF, TS_READY, TS_RUN, TS_WAIT, TS_ENTER };
46 ObjectWaiter* volatile _next;
47 ObjectWaiter* volatile _prev;
48 JavaThread* _thread;
49 OopHandle _vthread;
50 ObjectMonitor* _monitor;
51 uint64_t _notifier_tid;
52 int _recursions;
53 volatile TStates TState;
54 volatile bool _notified;
55 bool _is_wait;
56 bool _at_reenter;
57 bool _interrupted;
58 bool _interruptible;
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 ObjectWaiter* const badObjectWaiterPtr = (ObjectWaiter*) 0xBAD;
77 void set_bad_pointers() {
78 #ifdef ASSERT
183 // Separate _owner and _next_om on different cache lines since
184 // both can have busy multi-threaded access. _previous_owner_tid is only
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); }
311 }
312
313 bool has_anonymous_owner() const { return owner_raw() == ANONYMOUS_OWNER; }
314 void set_anonymous_owner() {
315 set_owner_from_raw(NO_OWNER, ANONYMOUS_OWNER);
316 }
317 void set_owner_from_anonymous(JavaThread* owner) {
318 set_owner_from(ANONYMOUS_OWNER, owner);
319 }
320
321 // Simply get _next_om field.
322 ObjectMonitor* next_om() const;
323 // Simply set _next_om field to new_value.
324 void set_next_om(ObjectMonitor* new_value);
325
326 int contentions() const;
327 void add_to_contentions(int value);
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:
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, bool interruptible);
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);
|