< prev index next >

src/hotspot/share/runtime/objectMonitor.hpp

Print this page

 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   // Used in LM_LEGACY mode to store BasicLock* in case of inflation by contending thread.
208   BasicLock* volatile _stack_locker;
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); }

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   // Get and set _stack_locker.
322   BasicLock* stack_locker() const;
323   void set_stack_locker(BasicLock* locker);
324 
325   // Simply get _next_om field.
326   ObjectMonitor* next_om() const;
327   // Simply set _next_om field to new_value.
328   void set_next_om(ObjectMonitor* new_value);
329 
330   int       contentions() const;
331   void      add_to_contentions(int value);
332   intx      recursions() const                                         { return _recursions; }
333   void      set_recursions(size_t recursions);
334   void      increment_recursions(JavaThread* current);



335 
336   // JVM/TI GetObjectMonitorUsage() needs this:
337   int waiters() const;
338   ObjectWaiter* first_waiter()                                         { return _wait_set; }
339   ObjectWaiter* next_waiter(ObjectWaiter* o)                           { return o->_next; }
340   JavaThread* thread_of_waiter(ObjectWaiter* o)                        { return o->_thread; }
341 
342   ObjectMonitor(oop object);
343   ~ObjectMonitor();
344 
345   oop       object() const;
346   oop       object_peek() const;
347   bool      object_is_dead() const;
348   bool      object_refers_to(oop obj) const;
349 
350   // Returns true if the specified thread owns the ObjectMonitor. Otherwise
351   // returns false and throws IllegalMonitorStateException (IMSE).
352   bool      check_owner(TRAPS);
353 
354  private:

391   void      print_debug_style_on(outputStream* st) const;
392 #endif
393   void      print_on(outputStream* st) const;
394 
395   // Use the following at your own risk
396   intx      complete_exit(JavaThread* current);
397 
398  private:
399   void      add_to_entry_list(JavaThread* current, ObjectWaiter* node);
400   void      add_waiter(ObjectWaiter* waiter);
401   bool      notify_internal(JavaThread* current);
402   ObjectWaiter* dequeue_waiter();
403   void      dequeue_specific_waiter(ObjectWaiter* waiter);
404   void      enter_internal(JavaThread* current);
405   void      reenter_internal(JavaThread* current, ObjectWaiter* current_node);
406   void      entry_list_build_dll(JavaThread* current);
407   void      unlink_after_acquire(JavaThread* current, ObjectWaiter* current_node);
408   ObjectWaiter* entry_list_tail(JavaThread* current);
409 
410   bool      vthread_monitor_enter(JavaThread* current, ObjectWaiter* node = nullptr);
411   void      vthread_wait(JavaThread* current, jlong millis);
412   bool      vthread_wait_reenter(JavaThread* current, ObjectWaiter* node, ContinuationWrapper& cont);
413   void      vthread_epilog(JavaThread* current, ObjectWaiter* node);
414 
415   enum class TryLockResult { Interference = -1, HasOwner = 0, Success = 1 };
416 
417   bool           try_lock_with_contention_mark(JavaThread* locking_thread, ObjectMonitorContentionMark& contention_mark);
418   bool           try_lock_or_add_to_entry_list(JavaThread* current, ObjectWaiter* node);
419   TryLockResult  try_lock(JavaThread* current);
420 
421   bool      try_spin(JavaThread* current);
422   bool      short_fixed_spin(JavaThread* current, int spin_count, bool adapt);
423   void      exit_epilog(JavaThread* current, ObjectWaiter* Wakee);
424 
425  public:
426   // Deflation support
427   bool      deflate_monitor(Thread* current);
428   void      install_displaced_markword_in_object(const oop obj);
429 
430   // JFR support
431   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   // Used in LM_LEGACY mode to store BasicLock* in case of inflation by contending thread.
211   BasicLock* volatile _stack_locker;
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); }

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   // Get and set _stack_locker.
325   BasicLock* stack_locker() const;
326   void set_stack_locker(BasicLock* locker);
327 
328   // Simply get _next_om field.
329   ObjectMonitor* next_om() const;
330   // Simply set _next_om field to new_value.
331   void set_next_om(ObjectMonitor* new_value);
332 
333   int       contentions() const;
334   void      add_to_contentions(int value);
335   intx      recursions() const                                         { return _recursions; }
336   void      set_recursions(size_t recursions);
337   void      increment_recursions(JavaThread* current);
338   void      inc_unmounted_vthreads();
339   void      dec_unmounted_vthreads();
340   bool      has_unmounted_vthreads() const;
341 
342   // JVM/TI GetObjectMonitorUsage() needs this:
343   int waiters() const;
344   ObjectWaiter* first_waiter()                                         { return _wait_set; }
345   ObjectWaiter* next_waiter(ObjectWaiter* o)                           { return o->_next; }
346   JavaThread* thread_of_waiter(ObjectWaiter* o)                        { return o->_thread; }
347 
348   ObjectMonitor(oop object);
349   ~ObjectMonitor();
350 
351   oop       object() const;
352   oop       object_peek() const;
353   bool      object_is_dead() const;
354   bool      object_refers_to(oop obj) const;
355 
356   // Returns true if the specified thread owns the ObjectMonitor. Otherwise
357   // returns false and throws IllegalMonitorStateException (IMSE).
358   bool      check_owner(TRAPS);
359 
360  private:

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      add_to_entry_list(JavaThread* current, ObjectWaiter* node);
406   void      add_waiter(ObjectWaiter* waiter);
407   bool      notify_internal(JavaThread* current);
408   ObjectWaiter* dequeue_waiter();
409   void      dequeue_specific_waiter(ObjectWaiter* waiter);
410   void      enter_internal(JavaThread* current);
411   void      reenter_internal(JavaThread* current, ObjectWaiter* current_node);
412   void      entry_list_build_dll(JavaThread* current);
413   void      unlink_after_acquire(JavaThread* current, ObjectWaiter* current_node);
414   ObjectWaiter* entry_list_tail(JavaThread* current);
415 
416   bool      vthread_monitor_enter(JavaThread* current, ObjectWaiter* node = nullptr);
417   void      vthread_wait(JavaThread* current, jlong millis, bool interruptible);
418   bool      vthread_wait_reenter(JavaThread* current, ObjectWaiter* node, ContinuationWrapper& cont);
419   void      vthread_epilog(JavaThread* current, ObjectWaiter* node);
420 
421   enum class TryLockResult { Interference = -1, HasOwner = 0, Success = 1 };
422 
423   bool           try_lock_with_contention_mark(JavaThread* locking_thread, ObjectMonitorContentionMark& contention_mark);
424   bool           try_lock_or_add_to_entry_list(JavaThread* current, ObjectWaiter* node);
425   TryLockResult  try_lock(JavaThread* current);
426 
427   bool      try_spin(JavaThread* current);
428   bool      short_fixed_spin(JavaThread* current, int spin_count, bool adapt);
429   void      exit_epilog(JavaThread* current, ObjectWaiter* Wakee);
430 
431  public:
432   // Deflation support
433   bool      deflate_monitor(Thread* current);
434   void      install_displaced_markword_in_object(const oop obj);
435 
436   // JFR support
437   static bool is_jfr_excluded(const Klass* monitor_klass);
< prev index next >