< prev index next >

src/hotspot/share/runtime/synchronizer.cpp

Print this page
rev 59376 : 8153224.v2.10.patch merged with 8153224.v2.11.patch.
rev 59377 : CR1 changes from dcubed, dholmes, eosterlund and rehn.
rev 59378 : CR changes from dholmes, dcubed; fix is_being_async_deflated() race found by eosterlund; WB_ForceSafepoint() should request a special clean up with AsyncDeflateIdleMonitors; add a barrier in install_displaced_markword_in_object() to separate the header load from the preceding loads in is_being_async_deflated().
rev 59379 : eosterlund CR - Switch from three part async deflation protocol to a two part async deflation protocol where a negative contentions field is a linearization point.


2105     *free_tail_p = mid;
2106     // At this point, mid->_next_om still refers to its current
2107     // value and another ObjectMonitor's _next_om field still
2108     // refers to this ObjectMonitor. Those linkages have to be
2109     // cleaned up by the caller who has the complete context.
2110     deflated = true;
2111   }
2112   return deflated;
2113 }
2114 
2115 // Deflate the specified ObjectMonitor if not in-use using a JavaThread.
2116 // Returns true if it was deflated and false otherwise.
2117 //
2118 // The async deflation protocol sets owner to DEFLATER_MARKER and
2119 // makes contentions negative as signals to contending threads that
2120 // an async deflation is in progress. There are a number of checks
2121 // as part of the protocol to make sure that the calling thread has
2122 // not lost the race to a contending thread.
2123 //
2124 // The ObjectMonitor has been successfully async deflated when:
2125 // (owner == DEFLATER_MARKER && contentions < 0)
2126 // Contending threads that see those values know to retry their operation.
2127 //
2128 bool ObjectSynchronizer::deflate_monitor_using_JT(ObjectMonitor* mid,
2129                                                   ObjectMonitor** free_head_p,
2130                                                   ObjectMonitor** free_tail_p) {
2131   assert(AsyncDeflateIdleMonitors, "sanity check");
2132   assert(Thread::current()->is_Java_thread(), "precondition");
2133   // A newly allocated ObjectMonitor should not be seen here so we
2134   // avoid an endless inflate/deflate cycle.
2135   assert(mid->is_old(), "must be old: allocation_state=%d",
2136          (int) mid->allocation_state());
2137 
2138   if (mid->is_busy()) {
2139     // Easy checks are first - the ObjectMonitor is busy so no deflation.
2140     return false;
2141   }
2142 
2143   // Set a NULL owner to DEFLATER_MARKER to force any contending thread
2144   // through the slow path. This is just the first part of the async
2145   // deflation dance.
2146   if (mid->try_set_owner_from(NULL, DEFLATER_MARKER) != NULL) {
2147     // The owner field is no longer NULL so we lost the race since the
2148     // ObjectMonitor is now busy.
2149     return false;
2150   }
2151 
2152   if (mid->contentions() > 0 || mid->_waiters != 0) {
2153     // Another thread has raced to enter the ObjectMonitor after
2154     // mid->is_busy() above or has already entered and waited on
2155     // it which makes it busy so no deflation. Restore owner to
2156     // NULL if it is still DEFLATER_MARKER.
2157     mid->try_set_owner_from(DEFLATER_MARKER, NULL);



2158     return false;
2159   }
2160 
2161   // Make a zero contentions field negative to force any contending threads
2162   // to retry. This is the second part of the async deflation dance.
2163   if (Atomic::cmpxchg(&mid->_contentions, (jint)0, -max_jint) != 0) {
2164     // Contentions was no longer 0 so we lost the race since the
2165     // ObjectMonitor is now busy. Restore owner to NULL if it is
2166     // still DEFLATER_MARKER:
2167     mid->try_set_owner_from(DEFLATER_MARKER, NULL);
2168     return false;

2169   }
2170 
2171   // If owner is still DEFLATER_MARKER, then we have successfully
2172   // signaled any contending threads to retry.
2173   if (!mid->owner_is_DEFLATER_MARKER()) {
2174     // If it is not, then we have lost the race to an entering thread
2175     // and the ObjectMonitor is now busy. This is the third and final
2176     // part of the async deflation dance.
2177     // Note: This owner check solves the ABA problem with contentions
2178     // where another thread acquired the ObjectMonitor, finished
2179     // using it and restored contentions to zero.
2180 
2181     // Add back max_jint to restore the contentions field to its
2182     // proper value (which may not be what we saw above):
2183     mid->add_to_contentions(max_jint);
2184 
2185 #ifdef ASSERT
2186     jint l_contentions = mid->contentions();
2187 #endif
2188     assert(l_contentions >= 0, "must not be negative: l_contentions=%d, contentions=%d",
2189            l_contentions, mid->contentions());
2190     return false;
2191   }
2192 
2193   // Sanity checks for the races:

2194   guarantee(mid->contentions() < 0, "must be negative: contentions=%d",
2195             mid->contentions());
2196   guarantee(mid->_waiters == 0, "must be 0: waiters=%d", mid->_waiters);
2197   guarantee(mid->_cxq == NULL, "must be no contending threads: cxq="
2198             INTPTR_FORMAT, p2i(mid->_cxq));
2199   guarantee(mid->_EntryList == NULL,
2200             "must be no entering threads: EntryList=" INTPTR_FORMAT,
2201             p2i(mid->_EntryList));
2202 
2203   const oop obj = (oop) mid->object();
2204   if (log_is_enabled(Trace, monitorinflation)) {
2205     ResourceMark rm;
2206     log_trace(monitorinflation)("deflate_monitor_using_JT: "
2207                                 "object=" INTPTR_FORMAT ", mark="
2208                                 INTPTR_FORMAT ", type='%s'",
2209                                 p2i(obj), obj->mark().value(),
2210                                 obj->klass()->external_name());
2211   }
2212 
2213   // Install the old mark word if nobody else has already done it.




2105     *free_tail_p = mid;
2106     // At this point, mid->_next_om still refers to its current
2107     // value and another ObjectMonitor's _next_om field still
2108     // refers to this ObjectMonitor. Those linkages have to be
2109     // cleaned up by the caller who has the complete context.
2110     deflated = true;
2111   }
2112   return deflated;
2113 }
2114 
2115 // Deflate the specified ObjectMonitor if not in-use using a JavaThread.
2116 // Returns true if it was deflated and false otherwise.
2117 //
2118 // The async deflation protocol sets owner to DEFLATER_MARKER and
2119 // makes contentions negative as signals to contending threads that
2120 // an async deflation is in progress. There are a number of checks
2121 // as part of the protocol to make sure that the calling thread has
2122 // not lost the race to a contending thread.
2123 //
2124 // The ObjectMonitor has been successfully async deflated when:
2125 //   (contentions < 0)
2126 // Contending threads that see that condition know to retry their operation.
2127 //
2128 bool ObjectSynchronizer::deflate_monitor_using_JT(ObjectMonitor* mid,
2129                                                   ObjectMonitor** free_head_p,
2130                                                   ObjectMonitor** free_tail_p) {
2131   assert(AsyncDeflateIdleMonitors, "sanity check");
2132   assert(Thread::current()->is_Java_thread(), "precondition");
2133   // A newly allocated ObjectMonitor should not be seen here so we
2134   // avoid an endless inflate/deflate cycle.
2135   assert(mid->is_old(), "must be old: allocation_state=%d",
2136          (int) mid->allocation_state());
2137 
2138   if (mid->is_busy()) {
2139     // Easy checks are first - the ObjectMonitor is busy so no deflation.
2140     return false;
2141   }
2142 
2143   // Set a NULL owner to DEFLATER_MARKER to force any contending thread
2144   // through the slow path. This is just the first part of the async
2145   // deflation dance.
2146   if (mid->try_set_owner_from(NULL, DEFLATER_MARKER) != NULL) {
2147     // The owner field is no longer NULL so we lost the race since the
2148     // ObjectMonitor is now busy.
2149     return false;
2150   }
2151 
2152   if (mid->contentions() > 0 || mid->_waiters != 0) {
2153     // Another thread has raced to enter the ObjectMonitor after
2154     // mid->is_busy() above or has already entered and waited on
2155     // it which makes it busy so no deflation. Restore owner to
2156     // NULL if it is still DEFLATER_MARKER.
2157     if (mid->try_set_owner_from(DEFLATER_MARKER, NULL) != DEFLATER_MARKER) {
2158       // Deferred decrement for the JT EnterI() that cancelled the async deflation.
2159       mid->add_to_contentions(-1);
2160     }
2161     return false;
2162   }
2163 
2164   // Make a zero contentions field negative to force any contending threads
2165   // to retry. This is the second part of the async deflation dance.
2166   if (Atomic::cmpxchg(&mid->_contentions, (jint)0, -max_jint) != 0) {
2167     // Contentions was no longer 0 so we lost the race since the
2168     // ObjectMonitor is now busy. Restore owner to NULL if it is
2169     // still DEFLATER_MARKER:
2170     if (mid->try_set_owner_from(DEFLATER_MARKER, NULL) != DEFLATER_MARKER) {
2171       // Deferred decrement for the JT EnterI() that cancelled the async deflation.
2172       mid->add_to_contentions(-1);
2173     }




















2174     return false;
2175   }
2176 
2177   // Sanity checks for the races:
2178   guarantee(mid->owner_is_DEFLATER_MARKER(), "must be deflater marker");
2179   guarantee(mid->contentions() < 0, "must be negative: contentions=%d",
2180             mid->contentions());
2181   guarantee(mid->_waiters == 0, "must be 0: waiters=%d", mid->_waiters);
2182   guarantee(mid->_cxq == NULL, "must be no contending threads: cxq="
2183             INTPTR_FORMAT, p2i(mid->_cxq));
2184   guarantee(mid->_EntryList == NULL,
2185             "must be no entering threads: EntryList=" INTPTR_FORMAT,
2186             p2i(mid->_EntryList));
2187 
2188   const oop obj = (oop) mid->object();
2189   if (log_is_enabled(Trace, monitorinflation)) {
2190     ResourceMark rm;
2191     log_trace(monitorinflation)("deflate_monitor_using_JT: "
2192                                 "object=" INTPTR_FORMAT ", mark="
2193                                 INTPTR_FORMAT ", type='%s'",
2194                                 p2i(obj), obj->mark().value(),
2195                                 obj->klass()->external_name());
2196   }
2197 
2198   // Install the old mark word if nobody else has already done it.


< prev index next >