< prev index next >

src/hotspot/share/runtime/objectMonitor.hpp

Print this page
rev 55489 : Checkpoint latest preliminary review patches for full OpenJDK review; merge with 8222295.patch.
rev 55490 : imported patch dcubed.monitor_deflate_conc.v2.01
rev 55492 : imported patch dcubed.monitor_deflate_conc.v2.03
rev 55494 : imported patch dcubed.monitor_deflate_conc.v2.05


 119 //     _recursions, _EntryList, _cxq, and _succ, all of which may be
 120 //     fetched in the inflated unlock path, on a different cache line
 121 //     would make them immune to CAS-based invalidation from the _owner
 122 //     field.
 123 //
 124 //   - The _recursions field should be of type int, or int32_t but not
 125 //     intptr_t. There's no reason to use a 64-bit type for this field
 126 //     in a 64-bit JVM.
 127 
 128 class ObjectMonitor {
 129  public:
 130   enum {
 131     OM_OK,                    // no error
 132     OM_SYSTEM_ERROR,          // operating system error
 133     OM_ILLEGAL_MONITOR_STATE, // IllegalMonitorStateException
 134     OM_INTERRUPTED,           // Thread.interrupt()
 135     OM_TIMED_OUT              // Object.wait() timed out
 136   };
 137 
 138  private:

 139   friend class ObjectSynchronizer;
 140   friend class ObjectWaiter;
 141   friend class VMStructs;
 142   JVMCI_ONLY(friend class JVMCIVMStructs;)
 143 
 144   volatile markOop   _header;       // displaced object header word - mark
 145   void*     volatile _object;       // backward object pointer - strong root
 146  public:
 147   ObjectMonitor*     FreeNext;      // Free list linkage
 148  private:
 149   DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE,
 150                         sizeof(volatile markOop) + sizeof(void * volatile) +
 151                         sizeof(ObjectMonitor *));
 152  protected:                         // protected for JvmtiRawMonitor


 153   void *  volatile _owner;          // pointer to owning thread OR BasicLock
 154   volatile jlong _previous_owner_tid;  // thread id of the previous owner of the monitor
 155   volatile intptr_t  _recursions;   // recursion count, 0 for first entry
 156   ObjectWaiter * volatile _EntryList; // Threads blocked on entry or reentry.
 157                                       // The list is actually composed of WaitNodes,
 158                                       // acting as proxies for Threads.
 159  private:
 160   ObjectWaiter * volatile _cxq;     // LL of recently-arrived threads blocked on entry.
 161   Thread * volatile _succ;          // Heir presumptive thread - used for futile wakeup throttling
 162   Thread * volatile _Responsible;
 163 
 164   volatile int _Spinner;            // for exit->spinner handoff optimization
 165   volatile int _SpinDuration;
 166 
 167   volatile jint  _contentions;      // Number of active contentions in enter(). It is used by is_busy()
 168                                     // along with other fields to determine if an ObjectMonitor can be
 169                                     // deflated. See ObjectSynchronizer::deflate_monitor().

 170  protected:
 171   ObjectWaiter * volatile _WaitSet; // LL of threads wait()ing on the monitor
 172   volatile jint  _waiters;          // number of waiting threads
 173  private:
 174   volatile int _WaitSetLock;        // protects Wait Queue - simple spinlock








 175 
 176  public:
 177   static void Initialize();
 178 
 179   // Only perform a PerfData operation if the PerfData object has been
 180   // allocated and if the PerfDataManager has not freed the PerfData
 181   // objects which can happen at normal VM shutdown.
 182   //
 183   #define OM_PERFDATA_OP(f, op_str)              \
 184     do {                                         \
 185       if (ObjectMonitor::_sync_ ## f != NULL &&  \
 186           PerfDataManager::has_PerfData()) {     \
 187         ObjectMonitor::_sync_ ## f->op_str;      \
 188       }                                          \
 189     } while (0)
 190 
 191   static PerfCounter * _sync_ContendedLockAttempts;
 192   static PerfCounter * _sync_FutileWakeups;
 193   static PerfCounter * _sync_Parks;
 194   static PerfCounter * _sync_Notifications;


 216   // ObjectMonitor references can be ORed with markOopDesc::monitor_value
 217   // as part of the ObjectMonitor tagging mechanism. When we combine an
 218   // ObjectMonitor reference with an offset, we need to remove the tag
 219   // value in order to generate the proper address.
 220   //
 221   // We can either adjust the ObjectMonitor reference and then add the
 222   // offset or we can adjust the offset that is added to the ObjectMonitor
 223   // reference. The latter avoids an AGI (Address Generation Interlock)
 224   // stall so the helper macro adjusts the offset value that is returned
 225   // to the ObjectMonitor reference manipulation code:
 226   //
 227   #define OM_OFFSET_NO_MONITOR_VALUE_TAG(f) \
 228     ((ObjectMonitor::f ## _offset_in_bytes()) - markOopDesc::monitor_value)
 229 
 230   markOop   header() const;
 231   volatile markOop* header_addr();
 232   void      set_header(markOop hdr);
 233 
 234   intptr_t is_busy() const {
 235     // TODO-FIXME: assert _owner == null implies _recursions = 0
 236     return _contentions|_waiters|intptr_t(_owner)|intptr_t(_cxq)|intptr_t(_EntryList);












 237   }
 238   const char* is_busy_to_string(stringStream* ss);
 239 
 240   intptr_t  is_entered(Thread* current) const;
 241 
 242   void*     owner() const;


 243   void      set_owner(void* owner);
 244 
 245   jint      waiters() const;
 246 
 247   jint      contentions() const;
 248   intptr_t  recursions() const                                         { return _recursions; }
 249 
 250   // JVM/TI GetObjectMonitorUsage() needs this:
 251   ObjectWaiter* first_waiter()                                         { return _WaitSet; }
 252   ObjectWaiter* next_waiter(ObjectWaiter* o)                           { return o->_next; }
 253   Thread* thread_of_waiter(ObjectWaiter* o)                            { return o->_thread; }
 254 
 255  protected:
 256   // We don't typically expect or want the ctors or dtors to run.
 257   // normal ObjectMonitors are type-stable and immortal.
 258   ObjectMonitor() { ::memset((void *)this, 0, sizeof(*this)); }
 259 
 260   ~ObjectMonitor() {
 261     // TODO: Add asserts ...
 262     // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0


 267   void Recycle() {
 268     // TODO: add stronger asserts ...
 269     // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0
 270     // _contentions == 0 EntryList  == NULL
 271     // _recursions == 0 _WaitSet == NULL
 272     DEBUG_ONLY(stringStream ss;)
 273     assert((is_busy() | _recursions) == 0, "freeing in-use monitor: %s, "
 274            "recursions=" INTPTR_FORMAT, is_busy_to_string(&ss), _recursions);
 275     _succ          = NULL;
 276     _EntryList     = NULL;
 277     _cxq           = NULL;
 278     _WaitSet       = NULL;
 279     _recursions    = 0;
 280   }
 281 
 282  public:
 283 
 284   void*     object() const;
 285   void*     object_addr();
 286   void      set_object(void* obj);









 287 
 288   bool      check(TRAPS);       // true if the thread owns the monitor.
 289   void      check_slow(TRAPS);
 290   void      clear();

 291 
 292   void      enter(TRAPS);
 293   void      exit(bool not_suspended, TRAPS);
 294   void      wait(jlong millis, bool interruptable, TRAPS);
 295   void      notify(TRAPS);
 296   void      notifyAll(TRAPS);
 297 
 298   void      print() const;
 299   void      print_on(outputStream* st) const;
 300 
 301 // Use the following at your own risk
 302   intptr_t  complete_exit(TRAPS);
 303   void      reenter(intptr_t recursions, TRAPS);
 304 
 305  private:
 306   void      AddWaiter(ObjectWaiter * waiter);
 307   void      INotify(Thread * Self);
 308   ObjectWaiter * DequeueWaiter();
 309   void      DequeueSpecificWaiter(ObjectWaiter * waiter);
 310   void      EnterI(TRAPS);
 311   void      ReenterI(Thread * Self, ObjectWaiter * SelfNode);
 312   void      UnlinkAfterAcquire(Thread * Self, ObjectWaiter * SelfNode);
 313   int       TryLock(Thread * Self);
 314   int       NotRunnable(Thread * Self, Thread * Owner);
 315   int       TrySpin(Thread * Self);
 316   void      ExitEpilog(Thread * Self, ObjectWaiter * Wakee);
 317   bool      ExitSuspendEquivalent(JavaThread * Self);

 318 };































 319 
 320 #endif // SHARE_RUNTIME_OBJECTMONITOR_HPP


 119 //     _recursions, _EntryList, _cxq, and _succ, all of which may be
 120 //     fetched in the inflated unlock path, on a different cache line
 121 //     would make them immune to CAS-based invalidation from the _owner
 122 //     field.
 123 //
 124 //   - The _recursions field should be of type int, or int32_t but not
 125 //     intptr_t. There's no reason to use a 64-bit type for this field
 126 //     in a 64-bit JVM.
 127 
 128 class ObjectMonitor {
 129  public:
 130   enum {
 131     OM_OK,                    // no error
 132     OM_SYSTEM_ERROR,          // operating system error
 133     OM_ILLEGAL_MONITOR_STATE, // IllegalMonitorStateException
 134     OM_INTERRUPTED,           // Thread.interrupt()
 135     OM_TIMED_OUT              // Object.wait() timed out
 136   };
 137 
 138  private:
 139   friend class ObjectMonitorHandle;
 140   friend class ObjectSynchronizer;
 141   friend class ObjectWaiter;
 142   friend class VMStructs;
 143   JVMCI_ONLY(friend class JVMCIVMStructs;)
 144 
 145   volatile markOop   _header;       // displaced object header word - mark
 146   void*     volatile _object;       // backward object pointer - strong root
 147  public:
 148   ObjectMonitor*     FreeNext;      // Free list linkage
 149  private:
 150   DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE,
 151                         sizeof(volatile markOop) + sizeof(void * volatile) +
 152                         sizeof(ObjectMonitor *));
 153  protected:                         // protected for JvmtiRawMonitor
 154   // Used by async deflation as a marker in the _owner field:
 155   #define DEFLATER_MARKER reinterpret_cast<void*>(-1)
 156   void *  volatile _owner;          // pointer to owning thread OR BasicLock
 157   volatile jlong _previous_owner_tid;  // thread id of the previous owner of the monitor
 158   volatile intptr_t  _recursions;   // recursion count, 0 for first entry
 159   ObjectWaiter * volatile _EntryList; // Threads blocked on entry or reentry.
 160                                       // The list is actually composed of WaitNodes,
 161                                       // acting as proxies for Threads.
 162  private:
 163   ObjectWaiter * volatile _cxq;     // LL of recently-arrived threads blocked on entry.
 164   Thread * volatile _succ;          // Heir presumptive thread - used for futile wakeup throttling
 165   Thread * volatile _Responsible;
 166 
 167   volatile int _Spinner;            // for exit->spinner handoff optimization
 168   volatile int _SpinDuration;
 169 
 170   volatile jint  _contentions;      // Number of active contentions in enter(). It is used by is_busy()
 171                                     // along with other fields to determine if an ObjectMonitor can be
 172                                     // deflated. See ObjectSynchronizer::deflate_monitor() and
 173                                     // ObjectSynchronizer::deflate_monitor_using_JT().
 174  protected:
 175   ObjectWaiter * volatile _WaitSet; // LL of threads wait()ing on the monitor
 176   volatile jint  _waiters;          // number of waiting threads
 177  private:
 178   volatile int _WaitSetLock;        // protects Wait Queue - simple spinlock
 179   volatile jint _ref_count;         // ref count for ObjectMonitor* and used by the async deflation
 180                                     // protocol. See ObjectSynchronizer::deflate_monitor_using_JT().
 181   typedef enum {
 182     Free = 0,  // Free must be 0 for monitor to be free after memset(..,0,..).
 183     New,
 184     Old
 185   } AllocationState;
 186   AllocationState _allocation_state;
 187 
 188  public:
 189   static void Initialize();
 190 
 191   // Only perform a PerfData operation if the PerfData object has been
 192   // allocated and if the PerfDataManager has not freed the PerfData
 193   // objects which can happen at normal VM shutdown.
 194   //
 195   #define OM_PERFDATA_OP(f, op_str)              \
 196     do {                                         \
 197       if (ObjectMonitor::_sync_ ## f != NULL &&  \
 198           PerfDataManager::has_PerfData()) {     \
 199         ObjectMonitor::_sync_ ## f->op_str;      \
 200       }                                          \
 201     } while (0)
 202 
 203   static PerfCounter * _sync_ContendedLockAttempts;
 204   static PerfCounter * _sync_FutileWakeups;
 205   static PerfCounter * _sync_Parks;
 206   static PerfCounter * _sync_Notifications;


 228   // ObjectMonitor references can be ORed with markOopDesc::monitor_value
 229   // as part of the ObjectMonitor tagging mechanism. When we combine an
 230   // ObjectMonitor reference with an offset, we need to remove the tag
 231   // value in order to generate the proper address.
 232   //
 233   // We can either adjust the ObjectMonitor reference and then add the
 234   // offset or we can adjust the offset that is added to the ObjectMonitor
 235   // reference. The latter avoids an AGI (Address Generation Interlock)
 236   // stall so the helper macro adjusts the offset value that is returned
 237   // to the ObjectMonitor reference manipulation code:
 238   //
 239   #define OM_OFFSET_NO_MONITOR_VALUE_TAG(f) \
 240     ((ObjectMonitor::f ## _offset_in_bytes()) - markOopDesc::monitor_value)
 241 
 242   markOop   header() const;
 243   volatile markOop* header_addr();
 244   void      set_header(markOop hdr);
 245 
 246   intptr_t is_busy() const {
 247     // TODO-FIXME: assert _owner == null implies _recursions = 0
 248     // We do not include _ref_count in the is_busy() check because
 249     // _ref_count is for indicating that the ObjectMonitor* is in
 250     // use which is orthogonal to whether the ObjectMonitor itself
 251     // is in use for a locking operation.
 252     intptr_t ret_code = _contentions | _waiters | intptr_t(_cxq) | intptr_t(_EntryList);
 253     if (!AsyncDeflateIdleMonitors) {
 254       ret_code |= intptr_t(_owner);
 255     } else {
 256       if (_owner != DEFLATER_MARKER) {
 257         ret_code |= intptr_t(_owner);
 258       }
 259     }
 260     return ret_code;
 261   }
 262   const char* is_busy_to_string(stringStream* ss);
 263 
 264   intptr_t  is_entered(Thread* current) const;
 265 
 266   void*     owner() const;  // Returns NULL if DEFLATER_MARKER is observed.
 267   // Returns true if owner field == DEFLATER_MARKER and false otherwise.
 268   bool      owner_is_DEFLATER_MARKER();
 269   void      set_owner(void* owner);
 270 
 271   jint      waiters() const;
 272 
 273   jint      contentions() const;
 274   intptr_t  recursions() const                                         { return _recursions; }
 275 
 276   // JVM/TI GetObjectMonitorUsage() needs this:
 277   ObjectWaiter* first_waiter()                                         { return _WaitSet; }
 278   ObjectWaiter* next_waiter(ObjectWaiter* o)                           { return o->_next; }
 279   Thread* thread_of_waiter(ObjectWaiter* o)                            { return o->_thread; }
 280 
 281  protected:
 282   // We don't typically expect or want the ctors or dtors to run.
 283   // normal ObjectMonitors are type-stable and immortal.
 284   ObjectMonitor() { ::memset((void *)this, 0, sizeof(*this)); }
 285 
 286   ~ObjectMonitor() {
 287     // TODO: Add asserts ...
 288     // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0


 293   void Recycle() {
 294     // TODO: add stronger asserts ...
 295     // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0
 296     // _contentions == 0 EntryList  == NULL
 297     // _recursions == 0 _WaitSet == NULL
 298     DEBUG_ONLY(stringStream ss;)
 299     assert((is_busy() | _recursions) == 0, "freeing in-use monitor: %s, "
 300            "recursions=" INTPTR_FORMAT, is_busy_to_string(&ss), _recursions);
 301     _succ          = NULL;
 302     _EntryList     = NULL;
 303     _cxq           = NULL;
 304     _WaitSet       = NULL;
 305     _recursions    = 0;
 306   }
 307 
 308  public:
 309 
 310   void*     object() const;
 311   void*     object_addr();
 312   void      set_object(void* obj);
 313   void      set_allocation_state(AllocationState s);
 314   AllocationState allocation_state() const;
 315   bool      is_free() const;
 316   bool      is_active() const;
 317   bool      is_old() const;
 318   bool      is_new() const;
 319   void      dec_ref_count();
 320   void      inc_ref_count();
 321   jint      ref_count() const;
 322 
 323   bool      check(TRAPS);       // true if the thread owns the monitor.
 324   void      check_slow(TRAPS);
 325   void      clear();
 326   void      clear_using_JT();
 327 
 328   void      enter(TRAPS);
 329   void      exit(bool not_suspended, TRAPS);
 330   void      wait(jlong millis, bool interruptable, TRAPS);
 331   void      notify(TRAPS);
 332   void      notifyAll(TRAPS);
 333 
 334   void      print() const;
 335   void      print_on(outputStream* st) const;
 336 
 337 // Use the following at your own risk
 338   intptr_t  complete_exit(TRAPS);
 339   void      reenter(intptr_t recursions, TRAPS);
 340 
 341  private:
 342   void      AddWaiter(ObjectWaiter * waiter);
 343   void      INotify(Thread * Self);
 344   ObjectWaiter * DequeueWaiter();
 345   void      DequeueSpecificWaiter(ObjectWaiter * waiter);
 346   void      EnterI(TRAPS);
 347   void      ReenterI(Thread * Self, ObjectWaiter * SelfNode);
 348   void      UnlinkAfterAcquire(Thread * Self, ObjectWaiter * SelfNode);
 349   int       TryLock(Thread * Self);
 350   int       NotRunnable(Thread * Self, Thread * Owner);
 351   int       TrySpin(Thread * Self);
 352   void      ExitEpilog(Thread * Self, ObjectWaiter * Wakee);
 353   bool      ExitSuspendEquivalent(JavaThread * Self);
 354   void      install_displaced_markword_in_object(const oop obj);
 355 };
 356 
 357 // A helper object for managing an ObjectMonitor*'s ref_count. There
 358 // are special safety considerations when async deflation is used.
 359 class ObjectMonitorHandle : public StackObj {
 360  private:
 361   ObjectMonitor * _om_ptr;
 362  public:
 363   ObjectMonitorHandle() { _om_ptr = NULL; }
 364   ~ObjectMonitorHandle();
 365 
 366   ObjectMonitor * om_ptr() const { return _om_ptr; }
 367   // Save the ObjectMonitor* associated with the specified markOop and
 368   // increment the ref_count.
 369   bool save_om_ptr(oop object, markOop mark);
 370 
 371   // For internal used by ObjectSynchronizer::monitors_iterate().
 372   ObjectMonitorHandle(ObjectMonitor * _om_ptr);
 373   // For internal use by ObjectSynchronizer::inflate().
 374   void set_om_ptr(ObjectMonitor * om_ptr);
 375 };
 376 
 377 // Macro to use guarantee() for more strict AsyncDeflateIdleMonitors
 378 // checks and assert() otherwise.
 379 #define ADIM_guarantee(p, ...)       \
 380   do {                               \
 381     if (AsyncDeflateIdleMonitors) {  \
 382       guarantee(p, __VA_ARGS__);     \
 383     } else {                         \
 384       assert(p, __VA_ARGS__);        \
 385     }                                \
 386   } while (0)
 387 
 388 #endif // SHARE_RUNTIME_OBJECTMONITOR_HPP
< prev index next >