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