56 }; |
57 }; |
57 |
58 |
58 // forward declaration to avoid include tracing.hpp |
59 // forward declaration to avoid include tracing.hpp |
59 class EventJavaMonitorWait; |
60 class EventJavaMonitorWait; |
60 |
61 |
61 // WARNING: |
62 // The ObjectMonitor class implements the heavyweight version of a |
62 // This is a very sensitive and fragile class. DO NOT make any |
63 // JavaMonitor. The lightweight BasicLock/stack lock version has been |
63 // change unless you are fully aware of the underlying semantics. |
64 // inflated into an ObjectMonitor. This inflation is typically due to |
64 |
65 // contention or use of Object.wait(). |
65 // This class can not inherit from any other class, because I have |
66 // |
66 // to let the displaced header be the very first word. Otherwise I |
67 // WARNING: This is a very sensitive and fragile class. DO NOT make any |
67 // have to let markOop include this file, which would export the |
68 // changes unless you are fully aware of the underlying semantics. |
68 // monitor data structure to everywhere. |
69 // |
69 // |
70 // Class JvmtiRawMonitor currently inherits from ObjectMonitor so |
70 // The ObjectMonitor class is used to implement JavaMonitors which have |
71 // changes in this class must be careful to not break JvmtiRawMonitor. |
71 // transformed from the lightweight structure of the thread stack to a |
72 // These two subsystems should be separated. |
72 // heavy weight lock due to contention |
73 // |
73 |
74 // ObjectMonitor Layout Overview/Highlights/Restrictions: |
74 // It is also used as RawMonitor by the JVMTI |
75 // |
75 |
76 // - The _header field must be at offset 0 because the displaced header |
|
77 // from markOop is stored there. We do not want markOop.hpp to include |
|
78 // ObjectMonitor.hpp to avoid exposing ObjectMonitor everywhere. This |
|
79 // means that ObjectMonitor cannot inherit from any other class nor can |
|
80 // it use any virtual member functions. This restriction is critical to |
|
81 // the proper functioning of the VM. |
|
82 // - The _header and _owner fields should be separated by enough space |
|
83 // to avoid false sharing due to parallel access by different threads. |
|
84 // This is an advisory recommendation. |
|
85 // - The general layout of the fields in ObjectMonitor is: |
|
86 // _header |
|
87 // <lightly_used_fields> |
|
88 // <optional padding> |
|
89 // _owner |
|
90 // <remaining_fields> |
|
91 // - The VM assumes write ordering and machine word alignment with |
|
92 // respect to the _owner field and the <remaining_fields> that can |
|
93 // be read in parallel by other threads. |
|
94 // - Generally fields that are accessed closely together in time should |
|
95 // be placed proximally in space to promote data cache locality. That |
|
96 // is, temporal locality should condition spatial locality. |
|
97 // - We have to balance avoiding false sharing with excessive invalidation |
|
98 // from coherence traffic. As such, we try to cluster fields that tend |
|
99 // to be _written_ at approximately the same time onto the same data |
|
100 // cache line. |
|
101 // - We also have to balance the natural tension between minimizing |
|
102 // single threaded capacity misses with excessive multi-threaded |
|
103 // coherency misses. There is no single optimal layout for both |
|
104 // single-threaded and multi-threaded environments. |
|
105 // |
|
106 // - See ObjectMonitor::sanity_checks() for how critical restrictions are |
|
107 // enforced and advisory recommendations are reported. |
|
108 // - Adjacent ObjectMonitors should be separated by enough space to avoid |
|
109 // false sharing. This is handled by the ObjectMonitor allocation code |
|
110 // in synchronizer.cpp. Also see ObjectSynchronizer::sanity_checks(). |
|
111 // |
|
112 // Futures notes: |
|
113 // - Separating _owner from the <remaining_fields> by enough space to |
|
114 // avoid false sharing might be profitable. Given |
|
115 // http://blogs.oracle.com/dave/entry/cas_and_cache_trivia_invalidate |
|
116 // we know that the CAS in monitorenter will invalidate the line |
|
117 // underlying _owner. We want to avoid an L1 data cache miss on that |
|
118 // same line for monitorexit. Putting these <remaining_fields>: |
|
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. |
76 |
127 |
77 class ObjectMonitor { |
128 class ObjectMonitor { |
78 public: |
129 public: |
79 enum { |
130 enum { |
80 OM_OK, // no error |
131 OM_OK, // no error |
82 OM_ILLEGAL_MONITOR_STATE, // IllegalMonitorStateException |
133 OM_ILLEGAL_MONITOR_STATE, // IllegalMonitorStateException |
83 OM_INTERRUPTED, // Thread.interrupt() |
134 OM_INTERRUPTED, // Thread.interrupt() |
84 OM_TIMED_OUT // Object.wait() timed out |
135 OM_TIMED_OUT // Object.wait() timed out |
85 }; |
136 }; |
86 |
137 |
87 public: |
|
88 // TODO-FIXME: the "offset" routines should return a type of off_t instead of int ... |
|
89 // ByteSize would also be an appropriate type. |
|
90 static int header_offset_in_bytes() { return offset_of(ObjectMonitor, _header); } |
|
91 static int object_offset_in_bytes() { return offset_of(ObjectMonitor, _object); } |
|
92 static int owner_offset_in_bytes() { return offset_of(ObjectMonitor, _owner); } |
|
93 static int count_offset_in_bytes() { return offset_of(ObjectMonitor, _count); } |
|
94 static int recursions_offset_in_bytes() { return offset_of(ObjectMonitor, _recursions); } |
|
95 static int cxq_offset_in_bytes() { return offset_of(ObjectMonitor, _cxq); } |
|
96 static int succ_offset_in_bytes() { return offset_of(ObjectMonitor, _succ); } |
|
97 static int EntryList_offset_in_bytes() { return offset_of(ObjectMonitor, _EntryList); } |
|
98 static int FreeNext_offset_in_bytes() { return offset_of(ObjectMonitor, FreeNext); } |
|
99 static int WaitSet_offset_in_bytes() { return offset_of(ObjectMonitor, _WaitSet); } |
|
100 static int Responsible_offset_in_bytes() { return offset_of(ObjectMonitor, _Responsible); } |
|
101 static int Spinner_offset_in_bytes() { return offset_of(ObjectMonitor, _Spinner); } |
|
102 |
|
103 public: |
|
104 // Eventually we'll make provisions for multiple callbacks, but |
|
105 // now one will suffice. |
|
106 static int (*SpinCallbackFunction)(intptr_t, int); |
|
107 static intptr_t SpinCallbackArgument; |
|
108 |
|
109 |
|
110 public: |
|
111 markOop header() const; |
|
112 void set_header(markOop hdr); |
|
113 |
|
114 intptr_t is_busy() const { |
|
115 // TODO-FIXME: merge _count and _waiters. |
|
116 // TODO-FIXME: assert _owner == null implies _recursions = 0 |
|
117 // TODO-FIXME: assert _WaitSet != null implies _count > 0 |
|
118 return _count|_waiters|intptr_t(_owner)|intptr_t(_cxq)|intptr_t(_EntryList); |
|
119 } |
|
120 |
|
121 intptr_t is_entered(Thread* current) const; |
|
122 |
|
123 void* owner() const; |
|
124 void set_owner(void* owner); |
|
125 |
|
126 intptr_t waiters() const; |
|
127 |
|
128 intptr_t count() const; |
|
129 void set_count(intptr_t count); |
|
130 intptr_t contentions() const; |
|
131 intptr_t recursions() const { return _recursions; } |
|
132 |
|
133 // JVM/DI GetMonitorInfo() needs this |
|
134 ObjectWaiter* first_waiter() { return _WaitSet; } |
|
135 ObjectWaiter* next_waiter(ObjectWaiter* o) { return o->_next; } |
|
136 Thread* thread_of_waiter(ObjectWaiter* o) { return o->_thread; } |
|
137 |
|
138 // initialize the monitor, exception the semaphore, all other fields |
|
139 // are simple integers or pointers |
|
140 ObjectMonitor() { |
|
141 _header = NULL; |
|
142 _count = 0; |
|
143 _waiters = 0; |
|
144 _recursions = 0; |
|
145 _object = NULL; |
|
146 _owner = NULL; |
|
147 _WaitSet = NULL; |
|
148 _WaitSetLock = 0; |
|
149 _Responsible = NULL; |
|
150 _succ = NULL; |
|
151 _cxq = NULL; |
|
152 FreeNext = NULL; |
|
153 _EntryList = NULL; |
|
154 _SpinFreq = 0; |
|
155 _SpinClock = 0; |
|
156 OwnerIsThread = 0; |
|
157 _previous_owner_tid = 0; |
|
158 } |
|
159 |
|
160 ~ObjectMonitor() { |
|
161 // TODO: Add asserts ... |
|
162 // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0 |
|
163 // _count == 0 _EntryList == NULL etc |
|
164 } |
|
165 |
|
166 private: |
|
167 void Recycle() { |
|
168 // TODO: add stronger asserts ... |
|
169 // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0 |
|
170 // _count == 0 EntryList == NULL |
|
171 // _recursions == 0 _WaitSet == NULL |
|
172 // TODO: assert (is_busy()|_recursions) == 0 |
|
173 _succ = NULL; |
|
174 _EntryList = NULL; |
|
175 _cxq = NULL; |
|
176 _WaitSet = NULL; |
|
177 _recursions = 0; |
|
178 _SpinFreq = 0; |
|
179 _SpinClock = 0; |
|
180 OwnerIsThread = 0; |
|
181 } |
|
182 |
|
183 public: |
|
184 |
|
185 void* object() const; |
|
186 void* object_addr(); |
|
187 void set_object(void* obj); |
|
188 |
|
189 bool check(TRAPS); // true if the thread owns the monitor. |
|
190 void check_slow(TRAPS); |
|
191 void clear(); |
|
192 static void sanity_checks(); // public for -XX:+ExecuteInternalVMTests |
|
193 // in PRODUCT for -XX:SyncKnobs=Verbose=1 |
|
194 #ifndef PRODUCT |
|
195 void verify(); |
|
196 void print(); |
|
197 #endif |
|
198 |
|
199 bool try_enter(TRAPS); |
|
200 void enter(TRAPS); |
|
201 void exit(bool not_suspended, TRAPS); |
|
202 void wait(jlong millis, bool interruptable, TRAPS); |
|
203 void notify(TRAPS); |
|
204 void notifyAll(TRAPS); |
|
205 |
|
206 // Use the following at your own risk |
|
207 intptr_t complete_exit(TRAPS); |
|
208 void reenter(intptr_t recursions, TRAPS); |
|
209 |
|
210 private: |
|
211 void AddWaiter(ObjectWaiter * waiter); |
|
212 static void DeferredInitialize(); |
|
213 |
|
214 ObjectWaiter * DequeueWaiter(); |
|
215 void DequeueSpecificWaiter(ObjectWaiter * waiter); |
|
216 void EnterI(TRAPS); |
|
217 void ReenterI(Thread * Self, ObjectWaiter * SelfNode); |
|
218 void UnlinkAfterAcquire(Thread * Self, ObjectWaiter * SelfNode); |
|
219 int TryLock(Thread * Self); |
|
220 int NotRunnable(Thread * Self, Thread * Owner); |
|
221 int TrySpin_Fixed(Thread * Self); |
|
222 int TrySpin_VaryFrequency(Thread * Self); |
|
223 int TrySpin_VaryDuration(Thread * Self); |
|
224 void ctAsserts(); |
|
225 void ExitEpilog(Thread * Self, ObjectWaiter * Wakee); |
|
226 bool ExitSuspendEquivalent(JavaThread * Self); |
|
227 void post_monitor_wait_event(EventJavaMonitorWait * event, |
|
228 jlong notifier_tid, |
|
229 jlong timeout, |
|
230 bool timedout); |
|
231 |
|
232 private: |
138 private: |
233 friend class ObjectSynchronizer; |
139 friend class ObjectSynchronizer; |
234 friend class ObjectWaiter; |
140 friend class ObjectWaiter; |
235 friend class VMStructs; |
141 friend class VMStructs; |
236 |
142 |
237 // WARNING: this must be the very first word of ObjectMonitor |
|
238 // This means this class can't use any virtual member functions. |
|
239 |
|
240 volatile markOop _header; // displaced object header word - mark |
143 volatile markOop _header; // displaced object header word - mark |
241 void* volatile _object; // backward object pointer - strong root |
144 void* volatile _object; // backward object pointer - strong root |
242 |
145 public: |
243 double SharingPad[1]; // temp to reduce false sharing |
146 ObjectMonitor * FreeNext; // Free list linkage |
244 |
147 private: |
245 // All the following fields must be machine word aligned |
148 DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, |
246 // The VM assumes write ordering wrt these fields, which can be |
149 sizeof(volatile markOop) + sizeof(void * volatile) + |
247 // read from other threads. |
150 sizeof(ObjectMonitor *)); |
248 |
151 protected: // protected for JvmtiRawMonitor |
249 protected: // protected for jvmtiRawMonitor |
|
250 void * volatile _owner; // pointer to owning thread OR BasicLock |
152 void * volatile _owner; // pointer to owning thread OR BasicLock |
251 volatile jlong _previous_owner_tid; // thread id of the previous owner of the monitor |
153 volatile jlong _previous_owner_tid; // thread id of the previous owner of the monitor |
252 volatile intptr_t _recursions; // recursion count, 0 for first entry |
154 volatile intptr_t _recursions; // recursion count, 0 for first entry |
253 private: |
155 ObjectWaiter * volatile _EntryList; // Threads blocked on entry or reentry. |
254 int OwnerIsThread; // _owner is (Thread *) vs SP/BasicLock |
156 // The list is actually composed of WaitNodes, |
|
157 // acting as proxies for Threads. |
|
158 private: |
255 ObjectWaiter * volatile _cxq; // LL of recently-arrived threads blocked on entry. |
159 ObjectWaiter * volatile _cxq; // LL of recently-arrived threads blocked on entry. |
256 // The list is actually composed of WaitNodes, acting |
|
257 // as proxies for Threads. |
|
258 protected: |
|
259 ObjectWaiter * volatile _EntryList; // Threads blocked on entry or reentry. |
|
260 private: |
|
261 Thread * volatile _succ; // Heir presumptive thread - used for futile wakeup throttling |
160 Thread * volatile _succ; // Heir presumptive thread - used for futile wakeup throttling |
262 Thread * volatile _Responsible; |
161 Thread * volatile _Responsible; |
263 int _PromptDrain; // rqst to drain cxq into EntryList ASAP |
|
264 |
162 |
265 volatile int _Spinner; // for exit->spinner handoff optimization |
163 volatile int _Spinner; // for exit->spinner handoff optimization |
266 volatile int _SpinFreq; // Spin 1-out-of-N attempts: success rate |
164 volatile int _SpinFreq; // Spin 1-out-of-N attempts: success rate |
267 volatile int _SpinClock; |
165 volatile int _SpinClock; |
|
166 volatile intptr_t _SpinState; // MCS/CLH list of spinners |
268 volatile int _SpinDuration; |
167 volatile int _SpinDuration; |
269 volatile intptr_t _SpinState; // MCS/CLH list of spinners |
168 |
270 |
169 volatile jint _count; // reference count to prevent reclamation/deflation |
271 // TODO-FIXME: _count, _waiters and _recursions should be of |
|
272 // type int, or int32_t but not intptr_t. There's no reason |
|
273 // to use 64-bit fields for these variables on a 64-bit JVM. |
|
274 |
|
275 volatile intptr_t _count; // reference count to prevent reclamation/deflation |
|
276 // at stop-the-world time. See deflate_idle_monitors(). |
170 // at stop-the-world time. See deflate_idle_monitors(). |
277 // _count is approximately |_WaitSet| + |_EntryList| |
171 // _count is approximately |_WaitSet| + |_EntryList| |
278 protected: |
172 protected: |
279 volatile intptr_t _waiters; // number of waiting threads |
|
280 private: |
|
281 protected: |
|
282 ObjectWaiter * volatile _WaitSet; // LL of threads wait()ing on the monitor |
173 ObjectWaiter * volatile _WaitSet; // LL of threads wait()ing on the monitor |
|
174 volatile jint _waiters; // number of waiting threads |
283 private: |
175 private: |
284 volatile int _WaitSetLock; // protects Wait Queue - simple spinlock |
176 volatile int _WaitSetLock; // protects Wait Queue - simple spinlock |
285 |
|
286 public: |
|
287 int _QMix; // Mixed prepend queue discipline |
|
288 ObjectMonitor * FreeNext; // Free list linkage |
|
289 intptr_t StatA, StatsB; |
|
290 |
177 |
291 public: |
178 public: |
292 static void Initialize(); |
179 static void Initialize(); |
293 static PerfCounter * _sync_ContendedLockAttempts; |
180 static PerfCounter * _sync_ContendedLockAttempts; |
294 static PerfCounter * _sync_FutileWakeups; |
181 static PerfCounter * _sync_FutileWakeups; |
323 FreeHeap(p, mtInternal); |
210 FreeHeap(p, mtInternal); |
324 } |
211 } |
325 void operator delete[] (void *p) { |
212 void operator delete[] (void *p) { |
326 operator delete(p); |
213 operator delete(p); |
327 } |
214 } |
|
215 |
|
216 // TODO-FIXME: the "offset" routines should return a type of off_t instead of int ... |
|
217 // ByteSize would also be an appropriate type. |
|
218 static int header_offset_in_bytes() { return offset_of(ObjectMonitor, _header); } |
|
219 static int object_offset_in_bytes() { return offset_of(ObjectMonitor, _object); } |
|
220 static int owner_offset_in_bytes() { return offset_of(ObjectMonitor, _owner); } |
|
221 static int count_offset_in_bytes() { return offset_of(ObjectMonitor, _count); } |
|
222 static int recursions_offset_in_bytes() { return offset_of(ObjectMonitor, _recursions); } |
|
223 static int cxq_offset_in_bytes() { return offset_of(ObjectMonitor, _cxq); } |
|
224 static int succ_offset_in_bytes() { return offset_of(ObjectMonitor, _succ); } |
|
225 static int EntryList_offset_in_bytes() { return offset_of(ObjectMonitor, _EntryList); } |
|
226 static int FreeNext_offset_in_bytes() { return offset_of(ObjectMonitor, FreeNext); } |
|
227 static int WaitSet_offset_in_bytes() { return offset_of(ObjectMonitor, _WaitSet); } |
|
228 static int Responsible_offset_in_bytes() { return offset_of(ObjectMonitor, _Responsible); } |
|
229 static int Spinner_offset_in_bytes() { return offset_of(ObjectMonitor, _Spinner); } |
|
230 |
|
231 // Eventually we'll make provisions for multiple callbacks, but |
|
232 // now one will suffice. |
|
233 static int (*SpinCallbackFunction)(intptr_t, int); |
|
234 static intptr_t SpinCallbackArgument; |
|
235 |
|
236 markOop header() const; |
|
237 void set_header(markOop hdr); |
|
238 |
|
239 intptr_t is_busy() const { |
|
240 // TODO-FIXME: merge _count and _waiters. |
|
241 // TODO-FIXME: assert _owner == null implies _recursions = 0 |
|
242 // TODO-FIXME: assert _WaitSet != null implies _count > 0 |
|
243 return _count|_waiters|intptr_t(_owner)|intptr_t(_cxq)|intptr_t(_EntryList); |
|
244 } |
|
245 |
|
246 intptr_t is_entered(Thread* current) const; |
|
247 |
|
248 void* owner() const; |
|
249 void set_owner(void* owner); |
|
250 |
|
251 jint waiters() const; |
|
252 |
|
253 jint count() const; |
|
254 void set_count(jint count); |
|
255 jint contentions() const; |
|
256 intptr_t recursions() const { return _recursions; } |
|
257 |
|
258 // JVM/TI GetObjectMonitorUsage() needs this: |
|
259 ObjectWaiter* first_waiter() { return _WaitSet; } |
|
260 ObjectWaiter* next_waiter(ObjectWaiter* o) { return o->_next; } |
|
261 Thread* thread_of_waiter(ObjectWaiter* o) { return o->_thread; } |
|
262 |
|
263 protected: |
|
264 // We don't typically expect or want the ctors or dtors to run. |
|
265 // normal ObjectMonitors are type-stable and immortal. |
|
266 ObjectMonitor() { ::memset((void *)this, 0, sizeof(*this)); } |
|
267 |
|
268 ~ObjectMonitor() { |
|
269 // TODO: Add asserts ... |
|
270 // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0 |
|
271 // _count == 0 _EntryList == NULL etc |
|
272 } |
|
273 |
|
274 private: |
|
275 void Recycle() { |
|
276 // TODO: add stronger asserts ... |
|
277 // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0 |
|
278 // _count == 0 EntryList == NULL |
|
279 // _recursions == 0 _WaitSet == NULL |
|
280 assert(((is_busy()|_recursions) == 0), "freeing inuse monitor"); |
|
281 _succ = NULL; |
|
282 _EntryList = NULL; |
|
283 _cxq = NULL; |
|
284 _WaitSet = NULL; |
|
285 _recursions = 0; |
|
286 _SpinFreq = 0; |
|
287 _SpinClock = 0; |
|
288 } |
|
289 |
|
290 public: |
|
291 |
|
292 void* object() const; |
|
293 void* object_addr(); |
|
294 void set_object(void* obj); |
|
295 |
|
296 bool check(TRAPS); // true if the thread owns the monitor. |
|
297 void check_slow(TRAPS); |
|
298 void clear(); |
|
299 static void sanity_checks(); // public for -XX:+ExecuteInternalVMTests |
|
300 // in PRODUCT for -XX:SyncKnobs=Verbose=1 |
|
301 #ifndef PRODUCT |
|
302 void verify(); |
|
303 void print(); |
|
304 #endif |
|
305 |
|
306 bool try_enter(TRAPS); |
|
307 void enter(TRAPS); |
|
308 void exit(bool not_suspended, TRAPS); |
|
309 void wait(jlong millis, bool interruptable, TRAPS); |
|
310 void notify(TRAPS); |
|
311 void notifyAll(TRAPS); |
|
312 |
|
313 // Use the following at your own risk |
|
314 intptr_t complete_exit(TRAPS); |
|
315 void reenter(intptr_t recursions, TRAPS); |
|
316 |
|
317 private: |
|
318 void AddWaiter(ObjectWaiter * waiter); |
|
319 static void DeferredInitialize(); |
|
320 |
|
321 ObjectWaiter * DequeueWaiter(); |
|
322 void DequeueSpecificWaiter(ObjectWaiter * waiter); |
|
323 void EnterI(TRAPS); |
|
324 void ReenterI(Thread * Self, ObjectWaiter * SelfNode); |
|
325 void UnlinkAfterAcquire(Thread * Self, ObjectWaiter * SelfNode); |
|
326 int TryLock(Thread * Self); |
|
327 int NotRunnable(Thread * Self, Thread * Owner); |
|
328 int TrySpin_Fixed(Thread * Self); |
|
329 int TrySpin_VaryFrequency(Thread * Self); |
|
330 int TrySpin_VaryDuration(Thread * Self); |
|
331 void ExitEpilog(Thread * Self, ObjectWaiter * Wakee); |
|
332 bool ExitSuspendEquivalent(JavaThread * Self); |
|
333 void post_monitor_wait_event(EventJavaMonitorWait * event, |
|
334 jlong notifier_tid, |
|
335 jlong timeout, |
|
336 bool timedout); |
|
337 |
328 }; |
338 }; |
329 |
339 |
330 #undef TEVENT |
340 #undef TEVENT |
331 #define TEVENT(nom) { if (SyncVerbose) FEVENT(nom); } |
341 #define TEVENT(nom) { if (SyncVerbose) FEVENT(nom); } |
332 |
342 |