< prev index next >

src/hotspot/share/runtime/objectMonitor.cpp

Print this page

   1 /*
   2  * Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/vmSymbols.hpp"
  27 #include "gc/shared/oopStorage.hpp"
  28 #include "gc/shared/oopStorageSet.hpp"
  29 #include "jfr/jfrEvents.hpp"
  30 #include "jfr/support/jfrThreadId.hpp"
  31 #include "logging/log.hpp"
  32 #include "logging/logStream.hpp"
  33 #include "memory/allocation.inline.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "oops/markWord.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "oops/oopHandle.inline.hpp"
  38 #include "oops/weakHandle.inline.hpp"
  39 #include "prims/jvmtiDeferredUpdates.hpp"
  40 #include "prims/jvmtiExport.hpp"
  41 #include "runtime/atomic.hpp"

  42 #include "runtime/handles.inline.hpp"
  43 #include "runtime/interfaceSupport.inline.hpp"
  44 #include "runtime/javaThread.inline.hpp"
  45 #include "runtime/mutexLocker.hpp"
  46 #include "runtime/objectMonitor.hpp"
  47 #include "runtime/objectMonitor.inline.hpp"
  48 #include "runtime/orderAccess.hpp"
  49 #include "runtime/osThread.hpp"
  50 #include "runtime/perfData.hpp"
  51 #include "runtime/safefetch.hpp"
  52 #include "runtime/safepointMechanism.inline.hpp"
  53 #include "runtime/sharedRuntime.hpp"
  54 #include "services/threadService.hpp"
  55 #include "utilities/dtrace.hpp"

  56 #include "utilities/macros.hpp"
  57 #include "utilities/preserveException.hpp"
  58 #if INCLUDE_JFR
  59 #include "jfr/support/jfrFlush.hpp"
  60 #endif
  61 
  62 #ifdef DTRACE_ENABLED
  63 
  64 // Only bother with this argument setup if dtrace is available
  65 // TODO-FIXME: probes should not fire when caller is _blocked.  assert() accordingly.
  66 
  67 
  68 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread)                           \
  69   char* bytes = nullptr;                                                   \
  70   int len = 0;                                                             \
  71   jlong jtid = SharedRuntime::get_java_tid(thread);                        \
  72   Symbol* klassname = obj->klass()->name();                                \
  73   if (klassname != nullptr) {                                              \
  74     bytes = (char*)klassname->bytes();                                     \
  75     len = klassname->utf8_length();                                        \

 295     // Don't need a full fence after clearing successor here because of the call to exit().
 296     _om->exit(current, false /* not_suspended */);
 297     _om_exited = true;
 298 
 299     current->set_current_pending_monitor(_om);
 300   }
 301 }
 302 
 303 void ObjectMonitor::ClearSuccOnSuspend::operator()(JavaThread* current) {
 304   if (current->is_suspended()) {
 305     if (_om->_succ == current) {
 306       _om->_succ = nullptr;
 307       OrderAccess::fence(); // always do a full fence when successor is cleared
 308     }
 309   }
 310 }
 311 
 312 // -----------------------------------------------------------------------------
 313 // Enter support
 314 






























































 315 bool ObjectMonitor::enter(JavaThread* current) {

 316   // The following code is ordered to check the most common cases first
 317   // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
 318 
 319   void* cur = try_set_owner_from(nullptr, current);
 320   if (cur == nullptr) {
 321     assert(_recursions == 0, "invariant");
 322     return true;
 323   }
 324 
 325   if (cur == current) {
 326     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
 327     _recursions++;
 328     return true;
 329   }
 330 
 331   if (LockingMode != LM_LIGHTWEIGHT && current->is_lock_owned((address)cur)) {
 332     assert(_recursions == 0, "internal state error");
 333     _recursions = 1;
 334     set_owner_from_BasicLock(cur, current);  // Convert from BasicLock* to Thread*.
 335     return true;

   1 /*
   2  * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/vmSymbols.hpp"
  27 #include "gc/shared/oopStorage.hpp"
  28 #include "gc/shared/oopStorageSet.hpp"
  29 #include "jfr/jfrEvents.hpp"
  30 #include "jfr/support/jfrThreadId.hpp"
  31 #include "logging/log.hpp"
  32 #include "logging/logStream.hpp"
  33 #include "memory/allocation.inline.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "oops/markWord.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "oops/oopHandle.inline.hpp"
  38 #include "oops/weakHandle.inline.hpp"
  39 #include "prims/jvmtiDeferredUpdates.hpp"
  40 #include "prims/jvmtiExport.hpp"
  41 #include "runtime/atomic.hpp"
  42 #include "runtime/globals.hpp"
  43 #include "runtime/handles.inline.hpp"
  44 #include "runtime/interfaceSupport.inline.hpp"
  45 #include "runtime/javaThread.inline.hpp"
  46 #include "runtime/mutexLocker.hpp"
  47 #include "runtime/objectMonitor.hpp"
  48 #include "runtime/objectMonitor.inline.hpp"
  49 #include "runtime/orderAccess.hpp"
  50 #include "runtime/osThread.hpp"
  51 #include "runtime/perfData.hpp"
  52 #include "runtime/safefetch.hpp"
  53 #include "runtime/safepointMechanism.inline.hpp"
  54 #include "runtime/sharedRuntime.hpp"
  55 #include "services/threadService.hpp"
  56 #include "utilities/dtrace.hpp"
  57 #include "utilities/globalDefinitions.hpp"
  58 #include "utilities/macros.hpp"
  59 #include "utilities/preserveException.hpp"
  60 #if INCLUDE_JFR
  61 #include "jfr/support/jfrFlush.hpp"
  62 #endif
  63 
  64 #ifdef DTRACE_ENABLED
  65 
  66 // Only bother with this argument setup if dtrace is available
  67 // TODO-FIXME: probes should not fire when caller is _blocked.  assert() accordingly.
  68 
  69 
  70 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread)                           \
  71   char* bytes = nullptr;                                                   \
  72   int len = 0;                                                             \
  73   jlong jtid = SharedRuntime::get_java_tid(thread);                        \
  74   Symbol* klassname = obj->klass()->name();                                \
  75   if (klassname != nullptr) {                                              \
  76     bytes = (char*)klassname->bytes();                                     \
  77     len = klassname->utf8_length();                                        \

 297     // Don't need a full fence after clearing successor here because of the call to exit().
 298     _om->exit(current, false /* not_suspended */);
 299     _om_exited = true;
 300 
 301     current->set_current_pending_monitor(_om);
 302   }
 303 }
 304 
 305 void ObjectMonitor::ClearSuccOnSuspend::operator()(JavaThread* current) {
 306   if (current->is_suspended()) {
 307     if (_om->_succ == current) {
 308       _om->_succ = nullptr;
 309       OrderAccess::fence(); // always do a full fence when successor is cleared
 310     }
 311   }
 312 }
 313 
 314 // -----------------------------------------------------------------------------
 315 // Enter support
 316 
 317 bool ObjectMonitor::enter_for(JavaThread* locking_thread) {
 318   // Used by ObjectSynchronizer::enter_for to enter for another thread.
 319   // The monitor is private to or already owned by locking_thread which must be suspended.
 320   // So this code may only contend with deflation.
 321   assert(locking_thread == Thread::current() || locking_thread->is_obj_deopt_suspend(), "must be");
 322 
 323   // Block out deflation as soon as possible.
 324   add_to_contentions(1);
 325 
 326   bool success = false;
 327   if (!is_being_async_deflated()) {
 328     void* prev_owner = try_set_owner_from(nullptr, locking_thread);
 329 
 330     if (prev_owner == nullptr) {
 331       assert(_recursions == 0, "invariant");
 332       success = true;
 333     } else if (prev_owner == locking_thread) {
 334       _recursions++;
 335       success = true;
 336     } else if (prev_owner == DEFLATER_MARKER) {
 337       // Racing with deflation.
 338       prev_owner = try_set_owner_from(DEFLATER_MARKER, locking_thread);
 339       if (prev_owner == DEFLATER_MARKER) {
 340         // Cancelled deflation. Increment contentions as part of the deflation protocol.
 341         add_to_contentions(1);
 342         success = true;
 343       } else if (prev_owner == nullptr) {
 344         // At this point we cannot race with deflation as we have both incremented
 345         // contentions, seen contention > 0 and seen a DEFLATER_MARKER.
 346         // success will only be false if this races with something other than
 347         // deflation.
 348         prev_owner = try_set_owner_from(nullptr, locking_thread);
 349         success = prev_owner == nullptr;
 350       }
 351     } else if (LockingMode == LM_LEGACY && locking_thread->is_lock_owned((address)prev_owner)) {
 352       assert(_recursions == 0, "must be");
 353       _recursions = 1;
 354       set_owner_from_BasicLock(prev_owner, locking_thread);
 355       success = true;
 356     }
 357     assert(success, "Failed to enter_for: locking_thread=" INTPTR_FORMAT
 358            ", this=" INTPTR_FORMAT "{owner=" INTPTR_FORMAT "}, observed owner: " INTPTR_FORMAT,
 359            p2i(locking_thread), p2i(this), p2i(owner_raw()), p2i(prev_owner));
 360   } else {
 361     // Async deflation is in progress and our contentions increment
 362     // above lost the race to async deflation. Undo the work and
 363     // force the caller to retry.
 364     const oop l_object = object();
 365     if (l_object != nullptr) {
 366       // Attempt to restore the header/dmw to the object's header so that
 367       // we only retry once if the deflater thread happens to be slow.
 368       install_displaced_markword_in_object(l_object);
 369     }
 370   }
 371 
 372   add_to_contentions(-1);
 373 
 374   assert(!success || owner_raw() == locking_thread, "must be");
 375 
 376   return success;
 377 }
 378 
 379 bool ObjectMonitor::enter(JavaThread* current) {
 380   assert(current == JavaThread::current(), "must be");
 381   // The following code is ordered to check the most common cases first
 382   // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
 383 
 384   void* cur = try_set_owner_from(nullptr, current);
 385   if (cur == nullptr) {
 386     assert(_recursions == 0, "invariant");
 387     return true;
 388   }
 389 
 390   if (cur == current) {
 391     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
 392     _recursions++;
 393     return true;
 394   }
 395 
 396   if (LockingMode != LM_LIGHTWEIGHT && current->is_lock_owned((address)cur)) {
 397     assert(_recursions == 0, "internal state error");
 398     _recursions = 1;
 399     set_owner_from_BasicLock(cur, current);  // Convert from BasicLock* to Thread*.
 400     return true;
< prev index next >