< prev index next >

src/hotspot/share/runtime/objectMonitor.hpp

Print this page

127 #endif
128 
129 class ObjectMonitor : public CHeapObj<mtObjectMonitor> {
130   friend class ObjectSynchronizer;
131   friend class ObjectWaiter;
132   friend class VMStructs;
133   JVMCI_ONLY(friend class JVMCIVMStructs;)
134 
135   static OopStorage* _oop_storage;
136 
137   // The sync code expects the header field to be at offset zero (0).
138   // Enforced by the assert() in header_addr().
139   volatile markWord _header;        // displaced object header word - mark
140   WeakHandle _object;               // backward object pointer
141   // Separate _header and _owner on different cache lines since both can
142   // have busy multi-threaded access. _header and _object are set at initial
143   // inflation. The _object does not change, so it is a good choice to share
144   // its cache line with _header.
145   DEFINE_PAD_MINUS_SIZE(0, OM_CACHE_LINE_SIZE, sizeof(volatile markWord) +
146                         sizeof(WeakHandle));
147   // Used by async deflation as a marker in the _owner field:
148   #define DEFLATER_MARKER reinterpret_cast<void*>(-1)








149   void* volatile _owner;            // pointer to owning thread OR BasicLock
150   volatile uint64_t _previous_owner_tid;  // thread id of the previous owner of the monitor
151   // Separate _owner and _next_om on different cache lines since
152   // both can have busy multi-threaded access. _previous_owner_tid is only
153   // changed by ObjectMonitor::exit() so it is a good choice to share the
154   // cache line with _owner.
155   DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(void* volatile) +
156                         sizeof(volatile uint64_t));
157   ObjectMonitor* _next_om;          // Next ObjectMonitor* linkage
158   volatile intx _recursions;        // recursion count, 0 for first entry
159   ObjectWaiter* volatile _EntryList;  // Threads blocked on entry or reentry.
160                                       // The list is actually composed of WaitNodes,
161                                       // acting as proxies for Threads.
162 
163   ObjectWaiter* volatile _cxq;      // LL of recently-arrived threads blocked on entry.
164   JavaThread* volatile _succ;       // Heir presumptive thread - used for futile wakeup throttling
165   JavaThread* volatile _Responsible;
166 
167   volatile int _Spinner;            // for exit->spinner handoff optimization
168   volatile int _SpinDuration;

187   #define OM_PERFDATA_OP(f, op_str)                 \
188     do {                                            \
189       if (ObjectMonitor::_sync_ ## f != nullptr &&  \
190           PerfDataManager::has_PerfData()) {        \
191         ObjectMonitor::_sync_ ## f->op_str;         \
192       }                                             \
193     } while (0)
194 
195   static PerfCounter * _sync_ContendedLockAttempts;
196   static PerfCounter * _sync_FutileWakeups;
197   static PerfCounter * _sync_Parks;
198   static PerfCounter * _sync_Notifications;
199   static PerfCounter * _sync_Inflations;
200   static PerfCounter * _sync_Deflations;
201   static PerfLongVariable * _sync_MonExtant;
202 
203   static int Knob_SpinLimit;
204 
205   // TODO-FIXME: the "offset" routines should return a type of off_t instead of int ...
206   // ByteSize would also be an appropriate type.

207   static int owner_offset_in_bytes()       { return offset_of(ObjectMonitor, _owner); }
208   static int recursions_offset_in_bytes()  { return offset_of(ObjectMonitor, _recursions); }
209   static int cxq_offset_in_bytes()         { return offset_of(ObjectMonitor, _cxq); }
210   static int succ_offset_in_bytes()        { return offset_of(ObjectMonitor, _succ); }
211   static int EntryList_offset_in_bytes()   { return offset_of(ObjectMonitor, _EntryList); }
212 
213   // ObjectMonitor references can be ORed with markWord::monitor_value
214   // as part of the ObjectMonitor tagging mechanism. When we combine an
215   // ObjectMonitor reference with an offset, we need to remove the tag
216   // value in order to generate the proper address.
217   //
218   // We can either adjust the ObjectMonitor reference and then add the
219   // offset or we can adjust the offset that is added to the ObjectMonitor
220   // reference. The latter avoids an AGI (Address Generation Interlock)
221   // stall so the helper macro adjusts the offset value that is returned
222   // to the ObjectMonitor reference manipulation code:
223   //
224   #define OM_OFFSET_NO_MONITOR_VALUE_TAG(f) \
225     ((ObjectMonitor::f ## _offset_in_bytes()) - markWord::monitor_value)
226 

246 
247   // Returns true if this OM has an owner, false otherwise.
248   bool      has_owner() const;
249   void*     owner() const;  // Returns null if DEFLATER_MARKER is observed.
250   void*     owner_raw() const;
251   // Returns true if owner field == DEFLATER_MARKER and false otherwise.
252   bool      owner_is_DEFLATER_MARKER() const;
253   // Returns true if 'this' is being async deflated and false otherwise.
254   bool      is_being_async_deflated();
255   // Clear _owner field; current value must match old_value.
256   void      release_clear_owner(void* old_value);
257   // Simply set _owner field to new_value; current value must match old_value.
258   void      set_owner_from(void* old_value, void* new_value);
259   // Simply set _owner field to current; current value must match basic_lock_p.
260   void      set_owner_from_BasicLock(void* basic_lock_p, JavaThread* current);
261   // Try to set _owner field to new_value if the current value matches
262   // old_value, using Atomic::cmpxchg(). Otherwise, does not change the
263   // _owner field. Returns the prior value of the _owner field.
264   void*     try_set_owner_from(void* old_value, void* new_value);
265 












266   // Simply get _next_om field.
267   ObjectMonitor* next_om() const;
268   // Simply set _next_om field to new_value.
269   void set_next_om(ObjectMonitor* new_value);
270 
271   int       waiters() const;
272 
273   int       contentions() const;
274   void      add_to_contentions(int value);
275   intx      recursions() const                                         { return _recursions; }
276 
277   // JVM/TI GetObjectMonitorUsage() needs this:
278   ObjectWaiter* first_waiter()                                         { return _WaitSet; }
279   ObjectWaiter* next_waiter(ObjectWaiter* o)                           { return o->_next; }
280   JavaThread* thread_of_waiter(ObjectWaiter* o)                        { return o->_thread; }
281 
282   ObjectMonitor(oop object);
283   ~ObjectMonitor();
284 
285   oop       object() const;

306     ClearSuccOnSuspend(ObjectMonitor* om) : _om(om)  {}
307     void operator()(JavaThread* current);
308   };
309  public:
310   bool      enter(JavaThread* current);
311   void      exit(JavaThread* current, bool not_suspended = true);
312   void      wait(jlong millis, bool interruptible, TRAPS);
313   void      notify(TRAPS);
314   void      notifyAll(TRAPS);
315 
316   void      print() const;
317 #ifdef ASSERT
318   void      print_debug_style_on(outputStream* st) const;
319 #endif
320   void      print_on(outputStream* st) const;
321 
322   // Use the following at your own risk
323   intx      complete_exit(JavaThread* current);
324   bool      reenter(intx recursions, JavaThread* current);
325 


326  private:
327   void      AddWaiter(ObjectWaiter* waiter);
328   void      INotify(JavaThread* current);
329   ObjectWaiter* DequeueWaiter();
330   void      DequeueSpecificWaiter(ObjectWaiter* waiter);
331   void      EnterI(JavaThread* current);
332   void      ReenterI(JavaThread* current, ObjectWaiter* current_node);
333   void      UnlinkAfterAcquire(JavaThread* current, ObjectWaiter* current_node);
334   int       TryLock(JavaThread* current);
335   int       NotRunnable(JavaThread* current, JavaThread* Owner);
336   int       TrySpin(JavaThread* current);
337   void      ExitEpilog(JavaThread* current, ObjectWaiter* Wakee);
338 
339   // Deflation support
340   bool      deflate_monitor();
341   void      install_displaced_markword_in_object(const oop obj);
342 };
343 
344 #endif // SHARE_RUNTIME_OBJECTMONITOR_HPP

127 #endif
128 
129 class ObjectMonitor : public CHeapObj<mtObjectMonitor> {
130   friend class ObjectSynchronizer;
131   friend class ObjectWaiter;
132   friend class VMStructs;
133   JVMCI_ONLY(friend class JVMCIVMStructs;)
134 
135   static OopStorage* _oop_storage;
136 
137   // The sync code expects the header field to be at offset zero (0).
138   // Enforced by the assert() in header_addr().
139   volatile markWord _header;        // displaced object header word - mark
140   WeakHandle _object;               // backward object pointer
141   // Separate _header and _owner on different cache lines since both can
142   // have busy multi-threaded access. _header and _object are set at initial
143   // inflation. The _object does not change, so it is a good choice to share
144   // its cache line with _header.
145   DEFINE_PAD_MINUS_SIZE(0, OM_CACHE_LINE_SIZE, sizeof(volatile markWord) +
146                         sizeof(WeakHandle));
147   // Used by async deflation as a marker in the _owner field.
148   // Note that the choice of the two markers is peculiar:
149   // - They need to represent values that cannot be pointers. In particular,
150   //   we achieve this by using the lowest two bits
151   // - ANONYMOUS_OWNER should be a small value, it is used in generated code
152   //   and small values encode much better
153   // - We test for anonymous owner by testing for the lowest bit, therefore
154   //   DEFLATER_MARKER must *not* have that bit set.
155   #define DEFLATER_MARKER reinterpret_cast<void*>(2)
156   #define ANONYMOUS_OWNER reinterpret_cast<void*>(1)
157   void* volatile _owner;            // pointer to owning thread OR BasicLock
158   volatile uint64_t _previous_owner_tid;  // thread id of the previous owner of the monitor
159   // Separate _owner and _next_om on different cache lines since
160   // both can have busy multi-threaded access. _previous_owner_tid is only
161   // changed by ObjectMonitor::exit() so it is a good choice to share the
162   // cache line with _owner.
163   DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(void* volatile) +
164                         sizeof(volatile uint64_t));
165   ObjectMonitor* _next_om;          // Next ObjectMonitor* linkage
166   volatile intx _recursions;        // recursion count, 0 for first entry
167   ObjectWaiter* volatile _EntryList;  // Threads blocked on entry or reentry.
168                                       // The list is actually composed of WaitNodes,
169                                       // acting as proxies for Threads.
170 
171   ObjectWaiter* volatile _cxq;      // LL of recently-arrived threads blocked on entry.
172   JavaThread* volatile _succ;       // Heir presumptive thread - used for futile wakeup throttling
173   JavaThread* volatile _Responsible;
174 
175   volatile int _Spinner;            // for exit->spinner handoff optimization
176   volatile int _SpinDuration;

195   #define OM_PERFDATA_OP(f, op_str)                 \
196     do {                                            \
197       if (ObjectMonitor::_sync_ ## f != nullptr &&  \
198           PerfDataManager::has_PerfData()) {        \
199         ObjectMonitor::_sync_ ## f->op_str;         \
200       }                                             \
201     } while (0)
202 
203   static PerfCounter * _sync_ContendedLockAttempts;
204   static PerfCounter * _sync_FutileWakeups;
205   static PerfCounter * _sync_Parks;
206   static PerfCounter * _sync_Notifications;
207   static PerfCounter * _sync_Inflations;
208   static PerfCounter * _sync_Deflations;
209   static PerfLongVariable * _sync_MonExtant;
210 
211   static int Knob_SpinLimit;
212 
213   // TODO-FIXME: the "offset" routines should return a type of off_t instead of int ...
214   // ByteSize would also be an appropriate type.
215   static int header_offset_in_bytes()      { return offset_of(ObjectMonitor, _header); }
216   static int owner_offset_in_bytes()       { return offset_of(ObjectMonitor, _owner); }
217   static int recursions_offset_in_bytes()  { return offset_of(ObjectMonitor, _recursions); }
218   static int cxq_offset_in_bytes()         { return offset_of(ObjectMonitor, _cxq); }
219   static int succ_offset_in_bytes()        { return offset_of(ObjectMonitor, _succ); }
220   static int EntryList_offset_in_bytes()   { return offset_of(ObjectMonitor, _EntryList); }
221 
222   // ObjectMonitor references can be ORed with markWord::monitor_value
223   // as part of the ObjectMonitor tagging mechanism. When we combine an
224   // ObjectMonitor reference with an offset, we need to remove the tag
225   // value in order to generate the proper address.
226   //
227   // We can either adjust the ObjectMonitor reference and then add the
228   // offset or we can adjust the offset that is added to the ObjectMonitor
229   // reference. The latter avoids an AGI (Address Generation Interlock)
230   // stall so the helper macro adjusts the offset value that is returned
231   // to the ObjectMonitor reference manipulation code:
232   //
233   #define OM_OFFSET_NO_MONITOR_VALUE_TAG(f) \
234     ((ObjectMonitor::f ## _offset_in_bytes()) - markWord::monitor_value)
235 

255 
256   // Returns true if this OM has an owner, false otherwise.
257   bool      has_owner() const;
258   void*     owner() const;  // Returns null if DEFLATER_MARKER is observed.
259   void*     owner_raw() const;
260   // Returns true if owner field == DEFLATER_MARKER and false otherwise.
261   bool      owner_is_DEFLATER_MARKER() const;
262   // Returns true if 'this' is being async deflated and false otherwise.
263   bool      is_being_async_deflated();
264   // Clear _owner field; current value must match old_value.
265   void      release_clear_owner(void* old_value);
266   // Simply set _owner field to new_value; current value must match old_value.
267   void      set_owner_from(void* old_value, void* new_value);
268   // Simply set _owner field to current; current value must match basic_lock_p.
269   void      set_owner_from_BasicLock(void* basic_lock_p, JavaThread* current);
270   // Try to set _owner field to new_value if the current value matches
271   // old_value, using Atomic::cmpxchg(). Otherwise, does not change the
272   // _owner field. Returns the prior value of the _owner field.
273   void*     try_set_owner_from(void* old_value, void* new_value);
274 
275   void set_owner_anonymous() {
276     set_owner_from(NULL, ANONYMOUS_OWNER);
277   }
278 
279   bool is_owner_anonymous() const {
280     return owner_raw() == ANONYMOUS_OWNER;
281   }
282 
283   void set_owner_from_anonymous(Thread* owner) {
284     set_owner_from(ANONYMOUS_OWNER, owner);
285   }
286 
287   // Simply get _next_om field.
288   ObjectMonitor* next_om() const;
289   // Simply set _next_om field to new_value.
290   void set_next_om(ObjectMonitor* new_value);
291 
292   int       waiters() const;
293 
294   int       contentions() const;
295   void      add_to_contentions(int value);
296   intx      recursions() const                                         { return _recursions; }
297 
298   // JVM/TI GetObjectMonitorUsage() needs this:
299   ObjectWaiter* first_waiter()                                         { return _WaitSet; }
300   ObjectWaiter* next_waiter(ObjectWaiter* o)                           { return o->_next; }
301   JavaThread* thread_of_waiter(ObjectWaiter* o)                        { return o->_thread; }
302 
303   ObjectMonitor(oop object);
304   ~ObjectMonitor();
305 
306   oop       object() const;

327     ClearSuccOnSuspend(ObjectMonitor* om) : _om(om)  {}
328     void operator()(JavaThread* current);
329   };
330  public:
331   bool      enter(JavaThread* current);
332   void      exit(JavaThread* current, bool not_suspended = true);
333   void      wait(jlong millis, bool interruptible, TRAPS);
334   void      notify(TRAPS);
335   void      notifyAll(TRAPS);
336 
337   void      print() const;
338 #ifdef ASSERT
339   void      print_debug_style_on(outputStream* st) const;
340 #endif
341   void      print_on(outputStream* st) const;
342 
343   // Use the following at your own risk
344   intx      complete_exit(JavaThread* current);
345   bool      reenter(intx recursions, JavaThread* current);
346 
347   static void maybe_deflate_dead(oop* p);
348 
349  private:
350   void      AddWaiter(ObjectWaiter* waiter);
351   void      INotify(JavaThread* current);
352   ObjectWaiter* DequeueWaiter();
353   void      DequeueSpecificWaiter(ObjectWaiter* waiter);
354   void      EnterI(JavaThread* current);
355   void      ReenterI(JavaThread* current, ObjectWaiter* current_node);
356   void      UnlinkAfterAcquire(JavaThread* current, ObjectWaiter* current_node);
357   int       TryLock(JavaThread* current);
358   int       NotRunnable(JavaThread* current, JavaThread* Owner);
359   int       TrySpin(JavaThread* current);
360   void      ExitEpilog(JavaThread* current, ObjectWaiter* Wakee);
361 
362   // Deflation support
363   bool      deflate_monitor();
364   void      install_displaced_markword_in_object(const oop obj);
365 };
366 
367 #endif // SHARE_RUNTIME_OBJECTMONITOR_HPP
< prev index next >