27 |
27 |
28 #include "memory/allocation.hpp" |
28 #include "memory/allocation.hpp" |
29 #include "runtime/os.hpp" |
29 #include "runtime/os.hpp" |
30 #include "utilities/histogram.hpp" |
30 #include "utilities/histogram.hpp" |
31 |
31 |
32 // The SplitWord construct allows us to colocate the contention queue |
32 |
33 // (cxq) with the lock-byte. The queue elements are ParkEvents, which are |
33 // A Mutex/Monitor is a simple wrapper around a native lock plus condition |
34 // always aligned on 256-byte addresses - the least significant byte of |
34 // variable that supports lock ownership tracking, lock ranking for deadlock |
35 // a ParkEvent is always 0. Colocating the lock-byte with the queue |
35 // detection and coordinates with the safepoint protocol. |
36 // allows us to easily avoid what would otherwise be a race in lock() |
|
37 // if we were to use two completely separate fields for the contention queue |
|
38 // and the lock indicator. Specifically, colocation renders us immune |
|
39 // from the race where a thread might enqueue itself in the lock() slow-path |
|
40 // immediately after the lock holder drops the outer lock in the unlock() |
|
41 // fast-path. |
|
42 // |
|
43 // Colocation allows us to use a fast-path unlock() form that uses |
|
44 // A MEMBAR instead of a CAS. MEMBAR has lower local latency than CAS |
|
45 // on many platforms. |
|
46 // |
|
47 // See: |
|
48 // + http://blogs.sun.com/dave/entry/biased_locking_in_hotspot |
|
49 // + http://blogs.sun.com/dave/resource/synchronization-public2.pdf |
|
50 // |
|
51 // Note that we're *not* using word-tearing the classic sense. |
|
52 // The lock() fast-path will CAS the lockword and the unlock() |
|
53 // fast-path will store into the lock-byte colocated within the lockword. |
|
54 // We depend on the fact that all our reference platforms have |
|
55 // coherent and atomic byte accesses. More precisely, byte stores |
|
56 // interoperate in a safe, sane, and expected manner with respect to |
|
57 // CAS, ST and LDs to the full-word containing the byte. |
|
58 // If you're porting HotSpot to a platform where that isn't the case |
|
59 // then you'll want change the unlock() fast path from: |
|
60 // STB;MEMBAR #storeload; LDN |
|
61 // to a full-word CAS of the lockword. |
|
62 |
|
63 |
|
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 class ParkEvent ; |
|
71 |
|
72 // See orderAccess.hpp. We assume throughout the VM that mutex lock and |
|
73 // try_lock do fence-lock-acquire, and that unlock does a release-unlock, |
|
74 // *in that order*. If their implementations change such that these |
|
75 // assumptions are violated, a whole lot of code will break. |
|
76 |
36 |
77 // The default length of monitor name was originally chosen to be 64 to avoid |
37 // The default length of monitor name was originally chosen to be 64 to avoid |
78 // false sharing. Now, PaddedMonitor is available for this purpose. |
38 // false sharing. Now, PaddedMonitor is available for this purpose. |
79 // TODO: Check if _name[MONITOR_NAME_LEN] should better get replaced by const char*. |
39 // TODO: Check if _name[MONITOR_NAME_LEN] should better get replaced by const char*. |
80 static const int MONITOR_NAME_LEN = 64; |
40 static const int MONITOR_NAME_LEN = 64; |
116 nonleaf = barrier + 1, |
76 nonleaf = barrier + 1, |
117 max_nonleaf = nonleaf + 900, |
77 max_nonleaf = nonleaf + 900, |
118 native = max_nonleaf + 1 |
78 native = max_nonleaf + 1 |
119 }; |
79 }; |
120 |
80 |
121 // The WaitSet and EntryList linked lists are composed of ParkEvents. |
|
122 // I use ParkEvent instead of threads as ParkEvents are immortal and |
|
123 // type-stable, meaning we can safely unpark() a possibly stale |
|
124 // list element in the unlock()-path. |
|
125 |
|
126 protected: // Monitor-Mutex metadata |
81 protected: // Monitor-Mutex metadata |
127 SplitWord _LockWord ; // Contention queue (cxq) colocated with Lock-byte |
|
128 Thread * volatile _owner; // The owner of the lock |
82 Thread * volatile _owner; // The owner of the lock |
129 // Consider sequestering _owner on its own $line |
83 os::PlatformMonitor _lock; // Native monitor implementation |
130 // to aid future synchronization mechanisms. |
84 char _name[MONITOR_NAME_LEN]; // Name of mutex/monitor |
131 ParkEvent * volatile _EntryList ; // List of threads waiting for entry |
|
132 ParkEvent * volatile _OnDeck ; // heir-presumptive |
|
133 volatile intptr_t _WaitLock [1] ; // Protects _WaitSet |
|
134 ParkEvent * volatile _WaitSet ; // LL of ParkEvents |
|
135 volatile bool _snuck; // Used for sneaky locking (evil). |
|
136 char _name[MONITOR_NAME_LEN]; // Name of mutex |
|
137 |
85 |
138 // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode) |
86 // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode) |
139 #ifndef PRODUCT |
87 #ifndef PRODUCT |
140 bool _allow_vm_block; |
88 bool _allow_vm_block; |
141 DEBUG_ONLY(int _rank;) // rank (to avoid/detect potential deadlocks) |
89 DEBUG_ONLY(int _rank;) // rank (to avoid/detect potential deadlocks) |
147 #endif |
95 #endif |
148 |
96 |
149 void set_owner_implementation(Thread* owner) PRODUCT_RETURN; |
97 void set_owner_implementation(Thread* owner) PRODUCT_RETURN; |
150 void check_prelock_state (Thread* thread, bool safepoint_check) PRODUCT_RETURN; |
98 void check_prelock_state (Thread* thread, bool safepoint_check) PRODUCT_RETURN; |
151 void check_block_state (Thread* thread) PRODUCT_RETURN; |
99 void check_block_state (Thread* thread) PRODUCT_RETURN; |
152 |
100 void assert_owner (Thread* expected) NOT_DEBUG_RETURN; |
153 // platform-dependent support code can go here (in os_<os_family>.cpp) |
101 |
154 public: |
102 public: |
155 enum { |
103 enum { |
156 _no_safepoint_check_flag = true, |
104 _no_safepoint_check_flag = true, |
157 _allow_vm_block_flag = true, |
105 _allow_vm_block_flag = true, |
158 _as_suspend_equivalent_flag = true |
106 _as_suspend_equivalent_flag = true |
162 // Monitor::lock and Monitor::lock_without_safepoint_check |
110 // Monitor::lock and Monitor::lock_without_safepoint_check |
163 // checks these flags when acquiring a lock to ensure |
111 // checks these flags when acquiring a lock to ensure |
164 // consistent checking for each lock. |
112 // consistent checking for each lock. |
165 // A few existing locks will sometimes have a safepoint check and |
113 // A few existing locks will sometimes have a safepoint check and |
166 // sometimes not, but these locks are set up in such a way to avoid deadlocks. |
114 // sometimes not, but these locks are set up in such a way to avoid deadlocks. |
|
115 // Note: monitors that may be shared between JavaThreads and the VMThread |
|
116 // should never encounter a safepoint check whilst they are held, else a |
|
117 // deadlock with the VMThread can occur. |
167 enum SafepointCheckRequired { |
118 enum SafepointCheckRequired { |
168 _safepoint_check_never, // Monitors with this value will cause errors |
119 _safepoint_check_never, // Monitors with this value will cause errors |
169 // when acquired with a safepoint check. |
120 // when acquired with a safepoint check. |
170 _safepoint_check_sometimes, // Certain locks are called sometimes with and |
121 _safepoint_check_sometimes, // Certain locks are called sometimes with and |
171 // sometimes without safepoint checks. These |
122 // sometimes without safepoint checks. These |
174 // check. |
125 // check. |
175 }; |
126 }; |
176 |
127 |
177 NOT_PRODUCT(SafepointCheckRequired _safepoint_check_required;) |
128 NOT_PRODUCT(SafepointCheckRequired _safepoint_check_required;) |
178 |
129 |
179 enum WaitResults { |
|
180 CONDVAR_EVENT, // Wait returned because of condition variable notification |
|
181 INTERRUPT_EVENT, // Wait returned because waiting thread was interrupted |
|
182 NUMBER_WAIT_RESULTS |
|
183 }; |
|
184 |
|
185 private: |
|
186 int TrySpin (Thread * Self) ; |
|
187 int TryLock () ; |
|
188 int TryFast () ; |
|
189 int AcquireOrPush (ParkEvent * ev) ; |
|
190 void IUnlock (bool RelaxAssert) ; |
|
191 void ILock (Thread * Self) ; |
|
192 int IWait (Thread * Self, jlong timo); |
|
193 int ILocked () ; |
|
194 |
|
195 protected: |
130 protected: |
196 static void ClearMonitor (Monitor * m, const char* name = NULL) ; |
131 static void ClearMonitor (Monitor * m, const char* name = NULL) ; |
197 Monitor() ; |
132 Monitor() ; |
198 |
133 |
199 public: |
134 public: |
206 // zero), and not a suspend-equivalent condition. Returns true if wait |
141 // zero), and not a suspend-equivalent condition. Returns true if wait |
207 // times out; otherwise returns false. |
142 // times out; otherwise returns false. |
208 bool wait(bool no_safepoint_check = !_no_safepoint_check_flag, |
143 bool wait(bool no_safepoint_check = !_no_safepoint_check_flag, |
209 long timeout = 0, |
144 long timeout = 0, |
210 bool as_suspend_equivalent = !_as_suspend_equivalent_flag); |
145 bool as_suspend_equivalent = !_as_suspend_equivalent_flag); |
211 bool notify(); |
146 void notify(); |
212 bool notify_all(); |
147 void notify_all(); |
213 |
148 |
214 |
149 |
215 void lock(); // prints out warning if VM thread blocks |
150 void lock(); // prints out warning if VM thread blocks |
216 void lock(Thread *thread); // overloaded with current thread |
151 void lock(Thread *thread); // overloaded with current thread |
217 void unlock(); |
152 void unlock(); |
218 bool is_locked() const { return _owner != NULL; } |
153 bool is_locked() const { return _owner != NULL; } |
219 |
154 |
220 bool try_lock(); // Like lock(), but unblocking. It returns false instead |
155 bool try_lock(); // Like lock(), but unblocking. It returns false instead |
|
156 |
|
157 void release_for_safepoint(); |
221 |
158 |
222 // Lock without safepoint check. Should ONLY be used by safepoint code and other code |
159 // Lock without safepoint check. Should ONLY be used by safepoint code and other code |
223 // that is guaranteed not to block while running inside the VM. |
160 // that is guaranteed not to block while running inside the VM. |
224 void lock_without_safepoint_check(); |
161 void lock_without_safepoint_check(); |
225 void lock_without_safepoint_check (Thread * Self) ; |
162 void lock_without_safepoint_check (Thread * Self) ; |
288 // An even better alternative is to simply eliminate Mutex:: and use Monitor:: instead. |
225 // An even better alternative is to simply eliminate Mutex:: and use Monitor:: instead. |
289 // After all, monitors are sufficient for Java-level synchronization. At one point in time |
226 // After all, monitors are sufficient for Java-level synchronization. At one point in time |
290 // there may have been some benefit to having distinct mutexes and monitors, but that time |
227 // there may have been some benefit to having distinct mutexes and monitors, but that time |
291 // has past. |
228 // has past. |
292 // |
229 // |
293 // The Mutex/Monitor design parallels that of Java-monitors, being based on |
|
294 // thread-specific park-unpark platform-specific primitives. |
|
295 |
|
296 |
230 |
297 class Mutex : public Monitor { // degenerate Monitor |
231 class Mutex : public Monitor { // degenerate Monitor |
298 public: |
232 public: |
299 Mutex(int rank, const char *name, bool allow_vm_block = false, |
233 Mutex(int rank, const char *name, bool allow_vm_block = false, |
300 SafepointCheckRequired safepoint_check_required = _safepoint_check_always); |
234 SafepointCheckRequired safepoint_check_required = _safepoint_check_always); |
301 // default destructor |
235 // default destructor |
302 private: |
236 private: |
303 bool notify () { ShouldNotReachHere(); return false; } |
237 void notify () { ShouldNotReachHere(); } |
304 bool notify_all() { ShouldNotReachHere(); return false; } |
238 void notify_all() { ShouldNotReachHere(); } |
305 bool wait (bool no_safepoint_check, long timeout, bool as_suspend_equivalent) { |
239 bool wait (bool no_safepoint_check, long timeout, bool as_suspend_equivalent) { |
306 ShouldNotReachHere() ; |
240 ShouldNotReachHere() ; |
307 return false ; |
241 return false ; |
308 } |
242 } |
309 }; |
243 }; |