src/share/vm/runtime/mutex.hpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8047290 Sdiff src/share/vm/runtime

src/share/vm/runtime/mutex.hpp

Print this page




 137   debug_only(int _rank;)                 // rank (to avoid/detect potential deadlocks)
 138   debug_only(Monitor * _next;)           // Used by a Thread to link up owned locks
 139   debug_only(Thread* _last_owner;)       // the last thread to own the lock
 140   debug_only(static bool contains(Monitor * locks, Monitor * lock);)
 141   debug_only(static Monitor * get_least_ranked_lock(Monitor * locks);)
 142   debug_only(Monitor * get_least_ranked_lock_besides_this(Monitor * locks);)
 143 #endif
 144 
 145   void set_owner_implementation(Thread* owner)                        PRODUCT_RETURN;
 146   void check_prelock_state     (Thread* thread)                       PRODUCT_RETURN;
 147   void check_block_state       (Thread* thread)                       PRODUCT_RETURN;
 148 
 149   // platform-dependent support code can go here (in os_<os_family>.cpp)
 150  public:
 151   enum {
 152     _no_safepoint_check_flag    = true,
 153     _allow_vm_block_flag        = true,
 154     _as_suspend_equivalent_flag = true
 155   };
 156 


















 157   enum WaitResults {
 158     CONDVAR_EVENT,         // Wait returned because of condition variable notification
 159     INTERRUPT_EVENT,       // Wait returned because waiting thread was interrupted
 160     NUMBER_WAIT_RESULTS
 161   };
 162 
 163  private:
 164    int  TrySpin (Thread * Self) ;
 165    int  TryLock () ;
 166    int  TryFast () ;
 167    int  AcquireOrPush (ParkEvent * ev) ;
 168    void IUnlock (bool RelaxAssert) ;
 169    void ILock (Thread * Self) ;
 170    int  IWait (Thread * Self, jlong timo);
 171    int  ILocked () ;
 172 
 173  protected:
 174    static void ClearMonitor (Monitor * m, const char* name = NULL) ;
 175    Monitor() ;
 176 
 177  public:
 178   Monitor(int rank, const char *name, bool allow_vm_block=false);

 179   ~Monitor();
 180 
 181   // Wait until monitor is notified (or times out).
 182   // Defaults are to make safepoint checks, wait time is forever (i.e.,
 183   // zero), and not a suspend-equivalent condition. Returns true if wait
 184   // times out; otherwise returns false.
 185   bool wait(bool no_safepoint_check = !_no_safepoint_check_flag,
 186             long timeout = 0,
 187             bool as_suspend_equivalent = !_as_suspend_equivalent_flag);
 188   bool notify();
 189   bool notify_all();
 190 
 191 
 192   void lock(); // prints out warning if VM thread blocks
 193   void lock(Thread *thread); // overloaded with current thread
 194   void unlock();
 195   bool is_locked() const                     { return _owner != NULL; }
 196 
 197   bool try_lock(); // Like lock(), but unblocking. It returns false instead
 198 


 244 // visibility of wait(), notify(), and notify_all().
 245 //
 246 // Another viable alternative would have been to have Monitor extend Mutex and
 247 // implement all the normal mutex and wait()-notify() logic in Mutex base class.
 248 // The wait()-notify() facility would be exposed via special protected member functions
 249 // (e.g., _Wait() and _Notify()) in Mutex.  Monitor would extend Mutex and expose wait()
 250 // as a call to _Wait().  That is, the public wait() would be a wrapper for the protected
 251 // _Wait().
 252 //
 253 // An even better alternative is to simply eliminate Mutex:: and use Monitor:: instead.
 254 // After all, monitors are sufficient for Java-level synchronization.   At one point in time
 255 // there may have been some benefit to having distinct mutexes and monitors, but that time
 256 // has past.
 257 //
 258 // The Mutex/Monitor design parallels that of Java-monitors, being based on
 259 // thread-specific park-unpark platform-specific primitives.
 260 
 261 
 262 class Mutex : public Monitor {      // degenerate Monitor
 263  public:
 264    Mutex (int rank, const char *name, bool allow_vm_block=false);

 265    ~Mutex () ;
 266  private:
 267    bool notify ()    { ShouldNotReachHere(); return false; }
 268    bool notify_all() { ShouldNotReachHere(); return false; }
 269    bool wait (bool no_safepoint_check, long timeout, bool as_suspend_equivalent) {
 270      ShouldNotReachHere() ;
 271      return false ;
 272    }
 273 };
 274 
 275 
 276 #endif // SHARE_VM_RUNTIME_MUTEX_HPP


 137   debug_only(int _rank;)                 // rank (to avoid/detect potential deadlocks)
 138   debug_only(Monitor * _next;)           // Used by a Thread to link up owned locks
 139   debug_only(Thread* _last_owner;)       // the last thread to own the lock
 140   debug_only(static bool contains(Monitor * locks, Monitor * lock);)
 141   debug_only(static Monitor * get_least_ranked_lock(Monitor * locks);)
 142   debug_only(Monitor * get_least_ranked_lock_besides_this(Monitor * locks);)
 143 #endif
 144 
 145   void set_owner_implementation(Thread* owner)                        PRODUCT_RETURN;
 146   void check_prelock_state     (Thread* thread)                       PRODUCT_RETURN;
 147   void check_block_state       (Thread* thread)                       PRODUCT_RETURN;
 148 
 149   // platform-dependent support code can go here (in os_<os_family>.cpp)
 150  public:
 151   enum {
 152     _no_safepoint_check_flag    = true,
 153     _allow_vm_block_flag        = true,
 154     _as_suspend_equivalent_flag = true
 155   };
 156 
 157  // Locks can be acquired with or without safepoint check.
 158  // MutexLockerEx checks these flags when acquiring a lock
 159  // to ensure consistent checking for each lock.
 160  // A few existing locks will sometimes have a safepoint check and
 161  // sometimes not, but these locks are set up in such a way to avoid deadlocks.
 162   enum SafepointCheckRequired {
 163     _safepoint_check_never,       // Monitors with this value will cause errors
 164                                   // when acquired with a safepoint check.
 165     _safepoint_check_sometimes,   // Certain locks are called sometimes with and
 166                                   // sometimes without safepoint checks. These
 167                                   // locks will not produce errors when acquired
 168                                   // by a MutexLockerEx.
 169     _safepoint_check_always       // Causes error if acquired by MutexLockerEx
 170                                   // without safepoint check.
 171         };
 172 
 173  NOT_PRODUCT(SafepointCheckRequired _safepoint_check_required;)
 174 
 175   enum WaitResults {
 176     CONDVAR_EVENT,         // Wait returned because of condition variable notification
 177     INTERRUPT_EVENT,       // Wait returned because waiting thread was interrupted
 178     NUMBER_WAIT_RESULTS
 179   };
 180 
 181  private:
 182    int  TrySpin (Thread * Self) ;
 183    int  TryLock () ;
 184    int  TryFast () ;
 185    int  AcquireOrPush (ParkEvent * ev) ;
 186    void IUnlock (bool RelaxAssert) ;
 187    void ILock (Thread * Self) ;
 188    int  IWait (Thread * Self, jlong timo);
 189    int  ILocked () ;
 190 
 191  protected:
 192    static void ClearMonitor (Monitor * m, const char* name = NULL) ;
 193    Monitor() ;
 194 
 195  public:
 196   Monitor(int rank, const char *name, bool allow_vm_block=false,
 197           SafepointCheckRequired safepoint_check_required= _safepoint_check_always); 
 198   ~Monitor();
 199 
 200   // Wait until monitor is notified (or times out).
 201   // Defaults are to make safepoint checks, wait time is forever (i.e.,
 202   // zero), and not a suspend-equivalent condition. Returns true if wait
 203   // times out; otherwise returns false.
 204   bool wait(bool no_safepoint_check = !_no_safepoint_check_flag,
 205             long timeout = 0,
 206             bool as_suspend_equivalent = !_as_suspend_equivalent_flag);
 207   bool notify();
 208   bool notify_all();
 209 
 210 
 211   void lock(); // prints out warning if VM thread blocks
 212   void lock(Thread *thread); // overloaded with current thread
 213   void unlock();
 214   bool is_locked() const                     { return _owner != NULL; }
 215 
 216   bool try_lock(); // Like lock(), but unblocking. It returns false instead
 217 


 263 // visibility of wait(), notify(), and notify_all().
 264 //
 265 // Another viable alternative would have been to have Monitor extend Mutex and
 266 // implement all the normal mutex and wait()-notify() logic in Mutex base class.
 267 // The wait()-notify() facility would be exposed via special protected member functions
 268 // (e.g., _Wait() and _Notify()) in Mutex.  Monitor would extend Mutex and expose wait()
 269 // as a call to _Wait().  That is, the public wait() would be a wrapper for the protected
 270 // _Wait().
 271 //
 272 // An even better alternative is to simply eliminate Mutex:: and use Monitor:: instead.
 273 // After all, monitors are sufficient for Java-level synchronization.   At one point in time
 274 // there may have been some benefit to having distinct mutexes and monitors, but that time
 275 // has past.
 276 //
 277 // The Mutex/Monitor design parallels that of Java-monitors, being based on
 278 // thread-specific park-unpark platform-specific primitives.
 279 
 280 
 281 class Mutex : public Monitor {      // degenerate Monitor
 282  public:
 283    Mutex (int rank, const char *name, bool allow_vm_block=false,
 284           SafepointCheckRequired safepoint_check_required = _safepoint_check_always);
 285    ~Mutex () ;
 286  private:
 287    bool notify ()    { ShouldNotReachHere(); return false; }
 288    bool notify_all() { ShouldNotReachHere(); return false; }
 289    bool wait (bool no_safepoint_check, long timeout, bool as_suspend_equivalent) {
 290      ShouldNotReachHere() ;
 291      return false ;
 292    }
 293 };
 294 
 295 
 296 #endif // SHARE_VM_RUNTIME_MUTEX_HPP
src/share/vm/runtime/mutex.hpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File