< prev index next >

src/share/vm/runtime/mutex.hpp

Print this page
rev 12096 : 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
Reviewed-by:

  64 union SplitWord {   // full-word with separately addressable LSB
  65   volatile intptr_t FullWord ;
  66   volatile void * Address ;
  67   volatile jbyte Bytes [sizeof(intptr_t)] ;
  68 } ;
  69 
  70 // Endian-ness ... index of least-significant byte in SplitWord.Bytes[]
  71 #ifdef VM_LITTLE_ENDIAN
  72  #define _LSBINDEX 0
  73 #else
  74  #define _LSBINDEX (sizeof(intptr_t)-1)
  75 #endif
  76 
  77 class ParkEvent ;
  78 
  79 // See orderAccess.hpp.  We assume throughout the VM that mutex lock and
  80 // try_lock do fence-lock-acquire, and that unlock does a release-unlock,
  81 // *in that order*.  If their implementations change such that these
  82 // assumptions are violated, a whole lot of code will break.
  83 
  84 // The default length of monitor name is chosen to be 64 to avoid false sharing.
  85 static const int MONITOR_NAME_LEN = 64;
  86 
  87 class Monitor : public CHeapObj<mtInternal> {
  88 
  89  public:
  90   // A special lock: Is a lock where you are guaranteed not to block while you are
  91   // holding it, i.e., no vm operation can happen, taking other locks, etc.
  92   // NOTE: It is critical that the rank 'special' be the lowest (earliest)
  93   // (except for "event"?) for the deadlock detection to work correctly.
  94   // The rank native is only for use in Mutex's created by JVM_RawMonitorCreate,
  95   // which being external to the VM are not subject to deadlock detection.
  96   // The rank safepoint is used only for synchronization in reaching a
  97   // safepoint and leaving a safepoint.  It is only used for the Safepoint_lock
  98   // currently.  While at a safepoint no mutexes of rank safepoint are held
  99   // by any thread.
 100   // The rank named "leaf" is probably historical (and should
 101   // be changed) -- mutexes of this rank aren't really leaf mutexes
 102   // at all.
 103   enum lock_types {
 104        event,
 105        special,
 106        suspend_resume,
 107        leaf        = suspend_resume +   2,
 108        safepoint   = leaf           +  10,


 112        native      = max_nonleaf    +   1
 113   };
 114 
 115   // The WaitSet and EntryList linked lists are composed of ParkEvents.
 116   // I use ParkEvent instead of threads as ParkEvents are immortal and
 117   // type-stable, meaning we can safely unpark() a possibly stale
 118   // list element in the unlock()-path.
 119 
 120  protected:                              // Monitor-Mutex metadata
 121   SplitWord _LockWord ;                  // Contention queue (cxq) colocated with Lock-byte
 122   enum LockWordBits { _LBIT=1 } ;
 123   Thread * volatile _owner;              // The owner of the lock
 124                                          // Consider sequestering _owner on its own $line
 125                                          // to aid future synchronization mechanisms.
 126   ParkEvent * volatile _EntryList ;      // List of threads waiting for entry
 127   ParkEvent * volatile _OnDeck ;         // heir-presumptive
 128   volatile intptr_t _WaitLock [1] ;      // Protects _WaitSet
 129   ParkEvent * volatile  _WaitSet ;       // LL of ParkEvents
 130   volatile bool     _snuck;              // Used for sneaky locking (evil).
 131   int NotifyCount ;                      // diagnostic assist
 132   char _name[MONITOR_NAME_LEN];          // Name of mutex
 133 
 134   // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode)
 135 #ifndef PRODUCT
 136   bool      _allow_vm_block;
 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   // Monitor::lock and Monitor::lock_without_safepoint_check
 159   // checks these flags when acquiring a lock to ensure
 160   // consistent checking for each lock.
 161   // A few existing locks will sometimes have a safepoint check and
 162   // sometimes not, but these locks are set up in such a way to avoid deadlocks.
 163   enum SafepointCheckRequired {
 164     _safepoint_check_never,       // Monitors with this value will cause errors
 165                                   // when acquired with a safepoint check.
 166     _safepoint_check_sometimes,   // Certain locks are called sometimes with and
 167                                   // sometimes without safepoint checks. These
 168                                   // locks will not produce errors when locked.
 169     _safepoint_check_always       // Causes error if locked without a safepoint
 170                                   // 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 



  64 union SplitWord {   // full-word with separately addressable LSB
  65   volatile intptr_t FullWord ;
  66   volatile void * Address ;
  67   volatile jbyte Bytes [sizeof(intptr_t)] ;
  68 } ;
  69 
  70 // Endian-ness ... index of least-significant byte in SplitWord.Bytes[]
  71 #ifdef VM_LITTLE_ENDIAN
  72  #define _LSBINDEX 0
  73 #else
  74  #define _LSBINDEX (sizeof(intptr_t)-1)
  75 #endif
  76 
  77 class ParkEvent ;
  78 
  79 // See orderAccess.hpp.  We assume throughout the VM that mutex lock and
  80 // try_lock do fence-lock-acquire, and that unlock does a release-unlock,
  81 // *in that order*.  If their implementations change such that these
  82 // assumptions are violated, a whole lot of code will break.
  83 
  84 class MonitorBase : public CHeapObj<mtInternal> {




  85  public:
  86   // A special lock: Is a lock where you are guaranteed not to block while you are
  87   // holding it, i.e., no vm operation can happen, taking other locks, etc.
  88   // NOTE: It is critical that the rank 'special' be the lowest (earliest)
  89   // (except for "event"?) for the deadlock detection to work correctly.
  90   // The rank native is only for use in Mutex's created by JVM_RawMonitorCreate,
  91   // which being external to the VM are not subject to deadlock detection.
  92   // The rank safepoint is used only for synchronization in reaching a
  93   // safepoint and leaving a safepoint.  It is only used for the Safepoint_lock
  94   // currently.  While at a safepoint no mutexes of rank safepoint are held
  95   // by any thread.
  96   // The rank named "leaf" is probably historical (and should
  97   // be changed) -- mutexes of this rank aren't really leaf mutexes
  98   // at all.
  99   enum lock_types {
 100        event,
 101        special,
 102        suspend_resume,
 103        leaf        = suspend_resume +   2,
 104        safepoint   = leaf           +  10,


 108        native      = max_nonleaf    +   1
 109   };
 110 
 111   // The WaitSet and EntryList linked lists are composed of ParkEvents.
 112   // I use ParkEvent instead of threads as ParkEvents are immortal and
 113   // type-stable, meaning we can safely unpark() a possibly stale
 114   // list element in the unlock()-path.
 115 
 116  protected:                              // Monitor-Mutex metadata
 117   SplitWord _LockWord ;                  // Contention queue (cxq) colocated with Lock-byte
 118   enum LockWordBits { _LBIT=1 } ;
 119   Thread * volatile _owner;              // The owner of the lock
 120                                          // Consider sequestering _owner on its own $line
 121                                          // to aid future synchronization mechanisms.
 122   ParkEvent * volatile _EntryList ;      // List of threads waiting for entry
 123   ParkEvent * volatile _OnDeck ;         // heir-presumptive
 124   volatile intptr_t _WaitLock [1] ;      // Protects _WaitSet
 125   ParkEvent * volatile  _WaitSet ;       // LL of ParkEvents
 126   volatile bool     _snuck;              // Used for sneaky locking (evil).
 127   int NotifyCount ;                      // diagnostic assist

 128 
 129   // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode)
 130 #ifndef PRODUCT
 131   bool      _allow_vm_block;
 132   debug_only(int _rank;)                 // rank (to avoid/detect potential deadlocks)
 133   debug_only(Monitor * _next;)           // Used by a Thread to link up owned locks
 134   debug_only(Thread* _last_owner;)       // the last thread to own the lock



 135 #endif
 136 




 137  // platform-dependent support code can go here (in os_<os_family>.cpp)
 138  public:
 139   enum {
 140     _no_safepoint_check_flag    = true,
 141     _allow_vm_block_flag        = true,
 142     _as_suspend_equivalent_flag = true
 143   };
 144 
 145   // Locks can be acquired with or without safepoint check.
 146   // Monitor::lock and Monitor::lock_without_safepoint_check
 147   // checks these flags when acquiring a lock to ensure
 148   // consistent checking for each lock.
 149   // A few existing locks will sometimes have a safepoint check and
 150   // sometimes not, but these locks are set up in such a way to avoid deadlocks.
 151   enum SafepointCheckRequired {
 152     _safepoint_check_never,       // Monitors with this value will cause errors
 153                                   // when acquired with a safepoint check.
 154     _safepoint_check_sometimes,   // Certain locks are called sometimes with and
 155                                   // sometimes without safepoint checks. These
 156                                   // locks will not produce errors when locked.
 157     _safepoint_check_always       // Causes error if locked without a safepoint
 158                                   // check.
 159   };
 160 
 161   NOT_PRODUCT(SafepointCheckRequired _safepoint_check_required;)
 162 
 163   enum WaitResults {
 164     CONDVAR_EVENT,         // Wait returned because of condition variable notification
 165     INTERRUPT_EVENT,       // Wait returned because waiting thread was interrupted
 166     NUMBER_WAIT_RESULTS
 167   };
 168 };
 169 
 170 class Monitor : public MonitorBase {
 171  protected:
 172   // The default length of monitor name is chosen to avoid false sharing.
 173   enum {
 174     CACHE_LINE_PADDING = DEFAULT_CACHE_LINE_SIZE - sizeof(MonitorBase),
 175     MONITOR_NAME_LEN = CACHE_LINE_PADDING > 64 ? CACHE_LINE_PADDING : 64
 176   };
 177   char _name[MONITOR_NAME_LEN];          // Name of mutex
 178   // Other fields should be declared in MonitorBase.
 179 
 180  public:
 181 #ifndef PRODUCT
 182   debug_only(static bool contains(Monitor * locks, Monitor * lock);)
 183   debug_only(static Monitor * get_least_ranked_lock(Monitor * locks);)
 184   debug_only(Monitor * get_least_ranked_lock_besides_this(Monitor * locks);)
 185 #endif
 186 
 187   void set_owner_implementation(Thread* owner)                        PRODUCT_RETURN;
 188   void check_prelock_state     (Thread* thread)                       PRODUCT_RETURN;
 189   void check_block_state       (Thread* thread)                       PRODUCT_RETURN;
 190 
 191  private:
 192    int  TrySpin (Thread * Self) ;
 193    int  TryLock () ;
 194    int  TryFast () ;
 195    int  AcquireOrPush (ParkEvent * ev) ;
 196    void IUnlock (bool RelaxAssert) ;
 197    void ILock (Thread * Self) ;
 198    int  IWait (Thread * Self, jlong timo);
 199    int  ILocked () ;
 200 
 201  protected:
 202    static void ClearMonitor (Monitor * m, const char* name = NULL) ;
 203    Monitor() ;
 204 
 205  public:
 206   Monitor(int rank, const char *name, bool allow_vm_block = false,
 207           SafepointCheckRequired safepoint_check_required = _safepoint_check_always);
 208   ~Monitor();
 209 


< prev index next >