< prev index next >

src/hotspot/share/runtime/objectMonitor.inline.hpp

Print this page
rev 59077 : 8153224.v2.09b.patch combined with 8153224.v2.10.patch; merge with jdk-15+21.
rev 59078 : eosterlund v2.10 CR: reorganize deflate_monitor_using_JT() to use "early exit" style; dcubed - clarify/fix/rearrange a few comments in deflate_monitor_using_JT(); eosterlund v2.10 CR: simplify install_displaced_markword_in_object() and save_om_ptr(); save_om_ptr()'s call to install_displaced_markword_in_object() can race with the deflater thread's clearing of the object field so handle that; fold 8153224.OMHandle_experiment into 8153224.v2.11.patch; merge with jdk-15+21.


  35   return 0;
  36 }
  37 
  38 inline markWord ObjectMonitor::header() const {
  39   return Atomic::load(&_header);
  40 }
  41 
  42 inline volatile markWord* ObjectMonitor::header_addr() {
  43   assert((intptr_t)this == (intptr_t)&_header, "sync code expects this");
  44   return &_header;
  45 }
  46 
  47 inline void ObjectMonitor::set_header(markWord hdr) {
  48   Atomic::store(&_header, hdr);
  49 }
  50 
  51 inline jint ObjectMonitor::waiters() const {
  52   return _waiters;
  53 }
  54 

  55 inline void* ObjectMonitor::owner() const {
  56   return _owner;













  57 }
  58 
  59 inline void ObjectMonitor::clear() {
  60   assert(Atomic::load(&_header).value() != 0, "must be non-zero");
  61   assert(_contentions == 0, "must be 0: contentions=%d", _contentions);























  62   assert(_waiters == 0, "must be 0: waiters=%d", _waiters);
  63   assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
  64   assert(_object != NULL, "must be non-NULL");
  65   assert(_owner == NULL, "must be NULL: owner=" INTPTR_FORMAT, p2i(_owner));
  66 
  67   Atomic::store(&_header, markWord::zero());
  68   _object = NULL;
  69 }
  70 
  71 inline void* ObjectMonitor::object() const {
  72   return _object;
  73 }
  74 
  75 inline void* ObjectMonitor::object_addr() {
  76   return (void *)(&_object);
  77 }
  78 
  79 inline void ObjectMonitor::set_object(void* obj) {
  80   _object = obj;
  81 }
  82 
  83 // return number of threads contending for this monitor
  84 inline jint ObjectMonitor::contentions() const {
  85   return _contentions;
  86 }
  87 
  88 // Clear _owner field; current value must match old_value.
  89 inline void ObjectMonitor::release_clear_owner(void* old_value) {
  90   DEBUG_ONLY(void* prev = Atomic::load(&_owner);)
  91   assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
  92          ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
  93   Atomic::release_store(&_owner, (void*)NULL);
  94   log_trace(monitorinflation, owner)("release_clear_owner(): mid="
  95                                      INTPTR_FORMAT ", old_value=" INTPTR_FORMAT,
  96                                      p2i(this), p2i(old_value));
  97 }
  98 
  99 // Simply set _owner field to new_value; current value must match old_value.
 100 // (Simple means no memory sync needed.)
 101 inline void ObjectMonitor::set_owner_from(void* old_value, void* new_value) {
 102   DEBUG_ONLY(void* prev = Atomic::load(&_owner);)
 103   assert(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
 104          ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
 105   Atomic::store(&_owner, new_value);
 106   log_trace(monitorinflation, owner)("set_owner_from(): mid="
 107                                      INTPTR_FORMAT ", old_value=" INTPTR_FORMAT
 108                                      ", new_value=" INTPTR_FORMAT, p2i(this),
 109                                      p2i(old_value), p2i(new_value));
 110 }
 111 

















 112 // Simply set _owner field to self; current value must match basic_lock_p.
 113 inline void ObjectMonitor::set_owner_from_BasicLock(void* basic_lock_p, Thread* self) {
 114   DEBUG_ONLY(void* prev = Atomic::load(&_owner);)
 115   assert(prev == basic_lock_p, "unexpected prev owner=" INTPTR_FORMAT
 116          ", expected=" INTPTR_FORMAT, p2i(prev), p2i(basic_lock_p));
 117   // Non-null owner field to non-null owner field is safe without
 118   // cmpxchg() as long as all readers can tolerate either flavor.
 119   Atomic::store(&_owner, self);
 120   log_trace(monitorinflation, owner)("set_owner_from_BasicLock(): mid="
 121                                      INTPTR_FORMAT ", basic_lock_p="
 122                                      INTPTR_FORMAT ", new_value=" INTPTR_FORMAT,
 123                                      p2i(this), p2i(basic_lock_p), p2i(self));
 124 }
 125 
 126 // Try to set _owner field to new_value if the current value matches
 127 // old_value. Otherwise, does not change the _owner field. Returns
 128 // the prior value of the _owner field.
 129 inline void* ObjectMonitor::try_set_owner_from(void* old_value, void* new_value) {
 130   void* prev = Atomic::cmpxchg(&_owner, old_value, new_value);
 131   if (prev == old_value) {
 132     log_trace(monitorinflation, owner)("try_set_owner_from(): mid="
 133                                        INTPTR_FORMAT ", prev=" INTPTR_FORMAT
 134                                        ", new=" INTPTR_FORMAT, p2i(this),
 135                                        p2i(prev), p2i(new_value));
 136   }
 137   return prev;
 138 }
 139 




















 140 // The _next_om field can be concurrently read and modified so we
 141 // use Atomic operations to disable compiler optimizations that
 142 // might try to elide loading and/or storing this field.
 143 
 144 inline ObjectMonitor* ObjectMonitor::next_om() const {
 145   return Atomic::load(&_next_om);
 146 }
 147 
 148 // Simply set _next_om field to new_value.
 149 inline void ObjectMonitor::set_next_om(ObjectMonitor* new_value) {
 150   Atomic::store(&_next_om, new_value);
 151 }
 152 
 153 // Try to set _next_om field to new_value if the current value matches
 154 // old_value. Otherwise, does not change the _next_om field. Returns
 155 // the prior value of the _next_om field.
 156 inline ObjectMonitor* ObjectMonitor::try_set_next_om(ObjectMonitor* old_value, ObjectMonitor* new_value) {
 157   return Atomic::cmpxchg(&_next_om, old_value, new_value);
 158 }
 159 


  35   return 0;
  36 }
  37 
  38 inline markWord ObjectMonitor::header() const {
  39   return Atomic::load(&_header);
  40 }
  41 
  42 inline volatile markWord* ObjectMonitor::header_addr() {
  43   assert((intptr_t)this == (intptr_t)&_header, "sync code expects this");
  44   return &_header;
  45 }
  46 
  47 inline void ObjectMonitor::set_header(markWord hdr) {
  48   Atomic::store(&_header, hdr);
  49 }
  50 
  51 inline jint ObjectMonitor::waiters() const {
  52   return _waiters;
  53 }
  54 
  55 // Returns NULL if DEFLATER_MARKER is observed.
  56 inline void* ObjectMonitor::owner() const {
  57   void* owner = _owner;
  58   return owner != DEFLATER_MARKER ? owner : NULL;
  59 }
  60 
  61 // Returns true if owner field == DEFLATER_MARKER and false otherwise.
  62 // This accessor is called when we really need to know if the owner
  63 // field == DEFLATER_MARKER and any non-NULL value won't do the trick.
  64 inline bool ObjectMonitor::owner_is_DEFLATER_MARKER() {
  65   return _owner == DEFLATER_MARKER;
  66 }
  67 
  68 // Returns true if 'this' is being async deflated and false otherwise.
  69 inline bool ObjectMonitor::is_being_async_deflated() {
  70   return owner_is_DEFLATER_MARKER() && contentions() < 0;
  71 }
  72 
  73 inline void ObjectMonitor::clear() {
  74   assert(Atomic::load(&_header).value() != 0, "must be non-zero");
  75   assert(_owner == NULL, "must be NULL: owner=" INTPTR_FORMAT, p2i(_owner));
  76 
  77   Atomic::store(&_header, markWord::zero());
  78 
  79   clear_using_JT();
  80 }
  81 
  82 inline void ObjectMonitor::clear_using_JT() {
  83   // Unlike other *_using_JT() functions, we cannot assert
  84   // AsyncDeflateIdleMonitors or Thread::current()->is_Java_thread()
  85   // because clear() calls this function for the rest of its checks.
  86 
  87   if (AsyncDeflateIdleMonitors) {
  88     // Async deflation protocol uses the header, owner and contentions
  89     // fields. While the ObjectMonitor being deflated is on the global free
  90     // list, we leave those three fields alone; owner == DEFLATER_MARKER
  91     // and contentions < 0 will force any racing threads to retry. The
  92     // header field is used by install_displaced_markword_in_object()
  93     // to restore the object's header so we cannot check its value here.
  94     guarantee(_owner == NULL || _owner == DEFLATER_MARKER,
  95               "must be NULL or DEFLATER_MARKER: owner=" INTPTR_FORMAT,
  96               p2i(_owner));
  97   }
  98   assert(contentions() <= 0, "must not be positive: contentions=%d", contentions());
  99   assert(_waiters == 0, "must be 0: waiters=%d", _waiters);
 100   assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
 101   assert(_object != NULL, "must be non-NULL");

 102 
 103   set_allocation_state(Free);
 104   _object = NULL;
 105 }
 106 
 107 inline void* ObjectMonitor::object() const {
 108   return _object;
 109 }
 110 
 111 inline void* ObjectMonitor::object_addr() {
 112   return (void *)(&_object);
 113 }
 114 
 115 inline void ObjectMonitor::set_object(void* obj) {
 116   _object = obj;
 117 }
 118 
 119 // Return number of threads contending for this monitor.
 120 inline jint ObjectMonitor::contentions() const {
 121   return Atomic::load(&_contentions);
 122 }
 123 
 124 // Clear _owner field; current value must match old_value.
 125 inline void ObjectMonitor::release_clear_owner(void* old_value) {
 126   void* prev = Atomic::load(&_owner);
 127   ADIM_guarantee(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
 128                  ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
 129   Atomic::release_store(&_owner, (void*)NULL);
 130   log_trace(monitorinflation, owner)("release_clear_owner(): mid="
 131                                      INTPTR_FORMAT ", old_value=" INTPTR_FORMAT,
 132                                      p2i(this), p2i(old_value));
 133 }
 134 
 135 // Simply set _owner field to new_value; current value must match old_value.
 136 // (Simple means no memory sync needed.)
 137 inline void ObjectMonitor::set_owner_from(void* old_value, void* new_value) {
 138   void* prev = Atomic::load(&_owner);
 139   ADIM_guarantee(prev == old_value, "unexpected prev owner=" INTPTR_FORMAT
 140                  ", expected=" INTPTR_FORMAT, p2i(prev), p2i(old_value));
 141   Atomic::store(&_owner, new_value);
 142   log_trace(monitorinflation, owner)("set_owner_from(): mid="
 143                                      INTPTR_FORMAT ", old_value=" INTPTR_FORMAT
 144                                      ", new_value=" INTPTR_FORMAT, p2i(this),
 145                                      p2i(old_value), p2i(new_value));
 146 }
 147 
 148 // Simply set _owner field to new_value; current value must match old_value1 or old_value2.
 149 // (Simple means no memory sync needed.)
 150 inline void ObjectMonitor::set_owner_from(void* old_value1, void* old_value2, void* new_value) {
 151   void* prev = Atomic::load(&_owner);
 152   ADIM_guarantee(prev == old_value1 || prev == old_value2,
 153                  "unexpected prev owner=" INTPTR_FORMAT ", expected1="
 154                  INTPTR_FORMAT " or expected2=" INTPTR_FORMAT, p2i(prev),
 155                  p2i(old_value1), p2i(old_value2));
 156   _owner = new_value;
 157   log_trace(monitorinflation, owner)("set_owner_from(old1=" INTPTR_FORMAT
 158                                      ", old2=" INTPTR_FORMAT "): mid="
 159                                      INTPTR_FORMAT ", prev=" INTPTR_FORMAT
 160                                      ", new=" INTPTR_FORMAT, p2i(old_value1),
 161                                      p2i(old_value2), p2i(this), p2i(prev),
 162                                      p2i(new_value));
 163 }
 164 
 165 // Simply set _owner field to self; current value must match basic_lock_p.
 166 inline void ObjectMonitor::set_owner_from_BasicLock(void* basic_lock_p, Thread* self) {
 167   void* prev = Atomic::load(&_owner);
 168   ADIM_guarantee(prev == basic_lock_p, "unexpected prev owner=" INTPTR_FORMAT
 169                  ", expected=" INTPTR_FORMAT, p2i(prev), p2i(basic_lock_p));
 170   // Non-null owner field to non-null owner field is safe without
 171   // cmpxchg() as long as all readers can tolerate either flavor.
 172   Atomic::store(&_owner, self);
 173   log_trace(monitorinflation, owner)("set_owner_from_BasicLock(): mid="
 174                                      INTPTR_FORMAT ", basic_lock_p="
 175                                      INTPTR_FORMAT ", new_value=" INTPTR_FORMAT,
 176                                      p2i(this), p2i(basic_lock_p), p2i(self));
 177 }
 178 
 179 // Try to set _owner field to new_value if the current value matches
 180 // old_value. Otherwise, does not change the _owner field. Returns
 181 // the prior value of the _owner field.
 182 inline void* ObjectMonitor::try_set_owner_from(void* old_value, void* new_value) {
 183   void* prev = Atomic::cmpxchg(&_owner, old_value, new_value);
 184   if (prev == old_value) {
 185     log_trace(monitorinflation, owner)("try_set_owner_from(): mid="
 186                                        INTPTR_FORMAT ", prev=" INTPTR_FORMAT
 187                                        ", new=" INTPTR_FORMAT, p2i(this),
 188                                        p2i(prev), p2i(new_value));
 189   }
 190   return prev;
 191 }
 192 
 193 inline void ObjectMonitor::set_allocation_state(ObjectMonitor::AllocationState s) {
 194   _allocation_state = s;
 195 }
 196 
 197 inline ObjectMonitor::AllocationState ObjectMonitor::allocation_state() const {
 198   return _allocation_state;
 199 }
 200 
 201 inline bool ObjectMonitor::is_free() const {
 202   return _allocation_state == Free;
 203 }
 204 
 205 inline bool ObjectMonitor::is_old() const {
 206   return _allocation_state == Old;
 207 }
 208 
 209 inline bool ObjectMonitor::is_new() const {
 210   return _allocation_state == New;
 211 }
 212 
 213 // The _next_om field can be concurrently read and modified so we
 214 // use Atomic operations to disable compiler optimizations that
 215 // might try to elide loading and/or storing this field.
 216 
 217 inline ObjectMonitor* ObjectMonitor::next_om() const {
 218   return Atomic::load(&_next_om);
 219 }
 220 
 221 // Simply set _next_om field to new_value.
 222 inline void ObjectMonitor::set_next_om(ObjectMonitor* new_value) {
 223   Atomic::store(&_next_om, new_value);
 224 }
 225 
 226 // Try to set _next_om field to new_value if the current value matches
 227 // old_value. Otherwise, does not change the _next_om field. Returns
 228 // the prior value of the _next_om field.
 229 inline ObjectMonitor* ObjectMonitor::try_set_next_om(ObjectMonitor* old_value, ObjectMonitor* new_value) {
 230   return Atomic::cmpxchg(&_next_om, old_value, new_value);
 231 }
 232 
< prev index next >