< prev index next >

src/share/vm/runtime/objectMonitor.hpp

Print this page




 154   volatile intptr_t  _recursions;   // recursion count, 0 for first entry
 155   ObjectWaiter * volatile _EntryList; // Threads blocked on entry or reentry.
 156                                       // The list is actually composed of WaitNodes,
 157                                       // acting as proxies for Threads.
 158  private:
 159   ObjectWaiter * volatile _cxq;     // LL of recently-arrived threads blocked on entry.
 160   Thread * volatile _succ;          // Heir presumptive thread - used for futile wakeup throttling
 161   Thread * volatile _Responsible;
 162 
 163   volatile int _Spinner;            // for exit->spinner handoff optimization
 164   volatile int _SpinDuration;
 165 
 166   volatile jint  _count;            // reference count to prevent reclamation/deflation
 167                                     // at stop-the-world time.  See deflate_idle_monitors().
 168                                     // _count is approximately |_WaitSet| + |_EntryList|
 169  protected:
 170   ObjectWaiter * volatile _WaitSet; // LL of threads wait()ing on the monitor
 171   volatile jint  _waiters;          // number of waiting threads
 172  private:
 173   volatile int _WaitSetLock;        // protects Wait Queue - simple spinlock


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


 251     ((ObjectMonitor::f ## _offset_in_bytes()) - markOopDesc::monitor_value)
 252 
 253   markOop   header() const;
 254   void      set_header(markOop hdr);
 255 
 256   intptr_t is_busy() const {
 257     // TODO-FIXME: merge _count and _waiters.
 258     // TODO-FIXME: assert _owner == null implies _recursions = 0
 259     // TODO-FIXME: assert _WaitSet != null implies _count > 0
 260     return _count|_waiters|intptr_t(_owner)|intptr_t(_cxq)|intptr_t(_EntryList);
 261   }
 262 
 263   intptr_t  is_entered(Thread* current) const;
 264 
 265   void*     owner() const;
 266   void      set_owner(void* owner);
 267 
 268   jint      waiters() const;
 269 
 270   jint      count() const;
 271   void      set_count(jint count);
 272   jint      contentions() const;
 273   intptr_t  recursions() const                                         { return _recursions; }
 274 
 275   // JVM/TI GetObjectMonitorUsage() needs this:
 276   ObjectWaiter* first_waiter()                                         { return _WaitSet; }
 277   ObjectWaiter* next_waiter(ObjectWaiter* o)                           { return o->_next; }
 278   Thread* thread_of_waiter(ObjectWaiter* o)                            { return o->_thread; }
 279 
 280  protected:
 281   // We don't typically expect or want the ctors or dtors to run.
 282   // normal ObjectMonitors are type-stable and immortal.
 283   ObjectMonitor() { ::memset((void *)this, 0, sizeof(*this)); }
 284 
 285   ~ObjectMonitor() {
 286     // TODO: Add asserts ...
 287     // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0
 288     // _count == 0 _EntryList  == NULL etc
 289   }
 290 
 291  private:
 292   void Recycle() {
 293     // TODO: add stronger asserts ...
 294     // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0
 295     // _count == 0 EntryList  == NULL
 296     // _recursions == 0 _WaitSet == NULL
 297     assert(((is_busy()|_recursions) == 0), "freeing inuse monitor");
 298     _succ          = NULL;
 299     _EntryList     = NULL;
 300     _cxq           = NULL;
 301     _WaitSet       = NULL;
 302     _recursions    = 0;
 303   }
 304 
 305  public:
 306 
 307   void*     object() const;
 308   void*     object_addr();
 309   void      set_object(void* obj);






 310 
 311   bool      check(TRAPS);       // true if the thread owns the monitor.
 312   void      check_slow(TRAPS);
 313   void      clear();
 314   static void sanity_checks();  // public for -XX:+ExecuteInternalVMTests
 315                                 // in PRODUCT for -XX:SyncKnobs=Verbose=1
 316 
 317   void      enter(TRAPS);
 318   void      exit(bool not_suspended, TRAPS);
 319   void      wait(jlong millis, bool interruptable, TRAPS);
 320   void      notify(TRAPS);
 321   void      notifyAll(TRAPS);
 322 
 323 // Use the following at your own risk
 324   intptr_t  complete_exit(TRAPS);
 325   void      reenter(intptr_t recursions, TRAPS);
 326 
 327  private:
 328   void      AddWaiter(ObjectWaiter * waiter);
 329   static    void DeferredInitialize();
 330   void      INotify(Thread * Self);
 331   ObjectWaiter * DequeueWaiter();
 332   void      DequeueSpecificWaiter(ObjectWaiter * waiter);
 333   void      EnterI(TRAPS);
 334   void      ReenterI(Thread * Self, ObjectWaiter * SelfNode);
 335   void      UnlinkAfterAcquire(Thread * Self, ObjectWaiter * SelfNode);
 336   int       TryLock(Thread * Self);
 337   int       NotRunnable(Thread * Self, Thread * Owner);
 338   int       TrySpin(Thread * Self);
 339   void      ExitEpilog(Thread * Self, ObjectWaiter * Wakee);
 340   bool      ExitSuspendEquivalent(JavaThread * Self);
 341   void      post_monitor_wait_event(EventJavaMonitorWait * event,
 342                                     jlong notifier_tid,
 343                                     jlong timeout,
 344                                     bool timedout);
 345 


 346 };
 347 
 348 #undef TEVENT
 349 #define TEVENT(nom) { if (SyncVerbose) FEVENT(nom); }
 350 
 351 #define FEVENT(nom)                             \
 352   {                                             \
 353     static volatile int ctr = 0;                \
 354     int v = ++ctr;                              \
 355     if ((v & (v - 1)) == 0) {                   \
 356       tty->print_cr("INFO: " #nom " : %d", v);  \
 357       tty->flush();                             \
 358     }                                           \
 359   }
 360 
 361 #undef  TEVENT
 362 #define TEVENT(nom) {;}
 363 
 364 
 365 #endif // SHARE_VM_RUNTIME_OBJECTMONITOR_HPP


 154   volatile intptr_t  _recursions;   // recursion count, 0 for first entry
 155   ObjectWaiter * volatile _EntryList; // Threads blocked on entry or reentry.
 156                                       // The list is actually composed of WaitNodes,
 157                                       // acting as proxies for Threads.
 158  private:
 159   ObjectWaiter * volatile _cxq;     // LL of recently-arrived threads blocked on entry.
 160   Thread * volatile _succ;          // Heir presumptive thread - used for futile wakeup throttling
 161   Thread * volatile _Responsible;
 162 
 163   volatile int _Spinner;            // for exit->spinner handoff optimization
 164   volatile int _SpinDuration;
 165 
 166   volatile jint  _count;            // reference count to prevent reclamation/deflation
 167                                     // at stop-the-world time.  See deflate_idle_monitors().
 168                                     // _count is approximately |_WaitSet| + |_EntryList|
 169  protected:
 170   ObjectWaiter * volatile _WaitSet; // LL of threads wait()ing on the monitor
 171   volatile jint  _waiters;          // number of waiting threads
 172  private:
 173   volatile int _WaitSetLock;        // protects Wait Queue - simple spinlock
 174   typedef enum { Free = 0, New, Old } AllocationState; // Free must be 0 for monitor to be free after memset(..,0,..).
 175   AllocationState _allocation_state;
 176 
 177  public:
 178   static void Initialize();
 179 
 180   // Only perform a PerfData operation if the PerfData object has been
 181   // allocated and if the PerfDataManager has not freed the PerfData
 182   // objects which can happen at normal VM shutdown.
 183   //
 184   #define OM_PERFDATA_OP(f, op_str)              \
 185     do {                                         \
 186       if (ObjectMonitor::_sync_ ## f != NULL &&  \
 187           PerfDataManager::has_PerfData()) {     \
 188         ObjectMonitor::_sync_ ## f->op_str;      \
 189       }                                          \
 190     } while (0)
 191 
 192   static PerfCounter * _sync_ContendedLockAttempts;
 193   static PerfCounter * _sync_FutileWakeups;
 194   static PerfCounter * _sync_Parks;
 195   static PerfCounter * _sync_EmptyNotifications;


 253     ((ObjectMonitor::f ## _offset_in_bytes()) - markOopDesc::monitor_value)
 254 
 255   markOop   header() const;
 256   void      set_header(markOop hdr);
 257 
 258   intptr_t is_busy() const {
 259     // TODO-FIXME: merge _count and _waiters.
 260     // TODO-FIXME: assert _owner == null implies _recursions = 0
 261     // TODO-FIXME: assert _WaitSet != null implies _count > 0
 262     return _count|_waiters|intptr_t(_owner)|intptr_t(_cxq)|intptr_t(_EntryList);
 263   }
 264 
 265   intptr_t  is_entered(Thread* current) const;
 266 
 267   void*     owner() const;
 268   void      set_owner(void* owner);
 269 
 270   jint      waiters() const;
 271 
 272   jint      count() const;

 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
 289     // _count == 0 _EntryList  == NULL etc
 290   }
 291 
 292  private:
 293   void Recycle() {
 294     // TODO: add stronger asserts ...
 295     // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0
 296     // _count == 0 EntryList  == NULL
 297     // _recursions == 0 _WaitSet == NULL
 298     assert(((is_busy()|_recursions) == 0), "freeing inuse monitor");
 299     _succ          = NULL;
 300     _EntryList     = NULL;
 301     _cxq           = NULL;
 302     _WaitSet       = NULL;
 303     _recursions    = 0;
 304   }
 305 
 306  public:
 307 
 308   void*     object() const;
 309   void*     object_addr();
 310   void      set_object(void* obj);
 311   void      set_allocation_state(AllocationState s) { _allocation_state = s; }
 312   AllocationState allocation_state() const { return _allocation_state; }
 313   bool      is_free() const { return _allocation_state == Free; }
 314   bool      is_active() const { return !is_free(); }
 315   bool      is_old() const { return _allocation_state == Old; }
 316   bool      is_new() const { return _allocation_state == New; }
 317 
 318   bool      check(TRAPS);       // true if the thread owns the monitor.
 319   void      check_slow(TRAPS);
 320   void      clear();
 321   static void sanity_checks();  // public for -XX:+ExecuteInternalVMTests
 322                                 // in PRODUCT for -XX:SyncKnobs=Verbose=1
 323 
 324   bool      enter(TRAPS); // returns false if monitor is being deflated and caller should retry locking the object.
 325   void      exit(bool not_suspended, TRAPS);
 326   void      wait(jlong millis, bool interruptable, TRAPS);
 327   void      notify(TRAPS);
 328   void      notifyAll(TRAPS);
 329 
 330 // Use the following at your own risk
 331   intptr_t  complete_exit(TRAPS);
 332   bool      reenter(intptr_t recursions, TRAPS); // returns false if monitor is being deflated and caller should retry locking the object.
 333 
 334  private:
 335   void      AddWaiter(ObjectWaiter * waiter);
 336   static    void DeferredInitialize();
 337   void      INotify(Thread * Self);
 338   ObjectWaiter * DequeueWaiter();
 339   void      DequeueSpecificWaiter(ObjectWaiter * waiter);
 340   void      EnterI(TRAPS);
 341   void      ReenterI(Thread * Self, ObjectWaiter * SelfNode);
 342   void      UnlinkAfterAcquire(Thread * Self, ObjectWaiter * SelfNode);
 343   int       TryLock(Thread * Self);
 344   int       NotRunnable(Thread * Self, Thread * Owner);
 345   int       TrySpin(Thread * Self);
 346   void      ExitEpilog(Thread * Self, ObjectWaiter * Wakee);
 347   bool      ExitSuspendEquivalent(JavaThread * Self);
 348   void      post_monitor_wait_event(EventJavaMonitorWait * event,
 349                                     jlong notifier_tid,
 350                                     jlong timeout,
 351                                     bool timedout);
 352 
 353   bool try_disable_monitor(); // Must be run by java thread in VM mode.
 354   void install_displaced_markword_in_object();
 355 };
 356 
 357 #undef TEVENT
 358 #define TEVENT(nom) { if (SyncVerbose) FEVENT(nom); }
 359 
 360 #define FEVENT(nom)                             \
 361   {                                             \
 362     static volatile int ctr = 0;                \
 363     int v = ++ctr;                              \
 364     if ((v & (v - 1)) == 0) {                   \
 365       tty->print_cr("INFO: " #nom " : %d", v);  \
 366       tty->flush();                             \
 367     }                                           \
 368   }
 369 
 370 #undef  TEVENT
 371 #define TEVENT(nom) {;}
 372 
 373 
 374 #endif // SHARE_VM_RUNTIME_OBJECTMONITOR_HPP
< prev index next >